C:/Users/vincent/Data/Perso/dev/ocilib/ocilib/src/threadkey.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: threadkey.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_ThreadKeyCreateInternal
00043  * ------------------------------------------------------------------------ */
00044 
00045 OCI_ThreadKey * OCI_ThreadKeyCreateInternal(POCI_THREADKEYDEST destfunc)
00046 {
00047     boolean  res       = TRUE;
00048     OCI_ThreadKey *key = NULL;
00049 
00050     /* allocate key structure */
00051 
00052     key = (OCI_ThreadKey *) OCI_MemAlloc(OCI_IPC_THREADKEY, sizeof(*key), 1, TRUE);
00053 
00054     if (key != NULL)
00055     {
00056         /* allocate error handle */
00057 
00058         res = (OCI_SUCCESS == OCI_HandleAlloc(OCILib.env, 
00059                                               (dvoid **) (void *) &key->err,
00060                                               OCI_HTYPE_ERROR, 0,
00061                                               (dvoid **) NULL));
00062 
00063         /* key initialization */
00064 
00065         OCI_CALL3
00066         (
00067             res, key->err,
00068             
00069             OCIThreadKeyInit(OCILib.env, key->err, &key->handle, destfunc)
00070         )
00071     }
00072     else
00073         res = FALSE;
00074 
00075     /* check errors */
00076 
00077     if (res == FALSE)
00078     {
00079         OCI_ThreadKeyFree(key);
00080         key = NULL;
00081     }
00082 
00083     return key;
00084 }
00085 /* ------------------------------------------------------------------------ *
00086  * OCI_ThreadKeyFree
00087  * ------------------------------------------------------------------------ */
00088 
00089 boolean OCI_ThreadKeyFree(OCI_ThreadKey *key)
00090 {
00091     boolean res = TRUE;
00092 
00093     OCI_CHECK(key == NULL, FALSE);
00094 
00095     /* close key handle */
00096 
00097     if (key->handle != NULL)
00098     {
00099         OCI_CALL0
00100         (
00101             res, key->err, 
00102             
00103             OCIThreadKeyDestroy(OCILib.env, key->err, &key->handle)
00104         )
00105     }
00106 
00107     /* close error handle */
00108 
00109     if (key->err != NULL)
00110     {
00111         OCI_HandleFree(key->err, OCI_HTYPE_ERROR);
00112     }
00113 
00114     /* free key structure */
00115 
00116     OCI_FREE(key);
00117 
00118     OCI_RESULT(res);
00119 
00120     return res;
00121 }
00122 
00123 /* ------------------------------------------------------------------------ *
00124  * OCI_ThreadKeySet
00125  * ------------------------------------------------------------------------ */
00126 
00127 boolean OCI_ThreadKeySet(OCI_ThreadKey *key, void *value)
00128 {
00129     boolean res = TRUE;
00130 
00131     OCI_CHECK(key == NULL, FALSE);
00132 
00133     OCI_CALL3
00134     (
00135         res, key->err, 
00136         
00137         OCIThreadKeySet(OCILib.env, key->err, key->handle, value)
00138      )
00139 
00140     return res;
00141 }
00142 
00143 /* ------------------------------------------------------------------------ *
00144  * OCI_ThreadKeyGet
00145  * ------------------------------------------------------------------------ */
00146 
00147 boolean OCI_ThreadKeyGet(OCI_ThreadKey* key, void **value)
00148 {
00149     boolean res  = TRUE;
00150 
00151     OCI_CHECK(key == NULL, FALSE);
00152  
00153     OCI_CALL3
00154     (
00155         res, key->err, 
00156         
00157         OCIThreadKeyGet(OCILib.env, key->err, key->handle, value)
00158      )
00159   
00160     return res;
00161 }
00162 
00163 /* ************************************************************************ *
00164  *                            PUBLIC FUNCTIONS
00165  * ************************************************************************ */
00166 
00167 /* ------------------------------------------------------------------------ *
00168  * OCI_ThreadKeyCreate
00169  * ------------------------------------------------------------------------ */
00170 
00171 boolean OCI_API OCI_ThreadKeyCreate(const mtext *name, POCI_THREADKEYDEST destfunc)
00172 {
00173     OCI_ThreadKey *key = NULL;
00174     boolean res        = TRUE;
00175 
00176     OCI_CHECK_PTR(OCI_IPC_STRING, name, FALSE);
00177 
00178     OCI_CHECK_INITIALIZED(FALSE);
00179 
00180     if (OCILib.key_map == NULL)
00181     {
00182         /* create the map at the first call to OCI_ThreadKeyCreate to save
00183            time and memory when it's not needed */
00184 
00185         OCILib.key_map = OCI_HashCreate(OCI_HASH_DEFAULT_SIZE, OCI_HASH_POINTER);
00186 
00187     }
00188 
00189     res = (OCILib.key_map != NULL);
00190 
00191     /* create key */
00192 
00193     if (res == TRUE)
00194     {
00195         key = OCI_ThreadKeyCreateInternal(destfunc);
00196 
00197         /* add key to internal key hash table */
00198 
00199         if (key != NULL)
00200             res = OCI_HashAddPointer(OCILib.key_map, name, key);
00201         else
00202             res = FALSE;
00203     }
00204 
00205     /* check errors */
00206 
00207     if (res == FALSE)
00208         OCI_ThreadKeyFree(key);
00209 
00210     OCI_RESULT(res);
00211 
00212     return res;
00213 }
00214 
00215 /* ------------------------------------------------------------------------ *
00216  * OCI_ThreadKeySetValue
00217  * ------------------------------------------------------------------------ */
00218 
00219 boolean OCI_API OCI_ThreadKeySetValue(const mtext *name, void *value)
00220 {
00221     boolean res        = TRUE;
00222     OCI_ThreadKey *key = NULL;
00223 
00224     OCI_CHECK_PTR(OCI_IPC_STRING, name, FALSE);
00225 
00226     key = (OCI_ThreadKey *) OCI_HashGetPointer(OCILib.key_map, name);
00227 
00228     res = OCI_ThreadKeySet(key, value);
00229 
00230     OCI_RESULT(res);
00231 
00232     return TRUE;
00233 }
00234 
00235 /* ------------------------------------------------------------------------ *
00236  * OCI_ThreadKeyGetValue
00237  * ------------------------------------------------------------------------ */
00238 
00239 void * OCI_API OCI_ThreadKeyGetValue(const mtext *name)
00240 {
00241     boolean res        = TRUE;
00242     void * value       = NULL;
00243     OCI_ThreadKey* key = NULL;
00244 
00245     OCI_CHECK_PTR(OCI_IPC_STRING, name, FALSE);
00246 
00247     key = (OCI_ThreadKey *) OCI_HashGetPointer(OCILib.key_map, name);
00248 
00249     res = OCI_ThreadKeyGet(key, &value);
00250 
00251     OCI_RESULT(res);
00252     
00253     return value;
00254 }

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