OCILIB (C Driver for Oracle) 3.9.1
D:/Perso/dev/ocilib/ocilib/src/thread.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-2011 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: thread.c, v 3.9.1 2011-07-08 00:00 Vincent Rogier $
00033  * --------------------------------------------------------------------------------------------- */
00034 
00035 #include "ocilib_internal.h"
00036 
00037 /* ********************************************************************************************* *
00038  *                             PRIVATE FUNCTIONS
00039  * ********************************************************************************************* */
00040 
00041 /* --------------------------------------------------------------------------------------------- *
00042  * OCI_ThreadProc
00043  * --------------------------------------------------------------------------------------------- */
00044 
00045 void OCI_ThreadProc
00046 (
00047     dvoid *arg
00048 )
00049 {
00050     OCI_Thread *thread = (OCI_Thread *) arg;
00051 
00052     if (thread != NULL)
00053     {
00054         thread->proc(thread, thread->arg);
00055     }
00056 }
00057 
00058 /* ********************************************************************************************* *
00059  *                            PUBLIC FUNCTIONS
00060  * ********************************************************************************************* */
00061 
00062 /* --------------------------------------------------------------------------------------------- *
00063  * OCI_ThreadCreate
00064  * --------------------------------------------------------------------------------------------- */
00065 
00066 OCI_Thread * OCI_API OCI_ThreadCreate
00067 (
00068     void
00069 )
00070 {
00071     OCI_Thread *thread = NULL;
00072     boolean res        = TRUE;
00073 
00074     OCI_CHECK_INITIALIZED(NULL);
00075 
00076     OCI_CHECK_THREAD_ENABLED(NULL);
00077 
00078     /* allocate thread structure */
00079 
00080     thread = (OCI_Thread *) OCI_MemAlloc(OCI_IPC_THREAD, sizeof(*thread), (size_t) 1, TRUE);
00081 
00082     if (thread != NULL)
00083     {
00084         /* allocate error handle */
00085 
00086         res = (OCI_SUCCESS == OCI_HandleAlloc(OCILib.env,
00087                                               (dvoid **) (void *) &thread->err,
00088                                               OCI_HTYPE_ERROR, (size_t) 0,
00089                                               (dvoid **) NULL));
00090 
00091         /* allocate thread handle */
00092 
00093         OCI_CALL3
00094         (
00095             res, thread->err,
00096 
00097             OCIThreadHndInit(OCILib.env, thread->err, &thread->handle)
00098         )
00099 
00100         /* allocate thread ID */
00101 
00102         OCI_CALL3
00103         (
00104             res, thread->err,
00105 
00106             OCIThreadIdInit(OCILib.env, thread->err, &thread->id)
00107         )
00108     }
00109     else
00110     {
00111         res = FALSE;
00112     }
00113 
00114     if (res == FALSE)
00115     {
00116         OCI_ThreadFree(thread);
00117         thread = NULL;
00118     }
00119 
00120     OCI_RESULT(res);
00121 
00122     return thread;
00123 }
00124 
00125 /* --------------------------------------------------------------------------------------------- *
00126  * OCI_ThreadFree
00127  * --------------------------------------------------------------------------------------------- */
00128 
00129 boolean OCI_API OCI_ThreadFree
00130 (
00131     OCI_Thread *thread
00132 )
00133 {
00134     boolean res = TRUE;
00135 
00136     OCI_CHECK_THREAD_ENABLED(FALSE);
00137 
00138     OCI_CHECK_PTR(OCI_IPC_THREAD, thread, FALSE);
00139 
00140     /* close thread handle */
00141 
00142     if (thread->handle != NULL)
00143     {
00144         OCI_CALL0
00145         (
00146             res, thread->err,
00147 
00148             OCIThreadClose(OCILib.env, thread->err, thread->handle)
00149         )
00150 
00151         OCI_CALL0
00152         (
00153             res, thread->err,
00154 
00155             OCIThreadHndDestroy(OCILib.env, thread->err, &thread->handle)
00156         )
00157     }
00158 
00159     /* close thread id */
00160 
00161     if (thread->id != NULL)
00162     {
00163         OCI_CALL0
00164         (
00165             res, thread->err,
00166 
00167             OCIThreadIdDestroy(OCILib.env, thread->err, &thread->id)
00168         )
00169     }
00170 
00171     /* close error handle */
00172 
00173     if (thread->err != NULL)
00174     {
00175         OCI_HandleFree(thread->err, OCI_HTYPE_ERROR);
00176     }
00177 
00178     /* free mutex structure */
00179 
00180     OCI_FREE(thread);
00181 
00182     OCI_RESULT(res);
00183 
00184     return res;
00185 }
00186 
00187 /* --------------------------------------------------------------------------------------------- *
00188  * OCI_ThreadRun
00189  * --------------------------------------------------------------------------------------------- */
00190 
00191 boolean OCI_API OCI_ThreadRun
00192 (
00193     OCI_Thread *thread,
00194     POCI_THREAD proc,
00195     void       *arg
00196 )
00197 {
00198     boolean res = TRUE;
00199 
00200     OCI_CHECK_THREAD_ENABLED(FALSE);
00201 
00202     OCI_CHECK_PTR(OCI_IPC_THREAD, thread, FALSE);
00203     OCI_CHECK_PTR(OCI_IPC_PROC, proc, FALSE);
00204 
00205     thread->proc = proc;
00206     thread->arg  = arg;
00207 
00208     OCI_CALL3
00209     (
00210         res, thread->err,
00211 
00212         OCIThreadCreate(OCILib.env, thread->err, OCI_ThreadProc,
00213                         thread, thread->id, thread->handle)
00214     )
00215 
00216     OCI_RESULT(res);
00217 
00218     return res;
00219 }
00220 
00221 /* --------------------------------------------------------------------------------------------- *
00222  * OCI_ThreadJoin
00223  * --------------------------------------------------------------------------------------------- */
00224 
00225 boolean OCI_API OCI_ThreadJoin
00226 (
00227     OCI_Thread *thread
00228 )
00229 {
00230     boolean res = TRUE;
00231 
00232     OCI_CHECK_THREAD_ENABLED(FALSE);
00233 
00234     OCI_CHECK_PTR(OCI_IPC_THREAD, thread, FALSE);
00235 
00236     OCI_CALL3
00237     (
00238         res, thread->err,
00239 
00240         OCIThreadJoin(OCILib.env, thread->err, thread->handle)
00241     )
00242 
00243     OCI_RESULT(res);
00244 
00245     return res;
00246 }