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

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

Generated on Mon Dec 13 2010 22:32:02 for OCILIB (C Driver for Oracle) by  doxygen 1.7.2