Index: doc/Thread.3 ================================================================== --- doc/Thread.3 +++ doc/Thread.3 @@ -43,11 +43,13 @@ .SH ARGUMENTS .AS Tcl_CreateThreadProc proc out .AP Tcl_Condition *condPtr in A condition variable, which must be associated with a mutex lock. .AP Tcl_Mutex *mutexPtr in -A mutex lock. +.VS TIP509 +A recursive mutex lock. +.VE TIP509 .AP "const Tcl_Time" *timePtr in A time limit on the condition wait. NULL to wait forever. Note that a polling value of 0 seconds does not make much sense. .AP Tcl_ThreadDataKey *keyPtr in This identifies a block of thread local storage. The key should be @@ -133,11 +135,15 @@ .SS "SYNCHRONIZATION AND COMMUNICATION" Tcl provides \fBTcl_ThreadQueueEvent\fR and \fBTcl_ThreadAlert\fR for handling event queuing in multithreaded applications. See the \fBNotifier\fR manual page for more information on these procedures. .PP -A mutex is a lock that is used to serialize all threads through a piece +A mutex is a +.VS TIP509 +recursive +.VE TIP509 +lock that is used to serialize all threads through a piece of code by calling \fBTcl_MutexLock\fR and \fBTcl_MutexUnlock\fR. If one thread holds a mutex, any other thread calling \fBTcl_MutexLock\fR will block until \fBTcl_MutexUnlock\fR is called. A mutex can be destroyed after its use by calling \fBTcl_MutexFinalize\fR. The result of locking a mutex twice from the same thread is undefined. Index: unix/configure ================================================================== --- unix/configure +++ unix/configure @@ -1864,10 +1864,56 @@ { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_res" >&5 $as_echo "$ac_res" >&6; } eval $as_lineno_stack; ${as_lineno_stack:+:} unset as_lineno } # ac_fn_c_check_func + +# ac_fn_c_check_decl LINENO SYMBOL VAR INCLUDES +# --------------------------------------------- +# Tests whether SYMBOL is declared in INCLUDES, setting cache variable VAR +# accordingly. +ac_fn_c_check_decl () +{ + as_lineno=${as_lineno-"$1"} as_lineno_stack=as_lineno_stack=$as_lineno_stack + as_decl_name=`echo $2|sed 's/ *(.*//'` + as_decl_use=`echo $2|sed -e 's/(/((/' -e 's/)/) 0&/' -e 's/,/) 0& (/g'` + { $as_echo "$as_me:${as_lineno-$LINENO}: checking whether $as_decl_name is declared" >&5 +$as_echo_n "checking whether $as_decl_name is declared... " >&6; } +if eval \${$3+:} false; then : + $as_echo_n "(cached) " >&6 +else + cat confdefs.h - <<_ACEOF >conftest.$ac_ext +/* end confdefs.h. */ +$4 +int +main () +{ +#ifndef $as_decl_name +#ifdef __cplusplus + (void) $as_decl_use; +#else + (void) $as_decl_name; +#endif +#endif + + ; + return 0; +} +_ACEOF +if ac_fn_c_try_compile "$LINENO"; then : + eval "$3=yes" +else + eval "$3=no" +fi +rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext +fi +eval ac_res=\$$3 + { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_res" >&5 +$as_echo "$ac_res" >&6; } + eval $as_lineno_stack; ${as_lineno_stack:+:} unset as_lineno + +} # ac_fn_c_check_decl # ac_fn_c_check_type LINENO TYPE VAR INCLUDES # ------------------------------------------- # Tests whether TYPE exists after having included INCLUDES, setting cache # variable VAR accordingly. @@ -4408,10 +4454,29 @@ fi done LIBS=$ac_saved_libs + + # TIP #509 + ac_fn_c_check_decl "$LINENO" "PTHREAD_MUTEX_RECURSIVE" "ac_cv_have_decl_PTHREAD_MUTEX_RECURSIVE" "#include +" +if test "x$ac_cv_have_decl_PTHREAD_MUTEX_RECURSIVE" = xyes; then : + ac_have_decl=1 +else + ac_have_decl=0 +fi + +cat >>confdefs.h <<_ACEOF +#define HAVE_DECL_PTHREAD_MUTEX_RECURSIVE $ac_have_decl +_ACEOF +if test $ac_have_decl = 1; then : + tcl_ok=yes +else + tcl_ok=no +fi + # Add the threads support libraries LIBS="$LIBS$THREADS_LIBS" Index: unix/tcl.m4 ================================================================== --- unix/tcl.m4 +++ unix/tcl.m4 @@ -2354,10 +2354,13 @@ ac_saved_libs=$LIBS LIBS="$LIBS $THREADS_LIBS" AC_CHECK_FUNCS(pthread_attr_setstacksize pthread_atfork) LIBS=$ac_saved_libs + + # TIP #509 + AC_CHECK_DECLS([PTHREAD_MUTEX_RECURSIVE],tcl_ok=yes,tcl_ok=no, [[#include ]]) ]) #-------------------------------------------------------------------- # SC_TCL_EARLY_FLAGS # Index: unix/tclUnixThrd.c ================================================================== --- unix/tclUnixThrd.c +++ unix/tclUnixThrd.c @@ -12,18 +12,163 @@ */ #include "tclInt.h" #if TCL_THREADS + +/* + * TIP #509. Ensures that Tcl's mutexes are reentrant. + * + *---------------------------------------------------------------------- + * + * PMutexInit -- + * + * Sets up the memory pointed to by its argument so that it contains the + * implementation of a recursive lock. Caller supplies the space. + * + *---------------------------------------------------------------------- + * + * PMutexDestroy -- + * + * Tears down the implementation of a recursive lock (but does not + * deallocate the space holding the lock). + * + *---------------------------------------------------------------------- + * + * PMutexLock -- + * + * Locks a recursive lock. (Similar to pthread_mutex_lock) + * + *---------------------------------------------------------------------- + * + * PMutexUnlock -- + * + * Unlocks a recursive lock. (Similar to pthread_mutex_unlock) + * + *---------------------------------------------------------------------- + * + * PCondWait -- + * + * Waits on a condition variable linked a recursive lock. (Similar to + * pthread_cond_wait) + * + *---------------------------------------------------------------------- + * + * PCondTimedWait -- + * + * Waits for a limited amount of time on a condition variable linked to a + * recursive lock. (Similar to pthread_cond_timedwait) + * + *---------------------------------------------------------------------- + */ +#ifndef HAVE_DECL_PTHREAD_MUTEX_RECURSIVE +#define HAVE_DECL_PTHREAD_MUTEX_RECURSIVE 0 +#endif + +#if HAVE_DECL_PTHREAD_MUTEX_RECURSIVE +/* + * Pthread has native reentrant (AKA recursive) mutexes. Use them for + * Tcl_Mutex. + */ + +typedef pthread_mutex_t PMutex; + +static void +PMutexInit( + PMutex *pmutexPtr) +{ + pthread_mutexattr_t attr; + + pthread_mutexattr_init(&attr); + pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); + pthread_mutex_init(pmutexPtr, &attr); +} + +#define PMutexDestroy pthread_mutex_destroy +#define PMutexLock pthread_mutex_lock +#define PMutexUnlock pthread_mutex_unlock +#define PCondWait pthread_cond_wait +#define PCondTimedWait pthread_cond_timedwait + +#else /* !HAVE_PTHREAD_MUTEX_RECURSIVE */ + +/* + * No native support for reentrant mutexes. Emulate them with regular mutexes + * and thread-local counters. + */ + +typedef struct PMutex { + pthread_mutex_t mutex; + pthread_t thread; + int counter; +} PMutex; + +static void +PMutexInit( + PMutex *pmutexPtr) +{ + pthread_mutex_init(&pmutexPtr->mutex, NULL); + pmutexPtr->thread = 0; + pmutexPtr->counter = 0; +} + +static void +PMutexDestroy( + PMutex *pmutexPtr) +{ + pthread_mutex_destroy(&pmutexPtr->mutex); +} + +static void +PMutexLock( + PMutex *pmutexPtr) +{ + if (pmutexPtr->thread != pthread_self() || pmutexPtr->counter == 0) { + pthread_mutex_lock(&pmutexPtr->mutex); + pmutexPtr->thread = pthread_self(); + pmutexPtr->counter = 0; + } + pmutexPtr->counter++; +} + +static void +PMutexUnlock( + PMutex *pmutexPtr) +{ + pmutexPtr->counter--; + if (pmutexPtr->counter == 0) { + pmutexPtr->thread = 0; + pthread_mutex_unlock(&pmutexPtr->mutex); + } +} + +static void +PCondWait( + pthread_cond_t *pcondPtr, + PMutex *pmutexPtr) +{ + pthread_cond_wait(pcondPtr, &pmutexPtr->mutex); +} + +static void +PCondTimedWait( + pthread_cond_t *pcondPtr, + PMutex *pmutexPtr, + struct timespec *ptime) +{ + pthread_cond_timedwait(pcondPtr, &pmutexPtr->mutex, ptime); +} +#endif /* HAVE_PTHREAD_MUTEX_RECURSIVE */ + #ifndef TCL_NO_DEPRECATED typedef struct { char nabuf[16]; } ThreadSpecificData; static Tcl_ThreadDataKey dataKey; -#endif +#endif /* TCL_NO_DEPRECATED */ /* * masterLock is used to serialize creation of mutexes, condition variables, * and thread local storage. This is the only place that can count on the * ability to statically initialize the mutex. @@ -41,12 +186,19 @@ /* * allocLock is used by Tcl's version of malloc for synchronization. For * obvious reasons, cannot use any dyamically allocated storage. */ -static pthread_mutex_t allocLock = PTHREAD_MUTEX_INITIALIZER; -static pthread_mutex_t *allocLockPtr = &allocLock; +static PMutex allocLock; +static pthread_once_t allocLockInitOnce = PTHREAD_ONCE_INIT; + +static void +allocLockInit(void) +{ + PMutexInit(&allocLock); +} +static PMutex *allocLockPtr = &allocLock; #endif /* TCL_THREADS */ /* *---------------------------------------------------------------------- @@ -107,21 +259,21 @@ } #endif /* TCL_THREAD_STACK_MIN */ } #endif /* HAVE_PTHREAD_ATTR_SETSTACKSIZE */ - if (! (flags & TCL_THREAD_JOINABLE)) { - pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); + if (!(flags & TCL_THREAD_JOINABLE)) { + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); } if (pthread_create(&theThread, &attr, - (void * (*)(void *))proc, (void *)clientData) && + (void * (*)(void *)) proc, (void *) clientData) && pthread_create(&theThread, NULL, - (void * (*)(void *))proc, (void *)clientData)) { + (void * (*)(void *)) proc, (void *) clientData)) { result = TCL_ERROR; } else { - *idPtr = (Tcl_ThreadId)theThread; + *idPtr = (Tcl_ThreadId) theThread; result = TCL_OK; } pthread_attr_destroy(&attr); return result; #else @@ -329,11 +481,10 @@ { #if TCL_THREADS pthread_mutex_lock(&masterLock); #endif } - /* *---------------------------------------------------------------------- * * TclpMasterUnlock @@ -379,11 +530,13 @@ Tcl_Mutex * Tcl_GetAllocMutex(void) { #if TCL_THREADS - pthread_mutex_t **allocLockPtrPtr = &allocLockPtr; + PMutex **allocLockPtrPtr = &allocLockPtr; + + pthread_once(&allocLockInitOnce, allocLockInit); return (Tcl_Mutex *) allocLockPtrPtr; #else return NULL; #endif } @@ -411,30 +564,30 @@ *---------------------------------------------------------------------- */ void Tcl_MutexLock( - Tcl_Mutex *mutexPtr) /* Really (pthread_mutex_t **) */ + Tcl_Mutex *mutexPtr) /* Really (PMutex **) */ { - pthread_mutex_t *pmutexPtr; + PMutex *pmutexPtr; if (*mutexPtr == NULL) { pthread_mutex_lock(&masterLock); if (*mutexPtr == NULL) { /* * Double inside master lock check to avoid a race condition. */ - pmutexPtr = ckalloc(sizeof(pthread_mutex_t)); - pthread_mutex_init(pmutexPtr, NULL); - *mutexPtr = (Tcl_Mutex)pmutexPtr; + pmutexPtr = ckalloc(sizeof(PMutex)); + PMutexInit(pmutexPtr); + *mutexPtr = (Tcl_Mutex) pmutexPtr; TclRememberMutex(mutexPtr); } pthread_mutex_unlock(&masterLock); } - pmutexPtr = *((pthread_mutex_t **)mutexPtr); - pthread_mutex_lock(pmutexPtr); + pmutexPtr = *((PMutex **) mutexPtr); + PMutexLock(pmutexPtr); } /* *---------------------------------------------------------------------- * @@ -452,15 +605,15 @@ *---------------------------------------------------------------------- */ void Tcl_MutexUnlock( - Tcl_Mutex *mutexPtr) /* Really (pthread_mutex_t **) */ + Tcl_Mutex *mutexPtr) /* Really (PMutex **) */ { - pthread_mutex_t *pmutexPtr = *(pthread_mutex_t **) mutexPtr; + PMutex *pmutexPtr = *(PMutex **) mutexPtr; - pthread_mutex_unlock(pmutexPtr); + PMutexUnlock(pmutexPtr); } /* *---------------------------------------------------------------------- * @@ -482,14 +635,14 @@ void TclpFinalizeMutex( Tcl_Mutex *mutexPtr) { - pthread_mutex_t *pmutexPtr = *(pthread_mutex_t **) mutexPtr; + PMutex *pmutexPtr = *(PMutex **) mutexPtr; if (pmutexPtr != NULL) { - pthread_mutex_destroy(pmutexPtr); + PMutexDestroy(pmutexPtr); ckfree(pmutexPtr); *mutexPtr = NULL; } } @@ -516,15 +669,15 @@ */ void Tcl_ConditionWait( Tcl_Condition *condPtr, /* Really (pthread_cond_t **) */ - Tcl_Mutex *mutexPtr, /* Really (pthread_mutex_t **) */ + Tcl_Mutex *mutexPtr, /* Really (PMutex **) */ const Tcl_Time *timePtr) /* Timeout on waiting period */ { pthread_cond_t *pcondPtr; - pthread_mutex_t *pmutexPtr; + PMutex *pmutexPtr; struct timespec ptime; if (*condPtr == NULL) { pthread_mutex_lock(&masterLock); @@ -539,14 +692,14 @@ *condPtr = (Tcl_Condition) pcondPtr; TclRememberCondition(condPtr); } pthread_mutex_unlock(&masterLock); } - pmutexPtr = *((pthread_mutex_t **)mutexPtr); - pcondPtr = *((pthread_cond_t **)condPtr); + pmutexPtr = *((PMutex **) mutexPtr); + pcondPtr = *((pthread_cond_t **) condPtr); if (timePtr == NULL) { - pthread_cond_wait(pcondPtr, pmutexPtr); + PCondWait(pcondPtr, pmutexPtr); } else { Tcl_Time now; /* * Make sure to take into account the microsecond component of the @@ -555,11 +708,11 @@ Tcl_GetTime(&now); ptime.tv_sec = timePtr->sec + now.sec + (timePtr->usec + now.usec) / 1000000; ptime.tv_nsec = 1000 * ((timePtr->usec + now.usec) % 1000000); - pthread_cond_timedwait(pcondPtr, pmutexPtr, &ptime); + PCondTimedWait(pcondPtr, pmutexPtr, &ptime); } } /* *---------------------------------------------------------------------- @@ -582,16 +735,17 @@ void Tcl_ConditionNotify( Tcl_Condition *condPtr) { - pthread_cond_t *pcondPtr = *((pthread_cond_t **)condPtr); + pthread_cond_t *pcondPtr = *((pthread_cond_t **) condPtr); + if (pcondPtr != NULL) { pthread_cond_broadcast(pcondPtr); } else { /* - * Noone has used the condition variable, so there are no waiters. + * No-one has used the condition variable, so there are no waiters. */ } } /* @@ -615,11 +769,11 @@ void TclpFinalizeCondition( Tcl_Condition *condPtr) { - pthread_cond_t *pcondPtr = *(pthread_cond_t **)condPtr; + pthread_cond_t *pcondPtr = *(pthread_cond_t **) condPtr; if (pcondPtr != NULL) { pthread_cond_destroy(pcondPtr); ckfree(pcondPtr); *condPtr = NULL; @@ -681,38 +835,39 @@ #ifdef USE_THREAD_ALLOC static pthread_key_t key; typedef struct { Tcl_Mutex tlock; - pthread_mutex_t plock; -} allocMutex; + PMutex plock; +} AllocMutex; Tcl_Mutex * TclpNewAllocMutex(void) { - allocMutex *lockPtr; - register pthread_mutex_t *plockPtr; + AllocMutex *lockPtr; + register PMutex *plockPtr; - lockPtr = malloc(sizeof(allocMutex)); + lockPtr = malloc(sizeof(AllocMutex)); if (lockPtr == NULL) { Tcl_Panic("could not allocate lock"); } plockPtr = &lockPtr->plock; lockPtr->tlock = (Tcl_Mutex) plockPtr; - pthread_mutex_init(&lockPtr->plock, NULL); + PMutexInit(&lockPtr->plock); return &lockPtr->tlock; } void TclpFreeAllocMutex( Tcl_Mutex *mutex) /* The alloc mutex to free. */ { - allocMutex* lockPtr = (allocMutex*) mutex; + AllocMutex *lockPtr = (AllocMutex *) mutex; + if (!lockPtr) { return; } - pthread_mutex_destroy(&lockPtr->plock); + PMutexDestroy(&lockPtr->plock); free(lockPtr); } void TclpInitAllocCache(void) @@ -760,11 +915,11 @@ void * TclpThreadCreateKey(void) { pthread_key_t *ptkeyPtr; - ptkeyPtr = TclpSysAlloc(sizeof *ptkeyPtr, 0); + ptkeyPtr = TclpSysAlloc(sizeof(pthread_key_t), 0); if (NULL == ptkeyPtr) { Tcl_Panic("unable to allocate thread key!"); } if (pthread_key_create(ptkeyPtr, NULL)) {