File indexing completed on 2025-01-18 10:12:54
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 #include "internal/_deprecated_header_message_guard.h"
0018
0019 #if !defined(__TBB_show_deprecation_message_critical_section_H) && defined(__TBB_show_deprecated_header_message)
0020 #define __TBB_show_deprecation_message_critical_section_H
0021 #pragma message("TBB Warning: tbb/critical_section.h is deprecated. For details, please see Deprecated Features appendix in the TBB reference manual.")
0022 #endif
0023
0024 #if defined(__TBB_show_deprecated_header_message)
0025 #undef __TBB_show_deprecated_header_message
0026 #endif
0027
0028 #ifndef _TBB_CRITICAL_SECTION_H_
0029 #define _TBB_CRITICAL_SECTION_H_
0030
0031 #define __TBB_critical_section_H_include_area
0032 #include "internal/_warning_suppress_enable_notice.h"
0033
0034 #if _WIN32||_WIN64
0035 #include "machine/windows_api.h"
0036 #else
0037 #include <pthread.h>
0038 #include <errno.h>
0039 #endif
0040
0041 #include "tbb_stddef.h"
0042 #include "tbb_thread.h"
0043 #include "tbb_exception.h"
0044
0045 #include "tbb_profiling.h"
0046
0047 namespace tbb {
0048
0049 namespace internal {
0050 class critical_section_v4 : internal::no_copy {
0051 #if _WIN32||_WIN64
0052 CRITICAL_SECTION my_impl;
0053 #else
0054 pthread_mutex_t my_impl;
0055 #endif
0056 tbb_thread::id my_tid;
0057 public:
0058
0059 void __TBB_EXPORTED_METHOD internal_construct();
0060
0061 critical_section_v4() {
0062 #if _WIN32||_WIN64
0063 InitializeCriticalSectionEx( &my_impl, 4000, 0 );
0064 #else
0065 pthread_mutex_init(&my_impl, NULL);
0066 #endif
0067 internal_construct();
0068 }
0069
0070 ~critical_section_v4() {
0071 __TBB_ASSERT(my_tid == tbb_thread::id(), "Destroying a still-held critical section");
0072 #if _WIN32||_WIN64
0073 DeleteCriticalSection(&my_impl);
0074 #else
0075 pthread_mutex_destroy(&my_impl);
0076 #endif
0077 }
0078
0079 class scoped_lock : internal::no_copy {
0080 private:
0081 critical_section_v4 &my_crit;
0082 public:
0083 scoped_lock( critical_section_v4& lock_me) :my_crit(lock_me) {
0084 my_crit.lock();
0085 }
0086
0087 ~scoped_lock() {
0088 my_crit.unlock();
0089 }
0090 };
0091
0092 void lock() {
0093 tbb_thread::id local_tid = this_tbb_thread::get_id();
0094 if(local_tid == my_tid) throw_exception( eid_improper_lock );
0095 #if _WIN32||_WIN64
0096 EnterCriticalSection( &my_impl );
0097 #else
0098 int rval = pthread_mutex_lock(&my_impl);
0099 __TBB_ASSERT_EX(!rval, "critical_section::lock: pthread_mutex_lock failed");
0100 #endif
0101 __TBB_ASSERT(my_tid == tbb_thread::id(), NULL);
0102 my_tid = local_tid;
0103 }
0104
0105 bool try_lock() {
0106 bool gotlock;
0107 tbb_thread::id local_tid = this_tbb_thread::get_id();
0108 if(local_tid == my_tid) return false;
0109 #if _WIN32||_WIN64
0110 gotlock = TryEnterCriticalSection( &my_impl ) != 0;
0111 #else
0112 int rval = pthread_mutex_trylock(&my_impl);
0113
0114 __TBB_ASSERT(rval == 0 || rval == EBUSY, "critical_section::trylock: pthread_mutex_trylock failed");
0115 gotlock = rval == 0;
0116 #endif
0117 if(gotlock) {
0118 my_tid = local_tid;
0119 }
0120 return gotlock;
0121 }
0122
0123 void unlock() {
0124 __TBB_ASSERT(this_tbb_thread::get_id() == my_tid, "thread unlocking critical_section is not thread that locked it");
0125 my_tid = tbb_thread::id();
0126 #if _WIN32||_WIN64
0127 LeaveCriticalSection( &my_impl );
0128 #else
0129 int rval = pthread_mutex_unlock(&my_impl);
0130 __TBB_ASSERT_EX(!rval, "critical_section::unlock: pthread_mutex_unlock failed");
0131 #endif
0132 }
0133
0134 static const bool is_rw_mutex = false;
0135 static const bool is_recursive_mutex = false;
0136 static const bool is_fair_mutex = true;
0137 };
0138 }
0139 __TBB_DEPRECATED_IN_VERBOSE_MODE_MSG("tbb::critical_section is deprecated, use std::mutex") typedef internal::critical_section_v4 critical_section;
0140
0141 __TBB_DEFINE_PROFILING_SET_NAME(critical_section)
0142 }
0143
0144 #include "internal/_warning_suppress_disable_notice.h"
0145 #undef __TBB_critical_section_H_include_area
0146
0147 #endif