Intel(R) Threading Building Blocks Doxygen Documentation  version 4.2.3
tbb::mutex Class Reference

Wrapper around the platform's native lock. More...

#include <mutex.h>

Inheritance diagram for tbb::mutex:
Collaboration diagram for tbb::mutex:

Classes

class  scoped_lock
 The scoped locking pattern. More...
 

Public Types

enum  state_t { INITIALIZED =0x1234, DESTROYED =0x789A, HELD =0x56CD }
 
typedef pthread_mutex_t * native_handle_type
 Return native_handle. More...
 

Public Member Functions

 mutex ()
 Construct unacquired mutex. More...
 
 ~mutex ()
 
void lock ()
 Acquire lock. More...
 
bool try_lock ()
 Try acquiring lock (non-blocking) More...
 
void unlock ()
 Release lock. More...
 
native_handle_type native_handle ()
 

Static Public Attributes

static const bool is_rw_mutex = false
 
static const bool is_recursive_mutex = false
 
static const bool is_fair_mutex = false
 

Private Member Functions

void __TBB_EXPORTED_METHOD internal_construct ()
 All checks from mutex constructor using mutex.state were moved here. More...
 
void __TBB_EXPORTED_METHOD internal_destroy ()
 All checks from mutex destructor using mutex.state were moved here. More...
 

Private Attributes

pthread_mutex_t impl
 

Friends

class scoped_lock
 

Detailed Description

Wrapper around the platform's native lock.

Definition at line 39 of file mutex.h.

Member Typedef Documentation

◆ native_handle_type

typedef pthread_mutex_t* tbb::mutex::native_handle_type

Return native_handle.

Definition at line 199 of file mutex.h.

Member Enumeration Documentation

◆ state_t

Enumerator
INITIALIZED 
DESTROYED 
HELD 

Definition at line 203 of file mutex.h.

203  {
204  INITIALIZED=0x1234,
205  DESTROYED=0x789A,
206  HELD=0x56CD
207  };

Constructor & Destructor Documentation

◆ mutex()

tbb::mutex::mutex ( )
inline

Construct unacquired mutex.

Definition at line 42 of file mutex.h.

42  {
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  };
void __TBB_EXPORTED_METHOD internal_construct()
All checks from mutex constructor using mutex.state were moved here.
Definition: mutex.cpp:116
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
pthread_mutex_t impl
Definition: mutex.h:213

References tbb::internal::handle_perror(), impl, and internal_construct().

Here is the call graph for this function:

◆ ~mutex()

tbb::mutex::~mutex ( )
inline

Definition at line 56 of file mutex.h.

56  {
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  };
void __TBB_EXPORTED_METHOD internal_destroy()
All checks from mutex destructor using mutex.state were moved here.
Definition: mutex.cpp:128
pthread_mutex_t impl
Definition: mutex.h:213

References impl, and internal_destroy().

Here is the call graph for this function:

Member Function Documentation

◆ internal_construct()

void tbb::mutex::internal_construct ( )
private

All checks from mutex constructor using mutex.state were moved here.

Definition at line 116 of file mutex.cpp.

116  {
117 #if _WIN32||_WIN64
118  InitializeCriticalSectionEx(&impl, 4000, 0);
119  state = INITIALIZED;
120 #else
121  int error_code = pthread_mutex_init(&impl,NULL);
122  if( error_code )
123  tbb::internal::handle_perror(error_code,"mutex: pthread_mutex_init failed");
124 #endif /* _WIN32||_WIN64*/
125  ITT_SYNC_CREATE(&impl, _T("tbb::mutex"), _T(""));
126 }
#define ITT_SYNC_CREATE(obj, type, name)
Definition: itt_notify.h:123
#define _T(string_literal)
Standard Windows style macro to markup the string literals.
Definition: itt_notify.h:66
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
pthread_mutex_t impl
Definition: mutex.h:213

References _T, tbb::internal::handle_perror(), impl, INITIALIZED, and ITT_SYNC_CREATE.

Referenced by mutex().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ internal_destroy()

void tbb::mutex::internal_destroy ( )
private

All checks from mutex destructor using mutex.state were moved here.

Definition at line 128 of file mutex.cpp.

