blocxx
|
00001 /******************************************************************************* 00002 * Copyright (C) 2005, Vintela, Inc. All rights reserved. 00003 * Copyright (C) 2006, Novell, Inc. All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions are met: 00007 * 00008 * * Redistributions of source code must retain the above copyright notice, 00009 * this list of conditions and the following disclaimer. 00010 * * Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * * Neither the name of 00014 * Vintela, Inc., 00015 * nor Novell, Inc., 00016 * nor the names of its contributors or employees may be used to 00017 * endorse or promote products derived from this software without 00018 * specific prior written permission. 00019 * 00020 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00021 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00022 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00023 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00024 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00025 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00026 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00027 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00028 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00029 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00030 * POSSIBILITY OF SUCH DAMAGE. 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 } // end namespace NonRecursiveMutexImpl 00222 } // end namespace BLOCXX_NAMESPACE 00223