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
00027 class UT_Condition;
00028 class UT_ConditionLock;
00029
00030 #if defined(LINUX) || defined(MBSD)
00031 #define HAVE_PLATFORM_LOCK
00032 #include <pthread.h>
00033 #elif defined(WIN32)
00034 #define HAVE_PLATFORM_LOCK
00035 struct ut_CriticalSection;
00036 #endif
00037
00038 #ifdef HAVE_PLATFORM_LOCK
00039 class UT_API UT_Lock
00040 {
00041 friend class UT_Condition;
00042 public:
00043
00044 explicit UT_Lock(bool initially_locked = false, bool thread_lock = true);
00045 ~UT_Lock();
00046
00047
00048 bool tryLock();
00049
00050
00051
00052
00053
00054 #if defined(LINUX) || defined(MBSD)
00055 void lockLW()
00056 {
00057 #if (UT_ASSERT_LEVEL > UT_ASSERT_LEVEL_NONE)
00058 int result = pthread_mutex_lock(&myLock);
00059 UT_ASSERT(result == 0);
00060 #else
00061 (void) pthread_mutex_lock(&myLock);
00062 #endif
00063 }
00064 #elif defined(WIN32)
00065 void lockLW();
00066 #endif
00067
00068 void lock() { lockLW(); }
00069 void lockNap() { lockLW(); }
00070
00071
00072 bool lockLW(int ntrials);
00073 bool lock(int ntrials) { return lockLW(ntrials); }
00074 bool lockNap(int ntrials) { return lockLW(ntrials); }
00075
00076
00077 #if defined(LINUX) || defined(MBSD)
00078 void unlock()
00079 {
00080 #if (UT_ASSERT_LEVEL > UT_ASSERT_LEVEL_NONE)
00081 int result = pthread_mutex_unlock(&myLock);
00082 UT_ASSERT(result == 0);
00083 #else
00084 (void) pthread_mutex_unlock(&myLock);
00085 #endif
00086 }
00087 #elif defined(WIN32)
00088 void unlock();
00089 #endif
00090
00091 bool isLocked();
00092 int getCollisions() { return 0; }
00093
00094 private:
00095 UT_Lock(const UT_Lock ©);
00096 UT_Lock &operator=(const UT_Lock ©);
00097
00098 #ifndef WIN32
00099
00100
00101
00102 explicit UT_Lock(UT_ConditionLock &);
00103 friend class UT_ConditionLock;
00104 #endif
00105
00106 private:
00107 #if defined(LINUX) || defined(MBSD)
00108 pthread_mutex_t myLock;
00109 pthread_mutexattr_t myLockAttributes;
00110 #elif defined(WIN32)
00111 ut_CriticalSection * myLock;
00112 int myThreadId;
00113 int myLockDepth;
00114 #endif
00115 };
00116
00117 #else
00118
00119 #include "UT_SpinLock.h"
00120 typedef UT_SpinLock UT_Lock;
00121
00122 #endif
00123
00124 typedef UT_AutoLockType<UT_Lock> UT_AutoLock;
00125 typedef UT_DebugLockType<UT_Lock> UT_DebugLock;
00126 typedef UT_ObjLockType<UT_Lock> UT_ObjLock;
00127 typedef UT_AutoObjLockType<UT_Lock> UT_AutoObjLock;
00128
00129 #endif