Intel(R) Threading Building Blocks Doxygen Documentation  version 4.2.3
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 #if _WIN32||_WIN64
22 #include <errno.h> // EDEADLK
23 #endif
24 #include "tbb/mutex.h"
25 #include "itt_notify.h"
26 #if __TBB_TSX_AVAILABLE
27 #include "governor.h" // for speculation_enabled()
28 #endif
29 
30 namespace tbb {
32 
33 #if _WIN32||_WIN64
34  switch( m.state ) {
35  case INITIALIZED:
36  case HELD:
37  EnterCriticalSection( &m.impl );
38  // If a thread comes here, and another thread holds the lock, it will block
39  // in EnterCriticalSection. When it returns from EnterCriticalSection,
40  // m.state must be set to INITIALIZED. If the same thread tries to acquire a lock it
41  // already holds, the lock is in HELD state, thus will cause throwing the exception.
42  if (m.state==HELD)
43  tbb::internal::handle_perror(EDEADLK,"mutex::scoped_lock: deadlock caused by attempt to reacquire held mutex");
44  m.state = HELD;
45  break;
46  case DESTROYED:
47  __TBB_ASSERT(false,"mutex::scoped_lock: mutex already destroyed");
48  break;
49  default:
50  __TBB_ASSERT(false,"mutex::scoped_lock: illegal mutex state");
51  break;
52  }
53 #else
54  int error_code = pthread_mutex_lock(&m.impl);
55  if( error_code )
56  tbb::internal::handle_perror(error_code,"mutex::scoped_lock: pthread_mutex_lock failed");
57 #endif /* _WIN32||_WIN64 */
58  my_mutex = &m;
59  }
60 
62  __TBB_ASSERT( my_mutex, "mutex::scoped_lock: not holding a mutex" );
63 #if _WIN32||_WIN64
64  switch( my_mutex->state ) {
65  case INITIALIZED:
66  __TBB_ASSERT(false,"mutex::scoped_lock: try to release the lock without acquisition");
67  break;
68  case HELD:
69  my_mutex->state = INITIALIZED;
70  LeaveCriticalSection(&my_mutex->impl);
71  break;
72  case DESTROYED:
73  __TBB_ASSERT(false,"mutex::scoped_lock: mutex already destroyed");
74  break;
75  default:
76  __TBB_ASSERT(false,"mutex::scoped_lock: illegal mutex state");
77  break;
78  }
79 #else
80  int error_code = pthread_mutex_unlock(&my_mutex->impl);
81  __TBB_ASSERT_EX(!error_code, "mutex::scoped_lock: pthread_mutex_unlock failed");
82 #endif /* _WIN32||_WIN64 */
83  my_mutex = NULL;
84 }
85 
87 #if _WIN32||_WIN64
88  switch( m.state ) {
89  case INITIALIZED:
90  case HELD:
91  break;
92  case DESTROYED:
93  __TBB_ASSERT(false,"mutex::scoped_lock: mutex already destroyed");
94  break;
95  default:
96  __TBB_ASSERT(false,"mutex::scoped_lock: illegal mutex state");
97  break;
98  }
99 #endif /* _WIN32||_WIN64 */
100 
101  bool result;
102 #if _WIN32||_WIN64
103  result = TryEnterCriticalSection(&m.impl)!=0;
104  if( result ) {
105  __TBB_ASSERT(m.state!=HELD, "mutex::scoped_lock: deadlock caused by attempt to reacquire held mutex");
106  m.state = HELD;
107  }
108 #else
109  result = pthread_mutex_trylock(&m.impl)==0;
110 #endif /* _WIN32||_WIN64 */
111  if( result )
112  my_mutex = &m;
113  return result;
114 }
115 
117 #if _WIN32||_WIN64
118  InitializeCriticalSectionEx(&impl, 4000, 0);
119  state = INITIALIZED;
120 #else
121  int error_code = pthread_mutex_init(&impl,NULL);
122  if( error_code )
123  tbb::internal::handle_perror(error_code,"mutex: pthread_mutex_init failed");
124 #endif /* _WIN32||_WIN64*/
125  ITT_SYNC_CREATE(&impl, _T("tbb::mutex"), _T(""));
126 }
127 
129 #if _WIN32||_WIN64
130  switch( state ) {
131  case INITIALIZED:
132  DeleteCriticalSection(&impl);
133  break;
134  case DESTROYED:
135  __TBB_ASSERT(false,"mutex: already destroyed");
136  break;
137  default:
138  __TBB_ASSERT(false,"mutex: illegal state for destruction");
139  break;
140  }
141  state = DESTROYED;
142 #else
143  int error_code = pthread_mutex_destroy(&impl);
144 #if __TBB_TSX_AVAILABLE
145  // For processors with speculative execution, skip the error code check due to glibc bug #16657
147 #endif
148  __TBB_ASSERT_EX(!error_code,"mutex: pthread_mutex_destroy failed");
149 #endif /* _WIN32||_WIN64 */
150 }
151 
152 } // namespace tbb
static bool speculation_enabled()
Definition: governor.h:155
#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
mutex * my_mutex
The pointer to the current mutex to work.
Definition: mutex.h:125
void __TBB_EXPORTED_METHOD internal_release()
All checks from release using mutex.state were moved here.
Definition: mutex.cpp:61
#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
The graph class.
#define _T(string_literal)
Standard Windows style macro to markup the string literals.
Definition: itt_notify.h:66
void __TBB_EXPORTED_METHOD internal_construct()
All checks from mutex constructor using mutex.state were moved here.
Definition: mutex.cpp:116
bool __TBB_EXPORTED_METHOD internal_try_acquire(mutex &m)
All checks from try_acquire using mutex.state were moved here.
Definition: mutex.cpp:86
void __TBB_EXPORTED_METHOD internal_destroy()
All checks from mutex destructor using mutex.state were moved here.
Definition: mutex.cpp:128
Wrapper around the platform's native lock.
Definition: mutex.h:39
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
pthread_mutex_t impl
Definition: mutex.h:213
void __TBB_EXPORTED_METHOD internal_acquire(mutex &m)
All checks from acquire using mutex.state were moved here.
Definition: mutex.cpp:31

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.