Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Changes In Branch tip-509 Excluding Merge-Ins
This is equivalent to a diff from bfedd422d9 to c35c3a7455
2018-09-28
| ||
09:50 | Implement TIP 509: reentrant mutexes on all platforms check-in: 27777c925a user: dkf tags: core-8-branch | |
09:49 | Clean up code style and add key phrases to documentation. Closed-Leaf check-in: c35c3a7455 user: dkf tags: tip-509 | |
09:18 | merge core-8-branch check-in: b3d9254efa user: dkf tags: tip-509 | |
07:24 | Fix minor typo check-in: bfedd422d9 user: dkf tags: core-8-branch | |
2018-09-27
| ||
11:08 | Implementation of TIP 426: Determining the "Type" of Commands check-in: 4dbf61778e user: dkf tags: core-8-branch | |
Changes to doc/Thread.3.
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
...
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
int \fBTcl_JoinThread\fR(\fIid, result\fR) .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. .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 static and process-wide, yet each thread will end up associating a different block of storage with this key. ................................................................................ allocated and initialized to all zeros the first time each thread asks for it. The storage is automatically deallocated by \fBTcl_FinalizeThread\fR. .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 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. On some platforms it will result in a deadlock. The \fBTcl_MutexLock\fR, \fBTcl_MutexUnlock\fR and \fBTcl_MutexFinalize\fR |
>
|
>
>
>
>
>
|
|
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
...
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
int \fBTcl_JoinThread\fR(\fIid, result\fR) .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 .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 static and process-wide, yet each thread will end up associating a different block of storage with this key. ................................................................................ allocated and initialized to all zeros the first time each thread asks for it. The storage is automatically deallocated by \fBTcl_FinalizeThread\fR. .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 .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. On some platforms it will result in a deadlock. The \fBTcl_MutexLock\fR, \fBTcl_MutexUnlock\fR and \fBTcl_MutexFinalize\fR |
Changes to unix/configure.
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
....
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
|
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_func
# ac_fn_c_check_type LINENO TYPE VAR INCLUDES
# -------------------------------------------
# Tests whether TYPE exists after having included INCLUDES, setting cache
# variable VAR accordingly.
ac_fn_c_check_type ()
{
................................................................................
#define `$as_echo "HAVE_$ac_func" | $as_tr_cpp` 1
_ACEOF
fi
done
LIBS=$ac_saved_libs
# Add the threads support libraries
LIBS="$LIBS$THREADS_LIBS"
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking how to build libraries" >&5
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
....
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
|
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_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. ac_fn_c_check_type () { ................................................................................ #define `$as_echo "HAVE_$ac_func" | $as_tr_cpp` 1 _ACEOF 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 <pthread.h> " 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" { $as_echo "$as_me:${as_lineno-$LINENO}: checking how to build libraries" >&5 |
Changes to unix/tcl.m4.
2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 |
# Does the pthread-implementation provide # 'pthread_attr_setstacksize' ? ac_saved_libs=$LIBS LIBS="$LIBS $THREADS_LIBS" AC_CHECK_FUNCS(pthread_attr_setstacksize pthread_atfork) LIBS=$ac_saved_libs ]) #-------------------------------------------------------------------- # SC_TCL_EARLY_FLAGS # # Check for what flags are needed to be passed so the correct OS # features are available. |
> > > |
2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 |
# Does the pthread-implementation provide # 'pthread_attr_setstacksize' ? 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 <pthread.h>]]) ]) #-------------------------------------------------------------------- # SC_TCL_EARLY_FLAGS # # Check for what flags are needed to be passed so the correct OS # features are available. |
Changes to unix/tclUnixThrd.c.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 .. 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 ... 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 ... 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 ... 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 ... 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 ... 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 ... 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 ... 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 ... 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 ... 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 ... 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 ... 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 ... 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 |
* See the file "license.terms" for information on usage and redistribution of * this file, and for a DISCLAIMER OF ALL WARRANTIES. */ #include "tclInt.h" #if TCL_THREADS #ifndef TCL_NO_DEPRECATED typedef struct { char nabuf[16]; } ThreadSpecificData; static Tcl_ThreadDataKey dataKey; #endif /* * 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. */ ................................................................................ static pthread_mutex_t initLock = PTHREAD_MUTEX_INITIALIZER; /* * 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; #endif /* TCL_THREADS */ /* *---------------------------------------------------------------------- * * TclpThreadCreate -- ................................................................................ if (!result && (size < TCL_THREAD_STACK_MIN)) { pthread_attr_setstacksize(&attr, (size_t) TCL_THREAD_STACK_MIN); } #endif /* TCL_THREAD_STACK_MIN */ } #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, (void * (*)(void *))proc, (void *)clientData)) { result = TCL_ERROR; } else { *idPtr = (Tcl_ThreadId)theThread; result = TCL_OK; } pthread_attr_destroy(&attr); return result; #else return TCL_ERROR; #endif /* TCL_THREADS */ ................................................................................ void TclpMasterLock(void) { #if TCL_THREADS pthread_mutex_lock(&masterLock); #endif } /* *---------------------------------------------------------------------- * * TclpMasterUnlock * * This procedure is used to release a lock that serializes creation and ................................................................................ *---------------------------------------------------------------------- */ Tcl_Mutex * Tcl_GetAllocMutex(void) { #if TCL_THREADS pthread_mutex_t **allocLockPtrPtr = &allocLockPtr; return (Tcl_Mutex *) allocLockPtrPtr; #else return NULL; #endif } #if TCL_THREADS ................................................................................ * first time this Tcl_Mutex is used. * *---------------------------------------------------------------------- */ void Tcl_MutexLock( Tcl_Mutex *mutexPtr) /* Really (pthread_mutex_t **) */ { pthread_mutex_t *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; TclRememberMutex(mutexPtr); } pthread_mutex_unlock(&masterLock); } pmutexPtr = *((pthread_mutex_t **)mutexPtr); pthread_mutex_lock(pmutexPtr); } /* *---------------------------------------------------------------------- * * Tcl_MutexUnlock -- * ................................................................................ * The mutex is released when this returns. * *---------------------------------------------------------------------- */ void Tcl_MutexUnlock( Tcl_Mutex *mutexPtr) /* Really (pthread_mutex_t **) */ { pthread_mutex_t *pmutexPtr = *(pthread_mutex_t **) mutexPtr; pthread_mutex_unlock(pmutexPtr); } /* *---------------------------------------------------------------------- * * TclpFinalizeMutex -- * ................................................................................ *---------------------------------------------------------------------- */ void TclpFinalizeMutex( Tcl_Mutex *mutexPtr) { pthread_mutex_t *pmutexPtr = *(pthread_mutex_t **) mutexPtr; if (pmutexPtr != NULL) { pthread_mutex_destroy(pmutexPtr); ckfree(pmutexPtr); *mutexPtr = NULL; } } /* *---------------------------------------------------------------------- ................................................................................ * *---------------------------------------------------------------------- */ void Tcl_ConditionWait( Tcl_Condition *condPtr, /* Really (pthread_cond_t **) */ Tcl_Mutex *mutexPtr, /* Really (pthread_mutex_t **) */ const Tcl_Time *timePtr) /* Timeout on waiting period */ { pthread_cond_t *pcondPtr; pthread_mutex_t *pmutexPtr; struct timespec ptime; if (*condPtr == NULL) { pthread_mutex_lock(&masterLock); /* * Double check inside mutex to avoid race, then initialize condition ................................................................................ pcondPtr = ckalloc(sizeof(pthread_cond_t)); pthread_cond_init(pcondPtr, NULL); *condPtr = (Tcl_Condition) pcondPtr; TclRememberCondition(condPtr); } pthread_mutex_unlock(&masterLock); } pmutexPtr = *((pthread_mutex_t **)mutexPtr); pcondPtr = *((pthread_cond_t **)condPtr); if (timePtr == NULL) { pthread_cond_wait(pcondPtr, pmutexPtr); } else { Tcl_Time now; /* * Make sure to take into account the microsecond component of the * current time, including possible overflow situations. [Bug #411603] */ 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); } } /* *---------------------------------------------------------------------- * * Tcl_ConditionNotify -- ................................................................................ *---------------------------------------------------------------------- */ void Tcl_ConditionNotify( Tcl_Condition *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. */ } } /* *---------------------------------------------------------------------- * ................................................................................ *---------------------------------------------------------------------- */ void TclpFinalizeCondition( Tcl_Condition *condPtr) { pthread_cond_t *pcondPtr = *(pthread_cond_t **)condPtr; if (pcondPtr != NULL) { pthread_cond_destroy(pcondPtr); ckfree(pcondPtr); *condPtr = NULL; } } ................................................................................ */ #ifdef USE_THREAD_ALLOC static pthread_key_t key; typedef struct { Tcl_Mutex tlock; pthread_mutex_t plock; } allocMutex; Tcl_Mutex * TclpNewAllocMutex(void) { allocMutex *lockPtr; register pthread_mutex_t *plockPtr; 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); return &lockPtr->tlock; } void TclpFreeAllocMutex( Tcl_Mutex *mutex) /* The alloc mutex to free. */ { allocMutex* lockPtr = (allocMutex*) mutex; if (!lockPtr) { return; } pthread_mutex_destroy(&lockPtr->plock); free(lockPtr); } void TclpInitAllocCache(void) { pthread_key_create(&key, NULL); ................................................................................ #endif /* USE_THREAD_ALLOC */ void * TclpThreadCreateKey(void) { pthread_key_t *ptkeyPtr; ptkeyPtr = TclpSysAlloc(sizeof *ptkeyPtr, 0); if (NULL == ptkeyPtr) { Tcl_Panic("unable to allocate thread key!"); } if (pthread_key_create(ptkeyPtr, NULL)) { Tcl_Panic("unable to create pthread key!"); } |
| > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > < > | > > > > > > > | | | | | | < | > > | | | | | | | | | | | | | | | | | | | > | | | | | | | | | > | | |
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 ... 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 ... 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 ... 479 480 481 482 483 484 485 486 487 488 489 490 491 492 ... 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 ... 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 ... 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 ... 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 ... 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 ... 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 ... 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 ... 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 ... 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 ... 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 |
* See the file "license.terms" for information on usage and redistribution of * this file, and for a DISCLAIMER OF ALL WARRANTIES. */ #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 /* 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. */ ................................................................................ static pthread_mutex_t initLock = PTHREAD_MUTEX_INITIALIZER; /* * allocLock is used by Tcl's version of malloc for synchronization. For * obvious reasons, cannot use any dyamically allocated storage. */ static PMutex allocLock; static pthread_once_t allocLockInitOnce = PTHREAD_ONCE_INIT; static void allocLockInit(void) { PMutexInit(&allocLock); } static PMutex *allocLockPtr = &allocLock; #endif /* TCL_THREADS */ /* *---------------------------------------------------------------------- * * TclpThreadCreate -- ................................................................................ if (!result && (size < TCL_THREAD_STACK_MIN)) { pthread_attr_setstacksize(&attr, (size_t) TCL_THREAD_STACK_MIN); } #endif /* TCL_THREAD_STACK_MIN */ } #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, (void * (*)(void *)) proc, (void *) clientData)) { result = TCL_ERROR; } else { *idPtr = (Tcl_ThreadId) theThread; result = TCL_OK; } pthread_attr_destroy(&attr); return result; #else return TCL_ERROR; #endif /* TCL_THREADS */ ................................................................................ void TclpMasterLock(void) { #if TCL_THREADS pthread_mutex_lock(&masterLock); #endif } /* *---------------------------------------------------------------------- * * TclpMasterUnlock * * This procedure is used to release a lock that serializes creation and ................................................................................ *---------------------------------------------------------------------- */ Tcl_Mutex * Tcl_GetAllocMutex(void) { #if TCL_THREADS PMutex **allocLockPtrPtr = &allocLockPtr; pthread_once(&allocLockInitOnce, allocLockInit); return (Tcl_Mutex *) allocLockPtrPtr; #else return NULL; #endif } #if TCL_THREADS ................................................................................ * first time this Tcl_Mutex is used. * *---------------------------------------------------------------------- */ void Tcl_MutexLock( Tcl_Mutex *mutexPtr) /* Really (PMutex **) */ { 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(PMutex)); PMutexInit(pmutexPtr); *mutexPtr = (Tcl_Mutex) pmutexPtr; TclRememberMutex(mutexPtr); } pthread_mutex_unlock(&masterLock); } pmutexPtr = *((PMutex **) mutexPtr); PMutexLock(pmutexPtr); } /* *---------------------------------------------------------------------- * * Tcl_MutexUnlock -- * ................................................................................ * The mutex is released when this returns. * *---------------------------------------------------------------------- */ void Tcl_MutexUnlock( Tcl_Mutex *mutexPtr) /* Really (PMutex **) */ { PMutex *pmutexPtr = *(PMutex **) mutexPtr; PMutexUnlock(pmutexPtr); } /* *---------------------------------------------------------------------- * * TclpFinalizeMutex -- * ................................................................................ *---------------------------------------------------------------------- */ void TclpFinalizeMutex( Tcl_Mutex *mutexPtr) { PMutex *pmutexPtr = *(PMutex **) mutexPtr; if (pmutexPtr != NULL) { PMutexDestroy(pmutexPtr); ckfree(pmutexPtr); *mutexPtr = NULL; } } /* *---------------------------------------------------------------------- ................................................................................ * *---------------------------------------------------------------------- */ void Tcl_ConditionWait( Tcl_Condition *condPtr, /* Really (pthread_cond_t **) */ Tcl_Mutex *mutexPtr, /* Really (PMutex **) */ const Tcl_Time *timePtr) /* Timeout on waiting period */ { pthread_cond_t *pcondPtr; PMutex *pmutexPtr; struct timespec ptime; if (*condPtr == NULL) { pthread_mutex_lock(&masterLock); /* * Double check inside mutex to avoid race, then initialize condition ................................................................................ pcondPtr = ckalloc(sizeof(pthread_cond_t)); pthread_cond_init(pcondPtr, NULL); *condPtr = (Tcl_Condition) pcondPtr; TclRememberCondition(condPtr); } pthread_mutex_unlock(&masterLock); } pmutexPtr = *((PMutex **) mutexPtr); pcondPtr = *((pthread_cond_t **) condPtr); if (timePtr == NULL) { PCondWait(pcondPtr, pmutexPtr); } else { Tcl_Time now; /* * Make sure to take into account the microsecond component of the * current time, including possible overflow situations. [Bug #411603] */ Tcl_GetTime(&now); ptime.tv_sec = timePtr->sec + now.sec + (timePtr->usec + now.usec) / 1000000; ptime.tv_nsec = 1000 * ((timePtr->usec + now.usec) % 1000000); PCondTimedWait(pcondPtr, pmutexPtr, &ptime); } } /* *---------------------------------------------------------------------- * * Tcl_ConditionNotify -- ................................................................................ *---------------------------------------------------------------------- */ void Tcl_ConditionNotify( Tcl_Condition *condPtr) { pthread_cond_t *pcondPtr = *((pthread_cond_t **) condPtr); if (pcondPtr != NULL) { pthread_cond_broadcast(pcondPtr); } else { /* * No-one has used the condition variable, so there are no waiters. */ } } /* *---------------------------------------------------------------------- * ................................................................................ *---------------------------------------------------------------------- */ void TclpFinalizeCondition( Tcl_Condition *condPtr) { pthread_cond_t *pcondPtr = *(pthread_cond_t **) condPtr; if (pcondPtr != NULL) { pthread_cond_destroy(pcondPtr); ckfree(pcondPtr); *condPtr = NULL; } } ................................................................................ */ #ifdef USE_THREAD_ALLOC static pthread_key_t key; typedef struct { Tcl_Mutex tlock; PMutex plock; } AllocMutex; Tcl_Mutex * TclpNewAllocMutex(void) { AllocMutex *lockPtr; register PMutex *plockPtr; lockPtr = malloc(sizeof(AllocMutex)); if (lockPtr == NULL) { Tcl_Panic("could not allocate lock"); } plockPtr = &lockPtr->plock; lockPtr->tlock = (Tcl_Mutex) plockPtr; PMutexInit(&lockPtr->plock); return &lockPtr->tlock; } void TclpFreeAllocMutex( Tcl_Mutex *mutex) /* The alloc mutex to free. */ { AllocMutex *lockPtr = (AllocMutex *) mutex; if (!lockPtr) { return; } PMutexDestroy(&lockPtr->plock); free(lockPtr); } void TclpInitAllocCache(void) { pthread_key_create(&key, NULL); ................................................................................ #endif /* USE_THREAD_ALLOC */ void * TclpThreadCreateKey(void) { pthread_key_t *ptkeyPtr; ptkeyPtr = TclpSysAlloc(sizeof(pthread_key_t), 0); if (NULL == ptkeyPtr) { Tcl_Panic("unable to allocate thread key!"); } if (pthread_key_create(ptkeyPtr, NULL)) { Tcl_Panic("unable to create pthread key!"); } |