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

The scoped locking pattern. More...

#include <mutex.h>

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

Public Member Functions

 scoped_lock ()
 Construct lock that has not acquired a mutex. More...
 
 scoped_lock (mutex &mutex)
 Acquire lock on given mutex. More...
 
 ~scoped_lock ()
 Release lock (if lock is held). More...
 
void acquire (mutex &mutex)
 Acquire lock on given mutex. More...
 
bool try_acquire (mutex &mutex)
 Try acquire lock on given mutex. More...
 
void release ()
 Release lock. More...
 

Private Member Functions

void __TBB_EXPORTED_METHOD internal_acquire (mutex &m)
 All checks from acquire using mutex.state were moved here. More...
 
bool __TBB_EXPORTED_METHOD internal_try_acquire (mutex &m)
 All checks from try_acquire using mutex.state were moved here. More...
 
void __TBB_EXPORTED_METHOD internal_release ()
 All checks from release using mutex.state were moved here. More...
 
- Private Member Functions inherited from tbb::internal::no_copy
 no_copy ()
 Allow default construction. More...
 

Private Attributes

mutexmy_mutex
 The pointer to the current mutex to work. More...
 

Friends

class mutex
 

Detailed Description

The scoped locking pattern.

It helps to avoid the common problem of forgetting to release lock. It also nicely provides the "node" for queuing locks.

Definition at line 71 of file mutex.h.

Constructor & Destructor Documentation

◆ scoped_lock() [1/2]

tbb::mutex::scoped_lock::scoped_lock ( )
inline

Construct lock that has not acquired a mutex.

Definition at line 74 of file mutex.h.

74 : my_mutex(NULL) {};
mutex * my_mutex
The pointer to the current mutex to work.
Definition: mutex.h:121

◆ scoped_lock() [2/2]

tbb::mutex::scoped_lock::scoped_lock ( mutex mutex)
inline

Acquire lock on given mutex.

Definition at line 77 of file mutex.h.

77  {
78  acquire( mutex );
79  }
friend class mutex
Definition: mutex.h:132
void acquire(mutex &mutex)
Acquire lock on given mutex.
Definition: mutex.h:88

References acquire().

Here is the call graph for this function:

◆ ~scoped_lock()

tbb::mutex::scoped_lock::~scoped_lock ( )
inline

Release lock (if lock is held).

Definition at line 82 of file mutex.h.

82  {
83  if( my_mutex )
84  release();
85  }
mutex * my_mutex
The pointer to the current mutex to work.
Definition: mutex.h:121
void release()
Release lock.
Definition: mutex.h:110

References my_mutex, and release().

Here is the call graph for this function:

Member Function Documentation

◆ acquire()

void tbb::mutex::scoped_lock::acquire ( mutex mutex)
inline

Acquire lock on given mutex.

Definition at line 88 of file mutex.h.

88  {
89 #if TBB_USE_ASSERT
91 #else
92  mutex.lock();
93  my_mutex = &mutex;
94 #endif /* TBB_USE_ASSERT */
95  }
mutex * my_mutex
The pointer to the current mutex to work.
Definition: mutex.h:121
friend class mutex
Definition: mutex.h:132
void __TBB_EXPORTED_METHOD internal_acquire(mutex &m)
All checks from acquire using mutex.state were moved here.
Definition: mutex.cpp:27

References internal_acquire(), tbb::mutex::lock(), mutex, and my_mutex.

Referenced by scoped_lock().

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

◆ internal_acquire()

void tbb::mutex::scoped_lock::internal_acquire ( mutex m)
private

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

Definition at line 27 of file mutex.cpp.

27  {
28 
29 #if _WIN32||_WIN64
30  switch( m.state ) {
31  case INITIALIZED:
32  case HELD:
33  EnterCriticalSection( &m.impl );
34  // If a thread comes here, and another thread holds the lock, it will block
35  // in EnterCriticalSection. When it returns from EnterCriticalSection,
36  // m.state must be set to INITIALIZED. If the same thread tries to acquire a lock it
37  // already holds, the lock is in HELD state, thus will cause throwing the exception.
38  if (m.state==HELD)
39  tbb::internal::handle_perror(EDEADLK,"mutex::scoped_lock: deadlock caused by attempt to reacquire held mutex");
40  m.state = HELD;
41  break;
42  case DESTROYED:
43  __TBB_ASSERT(false,"mutex::scoped_lock: mutex already destroyed");
44  break;
45  default:
46  __TBB_ASSERT(false,"mutex::scoped_lock: illegal mutex state");
47  break;
48  }
49 #else
50  int error_code = pthread_mutex_lock(&m.impl);
51  if( error_code )
52  tbb::internal::handle_perror(error_code,"mutex::scoped_lock: pthread_mutex_lock failed");
53 #endif /* _WIN32||_WIN64 */
54  my_mutex = &m;
55  }
mutex * my_mutex
The pointer to the current mutex to work.
Definition: mutex.h:121
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 __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:165