128  {
129 #if _WIN32||_WIN64
130  switch( state ) {
131  case INITIALIZED:
132  DeleteCriticalSection(&impl);
133  break;
134  case DESTROYED:
135  __TBB_ASSERT(false,"mutex: already destroyed");
136  break;
137  default:
138  __TBB_ASSERT(false,"mutex: illegal state for destruction");
139  break;
140  }
141  state = DESTROYED;
142 #else
143  int error_code = pthread_mutex_destroy(&impl);
144 #if __TBB_TSX_AVAILABLE
145  // For processors with speculative execution, skip the error code check due to glibc bug #16657
147 #endif
148  __TBB_ASSERT_EX(!error_code,"mutex: pthread_mutex_destroy failed");
149 #endif /* _WIN32||_WIN64 */
150 }
static bool speculation_enabled()
Definition: governor.h:155
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:169
#define __TBB_ASSERT_EX(predicate, comment)
"Extended" version is useful to suppress warnings if a variable is only used with an assert
Definition: tbb_stddef.h:171
pthread_mutex_t impl
Definition: mutex.h:213

References __TBB_ASSERT, __TBB_ASSERT_EX, DESTROYED, impl, INITIALIZED, and tbb::internal::governor::speculation_enabled().

Referenced by ~mutex().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ lock()

void tbb::mutex::lock ( )
inline

Acquire lock.

Definition at line 147 of file mutex.h.

147  {
148 #if TBB_USE_ASSERT
149  aligned_space<scoped_lock> tmp;
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  }
friend class scoped_lock
Definition: mutex.h:69
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
pthread_mutex_t impl
Definition: mutex.h:213

References tbb::aligned_space< T, N >::begin(), tbb::internal::handle_perror(), impl, and scoped_lock.

Referenced by tbb::mutex::scoped_lock::acquire(), and tbb::recursive_mutex::scoped_lock::acquire().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ native_handle()

native_handle_type tbb::mutex::native_handle ( )
inline

Definition at line 201 of file mutex.h.

201 { return (native_handle_type) &impl; }
pthread_mutex_t * native_handle_type
Return native_handle.
Definition: mutex.h:199
pthread_mutex_t impl
Definition: mutex.h:213

References impl.

◆ try_lock()

bool tbb::mutex::try_lock ( )
inline

Try acquiring lock (non-blocking)

Return true if lock acquired; false otherwise.

Definition at line 164 of file mutex.h.

164  {
165 #if TBB_USE_ASSERT
166  aligned_space<scoped_lock> tmp;
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  }
void const char const char int ITT_FORMAT __itt_group_sync s
friend class scoped_lock
Definition: mutex.h:69
pthread_mutex_t impl
Definition: mutex.h:213

References tbb::aligned_space< T, N >::begin(), impl, and s.

Referenced by tbb::mutex::scoped_lock::try_acquire(), and tbb::recursive_mutex::scoped_lock::try_acquire().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ unlock()

void tbb::mutex::unlock ( )
inline

Release lock.

Definition at line 180 of file mutex.h.

180  {
181 #if TBB_USE_ASSERT
182  aligned_space<scoped_lock> tmp;
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  }
void const char const char int ITT_FORMAT __itt_group_sync s
friend class scoped_lock
Definition: mutex.h:69
pthread_mutex_t impl
Definition: mutex.h:213

References tbb::aligned_space< T, N >::begin(), impl, and s.

Referenced by tbb::mutex::scoped_lock::release().

Here is the call graph for this function:
Here is the caller graph for this function:

Friends And Related Function Documentation

◆ scoped_lock

friend class scoped_lock
friend

Definition at line 69 of file mutex.h.

Referenced by lock().

Member Data Documentation

◆ impl

◆ is_fair_mutex

const bool tbb::mutex::is_fair_mutex = false
static

Definition at line 142 of file mutex.h.

◆ is_recursive_mutex

const bool tbb::mutex::is_recursive_mutex = false
static

Definition at line 141 of file mutex.h.

◆ is_rw_mutex

const bool tbb::mutex::is_rw_mutex = false
static

Definition at line 140 of file mutex.h.


The documentation for this class was generated from the following files:

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.