Debian Codegraph @

win/tclWinThrd.c

raw
1 /*
2 * tclWinThread.c --
3 *
4 * This file implements the Windows-specific thread operations.
5 *
6 * Copyright (c) 1998 Sun Microsystems, Inc.
7 * Copyright (c) 1999 Scriptics Corporation
8 * Copyright (c) 2008 George Peter Staplin
9 *
10 * See the file "license.terms" for information on usage and redistribution of
11 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
12 */
13
14 #include "tclWinInt.h"
15
16 #include <float.h>
17
18 /* Workaround for mingw versions which don't provide this in float.h */
19 #ifndef _MCW_EM
20 # define _MCW_EM 0x0008001F /* Error masks */
21 # define _MCW_RC 0x00000300 /* Rounding */
22 # define _MCW_PC 0x00030000 /* Precision */
23 _CRTIMP unsigned int __cdecl _controlfp (unsigned int unNew, unsigned int unMask);
24 #endif
25
26 /*
27 * This is the global lock used to serialize access to other serialization
28 * data structures.
29 */
30
31 static CRITICAL_SECTION globalLock;
32 static int initialized = 0;
33
34 /*
35 * This is the global lock used to serialize initialization and finalization
36 * of Tcl as a whole.
37 */
38
39 static CRITICAL_SECTION initLock;
40
41 /*
42 * allocLock is used by Tcl's version of malloc for synchronization. For
43 * obvious reasons, cannot use any dynamically allocated storage.
44 */
45
46 #ifdef TCL_THREADS
47
48 static struct Tcl_Mutex_ {
49 CRITICAL_SECTION crit;
50 } allocLock;
51 static Tcl_Mutex allocLockPtr = &allocLock;
52 static int allocOnce = 0;
53
54 #endif /* TCL_THREADS */
55
56 /*
57 * The joinLock serializes Create- and ExitThread. This is necessary to
58 * prevent a race where a new joinable thread exits before the creating thread
59 * had the time to create the necessary data structures in the emulation
60 * layer.
61 */
62
63 static CRITICAL_SECTION joinLock;
64
65 /*
66 * Condition variables are implemented with a combination of a per-thread
67 * Windows Event and a per-condition waiting queue. The idea is that each
68 * thread has its own Event that it waits on when it is doing a ConditionWait;
69 * it uses the same event for all condition variables because it only waits on
70 * one at a time. Each condition variable has a queue of waiting threads, and
71 * a mutex used to serialize access to this queue.
72 *
73 * Special thanks to David Nichols and Jim Davidson for advice on the
74 * Condition Variable implementation.
75 */
76
77 /*
78 * The per-thread event and queue pointers.
79 */
80
81 #ifdef TCL_THREADS
82
83 typedef struct ThreadSpecificData {
84 HANDLE condEvent; /* Per-thread condition event */
85 struct ThreadSpecificData *nextPtr; /* Queue pointers */
86 struct ThreadSpecificData *prevPtr;
87 int flags; /* See flags below */
88 } ThreadSpecificData;
89 static Tcl_ThreadDataKey dataKey;
90
91 #endif /* TCL_THREADS */
92
93 /*
94 * State bits for the thread.
95 * WIN_THREAD_UNINIT Uninitialized. Must be zero because of the way
96 * ThreadSpecificData is created.
97 * WIN_THREAD_RUNNING Running, not waiting.
98 * WIN_THREAD_BLOCKED Waiting, or trying to wait.
99 */
100
101 #define WIN_THREAD_UNINIT 0x0
102 #define WIN_THREAD_RUNNING 0x1
103 #define WIN_THREAD_BLOCKED 0x2
104
105 /*
106 * The per condition queue pointers and the Mutex used to serialize access to
107 * the queue.
108 */
109
110 typedef struct WinCondition {
111 CRITICAL_SECTION condLock; /* Lock to serialize queuing on the
112 * condition. */
113 struct ThreadSpecificData *firstPtr; /* Queue pointers */
114 struct ThreadSpecificData *lastPtr;
115 } WinCondition;
116
117 /*
118 * Additions by AOL for specialized thread memory allocator.
119 */
120
121 #ifdef USE_THREAD_ALLOC
122 static int once;
123 static DWORD tlsKey;
124
125 typedef struct allocMutex {
126 Tcl_Mutex tlock;
127 CRITICAL_SECTION wlock;
128 } allocMutex;
129 #endif /* USE_THREAD_ALLOC */
130
131 /*
132 * The per thread data passed from TclpThreadCreate
133 * to TclWinThreadStart.
134 */
135
136 typedef struct WinThread {
137 LPTHREAD_START_ROUTINE lpStartAddress; /* Original startup routine */
138 LPVOID lpParameter; /* Original startup data */
139 unsigned int fpControl; /* Floating point control word from the
140 * main thread */
141 } WinThread;
142
143
144 /*
145 *----------------------------------------------------------------------
146 *
147 * TclWinThreadStart --
148 *
149 * This procedure is the entry point for all new threads created
150 * by Tcl on Windows.
151 *
152 * Results:
153 * Various, depending on the result of the wrapped thread start
154 * routine.
155 *
156 * Side effects:
157 * Arbitrary, since user code is executed.
158 *
159 *----------------------------------------------------------------------
160 */
161
162 static DWORD WINAPI
163 TclWinThreadStart(
164 LPVOID lpParameter) /* The WinThread structure pointer passed
165 * from TclpThreadCreate */
166 {
167 WinThread *winThreadPtr = (WinThread *) lpParameter;
168 LPTHREAD_START_ROUTINE lpOrigStartAddress;
169 LPVOID lpOrigParameter;
170
171 if (!winThreadPtr) {
172 return TCL_ERROR;
173 }
174
175 _controlfp(winThreadPtr->fpControl, _MCW_EM | _MCW_RC | 0x03000000 /* _MCW_DN */
176 #if !defined(_WIN64)
177 | _MCW_PC
178 #endif
179 );
180
181 lpOrigStartAddress = winThreadPtr->lpStartAddress;
182 lpOrigParameter = winThreadPtr->lpParameter;
183
184 ckfree((char *)winThreadPtr);
185 return lpOrigStartAddress(lpOrigParameter);
186 }
187
188 /*
189 *----------------------------------------------------------------------
190 *
191 * TclpThreadCreate --
192 *
193 * This procedure creates a new thread.
194 *
195 * Results:
196 * TCL_OK if the thread could be created. The thread ID is returned in a
197 * parameter.
198 *
199 * Side effects:
200 * A new thread is created.
201 *
202 *----------------------------------------------------------------------
203 */
204
205 int
206 TclpThreadCreate(
207 Tcl_ThreadId *idPtr, /* Return, the ID of the thread. */
208 Tcl_ThreadCreateProc *proc, /* Main() function of the thread. */
209 ClientData clientData, /* The one argument to Main(). */
210 int stackSize, /* Size of stack for the new thread. */
211 int flags) /* Flags controlling behaviour of the new
212 * thread. */
213 {
214 WinThread *winThreadPtr; /* Per-thread startup info */
215 HANDLE tHandle;
216
217 winThreadPtr = (WinThread *)ckalloc(sizeof(WinThread));
218 winThreadPtr->lpStartAddress = (LPTHREAD_START_ROUTINE) proc;
219 winThreadPtr->lpParameter = clientData;
220 winThreadPtr->fpControl = _controlfp(0, 0);
221
222 EnterCriticalSection(&joinLock);
223
224 *idPtr = 0; /* must initialize as Tcl_Thread is a pointer and
225 * on WIN64 sizeof void* != sizeof unsigned */
226
227 #if defined(_MSC_VER) || defined(__MSVCRT__) || defined(__BORLANDC__)
228 tHandle = (HANDLE) _beginthreadex(NULL, (unsigned) stackSize,
229 (Tcl_ThreadCreateProc*) TclWinThreadStart, winThreadPtr,
230 0, (unsigned *)idPtr);
231 #else
232 tHandle = CreateThread(NULL, (DWORD)stackSize,
233 TclWinThreadStart, winThreadPtr, 0, (LPDWORD)idPtr);
234 #endif
235
236 if (tHandle == NULL) {
237 LeaveCriticalSection(&joinLock);
238 return TCL_ERROR;
239 } else {
240 if (flags & TCL_THREAD_JOINABLE) {
241 TclRememberJoinableThread(*idPtr);
242 }
243
244 /*
245 * The only purpose of this is to decrement the reference count so the
246 * OS resources will be reacquired when the thread closes.
247 */
248
249 CloseHandle(tHandle);
250 LeaveCriticalSection(&joinLock);
251 return TCL_OK;
252 }
253 }
254
255 /*
256 *----------------------------------------------------------------------
257 *
258 * Tcl_JoinThread --
259 *
260 * This procedure waits upon the exit of the specified thread.
261 *
262 * Results:
263 * TCL_OK if the wait was successful, TCL_ERROR else.
264 *
265 * Side effects:
266 * The result area is set to the exit code of the thread we
267 * waited upon.
268 *
269 *----------------------------------------------------------------------
270 */
271
272 int
273 Tcl_JoinThread(
274 Tcl_ThreadId threadId, /* Id of the thread to wait upon */
275 int *result) /* Reference to the storage the result of the
276 * thread we wait upon will be written into. */
277 {
278 return TclJoinThread(threadId, result);
279 }
280
281 /*
282 *----------------------------------------------------------------------
283 *
284 * TclpThreadExit --
285 *
286 * This procedure terminates the current thread.
287 *
288 * Results:
289 * None.
290 *
291 * Side effects:
292 * This procedure terminates the current thread.
293 *
294 *----------------------------------------------------------------------
295 */
296
297 TCL_NORETURN void
298 TclpThreadExit(
299 int status)
300 {
301 EnterCriticalSection(&joinLock);
302 TclSignalExitThread(Tcl_GetCurrentThread(), status);
303 LeaveCriticalSection(&joinLock);
304
305 #if defined(_MSC_VER) || defined(__MSVCRT__) || defined(__BORLANDC__)
306 _endthreadex((unsigned) status);
307 #else
308 ExitThread((DWORD) status);
309 #endif
310 }
311
312 /*
313 *----------------------------------------------------------------------
314 *
315 * Tcl_GetCurrentThread --
316 *
317 * This procedure returns the ID of the currently running thread.
318 *
319 * Results:
320 * A thread ID.
321 *
322 * Side effects:
323 * None.
324 *
325 *----------------------------------------------------------------------
326 */
327
328 Tcl_ThreadId
329 Tcl_GetCurrentThread(void)
330 {
331 return (Tcl_ThreadId)INT2PTR(GetCurrentThreadId());
332 }
333
334 /*
335 *----------------------------------------------------------------------
336 *
337 * TclpInitLock
338 *
339 * This procedure is used to grab a lock that serializes initialization
340 * and finalization of Tcl. On some platforms this may also initialize
341 * the mutex used to serialize creation of more mutexes and thread local
342 * storage keys.
343 *
344 * Results:
345 * None.
346 *
347 * Side effects:
348 * Acquire the initialization mutex.
349 *
350 *----------------------------------------------------------------------
351 */
352
353 void
354 TclpInitLock(void)
355 {
356 if (!initialized) {
357 /*
358 * There is a fundamental race here that is solved by creating the
359 * first Tcl interpreter in a single threaded environment. Once the
360 * interpreter has been created, it is safe to create more threads
361 * that create interpreters in parallel.
362 */
363
364 initialized = 1;
365 InitializeCriticalSection(&joinLock);
366 InitializeCriticalSection(&initLock);
367 InitializeCriticalSection(&globalLock);
368 }
369 EnterCriticalSection(&initLock);
370 }
371
372 /*
373 *----------------------------------------------------------------------
374 *
375 * TclpInitUnlock
376 *
377 * This procedure is used to release a lock that serializes
378 * initialization and finalization of Tcl.
379 *
380 * Results:
381 * None.
382 *
383 * Side effects:
384 * Release the initialization mutex.
385 *
386 *----------------------------------------------------------------------
387 */
388
389 void
390 TclpInitUnlock(void)
391 {
392 LeaveCriticalSection(&initLock);
393 }
394
395 /*
396 *----------------------------------------------------------------------
397 *
398 * TclpGlobalLock
399 *
400 * This procedure is used to grab a lock that serializes creation of
401 * mutexes, condition variables, and thread local storage keys.
402 *
403 * This lock must be different than the initLock because the initLock is
404 * held during creation of synchronization objects.
405 *
406 * Results:
407 * None.
408 *
409 * Side effects:
410 * Acquire the global mutex.
411 *
412 *----------------------------------------------------------------------
413 */
414
415 void
416 TclpGlobalLock(void)
417 {
418 if (!initialized) {
419 /*
420 * There is a fundamental race here that is solved by creating the
421 * first Tcl interpreter in a single threaded environment. Once the
422 * interpreter has been created, it is safe to create more threads
423 * that create interpreters in parallel.
424 */
425
426 initialized = 1;
427 InitializeCriticalSection(&joinLock);
428 InitializeCriticalSection(&initLock);
429 InitializeCriticalSection(&globalLock);
430 }
431 EnterCriticalSection(&globalLock);
432 }
433
434 /*
435 *----------------------------------------------------------------------
436 *
437 * TclpGlobalUnlock
438 *
439 * This procedure is used to release a lock that serializes creation and
440 * deletion of synchronization objects.
441 *
442 * Results:
443 * None.
444 *
445 * Side effects:
446 * Release the global mutex.
447 *
448 *----------------------------------------------------------------------
449 */
450
451 void
452 TclpGlobalUnlock(void)
453 {
454 LeaveCriticalSection(&globalLock);
455 }
456
457 /*
458 *----------------------------------------------------------------------
459 *
460 * Tcl_GetAllocMutex
461 *
462 * This procedure returns a pointer to a statically initialized mutex for
463 * use by the memory allocator. The allocator must use this lock, because
464 * all other locks are allocated...
465 *
466 * Results:
467 * A pointer to a mutex that is suitable for passing to Tcl_MutexLock and
468 * Tcl_MutexUnlock.
469 *
470 * Side effects:
471 * None.
472 *
473 *----------------------------------------------------------------------
474 */
475
476 Tcl_Mutex *
477 Tcl_GetAllocMutex(void)
478 {
479 #ifdef TCL_THREADS
480 if (!allocOnce) {
481 InitializeCriticalSection(&allocLock.crit);
482 allocOnce = 1;
483 }
484 return &allocLockPtr;
485 #else
486 return NULL;
487 #endif
488 }
489
490 /*
491 *----------------------------------------------------------------------
492 *
493 * TclFinalizeLock
494 *
495 * This procedure is used to destroy all private resources used in this
496 * file.
497 *
498 * Results:
499 * None.
500 *
501 * Side effects:
502 * Destroys everything private. TclpInitLock must be held entering this
503 * function.
504 *
505 *----------------------------------------------------------------------
506 */
507
508 void
509 TclFinalizeLock(void)
510 {
511 TclpGlobalLock();
512 DeleteCriticalSection(&joinLock);
513
514 /*
515 * Destroy the critical section that we are holding!
516 */
517
518 DeleteCriticalSection(&globalLock);
519 initialized = 0;
520
521 #ifdef TCL_THREADS
522 if (allocOnce) {
523 DeleteCriticalSection(&allocLock.crit);
524 allocOnce = 0;
525 }
526 #endif
527
528 LeaveCriticalSection(&initLock);
529
530 /*
531 * Destroy the critical section that we were holding.
532 */
533
534 DeleteCriticalSection(&initLock);
535 }
536
537 #ifdef TCL_THREADS
538
539 /* locally used prototype */
540 static void FinalizeConditionEvent(ClientData data);
541
542 /*
543 *----------------------------------------------------------------------
544 *
545 * Tcl_MutexLock --
546 *
547 * This procedure is invoked to lock a mutex. This is a self initializing
548 * mutex that is automatically finalized during Tcl_Finalize.
549 *
550 * Results:
551 * None.
552 *
553 * Side effects:
554 * May block the current thread. The mutex is acquired when this returns.
555 *
556 *----------------------------------------------------------------------
557 */
558
559 void
560 Tcl_MutexLock(
561 Tcl_Mutex *mutexPtr) /* The lock */
562 {
563 CRITICAL_SECTION *csPtr;
564
565 if (*mutexPtr == NULL) {
566 TclpGlobalLock();
567
568 /*
569 * Double inside global lock check to avoid a race.
570 */
571
572 if (*mutexPtr == NULL) {
573 csPtr = (CRITICAL_SECTION *)ckalloc(sizeof(CRITICAL_SECTION));
574 InitializeCriticalSection(csPtr);
575 *mutexPtr = (Tcl_Mutex)csPtr;
576 TclRememberMutex(mutexPtr);
577 }
578 TclpGlobalUnlock();
579 }
580 csPtr = *((CRITICAL_SECTION **)mutexPtr);
581 EnterCriticalSection(csPtr);
582 }
583
584 /*
585 *----------------------------------------------------------------------
586 *
587 * Tcl_MutexUnlock --
588 *
589 * This procedure is invoked to unlock a mutex.
590 *
591 * Results:
592 * None.
593 *
594 * Side effects:
595 * The mutex is released when this returns.
596 *
597 *----------------------------------------------------------------------
598 */
599
600 void
601 Tcl_MutexUnlock(
602 Tcl_Mutex *mutexPtr) /* The lock */
603 {
604 CRITICAL_SECTION *csPtr = *((CRITICAL_SECTION **)mutexPtr);
605
606 LeaveCriticalSection(csPtr);
607 }
608
609 /*
610 *----------------------------------------------------------------------
611 *
612 * TclpFinalizeMutex --
613 *
614 * This procedure is invoked to clean up one mutex. This is only safe to
615 * call at the end of time.
616 *
617 * Results:
618 * None.
619 *
620 * Side effects:
621 * The mutex list is deallocated.
622 *
623 *----------------------------------------------------------------------
624 */
625
626 void
627 TclpFinalizeMutex(
628 Tcl_Mutex *mutexPtr)
629 {
630 CRITICAL_SECTION *csPtr = *(CRITICAL_SECTION **)mutexPtr;
631
632 if (csPtr != NULL) {
633 DeleteCriticalSection(csPtr);
634 ckfree(csPtr);
635 *mutexPtr = NULL;
636 }
637 }
638
639 /*
640 *----------------------------------------------------------------------
641 *
642 * Tcl_ConditionWait --
643 *
644 * This procedure is invoked to wait on a condition variable. The mutex
645 * is atomically released as part of the wait, and automatically grabbed
646 * when the condition is signaled.
647 *
648 * The mutex must be held when this procedure is called.
649 *
650 * Results:
651 * None.
652 *
653 * Side effects:
654 * May block the current thread. The mutex is acquired when this returns.
655 * Will allocate memory for a HANDLE and initialize this the first time
656 * this Tcl_Condition is used.
657 *
658 *----------------------------------------------------------------------
659 */
660
661 void
662 Tcl_ConditionWait(
663 Tcl_Condition *condPtr, /* Really (WinCondition **) */
664 Tcl_Mutex *mutexPtr, /* Really (CRITICAL_SECTION **) */
665 const Tcl_Time *timePtr) /* Timeout on waiting period */
666 {
667 WinCondition *winCondPtr; /* Per-condition queue head */
668 CRITICAL_SECTION *csPtr; /* Caller's Mutex, after casting */
669 DWORD wtime; /* Windows time value */
670 int timeout; /* True if we got a timeout */
671 int doExit = 0; /* True if we need to do exit setup */
672 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
673
674 /*
675 * Self initialize the two parts of the condition. The per-condition and
676 * per-thread parts need to be handled independently.
677 */
678
679 if (tsdPtr->flags == WIN_THREAD_UNINIT) {
680 TclpGlobalLock();
681
682 /*
683 * Create the per-thread event and queue pointers.
684 */
685
686 if (tsdPtr->flags == WIN_THREAD_UNINIT) {
687 tsdPtr->condEvent = CreateEventW(NULL, TRUE /* manual reset */,
688 FALSE /* non signaled */, NULL);
689 tsdPtr->nextPtr = NULL;
690 tsdPtr->prevPtr = NULL;
691 tsdPtr->flags = WIN_THREAD_RUNNING;
692 doExit = 1;
693 }
694 TclpGlobalUnlock();
695
696 if (doExit) {
697 /*
698 * Create a per-thread exit handler to clean up the condEvent. We
699 * must be careful to do this outside the Global Lock because
700 * Tcl_CreateThreadExitHandler uses its own ThreadSpecificData,
701 * and initializing that may drop back into the Global Lock.
702 */
703
704 Tcl_CreateThreadExitHandler(FinalizeConditionEvent, tsdPtr);
705 }
706 }
707
708 if (*condPtr == NULL) {
709 TclpGlobalLock();
710
711 /*
712 * Initialize the per-condition queue pointers and Mutex.
713 */
714
715 if (*condPtr == NULL) {
716 winCondPtr = (WinCondition *)ckalloc(sizeof(WinCondition));
717 InitializeCriticalSection(&winCondPtr->condLock);
718 winCondPtr->firstPtr = NULL;
719 winCondPtr->lastPtr = NULL;
720 *condPtr = (Tcl_Condition) winCondPtr;
721 TclRememberCondition(condPtr);
722 }
723 TclpGlobalUnlock();
724 }
725 csPtr = *((CRITICAL_SECTION **)mutexPtr);
726 winCondPtr = *((WinCondition **)condPtr);
727 if (timePtr == NULL) {
728 wtime = INFINITE;
729 } else {
730 wtime = timePtr->sec * 1000 + timePtr->usec / 1000;
731 }
732
733 /*
734 * Queue the thread on the condition, using the per-condition lock for
735 * serialization.
736 */
737
738 tsdPtr->flags = WIN_THREAD_BLOCKED;
739 tsdPtr->nextPtr = NULL;
740 EnterCriticalSection(&winCondPtr->condLock);
741 tsdPtr->prevPtr = winCondPtr->lastPtr; /* A: */
742 winCondPtr->lastPtr = tsdPtr;
743 if (tsdPtr->prevPtr != NULL) {
744 tsdPtr->prevPtr->nextPtr = tsdPtr;
745 }
746 if (winCondPtr->firstPtr == NULL) {
747 winCondPtr->firstPtr = tsdPtr;
748 }
749
750 /*
751 * Unlock the caller's mutex and wait for the condition, or a timeout.
752 * There is a minor issue here in that we don't count down the timeout if
753 * we get notified, but another thread grabs the condition before we do.
754 * In that race condition we'll wait again for the full timeout. Timed
755 * waits are dubious anyway. Either you have the locking protocol wrong
756 * and are masking a deadlock, or you are using conditions to pause your
757 * thread.
758 */
759
760 LeaveCriticalSection(csPtr);
761 timeout = 0;
762 while (!timeout && (tsdPtr->flags & WIN_THREAD_BLOCKED)) {
763 ResetEvent(tsdPtr->condEvent);
764 LeaveCriticalSection(&winCondPtr->condLock);
765 if (WaitForSingleObjectEx(tsdPtr->condEvent, wtime,
766 TRUE) == WAIT_TIMEOUT) {
767 timeout = 1;
768 }
769 EnterCriticalSection(&winCondPtr->condLock);
770 }
771
772 /*
773 * Be careful on timeouts because the signal might arrive right around the
774 * time limit and someone else could have taken us off the queue.
775 */
776
777 if (timeout) {
778 if (tsdPtr->flags & WIN_THREAD_RUNNING) {
779 timeout = 0;
780 } else {
781 /*
782 * When dequeueing, we can leave the tsdPtr->nextPtr and
783 * tsdPtr->prevPtr with dangling pointers because they are
784 * reinitialized w/out reading them when the thread is enqueued
785 * later.
786 */
787
788 if (winCondPtr->firstPtr == tsdPtr) {
789 winCondPtr->firstPtr = tsdPtr->nextPtr;
790 } else {
791 tsdPtr->prevPtr->nextPtr = tsdPtr->nextPtr;
792 }
793 if (winCondPtr->lastPtr == tsdPtr) {
794 winCondPtr->lastPtr = tsdPtr->prevPtr;
795 } else {
796 tsdPtr->nextPtr->prevPtr = tsdPtr->prevPtr;
797 }
798 tsdPtr->flags = WIN_THREAD_RUNNING;
799 }
800 }
801
802 LeaveCriticalSection(&winCondPtr->condLock);
803 EnterCriticalSection(csPtr);
804 }
805
806 /*
807 *----------------------------------------------------------------------
808 *
809 * Tcl_ConditionNotify --
810 *
811 * This procedure is invoked to signal a condition variable.
812 *
813 * The mutex must be held during this call to avoid races, but this
814 * interface does not enforce that.
815 *
816 * Results:
817 * None.
818 *
819 * Side effects:
820 * May unblock another thread.
821 *
822 *----------------------------------------------------------------------
823 */
824
825 void
826 Tcl_ConditionNotify(
827 Tcl_Condition *condPtr)
828 {
829 WinCondition *winCondPtr;
830 ThreadSpecificData *tsdPtr;
831
832 if (*condPtr != NULL) {
833 winCondPtr = *((WinCondition **)condPtr);
834
835 if (winCondPtr == NULL) {
836 return;
837 }
838
839 /*
840 * Loop through all the threads waiting on the condition and notify
841 * them (i.e., broadcast semantics). The queue manipulation is guarded
842 * by the per-condition coordinating mutex.
843 */
844
845 EnterCriticalSection(&winCondPtr->condLock);
846 while (winCondPtr->firstPtr != NULL) {
847 tsdPtr = winCondPtr->firstPtr;
848 winCondPtr->firstPtr = tsdPtr->nextPtr;
849 if (winCondPtr->lastPtr == tsdPtr) {
850 winCondPtr->lastPtr = NULL;
851 }
852 tsdPtr->flags = WIN_THREAD_RUNNING;
853 tsdPtr->nextPtr = NULL;
854 tsdPtr->prevPtr = NULL; /* Not strictly necessary, see A: */
855 SetEvent(tsdPtr->condEvent);
856 }
857 LeaveCriticalSection(&winCondPtr->condLock);
858 } else {
859 /*
860 * No-one has used the condition variable, so there are no waiters.
861 */
862 }
863 }
864
865 /*
866 *----------------------------------------------------------------------
867 *
868 * FinalizeConditionEvent --
869 *
870 * This procedure is invoked to clean up the per-thread event used to
871 * implement condition waiting. This is only safe to call at the end of
872 * time.
873 *
874 * Results:
875 * None.
876 *
877 * Side effects:
878 * The per-thread event is closed.
879 *
880 *----------------------------------------------------------------------
881 */
882
883 static void
884 FinalizeConditionEvent(
885 ClientData data)
886 {
887 ThreadSpecificData *tsdPtr = (ThreadSpecificData *) data;
888
889 tsdPtr->flags = WIN_THREAD_UNINIT;
890 CloseHandle(tsdPtr->condEvent);
891 }
892
893 /*
894 *----------------------------------------------------------------------
895 *
896 * TclpFinalizeCondition --
897 *
898 * This procedure is invoked to clean up a condition variable. This is
899 * only safe to call at the end of time.
900 *
901 * This assumes the Global Lock is held.
902 *
903 * Results:
904 * None.
905 *
906 * Side effects:
907 * The condition variable is deallocated.
908 *
909 *----------------------------------------------------------------------
910 */
911
912 void
913 TclpFinalizeCondition(
914 Tcl_Condition *condPtr)
915 {
916 WinCondition *winCondPtr = *(WinCondition **)condPtr;
917
918 /*
919 * Note - this is called long after the thread-local storage is reclaimed.
920 * The per-thread condition waiting event is reclaimed earlier in a
921 * per-thread exit handler, which is called before thread local storage is
922 * reclaimed.
923 */
924
925 if (winCondPtr != NULL) {
926 DeleteCriticalSection(&winCondPtr->condLock);
927 ckfree(winCondPtr);
928 *condPtr = NULL;
929 }
930 }
931
932
933
934
935 /*
936 * Additions by AOL for specialized thread memory allocator.
937 */
938 #ifdef USE_THREAD_ALLOC
939
940 Tcl_Mutex *
941 TclpNewAllocMutex(void)
942 {
943 struct allocMutex *lockPtr;
944
945 lockPtr = (struct allocMutex *)malloc(sizeof(struct allocMutex));
946 if (lockPtr == NULL) {
947 Tcl_Panic("could not allocate lock");
948 }
949 lockPtr->tlock = (Tcl_Mutex) &lockPtr->wlock;
950 InitializeCriticalSection(&lockPtr->wlock);
951 return &lockPtr->tlock;
952 }
953
954 void
955 TclpFreeAllocMutex(
956 Tcl_Mutex *mutex) /* The alloc mutex to free. */
957 {
958 allocMutex *lockPtr = (allocMutex *) mutex;
959
960 if (!lockPtr) {
961 return;
962 }
963 DeleteCriticalSection(&lockPtr->wlock);
964 free(lockPtr);
965 }
966
967 void *
968 TclpGetAllocCache(void)
969 {
970 void *result;
971
972 if (!once) {
973 /*
974 * We need to make sure that TclpFreeAllocCache is called on each
975 * thread that calls this, but only on threads that call this.
976 */
977
978 tlsKey = TlsAlloc();
979 once = 1;
980 if (tlsKey == TLS_OUT_OF_INDEXES) {
981 Tcl_Panic("could not allocate thread local storage");
982 }
983 }
984
985 result = TlsGetValue(tlsKey);
986 if ((result == NULL) && (GetLastError() != NO_ERROR)) {
987 Tcl_Panic("TlsGetValue failed from TclpGetAllocCache");
988 }
989 return result;
990 }
991
992 void
993 TclpSetAllocCache(
994 void *ptr)
995 {
996 BOOL success;
997 success = TlsSetValue(tlsKey, ptr);
998 if (!success) {
999 Tcl_Panic("TlsSetValue failed from TclpSetAllocCache");
1000 }
1001 }
1002
1003 void
1004 TclpFreeAllocCache(
1005 void *ptr)
1006 {
1007 BOOL success;
1008
1009 if (ptr != NULL) {
1010 /*
1011 * Called by TclFinalizeThreadAlloc() and
1012 * TclFinalizeThreadAllocThread() during Tcl_Finalize() or
1013 * Tcl_FinalizeThread(). This function destroys the tsd key which
1014 * stores allocator caches in thread local storage.
1015 */
1016
1017 TclFreeAllocCache(ptr);
1018 success = TlsSetValue(tlsKey, NULL);
1019 if (!success) {
1020 Tcl_Panic("TlsSetValue failed from TclpFreeAllocCache");
1021 }
1022 } else if (once) {
1023 /*
1024 * Called by us in TclFinalizeThreadAlloc() during the library
1025 * finalization initiated from Tcl_Finalize()
1026 */
1027
1028 success = TlsFree(tlsKey);
1029 if (!success) {
1030 Tcl_Panic("TlsFree failed from TclpFreeAllocCache");
1031 }
1032 once = 0; /* reset for next time. */
1033 }
1034
1035 }
1036 #endif /* USE_THREAD_ALLOC */
1037
1038
1039 void *
1040 TclpThreadCreateKey(void)
1041 {
1042 DWORD *key;
1043
1044 key = (DWORD *)TclpSysAlloc(sizeof *key, 0);
1045 if (key == NULL) {
1046 Tcl_Panic("unable to allocate thread key!");
1047 }
1048
1049 *key = TlsAlloc();
1050
1051 if (*key == TLS_OUT_OF_INDEXES) {
1052 Tcl_Panic("unable to allocate thread-local storage");
1053 }
1054
1055 return key;
1056 }
1057
1058 void
1059 TclpThreadDeleteKey(
1060 void *keyPtr)
1061 {
1062 DWORD *key = (DWORD *)keyPtr;
1063
1064 if (!TlsFree(*key)) {
1065 Tcl_Panic("unable to delete key");
1066 }
1067
1068 TclpSysFree(keyPtr);
1069 }
1070
1071 void
1072 TclpThreadSetGlobalTSD(
1073 void *tsdKeyPtr,
1074 void *ptr)
1075 {
1076 DWORD *key = (DWORD *)tsdKeyPtr;
1077
1078 if (!TlsSetValue(*key, ptr)) {
1079 Tcl_Panic("unable to set global TSD value");
1080 }
1081 }
1082
1083 void *
1084 TclpThreadGetGlobalTSD(
1085 void *tsdKeyPtr)
1086 {
1087 DWORD *key = (DWORD *)tsdKeyPtr;
1088
1089 return TlsGetValue(*key);
1090 }
1091
1092 #endif /* TCL_THREADS */
1093
1094 /*
1095 * Local Variables:
1096 * mode: c
1097 * c-basic-offset: 4
1098 * fill-column: 78
1099 * End:
1100 */