References __TBB_ASSERT, tbb::mutex::DESTROYED, tbb::internal::handle_perror(), tbb::mutex::HELD, tbb::mutex::impl, tbb::mutex::INITIALIZED, and my_mutex.

Referenced by acquire().

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

◆ internal_release()

void tbb::mutex::scoped_lock::internal_release ( )
private

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

Definition at line 57 of file mutex.cpp.

57  {
58  __TBB_ASSERT( my_mutex, "mutex::scoped_lock: not holding a mutex" );
59 #if _WIN32||_WIN64
60  switch( my_mutex->state ) {
61  case INITIALIZED:
62  __TBB_ASSERT(false,"mutex::scoped_lock: try to release the lock without acquisition");
63  break;
64  case HELD:
65  my_mutex->state = INITIALIZED;
66  LeaveCriticalSection(&my_mutex->impl);
67  break;
68  case DESTROYED:
69  __TBB_ASSERT(false,"mutex::scoped_lock: mutex already destroyed");
70  break;
71  default:
72  __TBB_ASSERT(false,"mutex::scoped_lock: illegal mutex state");
73  break;
74  }
75 #else
76  int error_code = pthread_mutex_unlock(&my_mutex->impl);
77  __TBB_ASSERT_EX(!error_code, "mutex::scoped_lock: pthread_mutex_unlock failed");
78 #endif /* _WIN32||_WIN64 */
79  my_mutex = NULL;
80 }
mutex * my_mutex
The pointer to the current mutex to work.
Definition: mutex.h:121
#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
pthread_mutex_t impl
Definition: mutex.h:209

References __TBB_ASSERT, __TBB_ASSERT_EX, tbb::mutex::DESTROYED, tbb::mutex::HELD, and tbb::mutex::INITIALIZED.

Referenced by release().

Here is the caller graph for this function:

◆ internal_try_acquire()

bool tbb::mutex::scoped_lock::internal_try_acquire ( mutex m)
private

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

Definition at line 82 of file mutex.cpp.

82  {
83 #if _WIN32||_WIN64
84  switch( m.state ) {
85  case INITIALIZED:
86  case HELD:
87  break;
88  case DESTROYED:
89  __TBB_ASSERT(false,"mutex::scoped_lock: mutex already destroyed");
90  break;
91  default:
92  __TBB_ASSERT(false,"mutex::scoped_lock: illegal mutex state");
93  break;
94  }
95 #endif /* _WIN32||_WIN64 */
96 
97  bool result;
98 #if _WIN32||_WIN64
99  result = TryEnterCriticalSection(&m.impl)!=0;
100  if( result ) {
101  __TBB_ASSERT(m.state!=HELD, "mutex::scoped_lock: deadlock caused by attempt to reacquire held mutex");
102  m.state = HELD;
103  }
104 #else
105  result = pthread_mutex_trylock(&m.impl)==0;
106 #endif /* _WIN32||_WIN64 */
107  if( result )
108  my_mutex = &m;
109  return result;
110 }
mutex * my_mutex
The pointer to the current mutex to work.
Definition: mutex.h:121
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:165

References __TBB_ASSERT, tbb::mutex::DESTROYED, tbb::mutex::HELD, tbb::mutex::impl, and tbb::mutex::INITIALIZED.

Referenced by try_acquire().

Here is the caller graph for this function:

◆ release()

void tbb::mutex::scoped_lock::release ( )
inline

Release lock.

Definition at line 110 of file mutex.h.

110  {
111 #if TBB_USE_ASSERT
112  internal_release ();
113 #else
114  my_mutex->unlock();
115  my_mutex = NULL;
116 #endif /* TBB_USE_ASSERT */
117  }
mutex * my_mutex
The pointer to the current mutex to work.
Definition: mutex.h:121
void unlock()
Release lock.
Definition: mutex.h:176
void __TBB_EXPORTED_METHOD internal_release()
All checks from release using mutex.state were moved here.
Definition: mutex.cpp:57

References internal_release(), my_mutex, and tbb::mutex::unlock().

Referenced by ~scoped_lock().

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

◆ try_acquire()

bool tbb::mutex::scoped_lock::try_acquire ( mutex mutex)
inline

Try acquire lock on given mutex.

Definition at line 98 of file mutex.h.

98  {
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  }
mutex * my_mutex
The pointer to the current mutex to work.
Definition: mutex.h:121
bool __TBB_EXPORTED_METHOD internal_try_acquire(mutex &m)
All checks from try_acquire using mutex.state were moved here.
Definition: mutex.cpp:82
friend class mutex
Definition: mutex.h:132

References internal_try_acquire(), mutex, my_mutex, and tbb::mutex::try_lock().

Here is the call graph for this function:

Friends And Related Function Documentation

◆ mutex

friend class mutex
friend

Definition at line 132 of file mutex.h.

Referenced by acquire(), and try_acquire().

Member Data Documentation

◆ my_mutex

mutex* tbb::mutex::scoped_lock::my_mutex
private

The pointer to the current mutex to work.

Definition at line 121 of file mutex.h.

Referenced by acquire(), internal_acquire(), release(), try_acquire(), and ~scoped_lock().


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.