diff options
Diffstat (limited to 'win/tclWinThrd.c')
-rw-r--r-- | win/tclWinThrd.c | 112 |
1 files changed, 52 insertions, 60 deletions
diff --git a/win/tclWinThrd.c b/win/tclWinThrd.c index dffb5b6..da9133f 100644 --- a/win/tclWinThrd.c +++ b/win/tclWinThrd.c @@ -3,9 +3,9 @@ * * This file implements the Windows-specific thread operations. * - * Copyright (c) 1998 by Sun Microsystems, Inc. - * Copyright (c) 1999 by Scriptics Corporation - * Copyright (c) 2008 by George Peter Staplin + * Copyright © 1998 Sun Microsystems, Inc. + * Copyright © 1999 Scriptics Corporation + * Copyright © 2008 George Peter Staplin * * See the file "license.terms" for information on usage and redistribution of * this file, and for a DISCLAIMER OF ALL WARRANTIES. @@ -13,8 +13,6 @@ #include "tclWinInt.h" -#include <float.h> - /* Workaround for mingw versions which don't provide this in float.h */ #ifndef _MCW_EM # define _MCW_EM 0x0008001F /* Error masks */ @@ -29,10 +27,7 @@ _CRTIMP unsigned int __cdecl _controlfp (unsigned int unNew, unsigned int unMask */ static CRITICAL_SECTION globalLock; -static int init = 0; -#define GLOBAL_LOCK TclpGlobalLock() -#define GLOBAL_UNLOCK TclpGlobalUnlock() - +static int initialized = 0; /* * This is the global lock used to serialize initialization and finalization @@ -43,10 +38,10 @@ static CRITICAL_SECTION initLock; /* * allocLock is used by Tcl's version of malloc for synchronization. For - * obvious reasons, cannot use any dynamically allocated storage. + * obvious reasons, cannot use any dyamically allocated storage. */ -#ifdef TCL_THREADS +#if TCL_THREADS static struct Tcl_Mutex_ { CRITICAL_SECTION crit; @@ -81,7 +76,7 @@ static CRITICAL_SECTION joinLock; * The per-thread event and queue pointers. */ -#ifdef TCL_THREADS +#if TCL_THREADS typedef struct ThreadSpecificData { HANDLE condEvent; /* Per-thread condition event */ @@ -110,7 +105,7 @@ static Tcl_ThreadDataKey dataKey; * the queue. */ -typedef struct WinCondition { +typedef struct { CRITICAL_SECTION condLock; /* Lock to serialize queuing on the * condition. */ struct ThreadSpecificData *firstPtr; /* Queue pointers */ @@ -122,10 +117,9 @@ typedef struct WinCondition { */ #ifdef USE_THREAD_ALLOC -static int once; static DWORD tlsKey; -typedef struct allocMutex { +typedef struct { Tcl_Mutex tlock; CRITICAL_SECTION wlock; } allocMutex; @@ -136,7 +130,7 @@ typedef struct allocMutex { * to TclWinThreadStart. */ -typedef struct WinThread { +typedef struct { LPTHREAD_START_ROUTINE lpStartAddress; /* Original startup routine */ LPVOID lpParameter; /* Original startup data */ unsigned int fpControl; /* Floating point control word from the @@ -184,7 +178,7 @@ TclWinThreadStart( lpOrigStartAddress = winThreadPtr->lpStartAddress; lpOrigParameter = winThreadPtr->lpParameter; - ckfree((char *)winThreadPtr); + ckfree(winThreadPtr); return lpOrigStartAddress(lpOrigParameter); } @@ -210,7 +204,7 @@ TclpThreadCreate( Tcl_ThreadId *idPtr, /* Return, the ID 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. */ + TCL_HASH_TYPE stackSize, /* Size of stack for the new thread. */ int flags) /* Flags controlling behaviour of the new * thread. */ { @@ -228,12 +222,12 @@ TclpThreadCreate( * on WIN64 sizeof void* != sizeof unsigned */ -#if defined(_MSC_VER) || defined(__MSVCRT__) || defined(__BORLANDC__) - tHandle = (HANDLE) _beginthreadex(NULL, (unsigned) stackSize, +#if defined(_MSC_VER) || defined(__MSVCRT__) + tHandle = (HANDLE) _beginthreadex(NULL, (unsigned)stackSize, (Tcl_ThreadCreateProc*) TclWinThreadStart, winThreadPtr, 0, (unsigned *)idPtr); #else - tHandle = CreateThread(NULL, (DWORD) stackSize, + tHandle = CreateThread(NULL, (DWORD)stackSize, TclWinThreadStart, winThreadPtr, 0, (LPDWORD)idPtr); #endif @@ -306,7 +300,7 @@ TclpThreadExit( TclSignalExitThread(Tcl_GetCurrentThread(), status); LeaveCriticalSection(&joinLock); -#if defined(_MSC_VER) || defined(__MSVCRT__) || defined(__BORLANDC__) +#if defined(_MSC_VER) || defined(__MSVCRT__) _endthreadex((unsigned) status); #else ExitThread((DWORD) status); @@ -332,7 +326,7 @@ TclpThreadExit( Tcl_ThreadId Tcl_GetCurrentThread(void) { - return (Tcl_ThreadId)(size_t)GetCurrentThreadId(); + return (Tcl_ThreadId)INT2PTR(GetCurrentThreadId()); } /* @@ -357,7 +351,7 @@ Tcl_GetCurrentThread(void) void TclpInitLock(void) { - if (!init) { + if (!initialized) { /* * There is a fundamental race here that is solved by creating the * first Tcl interpreter in a single threaded environment. Once the @@ -365,7 +359,7 @@ TclpInitLock(void) * that create interpreters in parallel. */ - init = 1; + initialized = 1; InitializeCriticalSection(&joinLock); InitializeCriticalSection(&initLock); InitializeCriticalSection(&globalLock); @@ -419,7 +413,7 @@ TclpInitUnlock(void) void TclpGlobalLock(void) { - if (!init) { + if (!initialized) { /* * There is a fundamental race here that is solved by creating the * first Tcl interpreter in a single threaded environment. Once the @@ -427,7 +421,7 @@ TclpGlobalLock(void) * that create interpreters in parallel. */ - init = 1; + initialized = 1; InitializeCriticalSection(&joinLock); InitializeCriticalSection(&initLock); InitializeCriticalSection(&globalLock); @@ -464,7 +458,7 @@ TclpGlobalUnlock(void) * Tcl_GetAllocMutex * * This procedure returns a pointer to a statically initialized mutex for - * use by the memory allocator. The allocator must use this lock, because + * use by the memory allocator. The alloctor must use this lock, because * all other locks are allocated... * * Results: @@ -480,7 +474,7 @@ TclpGlobalUnlock(void) Tcl_Mutex * Tcl_GetAllocMutex(void) { -#ifdef TCL_THREADS +#if TCL_THREADS if (!allocOnce) { InitializeCriticalSection(&allocLock.crit); allocOnce = 1; @@ -494,7 +488,7 @@ Tcl_GetAllocMutex(void) /* *---------------------------------------------------------------------- * - * TclpFinalizeLock + * TclFinalizeLock * * This procedure is used to destroy all private resources used in this * file. @@ -512,7 +506,7 @@ Tcl_GetAllocMutex(void) void TclFinalizeLock(void) { - GLOBAL_LOCK; + TclpGlobalLock(); DeleteCriticalSection(&joinLock); /* @@ -520,9 +514,9 @@ TclFinalizeLock(void) */ DeleteCriticalSection(&globalLock); - init = 0; + initialized = 0; -#ifdef TCL_THREADS +#if TCL_THREADS if (allocOnce) { DeleteCriticalSection(&allocLock.crit); allocOnce = 0; @@ -538,7 +532,7 @@ TclFinalizeLock(void) DeleteCriticalSection(&initLock); } -#ifdef TCL_THREADS +#if TCL_THREADS /* locally used prototype */ static void FinalizeConditionEvent(ClientData data); @@ -567,7 +561,7 @@ Tcl_MutexLock( CRITICAL_SECTION *csPtr; if (*mutexPtr == NULL) { - GLOBAL_LOCK; + TclpGlobalLock(); /* * Double inside global lock check to avoid a race. @@ -579,7 +573,7 @@ Tcl_MutexLock( *mutexPtr = (Tcl_Mutex)csPtr; TclRememberMutex(mutexPtr); } - GLOBAL_UNLOCK; + TclpGlobalUnlock(); } csPtr = *((CRITICAL_SECTION **)mutexPtr); EnterCriticalSection(csPtr); @@ -681,7 +675,7 @@ Tcl_ConditionWait( */ if (tsdPtr->flags == WIN_THREAD_UNINIT) { - GLOBAL_LOCK; + TclpGlobalLock(); /* * Create the per-thread event and queue pointers. @@ -695,7 +689,7 @@ Tcl_ConditionWait( tsdPtr->flags = WIN_THREAD_RUNNING; doExit = 1; } - GLOBAL_UNLOCK; + TclpGlobalUnlock(); if (doExit) { /* @@ -710,7 +704,7 @@ Tcl_ConditionWait( } if (*condPtr == NULL) { - GLOBAL_LOCK; + TclpGlobalLock(); /* * Initialize the per-condition queue pointers and Mutex. @@ -724,14 +718,14 @@ Tcl_ConditionWait( *condPtr = (Tcl_Condition) winCondPtr; TclRememberCondition(condPtr); } - GLOBAL_UNLOCK; + TclpGlobalUnlock(); } csPtr = *((CRITICAL_SECTION **)mutexPtr); winCondPtr = *((WinCondition **)condPtr); if (timePtr == NULL) { wtime = INFINITE; } else { - wtime = timePtr->sec * 1000 + timePtr->usec / 1000; + wtime = (DWORD)timePtr->sec * 1000 + (DWORD)timePtr->usec / 1000; } /* @@ -944,9 +938,9 @@ TclpFinalizeCondition( Tcl_Mutex * TclpNewAllocMutex(void) { - struct allocMutex *lockPtr; + allocMutex *lockPtr; - lockPtr = (struct allocMutex *)malloc(sizeof(struct allocMutex)); + lockPtr = (allocMutex *)malloc(sizeof(allocMutex)); if (lockPtr == NULL) { Tcl_Panic("could not allocate lock"); } @@ -968,24 +962,24 @@ TclpFreeAllocMutex( free(lockPtr); } -void * -TclpGetAllocCache(void) +void +TclpInitAllocCache(void) { - void *result; - - if (!once) { - /* - * We need to make sure that TclpFreeAllocCache is called on each - * thread that calls this, but only on threads that call this. - */ + /* + * We need to make sure that TclpFreeAllocCache is called on each + * thread that calls this, but only on threads that call this. + */ - tlsKey = TlsAlloc(); - once = 1; - if (tlsKey == TLS_OUT_OF_INDEXES) { - Tcl_Panic("could not allocate thread local storage"); - } + tlsKey = TlsAlloc(); + if (tlsKey == TLS_OUT_OF_INDEXES) { + Tcl_Panic("could not allocate thread local storage"); } +} +void * +TclpGetAllocCache(void) +{ + void *result; result = TlsGetValue(tlsKey); if ((result == NULL) && (GetLastError() != NO_ERROR)) { Tcl_Panic("TlsGetValue failed from TclpGetAllocCache"); @@ -1023,7 +1017,7 @@ TclpFreeAllocCache( if (!success) { Tcl_Panic("TlsSetValue failed from TclpFreeAllocCache"); } - } else if (once) { + } else { /* * Called by us in TclFinalizeThreadAlloc() during the library * finalization initiated from Tcl_Finalize() @@ -1033,9 +1027,7 @@ TclpFreeAllocCache( if (!success) { Tcl_Panic("TlsFree failed from TclpFreeAllocCache"); } - once = 0; /* reset for next time. */ } - } #endif /* USE_THREAD_ALLOC */ |