Home ⌂Doc Index ◂Up ▴
Intel(R) Threading Building Blocks Doxygen Documentation  version 4.2.3
mutex.h
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 
18 
19 #if !defined(__TBB_show_deprecation_message_mutex_H) && defined(__TBB_show_deprecated_header_message)
20 #define __TBB_show_deprecation_message_mutex_H
21 #pragma message("TBB Warning: tbb/mutex.h is deprecated. For details, please see Deprecated Features appendix in the TBB reference manual.")
22 #endif
23 
24 #if defined(__TBB_show_deprecated_header_message)
25 #undef __TBB_show_deprecated_header_message
26 #endif
27 
28 #ifndef __TBB_mutex_H
29 #define __TBB_mutex_H
30 
31 #define __TBB_mutex_H_include_area
33 
34 #if _WIN32||_WIN64
35 #include "machine/windows_api.h"
36 #else
37 #include <pthread.h>
38 #endif /* _WIN32||_WIN64 */
39 
40 #include <new>
41 #include "aligned_space.h"
42 #include "tbb_stddef.h"
43 #include "tbb_profiling.h"
44 
45 namespace tbb {
46 
48 
49 class __TBB_DEPRECATED_IN_VERBOSE_MODE_MSG("tbb::critical_section is deprecated, use std::mutex") mutex : internal::mutex_copy_deprecated_and_disabled {
50 public:
52  mutex() {
53 #if TBB_USE_ASSERT || TBB_USE_THREADING_TOOLS
54  internal_construct();
55 #else
56  #if _WIN32||_WIN64
57  InitializeCriticalSectionEx(&impl, 4000, 0);
58  #else
59  int error_code = pthread_mutex_init(&impl,NULL);
60  if( error_code )
61  tbb::internal::handle_perror(error_code,"mutex: pthread_mutex_init failed");
62  #endif /* _WIN32||_WIN64*/
63 #endif /* TBB_USE_ASSERT */
64  };
65 
66  ~mutex() {
67 #if TBB_USE_ASSERT
68  internal_destroy();
69 #else
70  #if _WIN32||_WIN64
71  DeleteCriticalSection(&impl);
72  #else
73  pthread_mutex_destroy(&impl);
74 
75  #endif /* _WIN32||_WIN64 */
76 #endif /* TBB_USE_ASSERT */
77  };
78 
79  class scoped_lock;
80  friend class scoped_lock;
81 
83 
85  class scoped_lock : internal::no_copy {
86  public:
88  scoped_lock() : my_mutex(NULL) {};
89 
91  scoped_lock( mutex& mutex ) {
92  acquire( mutex );
93  }
94 
96  ~scoped_lock() {
97  if( my_mutex )
98  release();
99  }
100 
102  void acquire( mutex& mutex ) {
103 #if TBB_USE_ASSERT
104  internal_acquire(mutex);
105 #else
106  mutex.lock();
107  my_mutex = &mutex;
108 #endif /* TBB_USE_ASSERT */
109  }
110 
112  bool try_acquire( mutex& mutex ) {
113 #if TBB_USE_ASSERT
114  return internal_try_acquire (mutex);
115 #else
116  bool result = mutex.try_lock();
117  if( result )
118  my_mutex = &mutex;
119  return result;
120 #endif /* TBB_USE_ASSERT */
121  }
122 
124  void release() {
125 #if TBB_USE_ASSERT
126  internal_release ();
127 #else
128  my_mutex->unlock();
129  my_mutex = NULL;
130 #endif /* TBB_USE_ASSERT */
131  }
132 
133  private:
135  mutex* my_mutex;
136 
138  void __TBB_EXPORTED_METHOD internal_acquire( mutex& m );
139 
141  bool __TBB_EXPORTED_METHOD internal_try_acquire( mutex& m );
142 
144  void __TBB_EXPORTED_METHOD internal_release();
145 
146  friend class mutex;
147  };
148 
149  // Mutex traits
150  static const bool is_rw_mutex = false;
151  static const bool is_recursive_mutex = false;
152  static const bool is_fair_mutex = false;
153 
154  // ISO C++0x compatibility methods
155 
157  void lock() {
158 #if TBB_USE_ASSERT
159  aligned_space<scoped_lock> tmp;
160  new(tmp.begin()) scoped_lock(*this);
161 #else
162  #if _WIN32||_WIN64
163  EnterCriticalSection(&impl);
164  #else
165  int error_code = pthread_mutex_lock(&impl);
166  if( error_code )
167  tbb::internal::handle_perror(error_code,"mutex: pthread_mutex_lock failed");
168  #endif /* _WIN32||_WIN64 */
169 #endif /* TBB_USE_ASSERT */
170  }
171 
173 
174  bool try_lock() {
175 #if TBB_USE_ASSERT
176  aligned_space<scoped_lock> tmp;
177  scoped_lock& s = *tmp.begin();
178  s.my_mutex = NULL;
179  return s.internal_try_acquire(*this);
180 #else
181  #if _WIN32||_WIN64
182  return TryEnterCriticalSection(&impl)!=0;
183  #else
184  return pthread_mutex_trylock(&impl)==0;
185  #endif /* _WIN32||_WIN64 */
186 #endif /* TBB_USE_ASSERT */
187  }
188 
190  void unlock() {
191 #if TBB_USE_ASSERT
192  aligned_space<scoped_lock> tmp;
193  scoped_lock& s = *tmp.begin();
194  s.my_mutex = this;
195  s.internal_release();
196 #else
197  #if _WIN32||_WIN64
198  LeaveCriticalSection(&impl);
199  #else
200  pthread_mutex_unlock(&impl);
201  #endif /* _WIN32||_WIN64 */
202 #endif /* TBB_USE_ASSERT */
203  }
204 
206  #if _WIN32||_WIN64
207  typedef LPCRITICAL_SECTION native_handle_type;
208  #else
209  typedef pthread_mutex_t* native_handle_type;
210  #endif
211  native_handle_type native_handle() { return (native_handle_type) &impl; }
212 
213  enum state_t {
214  INITIALIZED=0x1234,
215  DESTROYED=0x789A,
216  HELD=0x56CD
217  };
218 private:
219 #if _WIN32||_WIN64
220  CRITICAL_SECTION impl;
221  enum state_t state;
222 #else
223  pthread_mutex_t impl;
224 #endif /* _WIN32||_WIN64 */
225 
227  void __TBB_EXPORTED_METHOD internal_construct();
228 
230  void __TBB_EXPORTED_METHOD internal_destroy();
231 
232 #if _WIN32||_WIN64
233 public:
235  void set_state( state_t to ) { state = to; }
236 #endif
237 };
238 
240 
241 } // namespace tbb
242 
244 #undef __TBB_mutex_H_include_area
245 
246 #endif /* __TBB_mutex_H */
class __TBB_DEPRECATED_IN_VERBOSE_MODE_MSG("tbb::aligned_space is deprecated, use std::aligned_storage") aligned_space
Block of space aligned sufficiently to construct an array T with N elements.
Definition: aligned_space.h:43
#define __TBB_DEFINE_PROFILING_SET_NAME(sync_object_type)
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
Release.
Definition: atomic.h:59
void const char const char int ITT_FORMAT __itt_group_sync s
#define __TBB_EXPORTED_METHOD
Definition: tbb_stddef.h:98
Acquire.
Definition: atomic.h:57
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance ITT_FORMAT p __itt_model_task __itt_model_task_instance ITT_FORMAT p void * lock
The graph class.

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.