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