00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef UT_SPINLOCK_H
00020 #define UT_SPINLOCK_H
00021
00022 #include "UT_API.h"
00023
00024 #include "UT_Thread.h"
00025 #include <SYS/SYS_AtomicInt.h>
00026
00027 class UT_API UT_SpinLock
00028 {
00029 public:
00030
00031
00032
00033 explicit UT_SpinLock(int lockstate = 0, bool threadlock = false);
00034 ~UT_SpinLock();
00035
00036
00037 bool tryLock();
00038
00039
00040
00041
00042
00043
00044
00045
00046 bool lock(int ntrials);
00047 bool lockLW(int ntrials);
00048 bool lockNap(int ntrials);
00049
00050
00051 void lock();
00052 void lockLW();
00053 void lockNap();
00054
00055
00056 void unlock();
00057
00058
00059
00060 void waitForTrigger();
00061 void trigger();
00062
00063 void debugLock(bool debug);
00064
00065 bool isLocked() { return myIsThreadLock ?
00066 myLockCount : (int32)mySpinLock; }
00067
00068 int getCollisions() { return myCollisions; }
00069
00070 private:
00071 UT_SpinLock(const UT_SpinLock ©);
00072 UT_SpinLock &operator=(const UT_SpinLock ©);
00073
00074 private:
00075 SYS_AtomicInt32 mySpinLock;
00076 ut_thread_id_t myLockedThread;
00077 int myLockCount;
00078 int myCollisions;
00079
00080 unsigned myDebugThisLock : 1,
00081 myIsThreadLock : 1;
00082 #ifdef UT_DEBUG
00083 bool myIsActive;
00084 #endif
00085 };
00086
00087
00088
00089
00090 class UT_API UT_ReentrantSpinLock : public UT_SpinLock
00091 {
00092 public:
00093 explicit UT_ReentrantSpinLock(int lockstate = 0)
00094 : UT_SpinLock(lockstate, true) {}
00095 };
00096
00097 #endif
00098