-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathrecursive_mutex.cpp
More file actions
136 lines (120 loc) · 4.34 KB
/
recursive_mutex.cpp
File metadata and controls
136 lines (120 loc) · 4.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
Copyright (c) 2005-2017 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "tbb/recursive_mutex.h"
#include "itt_notify.h"
namespace tbb {
void recursive_mutex::scoped_lock::internal_acquire( recursive_mutex& m ) {
#if _WIN32||_WIN64
switch( m.state ) {
case INITIALIZED:
// since we cannot look into the internal of the CriticalSection object
// we won't know how many times the lock has been acquired, and thus
// we won't know when we may safely set the state back to INITIALIZED
// if we change the state to HELD as in mutex.cpp. thus, we won't change
// the state for recursive_mutex
EnterCriticalSection( &m.impl );
break;
case DESTROYED:
__TBB_ASSERT(false,"recursive_mutex::scoped_lock: mutex already destroyed");
break;
default:
__TBB_ASSERT(false,"recursive_mutex::scoped_lock: illegal mutex state");
break;
}
#else
int error_code = pthread_mutex_lock(&m.impl);
if( error_code )
tbb::internal::handle_perror(error_code,"recursive_mutex::scoped_lock: pthread_mutex_lock failed");
#endif /* _WIN32||_WIN64 */
my_mutex = &m;
}
void recursive_mutex::scoped_lock::internal_release() {
__TBB_ASSERT( my_mutex, "recursive_mutex::scoped_lock: not holding a mutex" );
#if _WIN32||_WIN64
switch( my_mutex->state ) {
case INITIALIZED:
LeaveCriticalSection( &my_mutex->impl );
break;
case DESTROYED:
__TBB_ASSERT(false,"recursive_mutex::scoped_lock: mutex already destroyed");
break;
default:
__TBB_ASSERT(false,"recursive_mutex::scoped_lock: illegal mutex state");
break;
}
#else
int error_code = pthread_mutex_unlock(&my_mutex->impl);
__TBB_ASSERT_EX(!error_code, "recursive_mutex::scoped_lock: pthread_mutex_unlock failed");
#endif /* _WIN32||_WIN64 */
my_mutex = NULL;
}
bool recursive_mutex::scoped_lock::internal_try_acquire( recursive_mutex& m ) {
#if _WIN32||_WIN64
switch( m.state ) {
case INITIALIZED:
break;
case DESTROYED:
__TBB_ASSERT(false,"recursive_mutex::scoped_lock: mutex already destroyed");
break;
default:
__TBB_ASSERT(false,"recursive_mutex::scoped_lock: illegal mutex state");
break;
}
#endif /* _WIN32||_WIN64 */
bool result;
#if _WIN32||_WIN64
result = TryEnterCriticalSection(&m.impl)!=0;
#else
result = pthread_mutex_trylock(&m.impl)==0;
#endif /* _WIN32||_WIN64 */
if( result )
my_mutex = &m;
return result;
}
void recursive_mutex::internal_construct() {
#if _WIN32||_WIN64
InitializeCriticalSectionEx(&impl, 4000, 0);
state = INITIALIZED;
#else
pthread_mutexattr_t mtx_attr;
int error_code = pthread_mutexattr_init( &mtx_attr );
if( error_code )
tbb::internal::handle_perror(error_code,"recursive_mutex: pthread_mutexattr_init failed");
pthread_mutexattr_settype( &mtx_attr, PTHREAD_MUTEX_RECURSIVE );
error_code = pthread_mutex_init( &impl, &mtx_attr );
if( error_code )
tbb::internal::handle_perror(error_code,"recursive_mutex: pthread_mutex_init failed");
pthread_mutexattr_destroy( &mtx_attr );
#endif /* _WIN32||_WIN64*/
ITT_SYNC_CREATE(&impl, _T("tbb::recursive_mutex"), _T(""));
}
void recursive_mutex::internal_destroy() {
#if _WIN32||_WIN64
switch( state ) {
case INITIALIZED:
DeleteCriticalSection(&impl);
break;
case DESTROYED:
__TBB_ASSERT(false,"recursive_mutex: already destroyed");
break;
default:
__TBB_ASSERT(false,"recursive_mutex: illegal state for destruction");
break;
}
state = DESTROYED;
#else
int error_code = pthread_mutex_destroy(&impl);
__TBB_ASSERT_EX(!error_code,"recursive_mutex: pthread_mutex_destroy failed");
#endif /* _WIN32||_WIN64 */
}
} // namespace tbb