00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef __UT_Lock_h__
00021 #define __UT_Lock_h__
00022
00023 #include "UT_API.h"
00024 #include "UT_Assert.h"
00025 #include "UT_LockUtil.h"
00026 #include "UT_NonCopyable.h"
00027
00028 class UT_Condition;
00029 class UT_ConditionLock;
00030
00031 #if defined(LINUX) || defined(MBSD)
00032 #define HAVE_PLATFORM_LOCK
00033 #include <pthread.h>
00034 #elif defined(WIN32)
00035 #define HAVE_PLATFORM_LOCK
00036 struct ut_CriticalSection;
00037 #endif
00038
00039 #ifdef HAVE_PLATFORM_LOCK
00040 class UT_API UT_Lock : UT_NonCopyable
00041 {
00042 friend class UT_Condition;
00043 public:
00044
00045 explicit UT_Lock(bool initially_locked = false);
00046 ~UT_Lock();
00047
00048
00049 bool tryLock();
00050
00051
00052
00053
00054
00055 #if defined(LINUX) || defined(MBSD)
00056 void lockLW()
00057 {
00058 #if (UT_ASSERT_LEVEL > UT_ASSERT_LEVEL_NONE)
00059 int result = pthread_mutex_lock(&myLock);
00060 UT_ASSERT(result == 0);
00061 #else
00062 (void) pthread_mutex_lock(&myLock);
00063 #endif
00064 }
00065 #elif defined(WIN32)
00066 void lockLW();
00067 #endif
00068
00069 void lock() { lockLW(); }
00070 void lockNap() { lockLW(); }
00071 bool safeLock() { lockLW(); return true; }
00072
00073
00074 bool lockLW(int ntrials);
00075 bool lock(int ntrials) { return lockLW(ntrials); }
00076 bool lockNap(int ntrials) { return lockLW(ntrials); }
00077
00078
00079 #if defined(LINUX) || defined(MBSD)
00080 void unlock()
00081 {
00082 #if (UT_ASSERT_LEVEL > UT_ASSERT_LEVEL_NONE)
00083 int result = pthread_mutex_unlock(&myLock);
00084 UT_ASSERT(result == 0);
00085 #else
00086 (void) pthread_mutex_unlock(&myLock);
00087 #endif
00088 }
00089 #elif defined(WIN32)
00090 void unlock();
00091 #endif
00092
00093 bool isLocked();
00094 int getCollisions() { return 0; }
00095
00096
00097 typedef UT_LockScopeType<UT_Lock> Scope;
00098
00099 private:
00100
00101 #ifndef WIN32
00102
00103
00104
00105 explicit UT_Lock(UT_ConditionLock &);
00106 friend class UT_ConditionLock;
00107 #endif
00108
00109 private:
00110 #if defined(LINUX) || defined(MBSD)
00111 pthread_mutex_t myLock;
00112 pthread_mutexattr_t myLockAttributes;
00113 #elif defined(WIN32)
00114 ut_CriticalSection * myLock;
00115 #endif
00116 };
00117
00118 #else
00119
00120 #include "UT_SpinLock.h"
00121 typedef UT_RecursiveSpinLock UT_Lock;
00122
00123 #endif
00124
00125 typedef UT_AutoLockType<UT_Lock> UT_AutoLock;
00126 typedef UT_DebugLockType<UT_Lock> UT_DebugLock;
00127 typedef UT_ObjLockType<UT_Lock> UT_ObjLock;
00128 typedef UT_AutoObjLockType<UT_Lock> UT_AutoObjLock;
00129
00130 #endif