diff options
Diffstat (limited to 'unix/tclUnixThrd.c')
-rw-r--r-- | unix/tclUnixThrd.c | 80 |
1 files changed, 46 insertions, 34 deletions
diff --git a/unix/tclUnixThrd.c b/unix/tclUnixThrd.c index eac1bc8..d34bc88 100644 --- a/unix/tclUnixThrd.c +++ b/unix/tclUnixThrd.c @@ -9,16 +9,12 @@ * * See the file "license.terms" for information on usage and redistribution of * this file, and for a DISCLAIMER OF ALL WARRANTIES. - * - * RCS: @(#) $Id: tclUnixThrd.c,v 1.60 2008/08/13 23:08:39 das Exp $ */ #include "tclInt.h" #ifdef TCL_THREADS -#include "pthread.h" - typedef struct ThreadSpecificData { char nabuf[16]; } ThreadSpecificData; @@ -56,7 +52,6 @@ static pthread_mutex_t *allocLockPtr = &allocLock; #define MASTER_UNLOCK pthread_mutex_unlock(&masterLock) #endif /* TCL_THREADS */ - /* *---------------------------------------------------------------------- @@ -78,7 +73,7 @@ static pthread_mutex_t *allocLockPtr = &allocLock; int TclpThreadCreate( Tcl_ThreadId *idPtr, /* Return, the ID of the thread */ - Tcl_ThreadCreateProc proc, /* Main() function of the thread */ + Tcl_ThreadCreateProc *proc, /* Main() function of the thread */ ClientData clientData, /* The one argument to Main() */ int stackSize, /* Size of stack for the new thread */ int flags) /* Flags controlling behaviour of the new @@ -110,18 +105,19 @@ TclpThreadCreate( */ size_t size; + result = pthread_attr_getstacksize(&attr, &size); if (!result && (size < TCL_THREAD_STACK_MIN)) { pthread_attr_setstacksize(&attr, (size_t) TCL_THREAD_STACK_MIN); } -#endif +#endif /* TCL_THREAD_STACK_MIN */ } -#endif +#endif /* HAVE_PTHREAD_ATTR_SETSTACKSIZE */ + if (! (flags & TCL_THREAD_JOINABLE)) { pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); } - if (pthread_create(&theThread, &attr, (void * (*)(void *))proc, (void *)clientData) && pthread_create(&theThread, NULL, @@ -424,6 +420,7 @@ Tcl_MutexLock( Tcl_Mutex *mutexPtr) /* Really (pthread_mutex_t **) */ { pthread_mutex_t *pmutexPtr; + if (*mutexPtr == NULL) { MASTER_LOCK; if (*mutexPtr == NULL) { @@ -431,7 +428,7 @@ Tcl_MutexLock( * Double inside master lock check to avoid a race condition. */ - pmutexPtr = (pthread_mutex_t *)ckalloc(sizeof(pthread_mutex_t)); + pmutexPtr = ckalloc(sizeof(pthread_mutex_t)); pthread_mutex_init(pmutexPtr, NULL); *mutexPtr = (Tcl_Mutex)pmutexPtr; TclRememberMutex(mutexPtr); @@ -463,7 +460,8 @@ void Tcl_MutexUnlock( Tcl_Mutex *mutexPtr) /* Really (pthread_mutex_t **) */ { - pthread_mutex_t *pmutexPtr = *(pthread_mutex_t **)mutexPtr; + pthread_mutex_t *pmutexPtr = *(pthread_mutex_t **) mutexPtr; + pthread_mutex_unlock(pmutexPtr); } @@ -490,10 +488,11 @@ void TclpFinalizeMutex( Tcl_Mutex *mutexPtr) { - pthread_mutex_t *pmutexPtr = *(pthread_mutex_t **)mutexPtr; + pthread_mutex_t *pmutexPtr = *(pthread_mutex_t **) mutexPtr; + if (pmutexPtr != NULL) { pthread_mutex_destroy(pmutexPtr); - ckfree((char *) pmutexPtr); + ckfree(pmutexPtr); *mutexPtr = NULL; } } @@ -539,9 +538,9 @@ Tcl_ConditionWait( */ if (*condPtr == NULL) { - pcondPtr = (pthread_cond_t *) ckalloc(sizeof(pthread_cond_t)); + pcondPtr = ckalloc(sizeof(pthread_cond_t)); pthread_cond_init(pcondPtr, NULL); - *condPtr = (Tcl_Condition)pcondPtr; + *condPtr = (Tcl_Condition) pcondPtr; TclRememberCondition(condPtr); } MASTER_UNLOCK; @@ -623,9 +622,10 @@ TclpFinalizeCondition( Tcl_Condition *condPtr) { pthread_cond_t *pcondPtr = *(pthread_cond_t **)condPtr; + if (pcondPtr != NULL) { pthread_cond_destroy(pcondPtr); - ckfree((char *) pcondPtr); + ckfree(pcondPtr); *condPtr = NULL; } } @@ -634,7 +634,7 @@ TclpFinalizeCondition( /* *---------------------------------------------------------------------- * - * TclpReaddir, TclpLocaltime, TclpGmtime, TclpInetNtoa -- + * TclpReaddir, TclpInetNtoa -- * * These procedures replace core C versions to be used in a threaded * environment. @@ -659,6 +659,7 @@ TclpReaddir( return TclOSreaddir(dir); } +#undef TclpInetNtoa char * TclpInetNtoa( struct in_addr addr) @@ -726,6 +727,7 @@ TclpFreeAllocCache( */ TclFreeAllocCache(ptr); + pthread_setspecific(key, NULL); } else if (initialized) { /* @@ -760,45 +762,55 @@ TclpSetAllocCache( } #endif /* USE_THREAD_ALLOC */ +void * +TclpThreadCreateKey(void) +{ + pthread_key_t *ptkeyPtr; - -void *TclpThreadCreateKey(void) { - pthread_key_t *key; - - key = TclpSysAlloc(sizeof *key, 0); - if (NULL == key) { + ptkeyPtr = TclpSysAlloc(sizeof *ptkeyPtr, 0); + if (NULL == ptkeyPtr) { Tcl_Panic("unable to allocate thread key!"); } - if (pthread_key_create(key, NULL)) { + if (pthread_key_create(ptkeyPtr, NULL)) { Tcl_Panic("unable to create pthread key!"); } - return key; + return ptkeyPtr; } -void TclpThreadDeleteKey(void *keyPtr) { - pthread_key_t *key = keyPtr; +void +TclpThreadDeleteKey( + void *keyPtr) +{ + pthread_key_t *ptkeyPtr = keyPtr; - if (pthread_key_delete(*key)) { + if (pthread_key_delete(*ptkeyPtr)) { Tcl_Panic("unable to delete key!"); } TclpSysFree(keyPtr); } -void TclpThreadSetMasterTSD(void *tsdKeyPtr, void *ptr) { - pthread_key_t *key = tsdKeyPtr; +void +TclpThreadSetMasterTSD( + void *tsdKeyPtr, + void *ptr) +{ + pthread_key_t *ptkeyPtr = tsdKeyPtr; - if (pthread_setspecific(*key, ptr)) { + if (pthread_setspecific(*ptkeyPtr, ptr)) { Tcl_Panic("unable to set master TSD value"); } } -void *TclpThreadGetMasterTSD(void *tsdKeyPtr) { - pthread_key_t *key = tsdKeyPtr; +void * +TclpThreadGetMasterTSD( + void *tsdKeyPtr) +{ + pthread_key_t *ptkeyPtr = tsdKeyPtr; - return pthread_getspecific(*key); + return pthread_getspecific(*ptkeyPtr); } #endif /* TCL_THREADS */ |