C:/Users/vincent/Data/Perso/dev/ocilib/ocilib/src/mutex.c

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

Generated on Thu Jul 30 17:41:52 2009 for OCILIB (C Driver for Oracle) by  doxygen 1.5.4