Intel(R) Threading Building Blocks Doxygen Documentation  version 4.2.3
mutex.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2005-2019 Intel Corporation
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 
17 #ifndef __TBB_mutex_H
18 #define __TBB_mutex_H
19 
20 #if _WIN32||_WIN64
21 #include "machine/windows_api.h"
22 #else
23 #include <pthread.h>
24 #endif /* _WIN32||_WIN64 */
25 
26 #include <new>
27 #include "aligned_space.h"
28 #include "tbb_stddef.h"
29 #include "tbb_profiling.h"
30 
31 namespace tbb {
32 
34 
35 class mutex : internal::mutex_copy_deprecated_and_disabled {
36 public:
38  mutex() {
39 #if TBB_USE_ASSERT || TBB_USE_THREADING_TOOLS
41 #else
42  #if _WIN32||_WIN64
43  InitializeCriticalSectionEx(&impl, 4000, 0);
44  #else
45  int error_code = pthread_mutex_init(&impl,NULL);
46  if( error_code )
47  tbb::internal::handle_perror(error_code,"mutex: pthread_mutex_init failed");
48  #endif /* _WIN32||_WIN64*/
49 #endif /* TBB_USE_ASSERT */
50  };
51 
52  ~mutex() {
53 #if TBB_USE_ASSERT
55 #else
56  #if _WIN32||_WIN64
57  DeleteCriticalSection(&impl);
58  #else
59  pthread_mutex_destroy(&impl);
60 
61  #endif /* _WIN32||_WIN64 */
62 #endif /* TBB_USE_ASSERT */
63  };
64 
65  class scoped_lock;
66  friend class scoped_lock;
67 
69 
71  class scoped_lock : internal::no_copy {
72  public:
74  scoped_lock() : my_mutex(NULL) {};
75 
78  acquire( mutex );
79  }
80 
83  if( my_mutex )
84  release();
85  }
86 
88  void acquire( mutex& mutex ) {
89 #if TBB_USE_ASSERT
91 #else
92  mutex.lock();
93  my_mutex = &mutex;
94 #endif /* TBB_USE_ASSERT */
95  }
96 
98  bool try_acquire( mutex& mutex ) {
99 #if TBB_USE_ASSERT
100  return internal_try_acquire (mutex);
101 #else
102  bool result = mutex.try_lock();
103  if( result )
104  my_mutex = &mutex;
105  return result;
106 #endif /* TBB_USE_ASSERT */
107  }
108 
110  void release() {
111 #if TBB_USE_ASSERT
112  internal_release ();
113 #else
114  my_mutex->unlock();
115  my_mutex = NULL;
116 #endif /* TBB_USE_ASSERT */
117  }
118 
119  private:
122 
125 
128 
131 
132  friend class mutex;
133  };
134 
135  // Mutex traits
136  static const bool is_rw_mutex = false;
137  static const bool is_recursive_mutex = false;
138  static const bool is_fair_mutex = false;
139 
140  // ISO C++0x compatibility methods
141 
143  void lock() {
144 #if TBB_USE_ASSERT
146  new(tmp.begin()) scoped_lock(*this);
147 #else
148  #if _WIN32||_WIN64
149  EnterCriticalSection(&impl);
150  #else
151  int error_code = pthread_mutex_lock(&impl);
152  if( error_code )
153  tbb::internal::handle_perror(error_code,"mutex: pthread_mutex_lock failed");
154  #endif /* _WIN32||_WIN64 */
155 #endif /* TBB_USE_ASSERT */
156  }
157 
159 
160  bool try_lock() {
161 #if TBB_USE_ASSERT
163  scoped_lock& s = *tmp.begin();
164  s.my_mutex = NULL;
165  return s.internal_try_acquire(*this);
166 #else
167  #if _WIN32||_WIN64
168  return TryEnterCriticalSection(&impl)!=0;
169  #else
170  return pthread_mutex_trylock(&impl)==0;
171  #endif /* _WIN32||_WIN64 */
172 #endif /* TBB_USE_ASSERT */
173  }
174 
176  void unlock() {
177 #if TBB_USE_ASSERT
179  scoped_lock& s = *tmp.begin();
180  s.my_mutex = this;
181  s.internal_release();
182 #else
183  #if _WIN32||_WIN64
184  LeaveCriticalSection(&impl);
185  #else
186  pthread_mutex_unlock(&impl);
187  #endif /* _WIN32||_WIN64 */
188 #endif /* TBB_USE_ASSERT */
189  }
190 
192  #if _WIN32||_WIN64
193  typedef LPCRITICAL_SECTION native_handle_type;
194  #else
195  typedef pthread_mutex_t* native_handle_type;
196  #endif
198 
199  enum state_t {
200  INITIALIZED=0x1234,
201  DESTROYED=0x789A,
202  HELD=0x56CD
203  };
204 private:
205 #if _WIN32||_WIN64
206  CRITICAL_SECTION impl;
207  enum state_t state;
208 #else
209  pthread_mutex_t impl;
210 #endif /* _WIN32||_WIN64 */
211 
214 
217 
218 #if _WIN32||_WIN64
219 public:
221  void set_state( state_t to ) { state = to; }
222 #endif
223 };
224 
226 
227 } // namespace tbb
228 
229 #endif /* __TBB_mutex_H */
#define __TBB_DEFINE_PROFILING_SET_NAME(sync_object_type)
friend class scoped_lock
Definition: mutex.h:65
scoped_lock()
Construct lock that has not acquired a mutex.
Definition: mutex.h:74
void __TBB_EXPORTED_METHOD internal_destroy()
All checks from mutex destructor using mutex.state were moved here.
Definition: mutex.cpp:124
mutex * my_mutex
The pointer to the current mutex to work.
Definition: mutex.h:121
#define __TBB_EXPORTED_METHOD
Definition: tbb_stddef.h:98
void const char const char int ITT_FORMAT __itt_group_sync s
void unlock()
Release lock.
Definition: mutex.h:176
bool __TBB_EXPORTED_METHOD internal_try_acquire(mutex &m)
All checks from try_acquire using mutex.state were moved here.
Definition: mutex.cpp:82
void lock()
Acquire lock.
Definition: mutex.h:143
void __TBB_EXPORTED_FUNC handle_perror(int error_code, const char *aux_info)
Throws std::runtime_error with what() returning error_code description prefixed with aux_info.
Definition: tbb_misc.cpp:74
The graph class.
void __TBB_EXPORTED_METHOD internal_release()
All checks from release using mutex.state were moved here.
Definition: mutex.cpp:57
bool try_acquire(mutex &mutex)
Try acquire lock on given mutex.
Definition: mutex.h:98
The scoped locking pattern.
Definition: mutex.h:71
~scoped_lock()
Release lock (if lock is held).
Definition: mutex.h:82
T * begin() const
Pointer to beginning of array.
Definition: aligned_space.h:35
Block of space aligned sufficiently to construct an array T with N elements.
Definition: aligned_space.h:29
friend class mutex
Definition: mutex.h:132
scoped_lock(mutex &mutex)
Acquire lock on given mutex.
Definition: mutex.h:77
void acquire(mutex &mutex)
Acquire lock on given mutex.
Definition: mutex.h:88
~mutex()
Definition: mutex.h:52
static const bool is_recursive_mutex
Definition: mutex.h:137
bool try_lock()
Try acquiring lock (non-blocking)
Definition: mutex.h:160
void __TBB_EXPORTED_METHOD internal_construct()
All checks from mutex constructor using mutex.state were moved here.
Definition: mutex.cpp:112
static const bool is_rw_mutex
Definition: mutex.h:136
Wrapper around the platform's native lock.
Definition: mutex.h:35
pthread_mutex_t * native_handle_type
Return native_handle.
Definition: mutex.h:195
pthread_mutex_t impl
Definition: mutex.h:209
void __TBB_EXPORTED_METHOD internal_acquire(mutex &m)
All checks from acquire using mutex.state were moved here.
Definition: mutex.cpp:27
mutex()
Construct unacquired mutex.
Definition: mutex.h:38
static const bool is_fair_mutex
Definition: mutex.h:138
void release()
Release lock.
Definition: mutex.h:110
native_handle_type native_handle()
Definition: mutex.h:197

Copyright © 2005-2019 Intel Corporation. All Rights Reserved.

Intel, Pentium, Intel Xeon, Itanium, Intel XScale and VTune are registered trademarks or trademarks of Intel Corporation or its subsidiaries in the United States and other countries.

* Other names and brands may be claimed as the property of others.