00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef __TBB_spin_mutex_H
00022 #define __TBB_spin_mutex_H
00023
00024 #include <cstddef>
00025 #include "tbb_stddef.h"
00026 #include "tbb_machine.h"
00027 #include "tbb_profiling.h"
00028
00029 namespace tbb {
00030
00032
00037 class spin_mutex {
00039 unsigned char flag;
00040
00041 public:
00043
00044 spin_mutex() : flag(0) {
00045 #if TBB_USE_THREADING_TOOLS
00046 internal_construct();
00047 #endif
00048 }
00049
00051 class scoped_lock : internal::no_copy {
00052 private:
00054 spin_mutex* my_mutex;
00055
00057 internal::uintptr my_unlock_value;
00058
00060 void __TBB_EXPORTED_METHOD internal_acquire( spin_mutex& m );
00061
00063 bool __TBB_EXPORTED_METHOD internal_try_acquire( spin_mutex& m );
00064
00066 void __TBB_EXPORTED_METHOD internal_release();
00067
00068 public:
00070 scoped_lock() : my_mutex(NULL), my_unlock_value(0) {}
00071
00073 scoped_lock( spin_mutex& m ) {
00074 #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
00075 my_mutex=NULL;
00076 internal_acquire(m);
00077 #else
00078 my_unlock_value = __TBB_LockByte(m.flag);
00079 my_mutex=&m;
00080 #endif
00081 }
00082
00084 void acquire( spin_mutex& m ) {
00085 #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
00086 internal_acquire(m);
00087 #else
00088 my_unlock_value = __TBB_LockByte(m.flag);
00089 my_mutex = &m;
00090 #endif
00091 }
00092
00094 bool try_acquire( spin_mutex& m ) {
00095 #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
00096 return internal_try_acquire(m);
00097 #else
00098 bool result = __TBB_TryLockByte(m.flag);
00099 if( result ) {
00100 my_unlock_value = 0;
00101 my_mutex = &m;
00102 }
00103 return result;
00104 #endif
00105 }
00106
00108 void release() {
00109 #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
00110 internal_release();
00111 #else
00112 __TBB_store_with_release(my_mutex->flag, static_cast<unsigned char>(my_unlock_value));
00113 my_mutex = NULL;
00114 #endif
00115 }
00116
00118 ~scoped_lock() {
00119 if( my_mutex ) {
00120 #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
00121 internal_release();
00122 #else
00123 __TBB_store_with_release(my_mutex->flag, static_cast<unsigned char>(my_unlock_value));
00124 #endif
00125 }
00126 }
00127 };
00128
00129 void __TBB_EXPORTED_METHOD internal_construct();
00130
00131
00132 static const bool is_rw_mutex = false;
00133 static const bool is_recursive_mutex = false;
00134 static const bool is_fair_mutex = false;
00135
00136 friend class scoped_lock;
00137 };
00138
00139 __TBB_DEFINE_PROFILING_SET_NAME(spin_mutex)
00140
00141 }
00142
00143 #endif