Debian Codegraph @

unix/tclUnixThrd.c

raw
1 /*
2 * tclUnixThrd.c --
3 *
4 * This file implements the UNIX-specific thread support.
5 *
6 * Copyright (c) 1991-1994 The Regents of the University of California.
7 * Copyright (c) 1994-1997 Sun Microsystems, Inc.
8 * Copyright (c) 2008 by George Peter Staplin
9 *
10 * See the file "license.terms" for information on usage and redistribution of
11 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
12 */
13
14 #include "tclInt.h"
15
16 #ifdef TCL_THREADS
17
18 typedef struct ThreadSpecificData {
19 char nabuf[16];
20 } ThreadSpecificData;
21
22 static Tcl_ThreadDataKey dataKey;
23
24 /*
25 * globalLock is used to serialize creation of mutexes, condition variables,
26 * and thread local storage. This is the only place that can count on the
27 * ability to statically initialize the mutex.
28 */
29
30 static pthread_mutex_t globalLock = PTHREAD_MUTEX_INITIALIZER;
31
32 /*
33 * initLock is used to serialize initialization and finalization of Tcl. It
34 * cannot use any dynamically allocated storage.
35 */
36
37 static pthread_mutex_t initLock = PTHREAD_MUTEX_INITIALIZER;
38
39 /*
40 * allocLock is used by Tcl's version of malloc for synchronization. For
41 * obvious reasons, cannot use any dynamically allocated storage.
42 */
43
44 static pthread_mutex_t allocLock = PTHREAD_MUTEX_INITIALIZER;
45 static pthread_mutex_t *allocLockPtr = &allocLock;
46
47 /*
48 * These are for the critical sections inside this file.
49 */
50
51 #define GLOBAL_LOCK pthread_mutex_lock(&globalLock)
52 #define GLOBAL_UNLOCK pthread_mutex_unlock(&globalLock)
53
54 #endif /* TCL_THREADS */
55
56 /*
57 *----------------------------------------------------------------------
58 *
59 * TclpThreadCreate --
60 *
61 * This procedure creates a new thread.
62 *
63 * Results:
64 * TCL_OK if the thread could be created. The thread ID is returned in a
65 * parameter.
66 *
67 * Side effects:
68 * A new thread is created.
69 *
70 *----------------------------------------------------------------------
71 */
72
73 int
74 TclpThreadCreate(
75 Tcl_ThreadId *idPtr, /* Return, the ID of the thread */
76 Tcl_ThreadCreateProc *proc, /* Main() function of the thread */
77 ClientData clientData, /* The one argument to Main() */
78 int stackSize, /* Size of stack for the new thread */
79 int flags) /* Flags controlling behaviour of the new
80 * thread. */
81 {
82 #ifdef TCL_THREADS
83 pthread_attr_t attr;
84 pthread_t theThread;
85 int result;
86
87 pthread_attr_init(&attr);
88 pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
89
90 #ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE
91 if (stackSize != TCL_THREAD_STACK_DEFAULT) {
92 pthread_attr_setstacksize(&attr, stackSize);
93 #ifdef TCL_THREAD_STACK_MIN
94 } else {
95 /*
96 * Certain systems define a thread stack size that by default is too
97 * small for many operations. The user has the option of defining
98 * TCL_THREAD_STACK_MIN to a value large enough to work for their
99 * needs. This would look like (for 128K min stack):
100 * make MEM_DEBUG_FLAGS=-DTCL_THREAD_STACK_MIN=131072L
101 *
102 * This solution is not optimal, as we should allow the user to
103 * specify a size at runtime, but we don't want to slow this function
104 * down, and that would still leave the main thread at the default.
105 */
106
107 size_t size;
108
109 result = pthread_attr_getstacksize(&attr, &size);
110 if (!result && (size < TCL_THREAD_STACK_MIN)) {
111 pthread_attr_setstacksize(&attr, (size_t) TCL_THREAD_STACK_MIN);
112 }
113 #endif /* TCL_THREAD_STACK_MIN */
114 }
115 #endif /* HAVE_PTHREAD_ATTR_SETSTACKSIZE */
116
117 if (!(flags & TCL_THREAD_JOINABLE)) {
118 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
119 }
120
121 if (pthread_create(&theThread, &attr,
122 (void * (*)(void *))(void *)proc, (void *)clientData) &&
123 pthread_create(&theThread, NULL,
124 (void * (*)(void *))(void *)proc, (void *)clientData)) {
125 result = TCL_ERROR;
126 } else {
127 *idPtr = (Tcl_ThreadId)theThread;
128 result = TCL_OK;
129 }
130 pthread_attr_destroy(&attr);
131 return result;
132 #else
133 return TCL_ERROR;
134 #endif /* TCL_THREADS */
135 }
136
137 /*
138 *----------------------------------------------------------------------
139 *
140 * Tcl_JoinThread --
141 *
142 * This procedure waits upon the exit of the specified thread.
143 *
144 * Results:
145 * TCL_OK if the wait was successful, TCL_ERROR else.
146 *
147 * Side effects:
148 * The result area is set to the exit code of the thread we waited upon.
149 *
150 *----------------------------------------------------------------------
151 */
152
153 int
154 Tcl_JoinThread(
155 Tcl_ThreadId threadId, /* Id of the thread to wait upon. */
156 int *state) /* Reference to the storage the result of the
157 * thread we wait upon will be written into.
158 * May be NULL. */
159 {
160 #ifdef TCL_THREADS
161 int result;
162 unsigned long retcode, *retcodePtr = &retcode;
163
164 result = pthread_join((pthread_t) threadId, (void**) retcodePtr);
165 if (state) {
166 *state = (int) retcode;
167 }
168 return (result == 0) ? TCL_OK : TCL_ERROR;
169 #else
170 return TCL_ERROR;
171 #endif
172 }
173
174 #ifdef TCL_THREADS
175 /*
176 *----------------------------------------------------------------------
177 *
178 * TclpThreadExit --
179 *
180 * This procedure terminates the current thread.
181 *
182 * Results:
183 * None.
184 *
185 * Side effects:
186 * This procedure terminates the current thread.
187 *
188 *----------------------------------------------------------------------
189 */
190
191 TCL_NORETURN void
192 TclpThreadExit(
193 int status)
194 {
195 pthread_exit(INT2PTR(status));
196 }
197 #endif /* TCL_THREADS */
198
199 /*
200 *----------------------------------------------------------------------
201 *
202 * Tcl_GetCurrentThread --
203 *
204 * This procedure returns the ID of the currently running thread.
205 *
206 * Results:
207 * A thread ID.
208 *
209 * Side effects:
210 * None.
211 *
212 *----------------------------------------------------------------------
213 */
214
215 Tcl_ThreadId
216 Tcl_GetCurrentThread(void)
217 {
218 #ifdef TCL_THREADS
219 return (Tcl_ThreadId) pthread_self();
220 #else
221 return (Tcl_ThreadId) 0;
222 #endif
223 }
224
225 /*
226 *----------------------------------------------------------------------
227 *
228 * TclpInitLock
229 *
230 * This procedure is used to grab a lock that serializes initialization
231 * and finalization of Tcl. On some platforms this may also initialize
232 * the mutex used to serialize creation of more mutexes and thread local
233 * storage keys.
234 *
235 * Results:
236 * None.
237 *
238 * Side effects:
239 * Acquire the initialization mutex.
240 *
241 *----------------------------------------------------------------------
242 */
243
244 void
245 TclpInitLock(void)
246 {
247 #ifdef TCL_THREADS
248 pthread_mutex_lock(&initLock);
249 #endif
250 }
251
252 /*
253 *----------------------------------------------------------------------
254 *
255 * TclFinalizeLock
256 *
257 * This procedure is used to destroy all private resources used in this
258 * file.
259 *
260 * Results:
261 * None.
262 *
263 * Side effects:
264 * Destroys everything private. TclpInitLock must be held entering this
265 * function.
266 *
267 *----------------------------------------------------------------------
268 */
269
270 void
271 TclFinalizeLock(void)
272 {
273 #ifdef TCL_THREADS
274 /*
275 * You do not need to destroy mutexes that were created with the
276 * PTHREAD_MUTEX_INITIALIZER macro. These mutexes do not need any
277 * destruction: globalLock, allocLock, and initLock.
278 */
279
280 pthread_mutex_unlock(&initLock);
281 #endif
282 }
283
284 /*
285 *----------------------------------------------------------------------
286 *
287 * TclpInitUnlock
288 *
289 * This procedure is used to release a lock that serializes
290 * initialization and finalization of Tcl.
291 *
292 * Results:
293 * None.
294 *
295 * Side effects:
296 * Release the initialization mutex.
297 *
298 *----------------------------------------------------------------------
299 */
300
301 void
302 TclpInitUnlock(void)
303 {
304 #ifdef TCL_THREADS
305 pthread_mutex_unlock(&initLock);
306 #endif
307 }
308
309 /*
310 *----------------------------------------------------------------------
311 *
312 * TclpGlobalLock
313 *
314 * This procedure is used to grab a lock that serializes creation and
315 * finalization of serialization objects. This interface is only needed
316 * in finalization; it is hidden during creation of the objects.
317 *
318 * This lock must be different than the initLock because the initLock is
319 * held during creation of synchronization objects.
320 *
321 * Results:
322 * None.
323 *
324 * Side effects:
325 * Acquire the global mutex.
326 *
327 *----------------------------------------------------------------------
328 */
329
330 void
331 TclpGlobalLock(void)
332 {
333 #ifdef TCL_THREADS
334 pthread_mutex_lock(&globalLock);
335 #endif
336 }
337
338 /*
339 *----------------------------------------------------------------------
340 *
341 * TclpGlobalUnlock
342 *
343 * This procedure is used to release a lock that serializes creation and
344 * finalization of synchronization objects.
345 *
346 * Results:
347 * None.
348 *
349 * Side effects:
350 * Release the global mutex.
351 *
352 *----------------------------------------------------------------------
353 */
354
355 void
356 TclpGlobalUnlock(void)
357 {
358 #ifdef TCL_THREADS
359 pthread_mutex_unlock(&globalLock);
360 #endif
361 }
362
363 /*
364 *----------------------------------------------------------------------
365 *
366 * Tcl_GetAllocMutex
367 *
368 * This procedure returns a pointer to a statically initialized mutex for
369 * use by the memory allocator. The allocator must use this lock, because
370 * all other locks are allocated...
371 *
372 * Results:
373 * A pointer to a mutex that is suitable for passing to Tcl_MutexLock and
374 * Tcl_MutexUnlock.
375 *
376 * Side effects:
377 * None.
378 *
379 *----------------------------------------------------------------------
380 */
381
382 Tcl_Mutex *
383 Tcl_GetAllocMutex(void)
384 {
385 #ifdef TCL_THREADS
386 pthread_mutex_t **allocLockPtrPtr = &allocLockPtr;
387 return (Tcl_Mutex *) allocLockPtrPtr;
388 #else
389 return NULL;
390 #endif
391 }
392
393 #ifdef TCL_THREADS
394
395 /*
396 *----------------------------------------------------------------------
397 *
398 * Tcl_MutexLock --
399 *
400 * This procedure is invoked to lock a mutex. This procedure handles
401 * initializing the mutex, if necessary. The caller can rely on the fact
402 * that Tcl_Mutex is an opaque pointer. This routine will change that
403 * pointer from NULL after first use.
404 *
405 * Results:
406 * None.
407 *
408 * Side effects:
409 * May block the current thread. The mutex is acquired when this returns.
410 * Will allocate memory for a pthread_mutex_t and initialize this the
411 * first time this Tcl_Mutex is used.
412 *
413 *----------------------------------------------------------------------
414 */
415
416 void
417 Tcl_MutexLock(
418 Tcl_Mutex *mutexPtr) /* Really (pthread_mutex_t **) */
419 {
420 pthread_mutex_t *pmutexPtr;
421
422 if (*mutexPtr == NULL) {
423 GLOBAL_LOCK;
424 if (*mutexPtr == NULL) {
425 /*
426 * Double inside global lock check to avoid a race condition.
427 */
428
429 pmutexPtr = (pthread_mutex_t *)ckalloc(sizeof(pthread_mutex_t));
430 pthread_mutex_init(pmutexPtr, NULL);
431 *mutexPtr = (Tcl_Mutex)pmutexPtr;
432 TclRememberMutex(mutexPtr);
433 }
434 GLOBAL_UNLOCK;
435 }
436 pmutexPtr = *((pthread_mutex_t **)mutexPtr);
437 pthread_mutex_lock(pmutexPtr);
438 }
439
440 /*
441 *----------------------------------------------------------------------
442 *
443 * Tcl_MutexUnlock --
444 *
445 * This procedure is invoked to unlock a mutex. The mutex must have been
446 * locked by Tcl_MutexLock.
447 *
448 * Results:
449 * None.
450 *
451 * Side effects:
452 * The mutex is released when this returns.
453 *
454 *----------------------------------------------------------------------
455 */
456
457 void
458 Tcl_MutexUnlock(
459 Tcl_Mutex *mutexPtr) /* Really (pthread_mutex_t **) */
460 {
461 pthread_mutex_t *pmutexPtr = *(pthread_mutex_t **) mutexPtr;
462
463 pthread_mutex_unlock(pmutexPtr);
464 }
465
466 /*
467 *----------------------------------------------------------------------
468 *
469 * TclpFinalizeMutex --
470 *
471 * This procedure is invoked to clean up one mutex. This is only safe to
472 * call at the end of time.
473 *
474 * This assumes the Global Lock is held.
475 *
476 * Results:
477 * None.
478 *
479 * Side effects:
480 * The mutex list is deallocated.
481 *
482 *----------------------------------------------------------------------
483 */
484
485 void
486 TclpFinalizeMutex(
487 Tcl_Mutex *mutexPtr)
488 {
489 pthread_mutex_t *pmutexPtr = *(pthread_mutex_t **) mutexPtr;
490
491 if (pmutexPtr != NULL) {
492 pthread_mutex_destroy(pmutexPtr);
493 ckfree(pmutexPtr);
494 *mutexPtr = NULL;
495 }
496 }
497
498 /*
499 *----------------------------------------------------------------------
500 *
501 * Tcl_ConditionWait --
502 *
503 * This procedure is invoked to wait on a condition variable. The mutex
504 * is automically released as part of the wait, and automatically grabbed
505 * when the condition is signaled.
506 *
507 * The mutex must be held when this procedure is called.
508 *
509 * Results:
510 * None.
511 *
512 * Side effects:
513 * May block the current thread. The mutex is acquired when this returns.
514 * Will allocate memory for a pthread_mutex_t and initialize this the
515 * first time this Tcl_Mutex is used.
516 *
517 *----------------------------------------------------------------------
518 */
519
520 void
521 Tcl_ConditionWait(
522 Tcl_Condition *condPtr, /* Really (pthread_cond_t **) */
523 Tcl_Mutex *mutexPtr, /* Really (pthread_mutex_t **) */
524 const Tcl_Time *timePtr) /* Timeout on waiting period */
525 {
526 pthread_cond_t *pcondPtr;
527 pthread_mutex_t *pmutexPtr;
528 struct timespec ptime;
529
530 if (*condPtr == NULL) {
531 GLOBAL_LOCK;
532
533 /*
534 * Double check inside mutex to avoid race, then initialize condition
535 * variable if necessary.
536 */
537
538 if (*condPtr == NULL) {
539 pcondPtr = (pthread_cond_t *)ckalloc(sizeof(pthread_cond_t));
540 pthread_cond_init(pcondPtr, NULL);
541 *condPtr = (Tcl_Condition) pcondPtr;
542 TclRememberCondition(condPtr);
543 }
544 GLOBAL_UNLOCK;
545 }
546 pmutexPtr = *((pthread_mutex_t **)mutexPtr);
547 pcondPtr = *((pthread_cond_t **)condPtr);
548 if (timePtr == NULL) {
549 pthread_cond_wait(pcondPtr, pmutexPtr);
550 } else {
551 Tcl_Time now;
552
553 /*
554 * Make sure to take into account the microsecond component of the
555 * current time, including possible overflow situations. [Bug #411603]
556 */
557
558 Tcl_GetTime(&now);
559 ptime.tv_sec = timePtr->sec + now.sec +
560 (timePtr->usec + now.usec) / 1000000;
561 ptime.tv_nsec = 1000 * ((timePtr->usec + now.usec) % 1000000);
562 pthread_cond_timedwait(pcondPtr, pmutexPtr, &ptime);
563 }
564 }
565
566 /*
567 *----------------------------------------------------------------------
568 *
569 * Tcl_ConditionNotify --
570 *
571 * This procedure is invoked to signal a condition variable.
572 *
573 * The mutex must be held during this call to avoid races, but this
574 * interface does not enforce that.
575 *
576 * Results:
577 * None.
578 *
579 * Side effects:
580 * May unblock another thread.
581 *
582 *----------------------------------------------------------------------
583 */
584
585 void
586 Tcl_ConditionNotify(
587 Tcl_Condition *condPtr)
588 {
589 pthread_cond_t *pcondPtr = *((pthread_cond_t **)condPtr);
590
591 if (pcondPtr != NULL) {
592 pthread_cond_broadcast(pcondPtr);
593 } else {
594 /*
595 * No-one has used the condition variable, so there are no waiters.
596 */
597 }
598 }
599
600 /*
601 *----------------------------------------------------------------------
602 *
603 * TclpFinalizeCondition --
604 *
605 * This procedure is invoked to clean up a condition variable. This is
606 * only safe to call at the end of time.
607 *
608 * This assumes the Global Lock is held.
609 *
610 * Results:
611 * None.
612 *
613 * Side effects:
614 * The condition variable is deallocated.
615 *
616 *----------------------------------------------------------------------
617 */
618
619 void
620 TclpFinalizeCondition(
621 Tcl_Condition *condPtr)
622 {
623 pthread_cond_t *pcondPtr = *(pthread_cond_t **)condPtr;
624
625 if (pcondPtr != NULL) {
626 pthread_cond_destroy(pcondPtr);
627 ckfree(pcondPtr);
628 *condPtr = NULL;
629 }
630 }
631 #endif /* TCL_THREADS */
632
633 /*
634 *----------------------------------------------------------------------
635 *
636 * TclpReaddir, TclpInetNtoa --
637 *
638 * These procedures replace core C versions to be used in a threaded
639 * environment.
640 *
641 * Results:
642 * See documentation of C functions.
643 *
644 * Side effects:
645 * See documentation of C functions.
646 *
647 * Notes:
648 * TclpReaddir is no longer used by the core (see 1095909), but it
649 * appears in the internal stubs table (see #589526).
650 *
651 *----------------------------------------------------------------------
652 */
653
654 Tcl_DirEntry *
655 TclpReaddir(
656 TclDIR * dir)
657 {
658 return TclOSreaddir(dir);
659 }
660
661 #undef TclpInetNtoa
662 char *
663 TclpInetNtoa(
664 struct in_addr addr)
665 {
666 #ifdef TCL_THREADS
667 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
668 unsigned char *b = (unsigned char*) &addr.s_addr;
669
670 snprintf(tsdPtr->nabuf, sizeof(tsdPtr->nabuf), "%u.%u.%u.%u", b[0], b[1], b[2], b[3]);
671 return tsdPtr->nabuf;
672 #else
673 return inet_ntoa(addr);
674 #endif
675 }
676
677 #ifdef TCL_THREADS
678 /*
679 * Additions by AOL for specialized thread memory allocator.
680 */
681
682 #ifdef USE_THREAD_ALLOC
683 static volatile int initialized = 0;
684 static pthread_key_t key;
685
686 typedef struct {
687 Tcl_Mutex tlock;
688 pthread_mutex_t plock;
689 } AllocMutex;
690
691 Tcl_Mutex *
692 TclpNewAllocMutex(void)
693 {
694 AllocMutex *lockPtr;
695 pthread_mutex_t *plockPtr;
696
697 lockPtr = (AllocMutex *)malloc(sizeof(AllocMutex));
698 if (lockPtr == NULL) {
699 Tcl_Panic("could not allocate lock");
700 }
701 plockPtr = &lockPtr->plock;
702 lockPtr->tlock = (Tcl_Mutex) plockPtr;
703 pthread_mutex_init(&lockPtr->plock, NULL);
704 return &lockPtr->tlock;
705 }
706
707 void
708 TclpFreeAllocMutex(
709 Tcl_Mutex *mutex) /* The alloc mutex to free. */
710 {
711 AllocMutex *lockPtr = (AllocMutex *)mutex;
712
713 if (!lockPtr) {
714 return;
715 }
716 pthread_mutex_destroy(&lockPtr->plock);
717 free(lockPtr);
718 }
719
720 void
721 TclpFreeAllocCache(
722 void *ptr)
723 {
724 if (ptr != NULL) {
725 /*
726 * Called by TclFinalizeThreadAllocThread() during the thread
727 * finalization initiated from Tcl_FinalizeThread()
728 */
729
730 TclFreeAllocCache(ptr);
731 pthread_setspecific(key, NULL);
732
733 } else if (initialized) {
734 /*
735 * Called by TclFinalizeThreadAlloc() during the process
736 * finalization initiated from Tcl_Finalize()
737 */
738
739 pthread_key_delete(key);
740 initialized = 0;
741 }
742 }
743
744 void *
745 TclpGetAllocCache(void)
746 {
747 if (!initialized) {
748 pthread_mutex_lock(allocLockPtr);
749 if (!initialized) {
750 pthread_key_create(&key, NULL);
751 initialized = 1;
752 }
753 pthread_mutex_unlock(allocLockPtr);
754 }
755 return pthread_getspecific(key);
756 }
757
758 void
759 TclpSetAllocCache(
760 void *arg)
761 {
762 pthread_setspecific(key, arg);
763 }
764 #endif /* USE_THREAD_ALLOC */
765
766 void *
767 TclpThreadCreateKey(void)
768 {
769 pthread_key_t *ptkeyPtr;
770
771 ptkeyPtr = (pthread_key_t *)TclpSysAlloc(sizeof(pthread_key_t), 0);
772 if (NULL == ptkeyPtr) {
773 Tcl_Panic("unable to allocate thread key!");
774 }
775
776 if (pthread_key_create(ptkeyPtr, NULL)) {
777 Tcl_Panic("unable to create pthread key!");
778 }
779
780 return ptkeyPtr;
781 }
782
783 void
784 TclpThreadDeleteKey(
785 void *keyPtr)
786 {
787 pthread_key_t *ptkeyPtr = (pthread_key_t *)keyPtr;
788
789 if (pthread_key_delete(*ptkeyPtr)) {
790 Tcl_Panic("unable to delete key!");
791 }
792
793 TclpSysFree(keyPtr);
794 }
795
796 void
797 TclpThreadSetGlobalTSD(
798 void *tsdKeyPtr,
799 void *ptr)
800 {
801 pthread_key_t *ptkeyPtr = (pthread_key_t *)tsdKeyPtr;
802
803 if (pthread_setspecific(*ptkeyPtr, ptr)) {
804 Tcl_Panic("unable to set global TSD value");
805 }
806 }
807
808 void *
809 TclpThreadGetGlobalTSD(
810 void *tsdKeyPtr)
811 {
812 pthread_key_t *ptkeyPtr = (pthread_key_t *)tsdKeyPtr;
813
814 return pthread_getspecific(*ptkeyPtr);
815 }
816
817 #endif /* TCL_THREADS */
818
819 /*
820 * Local Variables:
821 * mode: c
822 * c-basic-offset: 4
823 * fill-column: 78
824 * End:
825 */