00001 /* 00002 +-----------------------------------------------------------------------------------------+ 00003 | | 00004 | OCILIB - C Driver for Oracle | 00005 | | 00006 | (C Wrapper for Oracle OCI) | 00007 | | 00008 | Website : http://www.ocilib.net | 00009 | | 00010 | Copyright (c) 2007-2010 Vincent ROGIER <vince.rogier@ocilib.net> | 00011 | | 00012 +-----------------------------------------------------------------------------------------+ 00013 | | 00014 | This library is free software; you can redistribute it and/or | 00015 | modify it under the terms of the GNU Lesser General Public | 00016 | License as published by the Free Software Foundation; either | 00017 | version 2 of the License, or (at your option) any later version. | 00018 | | 00019 | This library is distributed in the hope that it will be useful, | 00020 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 00021 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 00022 | Lesser General Public License for more details. | 00023 | | 00024 | You should have received a copy of the GNU Lesser General Public | 00025 | License along with this library; if not, write to the Free | 00026 | Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | 00027 | | 00028 +-----------------------------------------------------------------------------------------+ 00029 */ 00030 00031 /* --------------------------------------------------------------------------------------------- * 00032 * $Id: mutex.c, v 3.8.1 2010-12-13 00:00 Vincent Rogier $ 00033 * --------------------------------------------------------------------------------------------- */ 00034 00035 #include "ocilib_internal.h" 00036 00037 /* ********************************************************************************************* * 00038 * PRIVATE FUNCTIONS 00039 * ********************************************************************************************* */ 00040 00041 /* --------------------------------------------------------------------------------------------- * 00042 * OCI_MutexCreateInternal 00043 * --------------------------------------------------------------------------------------------- */ 00044 00045 OCI_Mutex * OCI_MutexCreateInternal 00046 ( 00047 void 00048 ) 00049 { 00050 OCI_Mutex *mutex = NULL; 00051 boolean res = TRUE; 00052 00053 /* allocate mutex structure */ 00054 00055 mutex = (OCI_Mutex *) OCI_MemAlloc(OCI_IPC_MUTEX, sizeof(*mutex), 00056 (size_t) 1, TRUE); 00057 00058 if (mutex != NULL) 00059 { 00060 /* allocate error handle */ 00061 00062 res = (OCI_SUCCESS == OCI_HandleAlloc(OCILib.env, 00063 (dvoid **) (void *) &mutex->err, 00064 OCI_HTYPE_ERROR, (size_t) 0, 00065 (dvoid **) NULL)); 00066 00067 /* allocate mutex handle */ 00068 00069 OCI_CALL3 00070 ( 00071 res, mutex->err, 00072 00073 OCIThreadMutexInit(OCILib.env, mutex->err, &mutex->handle) 00074 ) 00075 } 00076 else 00077 res = FALSE; 00078 00079 if (res == FALSE) 00080 { 00081 OCI_MutexFree(mutex); 00082 mutex = NULL; 00083 } 00084 00085 return mutex; 00086 } 00087 00088 /* ********************************************************************************************* * 00089 * PUBLIC FUNCTIONS 00090 * ********************************************************************************************* */ 00091 00092 /* --------------------------------------------------------------------------------------------- * 00093 * OCI_MutexCreate 00094 * --------------------------------------------------------------------------------------------- */ 00095 00096 OCI_Mutex * OCI_API OCI_MutexCreate 00097 ( 00098 void 00099 ) 00100 { 00101 OCI_Mutex *mutex = NULL; 00102 00103 OCI_CHECK_INITIALIZED(NULL); 00104 00105 mutex = OCI_MutexCreateInternal(); 00106 00107 OCI_RESULT(mutex != NULL); 00108 00109 return mutex; 00110 } 00111 00112 /* --------------------------------------------------------------------------------------------- * 00113 * OCI_MutexFree 00114 * --------------------------------------------------------------------------------------------- */ 00115 00116 boolean OCI_API OCI_MutexFree 00117 ( 00118 OCI_Mutex *mutex 00119 ) 00120 { 00121 boolean res = TRUE; 00122 00123 OCI_CHECK_PTR(OCI_IPC_MUTEX, mutex, FALSE); 00124 00125 /* close mutex handle */ 00126 00127 if (mutex->handle != NULL) 00128 { 00129 OCI_CALL0 00130 ( 00131 res, mutex->err, 00132 00133 OCIThreadMutexDestroy(OCILib.env, mutex->err, &mutex->handle) 00134 ) 00135 } 00136 00137 /* close error handle */ 00138 00139 if (mutex->err != NULL) 00140 { 00141 OCI_HandleFree(mutex->err, OCI_HTYPE_ERROR); 00142 } 00143 00144 /* free mutex structure */ 00145 00146 OCI_FREE(mutex); 00147 00148 OCI_RESULT(res); 00149 00150 return res; 00151 } 00152 00153 /* --------------------------------------------------------------------------------------------- * 00154 * OCI_MutexAcquire 00155 * --------------------------------------------------------------------------------------------- */ 00156 00157 boolean OCI_API OCI_MutexAcquire 00158 ( 00159 OCI_Mutex *mutex 00160 ) 00161 { 00162 boolean res = TRUE; 00163 00164 OCI_CHECK_PTR(OCI_IPC_MUTEX, mutex, FALSE); 00165 00166 OCI_CALL3 00167 ( 00168 res, mutex->err, 00169 00170 OCIThreadMutexAcquire(OCILib.env, mutex->err, mutex->handle) 00171 ) 00172 00173 OCI_RESULT(res); 00174 00175 return res; 00176 } 00177 00178 /* --------------------------------------------------------------------------------------------- * 00179 * OCI_MutexRelease 00180 * --------------------------------------------------------------------------------------------- */ 00181 00182 boolean OCI_API OCI_MutexRelease 00183 ( 00184 OCI_Mutex *mutex 00185 ) 00186 { 00187 boolean res = TRUE; 00188 00189 OCI_CHECK_PTR(OCI_IPC_MUTEX, mutex, FALSE); 00190 00191 OCI_CALL3 00192 ( 00193 res, mutex->err, 00194 00195 OCIThreadMutexRelease(OCILib.env, mutex->err, mutex->handle) 00196 ) 00197 00198 OCI_RESULT(res); 00199 00200 return TRUE; 00201 }