Home ⌂Doc Index ◂Up ▴
Intel(R) Threading Building Blocks Doxygen Documentation  version 4.2.3
critical_section.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_critical_section_H) && defined(__TBB_show_deprecated_header_message)
20 #define __TBB_show_deprecation_message_critical_section_H
21 #pragma message("TBB Warning: tbb/critical_section.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_CRITICAL_SECTION_H_
29 #define _TBB_CRITICAL_SECTION_H_
30 
31 #define __TBB_critical_section_H_include_area
33 
34 #if _WIN32||_WIN64
35 #include "machine/windows_api.h"
36 #else
37 #include <pthread.h>
38 #include <errno.h>
39 #endif // _WIN32||WIN64
40 
41 #include "tbb_stddef.h"
42 #include "tbb_thread.h"
43 #include "tbb_exception.h"
44 
45 #include "tbb_profiling.h"
46 
47 namespace tbb {
48 
49  namespace internal {
51 #if _WIN32||_WIN64
52  CRITICAL_SECTION my_impl;
53 #else
54  pthread_mutex_t my_impl;
55 #endif
57 public:
58 
60 
62 #if _WIN32||_WIN64
63  InitializeCriticalSectionEx( &my_impl, 4000, 0 );
64 #else
65  pthread_mutex_init(&my_impl, NULL);
66 #endif
68  }
69 
71  __TBB_ASSERT(my_tid == tbb_thread::id(), "Destroying a still-held critical section");
72 #if _WIN32||_WIN64
73  DeleteCriticalSection(&my_impl);
74 #else
75  pthread_mutex_destroy(&my_impl);
76 #endif
77  }
78 
80  private:
82  public:
83  scoped_lock( critical_section_v4& lock_me) :my_crit(lock_me) {
84  my_crit.lock();
85  }
86 
88  my_crit.unlock();
89  }
90  };
91 
92  void lock() {
94  if(local_tid == my_tid) throw_exception( eid_improper_lock );
95 #if _WIN32||_WIN64
96  EnterCriticalSection( &my_impl );
97 #else
98  int rval = pthread_mutex_lock(&my_impl);
99  __TBB_ASSERT_EX(!rval, "critical_section::lock: pthread_mutex_lock failed");
100 #endif
101  __TBB_ASSERT(my_tid == tbb_thread::id(), NULL);
102  my_tid = local_tid;
103  }
104 
105  bool try_lock() {
106  bool gotlock;
108  if(local_tid == my_tid) return false;
109 #if _WIN32||_WIN64
110  gotlock = TryEnterCriticalSection( &my_impl ) != 0;
111 #else
112  int rval = pthread_mutex_trylock(&my_impl);
113  // valid returns are 0 (locked) and [EBUSY]
114  __TBB_ASSERT(rval == 0 || rval == EBUSY, "critical_section::trylock: pthread_mutex_trylock failed");
115  gotlock = rval == 0;
116 #endif
117  if(gotlock) {
118  my_tid = local_tid;
119  }
120  return gotlock;
121  }
122 
123  void unlock() {
124  __TBB_ASSERT(this_tbb_thread::get_id() == my_tid, "thread unlocking critical_section is not thread that locked it");
126 #if _WIN32||_WIN64
127  LeaveCriticalSection( &my_impl );
128 #else
129  int rval = pthread_mutex_unlock(&my_impl);
130  __TBB_ASSERT_EX(!rval, "critical_section::unlock: pthread_mutex_unlock failed");
131 #endif
132  }
133 
134  static const bool is_rw_mutex = false;
135  static const bool is_recursive_mutex = false;
136  static const bool is_fair_mutex = true;
137 }; // critical_section_v4
138 } // namespace internal
139 __TBB_DEPRECATED_IN_VERBOSE_MODE_MSG("tbb::critical_section is deprecated, use std::mutex") typedef internal::critical_section_v4 critical_section;
140 
141 __TBB_DEFINE_PROFILING_SET_NAME(critical_section)
142 } // namespace tbb
143 
144 #include "internal/_warning_suppress_disable_notice.h"
145 #undef __TBB_critical_section_H_include_area
146 
147 #endif // _TBB_CRITICAL_SECTION_H_
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 ITT_FORMAT p void ITT_FORMAT p void size_t ITT_FORMAT d void ITT_FORMAT p const wchar_t ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s no args void ITT_FORMAT p size_t ITT_FORMAT d no args const wchar_t const wchar_t ITT_FORMAT s __itt_heap_function void size_t int ITT_FORMAT d __itt_heap_function void ITT_FORMAT p __itt_heap_function void void size_t int ITT_FORMAT d no args no args unsigned int ITT_FORMAT u const __itt_domain __itt_id id
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)
__TBB_DEPRECATED_IN_VERBOSE_MODE tbb_thread::id get_id()
Definition: tbb_thread.h:331
void __TBB_EXPORTED_METHOD internal_construct()
#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
Base class for types that should not be copied or assigned.
Definition: tbb_stddef.h:330
#define __TBB_EXPORTED_METHOD
Definition: tbb_stddef.h:98
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:165
void throw_exception(exception_id eid)
Versionless convenience wrapper for throw_exception_v4()
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.