00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00038 #include "blocxx/BLOCXX_config.h"
00039 #include "blocxx/NonRecursiveMutexImpl.hpp"
00040 #include <cerrno>
00041 #include <cassert>
00042
00043 namespace BLOCXX_NAMESPACE
00044 {
00045
00046 namespace NonRecursiveMutexImpl
00047 {
00048
00054 int
00055 createMutex(NonRecursiveMutex_t& handle)
00056 {
00057 #if defined(BLOCXX_USE_PTHREAD)
00058 pthread_mutexattr_t attr;
00059 int res = pthread_mutexattr_init(&attr);
00060 assert(res == 0);
00061 if (res != 0)
00062 {
00063 return -1;
00064 }
00065
00066 res = pthread_mutex_init(&handle.mutex, &attr);
00067 pthread_mutexattr_destroy(&attr);
00068 if (res != 0)
00069 {
00070 return -1;
00071 }
00072
00073 return 0;
00074 #elif defined(BLOCXX_WIN32)
00075 int cc = -1;
00076 if ((handle = CreateMutex(NULL, FALSE, NULL)))
00077 {
00078 cc = 0;
00079 }
00080 return cc;
00081 #else
00082 #error "port me!"
00083 #endif
00084 }
00094 int
00095 destroyMutex(NonRecursiveMutex_t& handle)
00096 {
00097 #if defined(BLOCXX_USE_PTHREAD)
00098 switch (pthread_mutex_destroy(&handle.mutex))
00099 {
00100 case 0:
00101 break;
00102 case EBUSY:
00103 return -1;
00104 break;
00105 default:
00106 return -2;
00107 }
00108 return 0;
00109 #elif defined (BLOCXX_WIN32)
00110 ReleaseMutex(handle);
00111 return (CloseHandle(handle) == 0) ? -2 : 0;
00112 #else
00113 #error "port me!"
00114 #endif
00115 }
00124 int
00125 acquireMutex(NonRecursiveMutex_t& handle)
00126 {
00127 #if defined (BLOCXX_USE_PTHREAD)
00128 int res = pthread_mutex_lock(&handle.mutex);
00129 assert(res == 0);
00130 return res;
00131 #elif defined (BLOCXX_WIN32)
00132 int cc = -1;
00133 if (WaitForSingleObject(handle, INFINITE) != WAIT_FAILED)
00134 {
00135 cc = 0;
00136 }
00137 return cc;
00138 #else
00139 #error "port me!"
00140 #endif
00141 }
00148 int
00149 releaseMutex(NonRecursiveMutex_t& handle)
00150 {
00151 #if defined (BLOCXX_USE_PTHREAD)
00152 int res = pthread_mutex_unlock(&handle.mutex);
00153 assert(res == 0);
00154 return res;
00155
00156 #elif defined (BLOCXX_WIN32)
00157 return (ReleaseMutex(handle)) ? 0 : -1;
00158 #else
00159 #error "port me!"
00160 #endif
00161 }
00162
00163 int
00164 conditionPreWait(NonRecursiveMutex_t& handle, NonRecursiveMutexLockState& state)
00165 {
00166 #if defined (BLOCXX_WIN32)
00167 state.pmutex = &handle;
00168 #else
00169 state.pmutex = &handle.mutex;
00170 #endif
00171 return 0;
00172 }
00173 int
00174 conditionPostWait(NonRecursiveMutex_t& handle, NonRecursiveMutexLockState& state)
00175 {
00176 return 0;
00177 }
00178
00179 }
00180 }
00181