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 35 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 195 of file mutex.h.

Member Enumeration Documentation

◆ state_t

Enumerator
INITIALIZED 
DESTROYED 
HELD 

Definition at line 199 of file mutex.h.

199  {
200  INITIALIZED=0x1234,
201  DESTROYED=0x789A,
202  HELD=0x56CD
203  };

Constructor & Destructor Documentation

◆ mutex()

tbb::mutex::mutex ( )
inline

Construct unacquired mutex.

Definition at line 38 of file mutex.h.

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

38  {
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  };
void __TBB_EXPORTED_METHOD internal_construct()
All checks from mutex constructor using mutex.state were moved here.
Definition: mutex.cpp:112
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
pthread_mutex_t impl
Definition: mutex.h:209
Here is the call graph for this function:

◆ ~mutex()

tbb::mutex::~mutex ( )
inline

Definition at line 52 of file mutex.h.

References impl, and internal_destroy().

52  {
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  };
void __TBB_EXPORTED_METHOD internal_destroy()
All checks from mutex destructor using mutex.state were moved here.
Definition: mutex.cpp:124
pthread_mutex_t impl
Definition: mutex.h:209
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 112 of file mutex.cpp.

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

Referenced by mutex().

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

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

Referenced by ~mutex().

124  {
125 #if _WIN32||_WIN64
126  switch( state ) {
127  case INITIALIZED:
128  DeleteCriticalSection(&impl);
129  break;
130  case DESTROYED:
131  __TBB_ASSERT(false,"mutex: already destroyed");
132  break;
133  default:
134  __TBB_ASSERT(false,"mutex: illegal state for destruction");
135  break;
136  }
137  state = DESTROYED;
138 #else
139  int error_code = pthread_mutex_destroy(&impl);
140 #if __TBB_TSX_AVAILABLE
141  // For processors with speculative execution, skip the error code check due to glibc bug #16657
143 #endif
144  __TBB_ASSERT_EX(!error_code,"mutex: pthread_mutex_destroy failed");
145 #endif /* _WIN32||_WIN64 */
146 }
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:165
#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:167
static bool speculation_enabled()
Definition: governor.h:151
pthread_mutex_t impl
Definition: mutex.h:209
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 143 of file mutex.h.

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

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

143  {
144 #if TBB_USE_ASSERT
145  aligned_space<scoped_lock> tmp;
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  }
friend class scoped_lock
Definition: mutex.h:65
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
pthread_mutex_t impl
Definition: mutex.h:209
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 197 of file mutex.h.

References impl.

197 { return (native_handle_type) &impl; }
pthread_mutex_t * native_handle_type
Return native_handle.
Definition: mutex.h:195
pthread_mutex_t impl
Definition: mutex.h:209

◆ try_lock()

bool tbb::mutex::try_lock ( )
inline

Try acquiring lock (non-blocking)

Return true if lock acquired; false otherwise.

Definition at line 160 of file mutex.h.

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

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

160  {
161 #if TBB_USE_ASSERT
162  aligned_space<scoped_lock> tmp;
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  }
friend class scoped_lock
Definition: mutex.h:65
void const char const char int ITT_FORMAT __itt_group_sync s
pthread_mutex_t impl
Definition: mutex.h:209
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 176 of file mutex.h.

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

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

176  {
177 #if TBB_USE_ASSERT
178  aligned_space<scoped_lock> tmp;
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  }
friend class scoped_lock
Definition: mutex.h:65
void const char const char int ITT_FORMAT __itt_group_sync s
pthread_mutex_t impl
Definition: mutex.h:209
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 65 of file mutex.h.

Member Data Documentation

◆ impl

◆ is_fair_mutex

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

Definition at line 138 of file mutex.h.

◆ is_recursive_mutex

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

Definition at line 137 of file mutex.h.

◆ is_rw_mutex

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

Definition at line 136 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.