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 #ifndef __ABICOLLAB_LOCK__
00020 #define __ABICOLLAB_LOCK__
00021
00022 #ifndef _WIN32
00023 #include <pthread.h>
00024 #endif
00025
00026 namespace abicollab
00027 {
00028
00029 class scoped_lock;
00030
00031 class mutex
00032 {
00033 friend class scoped_lock;
00034
00035 public:
00036 mutex()
00037 {
00038 #ifdef _WIN32
00039 repr = CreateMutex(0, FALSE, 0);
00040 #else
00041 pthread_mutex_init(&repr, NULL);
00042 #endif
00043 }
00044
00045 ~mutex()
00046 {
00047 #ifdef _WIN32
00048 CloseHandle(repr);
00049 #else
00050 pthread_mutex_destroy(&repr);
00051 #endif
00052 }
00053
00054 private:
00055
00056 mutex( const mutex& );
00057 const mutex& operator=( const mutex& );
00058
00059 #ifdef _WIN32
00060 HANDLE repr;
00061 #else
00062 pthread_mutex_t repr;
00063 #endif
00064 };
00065
00066 class scoped_lock
00067 {
00068 public:
00069 scoped_lock(mutex& mutex)
00070 : m_mutex(mutex)
00071 {
00072 #ifdef _WIN32
00073 WaitForSingleObject(m_mutex.repr, INFINITE);
00074 #else
00075 pthread_mutex_lock(&m_mutex.repr);
00076 #endif
00077 }
00078
00079 ~scoped_lock()
00080 {
00081 #ifdef _WIN32
00082 ReleaseMutex(m_mutex.repr);
00083 #else
00084 pthread_mutex_unlock(&m_mutex.repr);
00085 #endif
00086 }
00087
00088 private:
00089
00090 scoped_lock( const scoped_lock& );
00091 const scoped_lock& operator=( const scoped_lock& );
00092
00093 mutex& m_mutex;
00094 };
00095
00096 }
00097
00098 #endif