Intel(R) Threading Building Blocks Doxygen Documentation version 4.2.3
Loading...
Searching...
No Matches
recursive_mutex.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2005-2020 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
18
19#if !defined(__TBB_show_deprecation_message_recursive_mutex_H) && defined(__TBB_show_deprecated_header_message)
20#define __TBB_show_deprecation_message_recursive_mutex_H
21#pragma message("TBB Warning: tbb/recursive_mutex.h is deprecated. For details, please see Deprecated Features appendix in the TBB reference manual.")
22#endif
23
24#if defined(__TBB_show_deprecated_header_message)
25#undef __TBB_show_deprecated_header_message
26#endif
27
28#ifndef __TBB_recursive_mutex_H
29#define __TBB_recursive_mutex_H
30
31#define __TBB_recursive_mutex_H_include_area
33
34#if _WIN32||_WIN64
35#include "machine/windows_api.h"
36#else
37#include <pthread.h>
38#endif /* _WIN32||_WIN64 */
39
40#include <new>
41#include "aligned_space.h"
42#include "tbb_stddef.h"
43#include "tbb_profiling.h"
44
45namespace tbb {
47
49class __TBB_DEPRECATED_IN_VERBOSE_MODE_MSG("tbb::recursive_mutex is deprecated, use std::recursive_mutex")
50recursive_mutex : internal::mutex_copy_deprecated_and_disabled {
51public:
53 recursive_mutex() {
54#if TBB_USE_ASSERT || TBB_USE_THREADING_TOOLS
55 internal_construct();
56#else
57 #if _WIN32||_WIN64
58 InitializeCriticalSectionEx(&impl, 4000, 0);
59 #else
60 pthread_mutexattr_t mtx_attr;
61 int error_code = pthread_mutexattr_init( &mtx_attr );
62 if( error_code )
63 tbb::internal::handle_perror(error_code,"recursive_mutex: pthread_mutexattr_init failed");
64
65 pthread_mutexattr_settype( &mtx_attr, PTHREAD_MUTEX_RECURSIVE );
66 error_code = pthread_mutex_init( &impl, &mtx_attr );
67 if( error_code )
68 tbb::internal::handle_perror(error_code,"recursive_mutex: pthread_mutex_init failed");
69
70 pthread_mutexattr_destroy( &mtx_attr );
71 #endif /* _WIN32||_WIN64*/
72#endif /* TBB_USE_ASSERT */
73 };
74
75 ~recursive_mutex() {
76#if TBB_USE_ASSERT
77 internal_destroy();
78#else
79 #if _WIN32||_WIN64
80 DeleteCriticalSection(&impl);
81 #else
82 pthread_mutex_destroy(&impl);
83
84 #endif /* _WIN32||_WIN64 */
85#endif /* TBB_USE_ASSERT */
86 };
87
88 class scoped_lock;
89 friend class scoped_lock;
90
92
94 class scoped_lock: internal::no_copy {
95 public:
97 scoped_lock() : my_mutex(NULL) {};
98
100 scoped_lock( recursive_mutex& mutex ) {
101#if TBB_USE_ASSERT
102 my_mutex = &mutex;
103#endif /* TBB_USE_ASSERT */
104 acquire( mutex );
105 }
106
108 ~scoped_lock() {
109 if( my_mutex )
110 release();
111 }
112
114 void acquire( recursive_mutex& mutex ) {
115#if TBB_USE_ASSERT
116 internal_acquire( mutex );
117#else
118 my_mutex = &mutex;
119 mutex.lock();
120#endif /* TBB_USE_ASSERT */
121 }
122
124 bool try_acquire( recursive_mutex& mutex ) {
125#if TBB_USE_ASSERT
126 return internal_try_acquire( mutex );
127#else
128 bool result = mutex.try_lock();
129 if( result )
130 my_mutex = &mutex;
131 return result;
132#endif /* TBB_USE_ASSERT */
133 }
134
136 void release() {
137#if TBB_USE_ASSERT
138 internal_release();
139#else
140 my_mutex->unlock();
141 my_mutex = NULL;
142#endif /* TBB_USE_ASSERT */
143 }
144
145 private:
147 recursive_mutex* my_mutex;
148
150 void __TBB_EXPORTED_METHOD internal_acquire( recursive_mutex& m );
151
153 bool __TBB_EXPORTED_METHOD internal_try_acquire( recursive_mutex& m );
154
156 void __TBB_EXPORTED_METHOD internal_release();
157
158 friend class recursive_mutex;
159 };
160
161 // Mutex traits
162 static const bool is_rw_mutex = false;
163 static const bool is_recursive_mutex = true;
164 static const bool is_fair_mutex = false;
165
166 // C++0x compatibility interface
167
169 void lock() {
170#if TBB_USE_ASSERT
171 aligned_space<scoped_lock> tmp;
172 new(tmp.begin()) scoped_lock(*this);
173#else
174 #if _WIN32||_WIN64
175 EnterCriticalSection(&impl);
176 #else
177 int error_code = pthread_mutex_lock(&impl);
178 if( error_code )
179 tbb::internal::handle_perror(error_code,"recursive_mutex: pthread_mutex_lock failed");
180 #endif /* _WIN32||_WIN64 */
181#endif /* TBB_USE_ASSERT */
182 }
183
185
186 bool try_lock() {
187#if TBB_USE_ASSERT
188 aligned_space<scoped_lock> tmp;
189 return (new(tmp.begin()) scoped_lock)->internal_try_acquire(*this);
190#else
191 #if _WIN32||_WIN64
192 return TryEnterCriticalSection(&impl)!=0;
193 #else
194 return pthread_mutex_trylock(&impl)==0;
195 #endif /* _WIN32||_WIN64 */
196#endif /* TBB_USE_ASSERT */
197 }
198
200 void unlock() {
201#if TBB_USE_ASSERT
202 aligned_space<scoped_lock> tmp;
203 scoped_lock& s = *tmp.begin();
204 s.my_mutex = this;
205 s.internal_release();
206#else
207 #if _WIN32||_WIN64
208 LeaveCriticalSection(&impl);
209 #else
210 pthread_mutex_unlock(&impl);
211 #endif /* _WIN32||_WIN64 */
212#endif /* TBB_USE_ASSERT */
213 }
214
216 #if _WIN32||_WIN64
217 typedef LPCRITICAL_SECTION native_handle_type;
218 #else
219 typedef pthread_mutex_t* native_handle_type;
220 #endif
221 native_handle_type native_handle() { return (native_handle_type) &impl; }
222
223private:
224#if _WIN32||_WIN64
225 CRITICAL_SECTION impl;
226 enum state_t {
227 INITIALIZED=0x1234,
228 DESTROYED=0x789A,
229 } state;
230#else
231 pthread_mutex_t impl;
232#endif /* _WIN32||_WIN64 */
233
235 void __TBB_EXPORTED_METHOD internal_construct();
236
238 void __TBB_EXPORTED_METHOD internal_destroy();
239};
240
241__TBB_DEFINE_PROFILING_SET_NAME(recursive_mutex)
242
243} // namespace tbb
244
246#undef __TBB_recursive_mutex_H_include_area
247
248#endif /* __TBB_recursive_mutex_H */
#define __TBB_DEPRECATED_IN_VERBOSE_MODE_MSG(msg)
Definition tbb_config.h:648
#define __TBB_DEFINE_PROFILING_SET_NAME(sync_object_type)
#define __TBB_EXPORTED_METHOD
Definition tbb_stddef.h:98
void const char const char int ITT_FORMAT __itt_group_sync s
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance ITT_FORMAT p __itt_model_task __itt_model_task_instance ITT_FORMAT p void * lock
The graph class.
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:87

Copyright © 2005-2020 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.