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 if (res != 0)
00068 {
00069 return -1;
00070 }
00071
00072 return 0;
00073 #elif defined(BLOCXX_WIN32)
00074 int cc = -1;
00075 if ((handle = CreateMutex(NULL, FALSE, NULL)))
00076 {
00077 cc = 0;
00078 }
00079 return cc;
00080 #else
00081 #error "port me!"
00082 #endif
00083 }
00093 int
00094 destroyMutex(NonRecursiveMutex_t& handle)
00095 {
00096 #if defined(BLOCXX_USE_PTHREAD)
00097 switch (pthread_mutex_destroy(&handle.mutex))
00098 {
00099 case 0:
00100 break;
00101 case EBUSY:
00102 return -1;
00103 break;
00104 default:
00105 return -2;
00106 }
00107 return 0;
00108 #elif defined (BLOCXX_WIN32)
00109 ReleaseMutex(handle);
00110 return (CloseHandle(handle) == 0) ? -2 : 0;
00111 #else
00112 #error "port me!"
00113 #endif
00114 }
00123 int
00124 acquireMutex(NonRecursiveMutex_t& handle)
00125 {
00126 #if defined (BLOCXX_USE_PTHREAD)
00127 int res = pthread_mutex_lock(&handle.mutex);
00128 assert(res == 0);
00129 return res;
00130 #elif defined (BLOCXX_WIN32)
00131 int cc = -1;
00132 if (WaitForSingleObject(handle, INFINITE) != WAIT_FAILED)
00133 {
00134 cc = 0;
00135 }
00136 return cc;
00137 #else
00138 #error "port me!"
00139 #endif
00140 }
00147 int
00148 releaseMutex(NonRecursiveMutex_t& handle)
00149 {
00150 #if defined (BLOCXX_USE_PTHREAD)
00151 int res = pthread_mutex_unlock(&handle.mutex);
00152 assert(res == 0);
00153 return res;
00154
00155 #elif defined (BLOCXX_WIN32)
00156 return (ReleaseMutex(handle)) ? 0 : -1;
00157 #else
00158 #error "port me!"
00159 #endif
00160 }
00161
00162 int
00163 conditionPreWait(NonRecursiveMutex_t& handle, NonRecursiveMutexLockState& state)
00164 {
00165 #if defined (BLOCXX_WIN32)
00166 state.pmutex = &handle;
00167 #else
00168 state.pmutex = &handle.mutex;
00169 #endif
00170 return 0;
00171 }
00172 int
00173 conditionPostWait(NonRecursiveMutex_t& handle, NonRecursiveMutexLockState& state)
00174 {
00175 return 0;
00176 }
00177
00178 }
00179 }
00180