00001 #ifndef MUTEX_IMPLEMENTATION_FILE
00002 #define MUTEX_IMPLEMENTATION_FILE
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "mutex.h"
00023 #include "portable.h"
00024
00025 #include <stdlib.h>
00026
00027 #ifdef __UNIX__
00028 #include <pthread.h>
00029 #endif
00030
00031 mutex_base::mutex_base() { construct(); }
00032
00033 mutex_base::~mutex_base() { destruct(); }
00034
00035 void mutex_base::establish_lock() { lock(); }
00036
00037 void mutex_base::repeal_lock() { unlock(); }
00038
00039 void mutex_base::construct()
00040 {
00041 #ifdef __WIN32__
00042 _os_mutex = (CRITICAL_SECTION *)malloc(sizeof(CRITICAL_SECTION));
00043 InitializeCriticalSection((LPCRITICAL_SECTION)_os_mutex);
00044 #elif defined(__UNIX__)
00045 pthread_mutexattr_t attr;
00046 pthread_mutexattr_init(&attr);
00047 int ret = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
00048 if (ret != 0) {
00049
00050 }
00051 _os_mutex = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t));
00052 pthread_mutex_init((pthread_mutex_t *)_os_mutex, &attr);
00053 pthread_mutexattr_destroy(&attr);
00054 #else
00055 #pragma error("no implementation of mutexes for this OS yet!")
00056 #endif
00057 }
00058
00059 void mutex_base::destruct()
00060 {
00061 defang();
00062 }
00063
00064 void mutex_base::defang()
00065 {
00066 if (!_os_mutex) return;
00067 #ifdef __WIN32__
00068 DeleteCriticalSection((LPCRITICAL_SECTION)_os_mutex);
00069 free(_os_mutex);
00070 #elif defined(__UNIX__)
00071 pthread_mutex_destroy((pthread_mutex_t *)_os_mutex);
00072 free(_os_mutex);
00073 #else
00074 #pragma error("no implementation of mutexes for this OS yet!")
00075 #endif
00076 _os_mutex = 0;
00077 }
00078
00079 void mutex_base::lock()
00080 {
00081 if (!_os_mutex) return;
00082 #ifdef __WIN32__
00083 EnterCriticalSection((LPCRITICAL_SECTION)_os_mutex);
00084 #elif defined(__UNIX__)
00085 pthread_mutex_lock((pthread_mutex_t *)_os_mutex);
00086 #else
00087 #pragma error("no implementation of mutexes for this OS yet!")
00088 #endif
00089 }
00090
00091 void mutex_base::unlock()
00092 {
00093 if (!_os_mutex) return;
00094 #ifdef __WIN32__
00095 LeaveCriticalSection((LPCRITICAL_SECTION)_os_mutex);
00096 #elif defined(__UNIX__)
00097 pthread_mutex_unlock((pthread_mutex_t *)_os_mutex);
00098 #else
00099 #pragma error("no implementation of mutexes for this OS yet!")
00100 #endif
00101 }
00102
00104
00105 mutex::~mutex() {}
00106
00107
00108 #endif //MUTEX_IMPLEMENTATION_FILE
00109