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-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 
18 
19 */
20 
21 #ifndef _TBB_CRITICAL_SECTION_H_
22 #define _TBB_CRITICAL_SECTION_H_
23 
24 #if _WIN32||_WIN64
25 #include "machine/windows_api.h"
26 #else
27 #include <pthread.h>
28 #include <errno.h>
29 #endif // _WIN32||WIN64
30 
31 #include "tbb_stddef.h"
32 #include "tbb_thread.h"
33 #include "tbb_exception.h"
34 
35 #include "tbb_profiling.h"
36 
37 namespace tbb {
38 
39  namespace internal {
40 class critical_section_v4 : internal::no_copy {
41 #if _WIN32||_WIN64
42  CRITICAL_SECTION my_impl;
43 #else
44  pthread_mutex_t my_impl;
45 #endif
47 public:
48 
50 
52 #if _WIN32||_WIN64
53  InitializeCriticalSectionEx( &my_impl, 4000, 0 );
54 #else
55  pthread_mutex_init(&my_impl, NULL);
56 #endif
58  }
59 
61  __TBB_ASSERT(my_tid == tbb_thread::id(), "Destroying a still-held critical section");
62 #if _WIN32||_WIN64
63  DeleteCriticalSection(&my_impl);
64 #else
65  pthread_mutex_destroy(&my_impl);
66 #endif
67  }
68 
69  class scoped_lock : internal::no_copy {
70  private:
72  public:
73  scoped_lock( critical_section_v4& lock_me) :my_crit(lock_me) {
74  my_crit.lock();
75  }
76 
78  my_crit.unlock();
79  }
80  };
81 
82  void lock() {
84  if(local_tid == my_tid) throw_exception( eid_improper_lock );
85 #if _WIN32||_WIN64
86  EnterCriticalSection( &my_impl );
87 #else
88  int rval = pthread_mutex_lock(&my_impl);
89  __TBB_ASSERT_EX(!rval, "critical_section::lock: pthread_mutex_lock failed");
90 #endif
92  my_tid = local_tid;
93  }
94 
95  bool try_lock() {
96  bool gotlock;
98  if(local_tid == my_tid) return false;
99 #if _WIN32||_WIN64
100  gotlock = TryEnterCriticalSection( &my_impl ) != 0;
101 #else
102  int rval = pthread_mutex_trylock(&my_impl);
103  // valid returns are 0 (locked) and [EBUSY]
104  __TBB_ASSERT(rval == 0 || rval == EBUSY, "critical_section::trylock: pthread_mutex_trylock failed");
105  gotlock = rval == 0;
106 #endif
107  if(gotlock) {
108  my_tid = local_tid;
109  }
110  return gotlock;
111  }
112 
113  void unlock() {
114  __TBB_ASSERT(this_tbb_thread::get_id() == my_tid, "thread unlocking critical_section is not thread that locked it");
116 #if _WIN32||_WIN64
117  LeaveCriticalSection( &my_impl );
118 #else
119  int rval = pthread_mutex_unlock(&my_impl);
120  __TBB_ASSERT_EX(!rval, "critical_section::unlock: pthread_mutex_unlock failed");
121 #endif
122  }
123 
124  static const bool is_rw_mutex = false;
125  static const bool is_recursive_mutex = false;
126  static const bool is_fair_mutex = true;
127 }; // critical_section_v4
128 } // namespace internal
129 typedef internal::critical_section_v4 critical_section;
130 
132 } // namespace tbb
133 #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
#define __TBB_ASSERT(predicate, comment)
No-op version of __TBB_ASSERT.
Definition: tbb_stddef.h:169
#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:171
void throw_exception(exception_id eid)
Versionless convenience wrapper for throw_exception_v4()
#define __TBB_EXPORTED_METHOD
Definition: tbb_stddef.h:102
void __TBB_EXPORTED_METHOD internal_construct()
The graph class.
#define __TBB_DEFINE_PROFILING_SET_NAME(sync_object_type)
tbb_thread::id get_id()
Definition: tbb_thread.h:321
internal::critical_section_v4 critical_section

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.