os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/generic/tclThreadJoin.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/* 
sl@0
     2
 * tclThreadJoin.c --
sl@0
     3
 *
sl@0
     4
 *	This file implements a platform independent emulation layer for
sl@0
     5
 *	the handling of joinable threads. The Mac and Windows platforms
sl@0
     6
 *	use this code to provide the functionality of joining threads.
sl@0
     7
 *	This code is currently not necessary on Unix.
sl@0
     8
 *
sl@0
     9
 * Copyright (c) 2000 by Scriptics Corporation
sl@0
    10
 *
sl@0
    11
 * See the file "license.terms" for information on usage and redistribution
sl@0
    12
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
sl@0
    13
 *
sl@0
    14
 * RCS: @(#) $Id: tclThreadJoin.c,v 1.4 2002/04/24 20:35:40 hobbs Exp $
sl@0
    15
 */
sl@0
    16
sl@0
    17
#include "tclInt.h"
sl@0
    18
sl@0
    19
#if defined(WIN32) || defined(MAC_TCL)
sl@0
    20
sl@0
    21
/* The information about each joinable thread is remembered in a
sl@0
    22
 * structure as defined below.
sl@0
    23
 */
sl@0
    24
sl@0
    25
typedef struct JoinableThread {
sl@0
    26
  Tcl_ThreadId  id;                     /* The id of the joinable thread */
sl@0
    27
  int           result;                 /* A place for the result after the
sl@0
    28
					 * demise of the thread */
sl@0
    29
  int           done;                   /* Boolean flag. Initialized to 0
sl@0
    30
					 * and set to 1 after the exit of
sl@0
    31
					 * the thread. This allows a thread
sl@0
    32
					 * requesting a join to detect when
sl@0
    33
					 * waiting is not necessary. */
sl@0
    34
  int           waitedUpon;             /* Boolean flag. Initialized to 0
sl@0
    35
					 * and set to 1 by the thread waiting
sl@0
    36
					 * for this one via Tcl_JoinThread.
sl@0
    37
					 * Used to lock any other thread
sl@0
    38
					 * trying to wait on this one.
sl@0
    39
					 */
sl@0
    40
  Tcl_Mutex     threadMutex;            /* The mutex used to serialize access
sl@0
    41
					 * to this structure. */
sl@0
    42
  Tcl_Condition cond;                   /* This is the condition a thread has
sl@0
    43
					 * to wait upon to get notified of the
sl@0
    44
					 * end of the described thread. It is
sl@0
    45
					 * signaled indirectly by
sl@0
    46
					 * Tcl_ExitThread. */
sl@0
    47
  struct JoinableThread* nextThreadPtr; /* Reference to the next thread in the
sl@0
    48
					 * list of joinable threads */
sl@0
    49
} JoinableThread;
sl@0
    50
sl@0
    51
/* The following variable is used to maintain the global list of all
sl@0
    52
 * joinable threads. Usage by a thread is allowed only if the
sl@0
    53
 * thread acquired the 'joinMutex'.
sl@0
    54
 */
sl@0
    55
sl@0
    56
TCL_DECLARE_MUTEX(joinMutex)
sl@0
    57
sl@0
    58
static JoinableThread* firstThreadPtr;
sl@0
    59
sl@0
    60
sl@0
    61

sl@0
    62
/*
sl@0
    63
 *----------------------------------------------------------------------
sl@0
    64
 *
sl@0
    65
 * TclJoinThread --
sl@0
    66
 *
sl@0
    67
 *	This procedure waits for the exit of the thread with the specified
sl@0
    68
 *	id and returns its result.
sl@0
    69
 *
sl@0
    70
 * Results:
sl@0
    71
 *	A standard tcl result signaling the overall success/failure of the
sl@0
    72
 *	operation and an integer result delivered by the thread which was
sl@0
    73
 *	waited upon.
sl@0
    74
 *
sl@0
    75
 * Side effects:
sl@0
    76
 *	Deallocates the memory allocated by TclRememberJoinableThread.
sl@0
    77
 *	Removes the data associated to the thread waited upon from the
sl@0
    78
 *	list of joinable threads.
sl@0
    79
 *
sl@0
    80
 *----------------------------------------------------------------------
sl@0
    81
 */
