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 83 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 86 of file recursive_mutex.h.

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

86 : 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 89 of file recursive_mutex.h.

References acquire(), and my_mutex.

89  {
90 #if TBB_USE_ASSERT
91  my_mutex = &mutex;
92 #endif /* TBB_USE_ASSERT */
93  acquire( mutex );
94  }
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 97 of file recursive_mutex.h.

References my_mutex, and release().

97  {
98  if( my_mutex )
99  release();
100  }
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 103 of file recursive_mutex.h.

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

Referenced by scoped_lock().

103  {
104 #if TBB_USE_ASSERT
105  internal_acquire( mutex );
106 #else
107  my_mutex = &mutex;
108  mutex.lock();
109 #endif /* TBB_USE_ASSERT */
110  }
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.
void lock()
Acquire lock.
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 26 of file recursive_mutex.cpp.

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

Referenced by acquire().

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

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

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

Referenced by try_acquire().

73  {
74 #if _WIN32||_WIN64
75  switch( m.state ) {
76  case INITIALIZED:
77  break;
78  case DESTROYED:
79  __TBB_ASSERT(false,"recursive_mutex::scoped_lock: mutex already destroyed");
80  break;
81  default:
82  __TBB_ASSERT(false,"recursive_mutex::scoped_lock: illegal mutex state");
83  break;
84  }
85 #endif /* _WIN32||_WIN64 */
86  bool result;
87 #if _WIN32||_WIN64
88  result = TryEnterCriticalSection(&m.impl)!=0;
89 #else
90  result = pthread_mutex_trylock(&m.impl)==0;
91 #endif /* _WIN32||_WIN64 */
92  if( result )
93  my_mutex = &m;
94  return result;
95 }
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:169
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 125 of file recursive_mutex.h.

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

Referenced by ~scoped_lock().

125  {
126 #if TBB_USE_ASSERT
128 #else
129  my_mutex->unlock();
130  my_mutex = NULL;
131 #endif /* TBB_USE_ASSERT */
132  }
void __TBB_EXPORTED_METHOD internal_release()
All checks from release using mutex.state were moved here.
void unlock()
Release lock.
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 113 of file recursive_mutex.h.

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

113  {
114 #if TBB_USE_ASSERT
115  return internal_try_acquire( mutex );
116 #else
117  bool result = mutex.try_lock();
118  if( result )
119  my_mutex = &mutex;
120  return result;
121 #endif /* TBB_USE_ASSERT */
122  }
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 147 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.