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

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.