sl@0
    82
sl@0
    83
int
sl@0
    84
TclJoinThread(id, result)
sl@0
    85
    Tcl_ThreadId id;     /* The id of the thread to wait upon. */
sl@0
    86
    int*         result; /* Reference to a location for the result
sl@0
    87
			  * of the thread we are waiting upon. */
sl@0
    88
{
sl@0
    89
    /* Steps done here:
sl@0
    90
     * i.    Acquire the joinMutex and search for the thread.
sl@0
    91
     * ii.   Error out if it could not be found.
sl@0
    92
     * iii.  If found, switch from exclusive access to the list to exclusive
sl@0
    93
     *       access to the thread structure.
sl@0
    94
     * iv.   Error out if some other is already waiting.
sl@0
    95
     * v.    Skip the waiting part of the thread is already done.
sl@0
    96
     * vi.   Wait for the thread to exit, mark it as waited upon too.
sl@0
    97
     * vii.  Get the result form the structure, 
sl@0
    98
     * viii. switch to exclusive access of the list,
sl@0
    99
     * ix.   remove the structure from the list,
sl@0
   100
     * x.    then switch back to exclusive access to the structure
sl@0
   101
     * xi.   and delete it.
sl@0
   102
     */
sl@0
   103
sl@0
   104
    JoinableThread* threadPtr;
sl@0
   105
sl@0
   106
    Tcl_MutexLock (&joinMutex);
sl@0
   107
sl@0
   108
    for (threadPtr = firstThreadPtr;
sl@0
   109
	 (threadPtr != (JoinableThread*) NULL) && (threadPtr->id != id);
sl@0
   110
	 threadPtr = threadPtr->nextThreadPtr)
sl@0
   111
        /* empty body */
sl@0
   112
      ;
sl@0
   113
sl@0
   114
    if (threadPtr == (JoinableThread*) NULL) {
sl@0
   115
        /* Thread not found. Either not joinable, or already waited
sl@0
   116
	 * upon and exited. Whatever, an error is in order.
sl@0
   117
	 */
sl@0
   118
sl@0
   119
      Tcl_MutexUnlock (&joinMutex);
sl@0
   120
      return TCL_ERROR;
sl@0
   121
    }
sl@0
   122
sl@0
   123
    /* [1] If we don't lock the structure before giving up exclusive access
sl@0
   124
     * to the list some other thread just completing its wait on the same
sl@0
   125
     * thread can delete the structure from under us, leaving us with a
sl@0
   126
     * dangling pointer.
sl@0
   127
     */
sl@0
   128
sl@0
   129
    Tcl_MutexLock   (&threadPtr->threadMutex);
sl@0
   130
    Tcl_MutexUnlock (&joinMutex);
sl@0
   131
sl@0
   132
    /* [2] Now that we have the structure mutex any other thread that just
sl@0
   133
     * tries to delete structure will wait at location [3] until we are
sl@0
   134
     * done with the structure. And in that case we are done with it
sl@0
   135
     * rather quickly as 'waitedUpon' will be set and we will have to
sl@0
   136
     * error out.
sl@0
   137
     */
sl@0
   138
sl@0
   139
    if (threadPtr->waitedUpon) {
sl@0
   140
        Tcl_MutexUnlock (&threadPtr->threadMutex);
sl@0
   141
	return TCL_ERROR;
sl@0
   142
    }
sl@0
   143
sl@0
   144
    /* We are waiting now, let other threads recognize this
sl@0
   145
     */
sl@0
   146
sl@0
   147
    threadPtr->waitedUpon = 1;
sl@0
   148
sl@0
   149
    while (!threadPtr->done) {
sl@0
   150
      Tcl_ConditionWait (&threadPtr->cond, &threadPtr->threadMutex, NULL);
sl@0
   151
    }
sl@0
   152
sl@0
   153
    /* We have to release the structure before trying to access the list
sl@0
   154
     * again or we can run into deadlock with a thread at [1] (see above)
sl@0
   155
     * because of us holding the structure and the other holding the list.
sl@0
   156
     * There is no problem with dangling pointers here as 'waitedUpon == 1'
sl@0
   157
     * is still valid and any other thread will error out and not come to
sl@0
   158
     * this place. IOW, the fact that we are here also means that no other
sl@0
   159
     * thread came here before us and is able to delete the structure.
sl@0
   160
     */
sl@0
   161
sl@0
   162
    Tcl_MutexUnlock (&threadPtr->threadMutex);
sl@0
   163
    Tcl_MutexLock   (&joinMutex);
sl@0
   164
sl@0
   165
    /* We have to search the list again as its structure may (may, almost
sl@0
   166
     * certainly) have changed while we were waiting. Especially now is the
sl@0
   167
     * time to compute the predecessor in the list. Any earlier result can
sl@0
   168
     * be dangling by now.
sl@0
   169
     */
sl@0
   170
sl@0
   171
    if (firstThreadPtr == threadPtr) {
sl@0
   172
        firstThreadPtr = threadPtr->nextThreadPtr;
sl@0
   173
    } else {
sl@0
   174
        JoinableThread* prevThreadPtr;
sl@0
   175
sl@0
   176
	for (prevThreadPtr = firstThreadPtr;
sl@0
   177
	     prevThreadPtr->nextThreadPtr != threadPtr;
sl@0
   178
	     prevThreadPtr = prevThreadPtr->nextThreadPtr)
sl@0
   179
	    /* empty body */
sl@0
   180
	  ;
sl@0
   181
sl@0
   182
	prevThreadPtr->nextThreadPtr = threadPtr->nextThreadPtr;
sl@0
   183
    }
sl@0
   184
sl@0
   185
    Tcl_MutexUnlock (&joinMutex);
sl@0
   186
sl@0
   187
    /* [3] Now that the structure is not part of the list anymore no other
sl@0
   188
     * thread can acquire its mutex from now on. But it is possible that
sl@0
   189
     * another thread is still holding the mutex though, see location [2].
sl@0
   190
     * So we have to acquire the mutex one more time to wait for that thread
sl@0
   191
     * to finish. We can (and have to) release the mutex immediately.
sl@0
   192
     */
sl@0
   193
sl@0
   194
    Tcl_MutexLock   (&threadPtr->threadMutex);
sl@0
   195
    Tcl_MutexUnlock (&threadPtr->threadMutex);
sl@0
   196
sl@0
   197
    /* Copy the result to us, finalize the synchronisation objects, then
sl@0
   198
     * free the structure and return.
sl@0
   199
     */
sl@0
   200
sl@0
   201
    *result = threadPtr->result;
sl@0
   202
sl@0
   203
    Tcl_ConditionFinalize (&threadPtr->cond);
sl@0
   204
    Tcl_MutexFinalize (&threadPtr->threadMutex);
sl@0
   205
    ckfree ((VOID*) threadPtr);
sl@0
   206
sl@0
   207
    return TCL_OK;
sl@0
   208
}
sl@0
   209

