• Main Page
  • Modules
  • Data Structures
  • Files
  • File List

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

Generated on Sun Oct 24 2010 22:02:55 for OCILIB (C Driver for Oracle) by  doxygen 1.7.1