Diff
Not logged in

Differences From Artifact [cb72e0044a]:

To Artifact [b93e4a81d6]:


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
11
12
13
14
15
16
17













18
19
20
21
22
23
24







-
-
-
-
-
-
-
-
-
-
-
-
-







 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
 */

#include "tclInt.h"

#ifdef TCL_THREADS

/*
 * This is the number of milliseconds to wait between internal retries in
 * the Tcl_MutexLock function.  This value must be greater than zero and
 * should be a suitable value for the given platform.
 *
 * TODO: This may need to be dynamically determined, based on the relative
 *       performance of the running process.
 */

#ifndef TCL_MUTEX_LOCK_SLEEP_TIME
#  define TCL_MUTEX_LOCK_SLEEP_TIME	(25)
#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 masterLock = PTHREAD_MUTEX_INITIALIZER;
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
34
35
36
37
38
39
40







41
42
43
44
45
46
47







-
-
-
-
-
-
-







 * 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;

/*
 * The mutexLock serializes Tcl_MutexLock. This is necessary to prevent
 * races when finalizing a mutex that some other thread may want to lock.
 */

static pthread_mutex_t mutexLock = PTHREAD_MUTEX_INITIALIZER;

/*
 * These are for the critical sections inside this file.
 */

#define MASTER_LOCK	pthread_mutex_lock(&masterLock)
#define MASTER_UNLOCK	pthread_mutex_unlock(&masterLock)

370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
350
351
352
353
354
355
356

357
358
359
360
361
362
363







-







void
TclpMasterUnlock(void)
{
#ifdef TCL_THREADS
    pthread_mutex_unlock(&masterLock);
#endif
}


/*
 *----------------------------------------------------------------------
 *
 * Tcl_GetAllocMutex
 *
 *	This procedure returns a pointer to a statically initialized mutex for
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456

457
458

459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
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







-
-














-
-
-
+
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-








void
Tcl_MutexLock(
    Tcl_Mutex *mutexPtr)	/* Really (pthread_mutex_t **) */
{
    pthread_mutex_t *pmutexPtr;

retry:

    if (*mutexPtr == NULL) {
	MASTER_LOCK;
	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);
	}
	MASTER_UNLOCK;
    }
    while (1) {
	pthread_mutex_lock(&mutexLock);
	pmutexPtr = *((pthread_mutex_t **)mutexPtr);
    pmutexPtr = *((pthread_mutex_t **)mutexPtr);
	if (pmutexPtr == NULL) {
	    pthread_mutex_unlock(&mutexLock);
    pthread_mutex_lock(pmutexPtr);
	    goto retry;
	}
	if (pthread_mutex_trylock(pmutexPtr) == 0) {
	    pthread_mutex_unlock(&mutexLock);
	    return;
	}
	pthread_mutex_unlock(&mutexLock);
	/*
	 * 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);
#else
	Tcl_Sleep(TCL_MUTEX_LOCK_SLEEP_TIME);
#endif
    }
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_MutexUnlock --
 *