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/tbb_machine.h"
00027
00028 namespace tbb {
00029
00031
00036 class spin_mutex {
00038 unsigned char flag;
00039
00040 public:
00042
00043 spin_mutex() : flag(0) {}
00044
00046 class scoped_lock : private internal::no_copy {
00047 private:
00049 spin_mutex* my_mutex;
00050
00052 internal::uintptr my_unlock_value;
00053
00055 void internal_acquire( spin_mutex& m );
00056
00058 bool internal_try_acquire( spin_mutex& m );
00059
00061 void internal_release();
00062
00063 public:
00065 scoped_lock() : my_mutex(NULL), my_unlock_value(0) {}
00066
00068 scoped_lock( spin_mutex& m ) {
00069 #if TBB_DO_THREADING_TOOLS||TBB_DO_ASSERT
00070 my_mutex=NULL;
00071 internal_acquire(m);
00072 #else
00073 my_unlock_value = __TBB_LockByte(m.flag);
00074 my_mutex=&m;
00075 #endif
00076 }
00077
00079 void acquire( spin_mutex& m ) {
00080 #if TBB_DO_THREADING_TOOLS||TBB_DO_ASSERT
00081 internal_acquire(m);
00082 #else
00083 my_unlock_value = __TBB_LockByte(m.flag);
00084 my_mutex = &m;
00085 #endif
00086 }
00087
00089 bool try_acquire( spin_mutex& m ) {
00090 #if TBB_DO_THREADING_TOOLS||TBB_DO_ASSERT
00091 return internal_try_acquire(m);
00092 #else
00093 bool result = __TBB_TryLockByte(m.flag);
00094 if( result ) {
00095 my_unlock_value = 0;
00096 my_mutex = &m;
00097 }
00098 return result;
00099 #endif
00100 }
00101
00103 void release() {
00104 #if TBB_DO_THREADING_TOOLS||TBB_DO_ASSERT
00105 internal_release();
00106 #else
00107 __TBB_store_with_release(my_mutex->flag, static_cast<unsigned char>(my_unlock_value));
00108 my_mutex = NULL;
00109 #endif
00110 }
00111
00113 ~scoped_lock() {
00114 if( my_mutex ) {
00115 #if TBB_DO_THREADING_TOOLS||TBB_DO_ASSERT
00116 internal_release();
00117 #else
00118 __TBB_store_with_release(my_mutex->flag, static_cast<unsigned char>(my_unlock_value));
00119 #endif
00120 }
00121 }
00122 };
00123
00124
00125 static const bool is_rw_mutex = false;
00126 static const bool is_recursive_mutex = false;
00127 static const bool is_fair_mutex = false;
00128
00129 friend class scoped_lock;
00130 };
00131
00132 }
00133
00134 #endif