sl@0
   210
/*
sl@0
   211
 *----------------------------------------------------------------------
sl@0
   212
 *
sl@0
   213
 * TclRememberJoinableThread --
sl@0
   214
 *
sl@0
   215
 *	This procedure remebers a thread as joinable. Only a call to
sl@0
   216
 *	TclJoinThread will remove the structre created (and initialized)
sl@0
   217
 *	here. IOW, not waiting upon a joinable thread will cause memory
sl@0
   218
 *	leaks.
sl@0
   219
 *
sl@0
   220
 * Results:
sl@0
   221
 *	None.
sl@0
   222
 *
sl@0
   223
 * Side effects:
sl@0
   224
 *	Allocates memory, adds it to the global list of all joinable
sl@0
   225
 *	threads.
sl@0
   226
 *
sl@0
   227
 *----------------------------------------------------------------------
sl@0
   228
 */
sl@0
   229
sl@0
   230
VOID
sl@0
   231
TclRememberJoinableThread(id)
sl@0
   232
    Tcl_ThreadId id; /* The thread to remember as joinable */
sl@0
   233
{
sl@0
   234
    JoinableThread* threadPtr;
sl@0
   235
sl@0
   236
    threadPtr = (JoinableThread*) ckalloc (sizeof (JoinableThread));
sl@0
   237
    threadPtr->id          = id;
sl@0
   238
    threadPtr->done        = 0;
sl@0
   239
    threadPtr->waitedUpon  = 0;
sl@0
   240
    threadPtr->threadMutex = (Tcl_Mutex) NULL;
sl@0
   241
    threadPtr->cond        = (Tcl_Condition) NULL;
sl@0
   242
sl@0
   243
    Tcl_MutexLock (&joinMutex);
sl@0
   244
sl@0
   245
    threadPtr->nextThreadPtr = firstThreadPtr;
sl@0
   246
    firstThreadPtr           = threadPtr;
sl@0
   247
sl@0
   248
    Tcl_MutexUnlock (&joinMutex);
sl@0
   249
}
sl@0
   250

