Home ⌂Doc Index ◂Up ▴
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-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 #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 {
27  void mutex::scoped_lock::internal_acquire( mutex& m ) {
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 
57 void mutex::scoped_lock::internal_release() {
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 
82 bool mutex::scoped_lock::internal_try_acquire( mutex& m ) {
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 
112 void mutex::internal_construct() {
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 
124 void mutex::internal_destroy() {
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_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
#define _T(string_literal)
Standard Windows style macro to markup the string literals.
Definition: itt_notify.h:59
#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 ITT_SYNC_CREATE(obj, type, name)
Definition: itt_notify.h:115
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:165
The graph class.
static bool speculation_enabled()
Definition: governor.h:158

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.