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 #include "UT_String.h"
00028
00029 class UT_Condition;
00030 class UT_ConditionLock;
00031
00032 #if defined(LINUX) || defined(MBSD)
00033 #define HAVE_PLATFORM_LOCK
00034 #include <pthread.h>
00035 #elif defined(WIN32)
00036 #define HAVE_PLATFORM_LOCK
00037 struct ut_CriticalSection;
00038 #endif
00039
00040 #ifdef HAVE_PLATFORM_LOCK
00041
00042
00043
00044
00045
00046
00047 class UT_API UT_Lock : UT_NonCopyable
00048 {
00049 friend class UT_Condition;
00050 public:
00051
00052 explicit UT_Lock(bool initially_locked = false);
00053 ~UT_Lock();
00054
00055
00056 bool tryLock();
00057
00058
00059 #if defined(WIN32) || defined(DEBUG_LOCK_CALLERS)
00060 void lock();
00061 #elif defined(LINUX) || defined(MBSD)
00062 void lock()
00063 {
00064 UT_VERIFY(pthread_mutex_lock(&myLock) == 0);
00065 }
00066 #endif
00067
00068 bool safeLock() { lock(); return true; }
00069
00070
00071 bool lock(int ms);
00072
00073
00074 #if defined(LINUX) || defined(MBSD)
00075 void unlock()
00076 {
00077 #if (UT_ASSERT_LEVEL > UT_ASSERT_LEVEL_NONE)
00078 int result = pthread_mutex_unlock(&myLock);
00079 UT_ASSERT(result == 0);
00080 #else
00081 (void) pthread_mutex_unlock(&myLock);
00082 #endif
00083 }
00084 #elif defined(WIN32)
00085 void unlock();
00086 #endif
00087
00088 bool isLocked();
00089 int getCollisions() { return 0; }
00090
00091
00092 typedef UT_LockScopeType<UT_Lock> Scope;
00093
00094 private:
00095
00096 #ifndef WIN32
00097
00098
00099
00100 explicit UT_Lock(UT_ConditionLock &);
00101 friend class UT_ConditionLock;
00102 #endif
00103
00104 private:
00105 #if defined(LINUX) || defined(MBSD)
00106 pthread_mutex_t myLock;
00107 pthread_mutexattr_t myLockAttributes;
00108 #elif defined(WIN32)
00109 ut_CriticalSection * myLock;
00110 #endif
00111
00112 #if defined(DEBUG_LOCK_CALLERS)
00113 int myLockCount;
00114 UT_String myBacktrace;
00115 #endif
00116 };
00117
00118 #else // HAVE_PLATFORM_LOCK
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