Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef UT_MUTEXIMPL_H
00023 #define UT_MUTEXIMPL_H
00024
00025 #include <glib.h>
00026 #include "ut_assert.h"
00027
00031 class UT_MutexImpl
00032 {
00033 public:
00034
00035 UT_MutexImpl ()
00036 : mMutex ( 0 )
00037 {
00038 #if GLIB_CHECK_VERSION(2,32,0)
00039 mMutex = &mStaticMutex;
00040 g_mutex_init(&mStaticMutex);
00041 #else
00042 if (!g_thread_supported ())
00043 g_thread_init (NULL);
00044 mMutex = g_mutex_new () ;
00045 UT_ASSERT ( mMutex ) ;
00046 #endif
00047 }
00048
00049 ~UT_MutexImpl ()
00050 {
00051 #if GLIB_CHECK_VERSION(2,32,0)
00052 g_mutex_clear(&mStaticMutex);
00053 #else
00054 if ( mMutex )
00055 g_mutex_free ( mMutex ) ;
00056 #endif
00057 }
00058
00059 void lock ()
00060 {
00061 if ( mMutex && mLocker != g_thread_self())
00062 g_mutex_lock ( mMutex ) ;
00063 mLocker = g_thread_self();
00064 iLockCount++;
00065 }
00066
00067 void unlock ()
00068 {
00069 UT_ASSERT(mLocker == g_thread_self());
00070 if (--iLockCount == 0 && mMutex)
00071 g_mutex_unlock ( mMutex ) ;
00072 }
00073
00074 private:
00075
00076
00077 UT_MutexImpl (const UT_MutexImpl & other);
00078 UT_MutexImpl & operator=(const UT_MutexImpl & other);
00079
00080
00081
00082 #if GLIB_CHECK_VERSION(2,32,0)
00083 GMutex mStaticMutex;
00084 #endif
00085 GMutex *mMutex;
00086
00087
00088 GThread *mLocker;
00089 int iLockCount;
00090 };
00091
00092 #endif