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

The scoped locking pattern. More...

#include <recursive_mutex.h>

Inheritance diagram for tbb::recursive_mutex::scoped_lock:
Collaboration diagram for tbb::recursive_mutex::scoped_lock:

Public Member Functions

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

Private Member Functions

void __TBB_EXPORTED_METHOD internal_acquire (recursive_mutex &m)
 All checks from acquire using mutex.state were moved here. More...
 
bool __TBB_EXPORTED_METHOD internal_try_acquire (recursive_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

recursive_mutexmy_mutex
 The pointer to the current recursive_mutex to work. More...
 

Friends

class recursive_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 79 of file recursive_mutex.h.

Constructor & Destructor Documentation

◆ scoped_lock() [1/2]

tbb::recursive_mutex::scoped_lock::scoped_lock ( )
inline

Construct lock that has not acquired a recursive_mutex.

Definition at line 82 of file recursive_mutex.h.

Referenced by tbb::recursive_mutex::lock().

82 : my_mutex(NULL) {};
recursive_mutex * my_mutex
The pointer to the current recursive_mutex to work.
Here is the caller graph for this function:

◆ scoped_lock() [2/2]

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

Acquire lock on given mutex.

Definition at line 85 of file recursive_mutex.h.

References acquire(), and my_mutex.

85  {
86 #if TBB_USE_ASSERT
87  my_mutex = &mutex;
88 #endif /* TBB_USE_ASSERT */
89  acquire( mutex );
90  }
void acquire(recursive_mutex &mutex)
Acquire lock on given mutex.
recursive_mutex * my_mutex
The pointer to the current recursive_mutex to work.
Here is the call graph for this function:

◆ ~scoped_lock()

tbb::recursive_mutex::scoped_lock::~scoped_lock ( )
inline

Release lock (if lock is held).

Definition at line 93 of file recursive_mutex.h.

References my_mutex, and release().

93  {
94  if( my_mutex )
95  release();
96  }
recursive_mutex * my_mutex
The pointer to the current recursive_mutex to work.
Here is the call graph for this function:

Member Function Documentation

◆ acquire()

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

Acquire lock on given mutex.

Definition at line 99 of file recursive_mutex.h.

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

Referenced by scoped_lock().

99  {
100 #if TBB_USE_ASSERT
101  internal_acquire( mutex );
102 #else
103  my_mutex = &mutex;
104  mutex.lock();
105 #endif /* TBB_USE_ASSERT */
106  }
void lock()
Acquire lock.
void __TBB_EXPORTED_METHOD internal_acquire(recursive_mutex &m)
All checks from acquire using mutex.state were moved here.
recursive_mutex * my_mutex
The pointer to the current recursive_mutex to work.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ internal_acquire()

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

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

Definition at line 22 of file recursive_mutex.cpp.

References __TBB_ASSERT, tbb::internal::handle_perror(), tbb::recursive_mutex::impl, and my_mutex.

Referenced by acquire().

22  {
23 #if _WIN32||_WIN64
24  switch( m.state ) {
25  case INITIALIZED:
26  // since we cannot look into the internal of the CriticalSection object
27  // we won't know how many times the lock has been acquired, and thus
28  // we won't know when we may safely set the state back to INITIALIZED
29  // if we change the state to HELD as in mutex.cpp. thus, we won't change
30  // the state for recursive_mutex
31  EnterCriticalSection( &m.impl );
32  break;
33  case DESTROYED:
34  __TBB_ASSERT(false,"recursive_mutex::scoped_lock: mutex already destroyed");
35  break;
36  default:
37  __TBB_ASSERT(false,"recursive_mutex::scoped_lock: illegal mutex state");
38  break;
39  }
40 #else
41  int error_code = pthread_mutex_lock(&m.impl);
42  if( error_code )
43  tbb::internal::handle_perror(error_code,"recursive_mutex::scoped_lock: pthread_mutex_lock failed");
44 #endif /* _WIN32||_WIN64 */
45  my_mutex = &m;
46 }
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
recursive_mutex * my_mutex
The pointer to the current recursive_mutex to work.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ internal_release()

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

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

Definition at line 48 of file recursive_mutex.cpp.

References __TBB_ASSERT, __TBB_ASSERT_EX, tbb::recursive_mutex::impl, and my_mutex.

Referenced by release(), and tbb::recursive_mutex::unlock().

48  {
49  __TBB_ASSERT( my_mutex, "recursive_mutex::scoped_lock: not holding a mutex" );
50 #if _WIN32||_WIN64
51  switch( my_mutex->state ) {
52  case INITIALIZED:
53  LeaveCriticalSection( &my_mutex->impl );
54  break;
55  case DESTROYED:
56  __TBB_ASSERT(false,"recursive_mutex::scoped_lock: mutex already destroyed");
57  break;
58  default:
59  __TBB_ASSERT(false,"recursive_mutex::scoped_lock: illegal mutex state");
60  break;
61  }
62 #else
63  int error_code = pthread_mutex_unlock(&my_mutex->impl);
64  __TBB_ASSERT_EX(!error_code, "recursive_mutex::scoped_lock: pthread_mutex_unlock failed");
65 #endif /* _WIN32||_WIN64 */
66  my_mutex = NULL;
67 }
pthread_mutex_t impl
#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
recursive_mutex * my_mutex
The pointer to the current recursive_mutex to work.
Here is the caller graph for this function:

◆ internal_try_acquire()

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

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

Definition at line 69 of file recursive_mutex.cpp.

References __TBB_ASSERT, tbb::recursive_mutex::impl, and my_mutex.

Referenced by try_acquire().

69  {
70 #if _WIN32||_WIN64
71  switch( m.state ) {
72  case INITIALIZED:
73  break;
74  case DESTROYED:
75  __TBB_ASSERT(false,"recursive_mutex::scoped_lock: mutex already destroyed");
76  break;
77  default:
78  __TBB_ASSERT(false,"recursive_mutex::scoped_lock: illegal mutex state");
79  break;
80  }
81 #endif /* _WIN32||_WIN64 */
82  bool result;
83 #if _WIN32||_WIN64
84  result = TryEnterCriticalSection(&m.impl)!=0;
85 #else
86  result = pthread_mutex_trylock(&m.impl)==0;
87 #endif /* _WIN32||_WIN64 */
88  if( result )
89  my_mutex = &m;
90  return result;
91 }
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:165
recursive_mutex * my_mutex
The pointer to the current recursive_mutex to work.
Here is the caller graph for this function:

◆ release()

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

Release lock.

Definition at line 121 of file recursive_mutex.h.

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

Referenced by ~scoped_lock().

121  {
122 #if TBB_USE_ASSERT
124 #else
125  my_mutex->unlock();
126  my_mutex = NULL;
127 #endif /* TBB_USE_ASSERT */
128  }
void unlock()
Release lock.
void __TBB_EXPORTED_METHOD internal_release()
All checks from release using mutex.state were moved here.
recursive_mutex * my_mutex
The pointer to the current recursive_mutex to work.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ try_acquire()

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

Try acquire lock on given recursive_mutex.

Definition at line 109 of file recursive_mutex.h.

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

109  {
110 #if TBB_USE_ASSERT
111  return internal_try_acquire( mutex );
112 #else
113  bool result = mutex.try_lock();
114  if( result )
115  my_mutex = &mutex;
116  return result;
117 #endif /* TBB_USE_ASSERT */
118  }
bool __TBB_EXPORTED_METHOD internal_try_acquire(recursive_mutex &m)
All checks from try_acquire using mutex.state were moved here.
recursive_mutex * my_mutex
The pointer to the current recursive_mutex to work.
Here is the call graph for this function:

Friends And Related Function Documentation

◆ recursive_mutex

friend class recursive_mutex
friend

Definition at line 143 of file recursive_mutex.h.

Member Data Documentation

◆ my_mutex

recursive_mutex* tbb::recursive_mutex::scoped_lock::my_mutex
private

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.