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

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