Intel(R) Threading Building Blocks Doxygen Documentation version 4.2.3
Loading...
Searching...
No Matches
recursive_mutex.cpp
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
17#include "tbb/recursive_mutex.h"
18#include "itt_notify.h"
19
20namespace tbb {
21
22void recursive_mutex::scoped_lock::internal_acquire( recursive_mutex& m ) {
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}
47
48void recursive_mutex::scoped_lock::internal_release() {
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}
68
69bool recursive_mutex::scoped_lock::internal_try_acquire( recursive_mutex& m ) {
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}
92
93void recursive_mutex::internal_construct() {
94#if _WIN32||_WIN64
95 InitializeCriticalSectionEx(&impl, 4000, 0);
96 state = INITIALIZED;
97#else
98 pthread_mutexattr_t mtx_attr;
99 int error_code = pthread_mutexattr_init( &mtx_attr );
100 if( error_code )
101 tbb::internal::handle_perror(error_code,"recursive_mutex: pthread_mutexattr_init failed");
102
103 pthread_mutexattr_settype( &mtx_attr, PTHREAD_MUTEX_RECURSIVE );
104 error_code = pthread_mutex_init( &impl, &mtx_attr );
105 if( error_code )
106 tbb::internal::handle_perror(error_code,"recursive_mutex: pthread_mutex_init failed");
107 pthread_mutexattr_destroy( &mtx_attr );
108#endif /* _WIN32||_WIN64*/
109 ITT_SYNC_CREATE(&impl, _T("tbb::recursive_mutex"), _T(""));
110}
111
112void recursive_mutex::internal_destroy() {
113#if _WIN32||_WIN64
114 switch( state ) {
115 case INITIALIZED:
116 DeleteCriticalSection(&impl);
117 break;
118 case DESTROYED:
119 __TBB_ASSERT(false,"recursive_mutex: already destroyed");
120 break;
121 default:
122 __TBB_ASSERT(false,"recursive_mutex: illegal state for destruction");
123 break;
124 }
125 state = DESTROYED;
126#else
127 int error_code = pthread_mutex_destroy(&impl);
128 __TBB_ASSERT_EX(!error_code,"recursive_mutex: pthread_mutex_destroy failed");
129#endif /* _WIN32||_WIN64 */
130}
131
132} // namespace tbb
#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
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition tbb_stddef.h:165
#define _T(string_literal)
Standard Windows style macro to markup the string literals.
Definition itt_notify.h:59
#define ITT_SYNC_CREATE(obj, type, name)
Definition itt_notify.h:115
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.