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
00033
00039 #include "blocxx/BLOCXX_config.h"
00040 #include "blocxx/NonRecursiveMutexImpl.hpp"
00041 #include <cerrno>
00042 #include <cassert>
00043
00044 namespace BLOCXX_NAMESPACE
00045 {
00046
00047 namespace NonRecursiveMutexImpl
00048 {
00049
00050 #if defined (BLOCXX_USE_PTHREAD)
00051
00052 #if !defined (BLOCXX_NCR)
00053
00058 int
00059 createMutex(NonRecursiveMutex_t& handle)
00060 {
00061 pthread_mutexattr_t attr;
00062 int res = pthread_mutexattr_init(&attr);
00063 assert(res == 0);
00064 if (res != 0)
00065 {
00066 return -1;
00067 }
00068
00069 res = pthread_mutex_init(&handle.mutex, &attr);
00070 pthread_mutexattr_destroy(&attr);
00071 if (res != 0)
00072 {
00073 return -1;
00074 }
00075
00076 return 0;
00077 }
00078
00079 #else //#if !defined (BLOCXX_NCR)
00080 int
00081 createMutex(NonRecursiveMutex_t& handle)
00082 {
00083 pthread_mutexattr_t attr;
00084 int res = pthread_mutexattr_create(&attr);
00085 assert(res == 0);
00086 if (res != 0)
00087 {
00088 return -1;
00089 }
00090
00091 res = pthread_mutex_init(&handle.mutex, attr);
00092 pthread_mutexattr_delete(&attr);
00093 if (res != 0)
00094 {
00095 return -1;
00096 }
00097
00098 return 0;
00099 }
00100 #endif //#if !defined (BLOCXX_NCR)
00101
00111 int
00112 destroyMutex(NonRecursiveMutex_t& handle)
00113 {
00114 switch (pthread_mutex_destroy(&handle.mutex))
00115 {
00116 case 0:
00117 break;
00118 case EBUSY:
00119 return -1;
00120 break;
00121 default:
00122 return -2;
00123 }
00124 return 0;
00125 }
00134 int
00135 acquireMutex(NonRecursiveMutex_t& handle)
00136 {
00137 int res = pthread_mutex_lock(&handle.mutex);
00138 assert(res == 0);
00139 return res;
00140 }
00147 int
00148 releaseMutex(NonRecursiveMutex_t& handle)
00149 {
00150 int res = pthread_mutex_unlock(&handle.mutex);
00151 assert(res == 0);
00152 return res;
00153 }
00154
00155 int
00156 conditionPreWait(NonRecursiveMutex_t& handle, NonRecursiveMutexLockState& state)
00157 {
00158 state.pmutex = &handle.mutex;
00159 return 0;
00160 }
00161
00162 int
00163 conditionPostWait(NonRecursiveMutex_t& handle, NonRecursiveMutexLockState& state)
00164 {
00165 return 0;
00166 }
00167
00168 #endif //#if defined (BLOCXX_USE_PTHREAD)
00169
00170 #if defined(BLOCXX_WIN32)
00171 int
00172 createMutex(NonRecursiveMutex_t& handle)
00173 {
00174 int cc = -1;
00175 if ((handle = CreateMutex(NULL, FALSE, NULL)))
00176 {
00177 cc = 0;
00178 }
00179 return cc;
00180 }
00181
00182 int
00183 destroyMutex(NonRecursiveMutex_t& handle)
00184 {
00185 ReleaseMutex(handle);
00186 return (CloseHandle(handle) == 0) ? -2 : 0;
00187 }
00188
00189 int
00190 acquireMutex(NonRecursiveMutex_t& handle)
00191 {
00192 int cc = -1;
00193 if (WaitForSingleObject(handle, INFINITE) != WAIT_FAILED)
00194 {
00195 cc = 0;
00196 }
00197 return cc;
00198 }
00199
00200 int
00201 releaseMutex(NonRecursiveMutex_t& handle)
00202 {
00203 return (ReleaseMutex(handle)) ? 0 : -1;
00204 }
00205
00206 int
00207 conditionPreWait(NonRecursiveMutex_t& handle, NonRecursiveMutexLockState& state)
00208 {
00209 state.pmutex = &handle;
00210 return 0;
00211 }
00212
00213 int
00214 conditionPostWait(NonRecursiveMutex_t& handle, NonRecursiveMutexLockState& state)
00215 {
00216 return 0;
00217 }
00218
00219 #endif //#if defined(BLOCXX_WIN32)
00220
00221 }
00222 }
00223