Diff
Not logged in

Differences From Artifact [a7045b2d0e]:

To Artifact [6bedefad87]:


13
14
15
16
17
18
19
20

21
22
23
24
25
26
27
13
14
15
16
17
18
19

20
21
22
23
24
25
26
27







-
+







 * RCS: @(#) $Id: tclUnixThrd.c,v 1.58 2008/05/09 04:58:54 georgeps Exp $
 */

#include "tclInt.h"

#ifdef TCL_THREADS

#include "pthread.h"
#include <pthread.h>

typedef struct ThreadSpecificData {
    char nabuf[16];
} ThreadSpecificData;

static Tcl_ThreadDataKey dataKey;

52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
52
53
54
55
56
57
58

59
60
61
62
63
64
65







-







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

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

#endif /* TCL_THREADS */


/*
 *----------------------------------------------------------------------
 *
 * TclpThreadCreate --
 *
 *	This procedure creates a new thread.
74
75
76
77
78
79
80
81

82
83
84
85
86
87
88
73
74
75
76
77
78
79

80
81
82
83
84
85
86
87







-
+







 *
 *----------------------------------------------------------------------
 */

int
TclpThreadCreate(
    Tcl_ThreadId *idPtr,	/* Return, the ID of the thread */
    Tcl_ThreadCreateProc proc,	/* Main() function of the thread */
    Tcl_ThreadCreateProc *proc,	/* Main() function of the thread */
    ClientData clientData,	/* The one argument to Main() */
    int stackSize,		/* Size of stack for the new thread */
    int flags)			/* Flags controlling behaviour of the new
				 * thread. */
{
#ifdef TCL_THREADS
    pthread_attr_t attr;
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
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







+




-
+

-
+
+



-







	 *
	 * This solution is not optimal, as we should allow the user to
	 * specify a size at runtime, but we don't want to slow this function
	 * down, and that would still leave the main thread at the default.
	 */

	size_t size;

	result = pthread_attr_getstacksize(&attr, &size);
	if (!result && (size < TCL_THREAD_STACK_MIN)) {
	    pthread_attr_setstacksize(&attr, (size_t) TCL_THREAD_STACK_MIN);
	}
#endif
#endif /* TCL_THREAD_STACK_MIN */
    }
#endif
#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 {
420
421
422
423
424
425
426

427
428
429
430
431
432
433
434

435
436
437
438
439
440
441
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434

435
436
437
438
439
440
441
442







+







-
+







 */

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

    if (*mutexPtr == NULL) {
	MASTER_LOCK;
	if (*mutexPtr == NULL) {
	    /*
	     * Double inside master lock check to avoid a race condition.
	     */

	    pmutexPtr = (pthread_mutex_t *)ckalloc(sizeof(pthread_mutex_t));
	    pmutexPtr = (pthread_mutex_t *) ckalloc(sizeof(pthread_mutex_t));
	    pthread_mutex_init(pmutexPtr, NULL);
	    *mutexPtr = (Tcl_Mutex)pmutexPtr;
	    TclRememberMutex(mutexPtr);
	}
	MASTER_UNLOCK;
    }
    pmutexPtr = *((pthread_mutex_t **)mutexPtr);
459
460
461
462
463
464
465
466


467
468
469
470
471
472
473
460
461
462
463
464
465
466

467
468
469
470
471
472
473
474
475







-
+
+







 *----------------------------------------------------------------------
 */

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

    pthread_mutex_unlock(pmutexPtr);
}

/*
 *----------------------------------------------------------------------
 *
 * TclpFinalizeMutex --
486
487
488
489
490
491
492
493


494
495
496
497
498
499
500
488
489
490
491
492
493
494

495
496
497
498
499
500
501
502
503







-
+
+







 *----------------------------------------------------------------------
 */

void
TclpFinalizeMutex(
    Tcl_Mutex *mutexPtr)
{
    pthread_mutex_t *pmutexPtr = *(pthread_mutex_t **)mutexPtr;
    pthread_mutex_t *pmutexPtr = *(pthread_mutex_t **) mutexPtr;

    if (pmutexPtr != NULL) {
	pthread_mutex_destroy(pmutexPtr);
	ckfree((char *) pmutexPtr);
	*mutexPtr = NULL;
    }
}

756
757
758
759
760
761
762
763
764



765
766

767
768
769


770
771
772
773

774
775
776
777

778
779

780
781




782
783

784
785
786
787
788
789

790
791





792
793

794
795
796
797

798
799




800
801

802
803
804
805
806
807
808
809
810
811
812
759
760
761
762
763
764
765


766
767
768


769
770


771
772
773
774
775

776
777
778
779

780
781
782
783


784
785
786
787
788

789
790
791
792
793
794
795
796


797
798
799
800
801
802

803
804
805
806
807
808


809
810
811
812
813

814
815
816
817
818
819
820
821
822
823
824
825







-
-
+
+
+
-
-
+

-
-
+
+



-
+



-
+


+
-
-
+
+
+
+

-
+






+
-
-
+
+
+
+
+

-
+




+
-
-
+
+
+
+

-
+











TclpSetAllocCache(
    void *arg)
{
    pthread_setspecific(key, arg);
}
#endif /* USE_THREAD_ALLOC */



void *
TclpThreadCreateKey(void)
{
void *TclpThreadCreateKey(void) {
    pthread_key_t *key;
    pthread_key_t *ptkeyPtr;

    key = TclpSysAlloc(sizeof *key, 0);
    if (NULL == key) {
    ptkeyPtr = TclpSysAlloc(sizeof *ptkeyPtr, 0);
    if (NULL == ptkeyPtr) {
	Tcl_Panic("unable to allocate thread key!");
    }

    if (pthread_key_create(key, NULL)) {
    if (pthread_key_create(ptkeyPtr, NULL)) {
	Tcl_Panic("unable to create pthread key!");
    }

    return key;
    return ptkeyPtr;
}

void
void TclpThreadDeleteKey(void *keyPtr) {
    pthread_key_t *key = keyPtr;
TclpThreadDeleteKey(
    void *keyPtr)
{
    pthread_key_t *ptkeyPtr = keyPtr;

    if (pthread_key_delete(*key)) {
    if (pthread_key_delete(*ptkeyPtr)) {
	Tcl_Panic("unable to delete key!");
    }

    TclpSysFree(keyPtr);
}

void
void TclpThreadSetMasterTSD(void *tsdKeyPtr, void *ptr) {
    pthread_key_t *key = tsdKeyPtr;
TclpThreadSetMasterTSD(
    void *tsdKeyPtr,
    void *ptr)
{
    pthread_key_t *ptkeyPtr = tsdKeyPtr;

    if (pthread_setspecific(*key, ptr)) {
    if (pthread_setspecific(*ptkeyPtr, ptr)) {
	Tcl_Panic("unable to set master TSD value");
    }
}

void *
void *TclpThreadGetMasterTSD(void *tsdKeyPtr) {
    pthread_key_t *key = tsdKeyPtr;
TclpThreadGetMasterTSD(
    void *tsdKeyPtr)
{
    pthread_key_t *ptkeyPtr = tsdKeyPtr;

    return pthread_getspecific(*key);
    return pthread_getspecific(*ptkeyPtr);
}

#endif /* TCL_THREADS */

/*
 * Local Variables:
 * mode: c
 * c-basic-offset: 4
 * fill-column: 78
 * End:
 */