477
478
479
480
481
482
483
484
485
486
487
488
489
490
|
}
#ifdef TCL_THREADS
/*
*----------------------------------------------------------------------
*
* Tcl_MutexLock --
*
* This procedure is invoked to lock a mutex. This procedure handles
* initializing the mutex, if necessary. The caller can rely on the fact
* that Tcl_Mutex is an opaque pointer. This routine will change that
* pointer from NULL after first use.
*
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
|
}
#ifdef TCL_THREADS
/*
*----------------------------------------------------------------------
*
* TclpMutexWait
*
* This procedure is used to delay for a short interval while
* waiting for a mutex to be unlocked by another thread.
*
* Results:
* None.
*
* Side effects:
* The value of the retry parameter is changed.
*
*----------------------------------------------------------------------
*/
void
TclpMutexWait(
Tcl_Mutex *mutexPtr, /* Mutex passed to Tcl_MutexLock. */
int *retry, /* The number of retries so far. */
ClientData clientData) /* The extra data, if any. */
{
int nRetry = (retry != NULL) ? *retry : 0;
if (nRetry > TCL_MUTEX_LOCK_RESET_LIMIT) {
nRetry = 0;
}
/*
* BUGBUG: All core and Thread package tests pass when usleep()
* is used; however, the Thread package tests hang at
* various places when Tcl_Sleep() is used, typically
* while running test "thread-17.8", "thread-17.9", or
* "thread-17.11a". Really, what we want here is just
* to yield to other threads for a while.
*/
#ifdef HAVE_USLEEP
usleep(TCL_MUTEX_LOCK_SLEEP_TIME * 1000 * nRetry);
#else
Tcl_Sleep(TCL_MUTEX_LOCK_SLEEP_TIME * nRetry);
#endif
if (retry != NULL) *retry = nRetry;
}
/*
*----------------------------------------------------------------------
*
* Tcl_MutexLock --
*
* This procedure is invoked to lock a mutex. This procedure handles
* initializing the mutex, if necessary. The caller can rely on the fact
* that Tcl_Mutex is an opaque pointer. This routine will change that
* pointer from NULL after first use.
*
|
533
534
535
536
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
|
if (pthread_mutex_trylock(pmutexPtr) == 0) {
TclpMutexUnlock();
return;
}
TclpMutexUnlock();
mutexWaitProc = TclGetMutexWaitProc();
if (mutexWaitProc != NULL) {
mutexWaitProc(mutexPtr, nRetry, NULL);
} else {
if (nRetry > TCL_MUTEX_LOCK_RESET_LIMIT) {
nRetry = 0;
}
/*
* BUGBUG: All core and Thread package tests pass when usleep()
* is used; however, the Thread package tests hang at
* various places when Tcl_Sleep() is used, typically
* while running test "thread-17.8", "thread-17.9", or
* "thread-17.11a". Really, what we want here is just
* to yield to other threads for a while.
*/
#ifdef HAVE_USLEEP
usleep(TCL_MUTEX_LOCK_SLEEP_TIME * 1000 * nRetry);
#else
Tcl_Sleep(TCL_MUTEX_LOCK_SLEEP_TIME * nRetry);
#endif
}
nRetry++;
}
}
/*
*----------------------------------------------------------------------
|
|
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
|
if (pthread_mutex_trylock(pmutexPtr) == 0) {
TclpMutexUnlock();
return;
}
TclpMutexUnlock();
mutexWaitProc = TclGetMutexWaitProc();
if (mutexWaitProc != NULL) {
mutexWaitProc(mutexPtr, &nRetry, NULL);
} else {
TclpMutexWait(mutexPtr, &nRetry, NULL);
}
nRetry++;
}
}
/*
*----------------------------------------------------------------------
|