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 
18 
19 */
20 
21 #ifndef __TBB_mutex_H
22 #define __TBB_mutex_H
23 
24 #if _WIN32||_WIN64
25 #include "machine/windows_api.h"
26 #else
27 #include <pthread.h>
28 #endif /* _WIN32||_WIN64 */
29 
30 #include <new>
31 #include "aligned_space.h"
32 #include "tbb_stddef.h"
33 #include "tbb_profiling.h"
34 
35 namespace tbb {
36 
38 
39 class mutex : internal::mutex_copy_deprecated_and_disabled {
40 public:
42  mutex() {
43 #if TBB_USE_ASSERT || TBB_USE_THREADING_TOOLS
45 #else
46  #if _WIN32||_WIN64
47  InitializeCriticalSectionEx(&impl, 4000, 0);
48  #else
49  int error_code = pthread_mutex_init(&impl,NULL);
50  if( error_code )
51  tbb::internal::handle_perror(error_code,"mutex: pthread_mutex_init failed");
52  #endif /* _WIN32||_WIN64*/
53 #endif /* TBB_USE_ASSERT */
54  };
55 
56  ~mutex() {
57 #if TBB_USE_ASSERT
59 #else
60  #if _WIN32||_WIN64
61  DeleteCriticalSection(&impl);
62  #else
63  pthread_mutex_destroy(&impl);
64 
65  #endif /* _WIN32||_WIN64 */
66 #endif /* TBB_USE_ASSERT */
67  };
68 
69  class scoped_lock;
70  friend class scoped_lock;
71 
73 
75  class scoped_lock : internal::no_copy {
76  public:
78  scoped_lock() : my_mutex(NULL) {};
79 
82  acquire( mutex );
83  }
84 
87  if( my_mutex )
88  release();
89  }
90 
92  void acquire( mutex& mutex ) {
93 #if TBB_USE_ASSERT
95 #else
96  mutex.lock();
97  my_mutex = &mutex;
98 #endif /* TBB_USE_ASSERT */
99  }
100 
103 #if TBB_USE_ASSERT
104  return internal_try_acquire (mutex);
105 #else
106  bool result = mutex.try_lock();
107  if( result )
108  my_mutex = &mutex;
109  return result;
110 #endif /* TBB_USE_ASSERT */
111  }
112 
114  void release() {
115 #if TBB_USE_ASSERT
116  internal_release ();
117 #else
118  my_mutex->unlock();
119  my_mutex = NULL;
120 #endif /* TBB_USE_ASSERT */
121  }
122 
123  private:
126 
129 
132 
135 
136  friend class mutex;
137  };
138 
139  // Mutex traits
140  static const bool is_rw_mutex = false;
141  static const bool is_recursive_mutex = false;
142  static const bool is_fair_mutex = false;
143 
144  // ISO C++0x compatibility methods
145 
147  void lock() {
148 #if TBB_USE_ASSERT
150  new(tmp.begin()) scoped_lock(*this);
151 #else
152  #if _WIN32||_WIN64
153  EnterCriticalSection(&impl);
154  #else
155  int error_code = pthread_mutex_lock(&impl);
156  if( error_code )
157  tbb::internal::handle_perror(error_code,"mutex: pthread_mutex_lock failed");
158  #endif /* _WIN32||_WIN64 */
159 #endif /* TBB_USE_ASSERT */
160  }
161 
163 
164  bool try_lock() {
165 #if TBB_USE_ASSERT
167  scoped_lock& s = *tmp.begin();
168  s.my_mutex = NULL;
169  return s.internal_try_acquire(*this);
170 #else
171  #if _WIN32||_WIN64
172  return TryEnterCriticalSection(&impl)!=0;
173  #else
174  return pthread_mutex_trylock(&impl)==0;
175  #endif /* _WIN32||_WIN64 */
176 #endif /* TBB_USE_ASSERT */
177  }
178 
180  void unlock() {
181 #if TBB_USE_ASSERT
183  scoped_lock& s = *tmp.begin();
184  s.my_mutex = this;
185  s.internal_release();
186 #else
187  #if _WIN32||_WIN64
188  LeaveCriticalSection(&impl);
189  #else
190  pthread_mutex_unlock(&impl);
191  #endif /* _WIN32||_WIN64 */
192 #endif /* TBB_USE_ASSERT */
193  }
194 
196  #if _WIN32||_WIN64
197  typedef LPCRITICAL_SECTION native_handle_type;
198  #else
199  typedef pthread_mutex_t* native_handle_type;
200  #endif
202 
203  enum state_t {
204  INITIALIZED=0x1234,
205  DESTROYED=0x789A,
206  HELD=0x56CD
207  };
208 private:
209 #if _WIN32||_WIN64
210  CRITICAL_SECTION impl;
211  enum state_t state;
212 #else
213  pthread_mutex_t impl;
214 #endif /* _WIN32||_WIN64 */
215 
218 
221 
222 #if _WIN32||_WIN64
223 public:
225  void set_state( state_t to ) { state = to; }
226 #endif
227 };
228 
230 
231 } // namespace tbb
232 
233 #endif /* __TBB_mutex_H */
void lock()
Acquire lock.
Definition: mutex.h:147
static const bool is_rw_mutex
Definition: mutex.h:140
T * begin() const
Pointer to beginning of array.
Definition: aligned_space.h:39
static const bool is_recursive_mutex
Definition: mutex.h:141
mutex * my_mutex
The pointer to the current mutex to work.
Definition: mutex.h:125
void __TBB_EXPORTED_METHOD internal_release()
All checks from release using mutex.state were moved here.
Definition: mutex.cpp:61
The scoped locking pattern.
Definition: mutex.h:75
~scoped_lock()
Release lock (if lock is held).
Definition: mutex.h:86
bool try_acquire(mutex &mutex)
Try acquire lock on given mutex.
Definition: mutex.h:102
void release()
Release lock.
Definition: mutex.h:114
void acquire(mutex &mutex)
Acquire lock on given mutex.
Definition: mutex.h:92
#define __TBB_EXPORTED_METHOD
Definition: tbb_stddef.h:102
native_handle_type native_handle()
Definition: mutex.h:201
bool try_lock()
Try acquiring lock (non-blocking)
Definition: mutex.h:164
scoped_lock()
Construct lock that has not acquired a mutex.
Definition: mutex.h:78
The graph class.
#define __TBB_DEFINE_PROFILING_SET_NAME(sync_object_type)
scoped_lock(mutex &mutex)
Acquire lock on given mutex.
Definition: mutex.h:81
static const bool is_fair_mutex
Definition: mutex.h:142
void unlock()
Release lock.
Definition: mutex.h:180
void __TBB_EXPORTED_METHOD internal_construct()
All checks from mutex constructor using mutex.state were moved here.
Definition: mutex.cpp:116
pthread_mutex_t * native_handle_type
Return native_handle.
Definition: mutex.h:199
void const char const char int ITT_FORMAT __itt_group_sync s
friend class scoped_lock
Definition: mutex.h:69
mutex()
Construct unacquired mutex.
Definition: mutex.h:42
bool __TBB_EXPORTED_METHOD internal_try_acquire(mutex &m)
All checks from try_acquire using mutex.state were moved here.
Definition: mutex.cpp:86
void __TBB_EXPORTED_METHOD internal_destroy()
All checks from mutex destructor using mutex.state were moved here.
Definition: mutex.cpp:128
Wrapper around the platform's native lock.
Definition: mutex.h:39
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:78
~mutex()
Definition: mutex.h:56
Block of space aligned sufficiently to construct an array T with N elements.
Definition: aligned_space.h:33
pthread_mutex_t impl
Definition: mutex.h:213
void __TBB_EXPORTED_METHOD internal_acquire(mutex &m)
All checks from acquire using mutex.state were moved here.
Definition: mutex.cpp:31
friend class mutex
Definition: mutex.h:136

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.