Intel(R) Threading Building Blocks Doxygen Documentation  version 4.2.3
recursive_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 #include "tbb/recursive_mutex.h"
18 #include "itt_notify.h"
19 
20 namespace tbb {
21 
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 
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 
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 
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 
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
void __TBB_EXPORTED_METHOD internal_construct()
All checks from mutex constructor using mutex.state were moved here.
#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
The graph class.
#define _T(string_literal)
Standard Windows style macro to markup the string literals.
Definition: itt_notify.h:62
void __TBB_EXPORTED_METHOD internal_destroy()
All checks from mutex destructor using mutex.state were moved here.
pthread_mutex_t impl
bool __TBB_EXPORTED_METHOD internal_try_acquire(recursive_mutex &m)
All checks from try_acquire using mutex.state were moved here.
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:165
void __TBB_EXPORTED_METHOD internal_release()
All checks from release using mutex.state were moved here.
#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
void __TBB_EXPORTED_METHOD internal_acquire(recursive_mutex &m)
All checks from acquire using mutex.state were moved here.
recursive_mutex * my_mutex
The pointer to the current recursive_mutex to work.
Mutex that allows recursive mutex acquisition.

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.