sl@0
   251
/*
sl@0
   252
 *----------------------------------------------------------------------
sl@0
   253
 *
sl@0
   254
 * TclSignalExitThread --
sl@0
   255
 *
sl@0
   256
 *	This procedure signals that the specified thread is done with
sl@0
   257
 *	its work. If the thread is joinable this signal is propagated
sl@0
   258
 *	to the thread waiting upon it.
sl@0
   259
 *
sl@0
   260
 * Results:
sl@0
   261
 *	None.
sl@0
   262
 *
sl@0
   263
 * Side effects:
sl@0
   264
 *	Modifies the associated structure to hold the result.
sl@0
   265
 *
sl@0
   266
 *----------------------------------------------------------------------
sl@0
   267
 */
sl@0
   268
sl@0
   269
VOID
sl@0
   270
TclSignalExitThread(id,result)
sl@0
   271
    Tcl_ThreadId id;     /* Id of the thread signaling its exit */
sl@0
   272
    int          result; /* The result from the thread */
sl@0
   273
{
sl@0
   274
    JoinableThread* threadPtr;
sl@0
   275
sl@0
   276
    Tcl_MutexLock (&joinMutex);
sl@0
   277
sl@0
   278
    for (threadPtr = firstThreadPtr;
sl@0
   279
	 (threadPtr != (JoinableThread*) NULL) && (threadPtr->id != id);
sl@0
   280
	 threadPtr = threadPtr->nextThreadPtr)
sl@0
   281
        /* empty body */
sl@0
   282
      ;
sl@0
   283
sl@0
   284
    if (threadPtr == (JoinableThread*) NULL) {
sl@0
   285
        /* Thread not found. Not joinable. No problem, nothing to do.
sl@0
   286
	 */
sl@0
   287
sl@0
   288
        Tcl_MutexUnlock (&joinMutex);
sl@0
   289
	return;
sl@0
   290
    }
sl@0
   291
sl@0
   292
    /* Switch over the exclusive access from the list to the structure,
sl@0
   293
     * then store the result, set the flag and notify the waiting thread,
sl@0
   294
     * provided that it exists. The order of lock/unlock ensures that a
sl@0
   295
     * thread entering 'TclJoinThread' will not interfere with us.
sl@0
   296
     */
sl@0
   297
sl@0
   298
    Tcl_MutexLock   (&threadPtr->threadMutex);
sl@0
   299
    Tcl_MutexUnlock (&joinMutex);
sl@0
   300
sl@0
   301
    threadPtr->done   = 1;
sl@0
   302
    threadPtr->result = result;
sl@0
   303
sl@0
   304
    if (threadPtr->waitedUpon) {
sl@0
   305
      Tcl_ConditionNotify (&threadPtr->cond);
sl@0
   306
    }
sl@0
   307
sl@0
   308
    Tcl_MutexUnlock (&threadPtr->threadMutex);
sl@0
   309
}
sl@0
   310
sl@0
   311
#endif /* WIN32 || MAC_TCL */