OCILIB (C Driver for Oracle) 3.12.1
mutex.c
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-2013 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, 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), (size_t) 1, TRUE);
00056 
00057     if (mutex != NULL)
00058     {
00059         /* allocate error handle */
00060 
00061         res = (OCI_SUCCESS == OCI_HandleAlloc(OCILib.env, (dvoid **) (void *) &mutex->err,
00062                                               OCI_HTYPE_ERROR, (size_t) 0, (dvoid **) NULL));
00063 
00064         /* allocate mutex handle */
00065 
00066         OCI_CALL3
00067         (
00068             res, mutex->err,
00069 
00070             OCIThreadMutexInit(OCILib.env, mutex->err, &mutex->handle)
00071         )
00072     }
00073     else
00074     {
00075         res = FALSE;
00076     }
00077 
00078     if (res == FALSE)
00079     {
00080         OCI_MutexFree(mutex);
00081         mutex = NULL;
00082     }
00083 
00084     return mutex;
00085 }
00086 
00087 /* ********************************************************************************************* *
00088  *                            PUBLIC FUNCTIONS
00089  * ********************************************************************************************* */
00090 
00091 /* --------------------------------------------------------------------------------------------- *
00092  * OCI_MutexCreate
00093  * --------------------------------------------------------------------------------------------- */
00094 
00095 OCI_Mutex * OCI_API OCI_MutexCreate
00096 (
00097     void
00098 )
00099 {
00100     OCI_Mutex *mutex = NULL;
00101 
00102     OCI_CHECK_INITIALIZED(NULL);
00103 
00104     mutex = OCI_MutexCreateInternal();
00105 
00106     OCI_RESULT(mutex != NULL);
00107 
00108     return mutex;
00109 }
00110 
00111 /* --------------------------------------------------------------------------------------------- *
00112  * OCI_MutexFree
00113  * --------------------------------------------------------------------------------------------- */
00114 
00115 boolean OCI_API OCI_MutexFree
00116 (
00117     OCI_Mutex *mutex
00118 )
00119 {
00120     boolean res = TRUE;
00121 
00122     OCI_CHECK_PTR(OCI_IPC_MUTEX, mutex, FALSE);
00123 
00124     /* close mutex handle */
00125 
00126     if (mutex->handle != NULL)
00127     {
00128         OCI_CALL0
00129         (
00130             res, mutex->err,
00131 
00132             OCIThreadMutexDestroy(OCILib.env, mutex->err, &mutex->handle)
00133         )
00134     }
00135 
00136     /* close error handle */
00137 
00138     if (mutex->err != NULL)
00139     {
00140         OCI_HandleFree(mutex->err, OCI_HTYPE_ERROR);
00141     }
00142 
00143     /* free mutex structure */
00144 
00145     OCI_FREE(mutex);
00146 
00147     OCI_RESULT(res);
00148 
00149     return res;
00150 }
00151 
00152 /* --------------------------------------------------------------------------------------------- *
00153  * OCI_MutexAcquire
00154  * --------------------------------------------------------------------------------------------- */
00155 
00156 boolean OCI_API OCI_MutexAcquire
00157 (
00158     OCI_Mutex *mutex
00159 )
00160 {
00161     boolean res = TRUE;
00162 
00163     OCI_CHECK_PTR(OCI_IPC_MUTEX, mutex, FALSE);
00164 
00165     OCI_CALL3
00166     (
00167         res, mutex->err,
00168 
00169         OCIThreadMutexAcquire(OCILib.env, mutex->err, mutex->handle)
00170     )
00171 
00172     OCI_RESULT(res);
00173 
00174     return res;
00175 }
00176 
00177 /* --------------------------------------------------------------------------------------------- *
00178  * OCI_MutexRelease
00179  * --------------------------------------------------------------------------------------------- */
00180 
00181 boolean OCI_API OCI_MutexRelease
00182 (
00183     OCI_Mutex *mutex
00184 )
00185 {
00186     boolean res = TRUE;
00187 
00188     OCI_CHECK_PTR(OCI_IPC_MUTEX, mutex, FALSE);
00189 
00190     OCI_CALL3
00191     (
00192         res, mutex->err,
00193 
00194         OCIThreadMutexRelease(OCILib.env, mutex->err, mutex->handle)
00195     )
00196 
00197     OCI_RESULT(res);
00198 
00199     return TRUE;
00200 }