Intel(R) Threading Building Blocks Doxygen Documentation  version 4.2.3
recursive_mutex.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2005-2019 Intel Corporation
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 
16 
17 
18 
19 */
20 
21 #include "tbb/recursive_mutex.h"
22 #include "itt_notify.h"
23 
24 namespace tbb {
25 
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 }
51 
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 }
72 
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 }
96 
98 #if _WIN32||_WIN64
99  InitializeCriticalSectionEx(&impl, 4000, 0);
100  state = INITIALIZED;
101 #else
102  pthread_mutexattr_t mtx_attr;
103  int error_code = pthread_mutexattr_init( &mtx_attr );
104  if( error_code )
105  tbb::internal::handle_perror(error_code,"recursive_mutex: pthread_mutexattr_init failed");
106 
107  pthread_mutexattr_settype( &mtx_attr, PTHREAD_MUTEX_RECURSIVE );
108  error_code = pthread_mutex_init( &impl, &mtx_attr );
109  if( error_code )
110  tbb::internal::handle_perror(error_code,"recursive_mutex: pthread_mutex_init failed");
111  pthread_mutexattr_destroy( &mtx_attr );
112 #endif /* _WIN32||_WIN64*/
113  ITT_SYNC_CREATE(&impl, _T("tbb::recursive_mutex"), _T(""));
114 }
115 
117 #if _WIN32||_WIN64
118  switch( state ) {
119  case INITIALIZED:
120  DeleteCriticalSection(&impl);
121  break;
122  case DESTROYED:
123  __TBB_ASSERT(false,"recursive_mutex: already destroyed");
124  break;
125  default:
126  __TBB_ASSERT(false,"recursive_mutex: illegal state for destruction");
127  break;
128  }
129  state = DESTROYED;
130 #else
131  int error_code = pthread_mutex_destroy(&impl);
132  __TBB_ASSERT_EX(!error_code,"recursive_mutex: pthread_mutex_destroy failed");
133 #endif /* _WIN32||_WIN64 */
134 }
135 
136 } // namespace tbb
void __TBB_EXPORTED_METHOD internal_destroy()
All checks from mutex destructor using mutex.state were moved here.
pthread_mutex_t impl
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:169
#define ITT_SYNC_CREATE(obj, type, name)
Definition: itt_notify.h:123
#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
void __TBB_EXPORTED_METHOD internal_construct()
All checks from mutex constructor using mutex.state were moved here.
void __TBB_EXPORTED_METHOD internal_acquire(recursive_mutex &m)
All checks from acquire using mutex.state were moved here.
Mutex that allows recursive mutex acquisition.
void __TBB_EXPORTED_METHOD internal_release()
All checks from release using mutex.state were moved here.
The graph class.
#define _T(string_literal)
Standard Windows style macro to markup the string literals.
Definition: itt_notify.h:66
recursive_mutex * my_mutex
The pointer to the current recursive_mutex to work.
bool __TBB_EXPORTED_METHOD internal_try_acquire(recursive_mutex &m)
All checks from try_acquire using mutex.state were moved here.
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

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.