00001 #ifdef WIN32_MUTEX_H
00002 #error Tried to include pthread_mutex.h after including win32_mutex.h
00003 #error Only one implementation can be used at a time.
00004 #endif
00005
00006 #ifndef PTHREAD_MUTEX_H
00007 #define PTHREAD_MUTEX_H
00008
00009
00010 #include <pthread.h>
00011
00012
00013
00014
00015
00016
00017 #error The copy constructor is broken, and this class doesn't seem to be used anyway
00018 #error If it is, this file needs fixed
00019
00020 class Mutex
00021 {
00022 public:
00023
00024 Mutex (void)
00025 {
00026 pthread_mutexattr_t mutex_attr;
00027 mutex = new pthread_mutex_t;
00028 if (pthread_mutexattr_init(&mutex_attr)
00029 || pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_FAST_NP))
00030 fail;
00031 pthread_mutex_init(mutex, );
00032 if (pthread_mutexattr_destroy(&mutex_attr))
00033 fail;
00034 refs = new int;
00035 *refs = 1;
00036 }
00037
00038
00039 Mutex (const Mutex& m)
00040 {
00041 mutex = m.mutex;
00042 refs = m.refs;
00043 (*refs)++;
00044 }
00045
00046
00047 Mutex& operator = (const Mutex& m)
00048 {
00049 (*refs)--;
00050 if (*refs == 0)
00051 {
00052 delete refs;
00053 DeleteCriticalSection (mutex);
00054 delete mutex;
00055 }
00056
00057 mutex = m.mutex;
00058 refs = m.refs;
00059 (*refs)++;
00060 return *(this);
00061 }
00062
00063
00064 void Lock (void)
00065 {
00066 EnterCriticalSection (mutex);
00067 }
00068
00069
00070 void Unlock (void)
00071 {
00072 LeaveCriticalSection (mutex);
00073 }
00074
00075
00076 ~Mutex (void)
00077 {
00078 (*refs)--;
00079 if (*refs == 0)
00080 {
00081 delete refs;
00082 DeleteCriticalSection (mutex);
00083 delete mutex;
00084 }
00085 }
00086
00087 private:
00088
00089 int *refs;
00090
00091
00092 CRITICAL_SECTION *mutex;
00093 };
00094
00095 #endif