Debian Codegraph @

pTk/mTk/tclUnix/tclUnixThrd.c

raw symbols SCIP
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 *
9 * See the file "license.terms" for information on usage and redistribution
10 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11 *
12 * SCCS: @(#) tclUnixThrd.c 1.18 98/02/19 14:24:12
13 */
14
15 #include "tclInt.h"
16 #include "tclPort.h"
17
18 #ifdef TCL_THREADS
19
20 #include "pthread.h"
21
22 typedef struct ThreadSpecificData {
23 char nabuf[16];
24 struct tm gtbuf;
25 struct tm ltbuf;
26 struct {
27 Tcl_DirEntry ent;
28 char name[MAXNAMLEN+1];
29 } rdbuf;
30 } ThreadSpecificData;
31
32 static Tcl_ThreadDataKey dataKey;
33
34 /*
35 * masterLock is used to serialize creation of mutexes, condition
36 * variables, and thread local storage.
37 * This is the only place that can count on the ability to statically
38 * initialize the mutex.
39 */
40
41 static pthread_mutex_t masterLock = PTHREAD_MUTEX_INITIALIZER;
42
43 /*
44 * initLock is used to serialize initialization and finalization
45 * of Tcl. It cannot use any dyamically allocated storage.
46 */
47
48 static pthread_mutex_t initLock = PTHREAD_MUTEX_INITIALIZER;
49
50 /*
51 * allocLock is used by Tcl's version of malloc for synchronization.
52 * For obvious reasons, cannot use any dyamically allocated storage.
53 */
54
55 static pthread_mutex_t allocLock = PTHREAD_MUTEX_INITIALIZER;
56 static pthread_mutex_t *allocLockPtr = &allocLock;
57
58 /*
59 * These are for the critical sections inside this file.
60 */
61
62 #define MASTER_LOCK pthread_mutex_lock(&masterLock)
63 #define MASTER_UNLOCK pthread_mutex_unlock(&masterLock)
64
65 #endif /* TCL_THREADS */
66
67
68 /*
69 *----------------------------------------------------------------------
70 *
71 * Tcl_CreateThread --
72 *
73 * This procedure creates a new thread.
74 *
75 * Results:
76 * TCL_OK if the thread could be created. The thread ID is
77 * returned in a parameter.
78 *
79 * Side effects:
80 * A new thread is created.
81 *
82 *----------------------------------------------------------------------
83 */
84
85 int
86 Tcl_CreateThread(idPtr, proc, clientData, stackSize, flags)
87 Tcl_ThreadId *idPtr; /* Return, the ID of the thread */
88 Tcl_ThreadCreateProc proc; /* Main() function of the thread */
89 ClientData clientData; /* The one argument to Main() */
90 int stackSize; /* Size of stack for the new thread */
91 int flags; /* Flags controlling behaviour of
92 * the new thread */
93 {
94 #ifdef TCL_THREADS
95 pthread_attr_t attr;
96 int result;
97
98 pthread_attr_init(&attr);
99 pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
100
101 #ifdef HAVE_PTHREAD_ATTR_SETSTACKSIZE
102 if (stackSize != TCL_THREAD_STACK_DEFAULT) {
103 pthread_attr_setstacksize(&attr, (size_t) stackSize);
104 #ifdef TCL_THREAD_STACK_MIN
105 } else {
106 /*
107 * Certain systems define a thread stack size that by default is
108 * too small for many operations. The user has the option of
109 * defining TCL_THREAD_STACK_MIN to a value large enough to work
110 * for their needs. This would look like (for 128K min stack):
111 * make MEM_DEBUG_FLAGS=-DTCL_THREAD_STACK_MIN=131072L
112 *
113 * This solution is not optimal, as we should allow the user to
114 * specify a size at runtime, but we don't want to slow this function
115 * down, and that would still leave the main thread at the default.
116 */
117
118 size_t size;
119 result = pthread_attr_getstacksize(&attr, &size);
120 if (!result && (size < TCL_THREAD_STACK_MIN)) {
121 pthread_attr_setstacksize(&attr, (size_t) TCL_THREAD_STACK_MIN);
122 }
123 #endif
124 }
125 #endif
126 if (! (flags & TCL_THREAD_JOINABLE)) {
127 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
128 }
129
130
131 if (pthread_create((pthread_t *)idPtr, &attr,
132 (void * (*)())proc, (void *)clientData) &&
133 pthread_create((pthread_t *)idPtr, NULL,
134 (void * (*)())proc, (void *)clientData)) {
135 result = TCL_ERROR;
136 } else {
137 result = TCL_OK;
138 }
139 pthread_attr_destroy(&attr);
140 return result;
141 #else
142 return TCL_ERROR;
143 #endif /* TCL_THREADS */
144 }
145
146 /*
147 *----------------------------------------------------------------------
148 *
149 * Tcl_JoinThread --
150 *
151 * This procedure waits upon the exit of the specified thread.
152 *
153 * Results:
154 * TCL_OK if the wait was successful, TCL_ERROR else.
155 *
156 * Side effects:
157 * The result area is set to the exit code of the thread we
158 * waited upon.
159 *
160 *----------------------------------------------------------------------
161 */
162
163 int
164 Tcl_JoinThread(threadId, state)
165 Tcl_ThreadId threadId; /* Id of the thread to wait upon */
166 int* state; /* Reference to the storage the result
167 * of the thread we wait upon will be
168 * written into. */
169 {
170 #ifdef TCL_THREADS
171 int result;
172
173 result = pthread_join ((pthread_t) threadId, (VOID**) state);
174 return (result == 0) ? TCL_OK : TCL_ERROR;
175 #else
176 return TCL_ERROR;
177 #endif
178 }
179
180 #ifdef TCL_THREADS
181 /*
182 *----------------------------------------------------------------------
183 *
184 * TclpThreadExit --
185 *
186 * This procedure terminates the current thread.
187 *
188 * Results:
189 * None.
190 *
191 * Side effects:
192 * This procedure terminates the current thread.
193 *
194 *----------------------------------------------------------------------
195 */
196
197 void
198 TclpThreadExit(status)
199 int status;
200 {
201 pthread_exit((VOID *)status);
202 }
203 #endif /* TCL_THREADS */
204
205 /*
206 *----------------------------------------------------------------------
207 *
208 * Tcl_GetCurrentThread --
209 *
210 * This procedure returns the ID of the currently running thread.
211 *
212 * Results:
213 * A thread ID.
214 *
215 * Side effects:
216 * None.
217 *
218 *----------------------------------------------------------------------
219 */
220
221 Tcl_ThreadId
222 Tcl_GetCurrentThread()
223 {
224 #ifdef TCL_THREADS
225 return (Tcl_ThreadId) pthread_self();
226 #else
227 return (Tcl_ThreadId) 0;
228 #endif
229 }
230
231
232 /*
233 *----------------------------------------------------------------------
234 *
235 * TclpInitLock
236 *
237 * This procedure is used to grab a lock that serializes initialization
238 * and finalization of Tcl. On some platforms this may also initialize
239 * the mutex used to serialize creation of more mutexes and thread
240 * local storage keys.
241 *
242 * Results:
243 * None.
244 *
245 * Side effects:
246 * Acquire the initialization mutex.
247 *
248 *----------------------------------------------------------------------
249 */
250
251 void
252 TclpInitLock()
253 {
254 #ifdef TCL_THREADS
255 pthread_mutex_lock(&initLock);
256 #endif
257 }
258
259
260 /*
261 *----------------------------------------------------------------------
262 *
263 * TclpInitUnlock
264 *
265 * This procedure is used to release a lock that serializes initialization
266 * and finalization of Tcl.
267 *
268 * Results:
269 * None.
270 *
271 * Side effects:
272 * Release the initialization mutex.
273 *
274 *----------------------------------------------------------------------
275 */
276
277 void
278 TclpInitUnlock()
279 {
280 #ifdef TCL_THREADS
281 pthread_mutex_unlock(&initLock);
282 #endif
283 }
284
285 /*
286 *----------------------------------------------------------------------
287 *
288 * TclpMasterLock
289 *
290 * This procedure is used to grab a lock that serializes creation
291 * and finalization of serialization objects. This interface is
292 * only needed in finalization; it is hidden during
293 * creation of the objects.
294 *
295 * This lock must be different than the initLock because the
296 * initLock is held during creation of syncronization objects.
297 *
298 * Results:
299 * None.
300 *
301 * Side effects:
302 * Acquire the master mutex.
303 *
304 *----------------------------------------------------------------------
305 */
306
307 void
308 TclpMasterLock()
309 {
310 #ifdef TCL_THREADS
311 pthread_mutex_lock(&masterLock);
312 #endif
313 }
314
315
316 /*
317 *----------------------------------------------------------------------
318 *
319 * TclpMasterUnlock
320 *
321 * This procedure is used to release a lock that serializes creation
322 * and finalization of synchronization objects.
323 *
324 * Results:
325 * None.
326 *
327 * Side effects:
328 * Release the master mutex.
329 *
330 *----------------------------------------------------------------------
331 */
332
333 void
334 TclpMasterUnlock()
335 {
336 #ifdef TCL_THREADS
337 pthread_mutex_unlock(&masterLock);
338 #endif
339 }
340
341
342 /*
343 *----------------------------------------------------------------------
344 *
345 * Tcl_GetAllocMutex
346 *
347 * This procedure returns a pointer to a statically initialized
348 * mutex for use by the memory allocator. The alloctor must
349 * use this lock, because all other locks are allocated...
350 *
351 * Results:
352 * A pointer to a mutex that is suitable for passing to
353 * Tcl_MutexLock and Tcl_MutexUnlock.
354 *
355 * Side effects:
356 * None.
357 *
358 *----------------------------------------------------------------------
359 */
360
361 Tcl_Mutex *
362 Tcl_GetAllocMutex()
363 {
364 #ifdef TCL_THREADS
365 return (Tcl_Mutex *)&allocLockPtr;
366 #else
367 return NULL;
368 #endif
369 }
370
371 #ifdef TCL_THREADS
372
373 /*
374 *----------------------------------------------------------------------
375 *
376 * Tcl_MutexLock --
377 *
378 * This procedure is invoked to lock a mutex. This procedure
379 * handles initializing the mutex, if necessary. The caller
380 * can rely on the fact that Tcl_Mutex is an opaque pointer.
381 * This routine will change that pointer from NULL after first use.
382 *
383 * Results:
384 * None.
385 *
386 * Side effects:
387 * May block the current thread. The mutex is aquired when
388 * this returns. Will allocate memory for a pthread_mutex_t
389 * and initialize this the first time this Tcl_Mutex is used.
390 *
391 *----------------------------------------------------------------------
392 */
393
394 void
395 Tcl_MutexLock(mutexPtr)
396 Tcl_Mutex *mutexPtr; /* Really (pthread_mutex_t **) */
397 {
398 pthread_mutex_t *pmutexPtr;
399 if (*mutexPtr == NULL) {
400 MASTER_LOCK;
401 if (*mutexPtr == NULL) {
402 /*
403 * Double inside master lock check to avoid a race condition.
404 */
405
406 pmutexPtr = (pthread_mutex_t *)ckalloc(sizeof(pthread_mutex_t));
407 pthread_mutex_init(pmutexPtr, NULL);
408 *mutexPtr = (Tcl_Mutex)pmutexPtr;
409 TclRememberMutex(mutexPtr);
410 }
411 MASTER_UNLOCK;
412 }
413 pmutexPtr = *((pthread_mutex_t **)mutexPtr);
414 pthread_mutex_lock(pmutexPtr);
415 }
416
417
418 /*
419 *----------------------------------------------------------------------
420 *
421 * Tcl_MutexUnlock --
422 *
423 * This procedure is invoked to unlock a mutex. The mutex must
424 * have been locked by Tcl_MutexLock.
425 *
426 * Results:
427 * None.
428 *
429 * Side effects:
430 * The mutex is released when this returns.
431 *
432 *----------------------------------------------------------------------
433 */
434
435 void
436 Tcl_MutexUnlock(mutexPtr)
437 Tcl_Mutex *mutexPtr; /* Really (pthread_mutex_t **) */
438 {
439 pthread_mutex_t *pmutexPtr = *(pthread_mutex_t **)mutexPtr;
440 pthread_mutex_unlock(pmutexPtr);
441 }
442
443
444 /*
445 *----------------------------------------------------------------------
446 *
447 * TclpFinalizeMutex --
448 *
449 * This procedure is invoked to clean up one mutex. This is only
450 * safe to call at the end of time.
451 *
452 * This assumes the Master Lock is held.
453 *
454 * Results:
455 * None.
456 *
457 * Side effects:
458 * The mutex list is deallocated.
459 *
460 *----------------------------------------------------------------------
461 */
462
463 void
464 TclpFinalizeMutex(mutexPtr)
465 Tcl_Mutex *mutexPtr;
466 {
467 pthread_mutex_t *pmutexPtr = *(pthread_mutex_t **)mutexPtr;
468 if (pmutexPtr != NULL) {
469 ckfree((char *)pmutexPtr);
470 *mutexPtr = NULL;
471 }
472 }
473
474
475 /*
476 *----------------------------------------------------------------------
477 *
478 * TclpThreadDataKeyInit --
479 *
480 * This procedure initializes a thread specific data block key.
481 * Each thread has table of pointers to thread specific data.
482 * all threads agree on which table entry is used by each module.
483 * this is remembered in a "data key", that is just an index into
484 * this table. To allow self initialization, the interface
485 * passes a pointer to this key and the first thread to use
486 * the key fills in the pointer to the key. The key should be
487 * a process-wide static.
488 *
489 * Results:
490 * None.
491 *
492 * Side effects:
493 * Will allocate memory the first time this process calls for
494 * this key. In this case it modifies its argument
495 * to hold the pointer to information about the key.
496 *
497 *----------------------------------------------------------------------
498 */
499
500 void
501 TclpThreadDataKeyInit(keyPtr)
502 Tcl_ThreadDataKey *keyPtr; /* Identifier for the data chunk,
503 * really (pthread_key_t **) */
504 {
505 pthread_key_t *pkeyPtr;
506
507 MASTER_LOCK;
508 if (*keyPtr == NULL) {
509 pkeyPtr = (pthread_key_t *)ckalloc(sizeof(pthread_key_t));
510 pthread_key_create(pkeyPtr, NULL);
511 *keyPtr = (Tcl_ThreadDataKey)pkeyPtr;
512 TclRememberDataKey(keyPtr);
513 }
514 MASTER_UNLOCK;
515 }
516
517 /*
518 *----------------------------------------------------------------------
519 *
520 * TclpThreadDataKeyGet --
521 *
522 * This procedure returns a pointer to a block of thread local storage.
523 *
524 * Results:
525 * A thread-specific pointer to the data structure, or NULL
526 * if the memory has not been assigned to this key for this thread.
527 *
528 * Side effects:
529 * None.
530 *
531 *----------------------------------------------------------------------
532 */
533
534 VOID *
535 TclpThreadDataKeyGet(keyPtr)
536 Tcl_ThreadDataKey *keyPtr; /* Identifier for the data chunk,
537 * really (pthread_key_t **) */
538 {
539 pthread_key_t *pkeyPtr = *(pthread_key_t **)keyPtr;
540 if (pkeyPtr == NULL) {
541 return NULL;
542 } else {
543 return (VOID *)pthread_getspecific(*pkeyPtr);
544 }
545 }
546
547
548 /*
549 *----------------------------------------------------------------------
550 *
551 * TclpThreadDataKeySet --
552 *
553 * This procedure sets the pointer to a block of thread local storage.
554 *
555 * Results:
556 * None.
557 *
558 * Side effects:
559 * Sets up the thread so future calls to TclpThreadDataKeyGet with
560 * this key will return the data pointer.
561 *
562 *----------------------------------------------------------------------
563 */
564
565 void
566 TclpThreadDataKeySet(keyPtr, data)
567 Tcl_ThreadDataKey *keyPtr; /* Identifier for the data chunk,
568 * really (pthread_key_t **) */
569 VOID *data; /* Thread local storage */
570 {
571 pthread_key_t *pkeyPtr = *(pthread_key_t **)keyPtr;
572 pthread_setspecific(*pkeyPtr, data);
573 }
574
575 /*
576 *----------------------------------------------------------------------
577 *
578 * TclpFinalizeThreadData --
579 *
580 * This procedure cleans up the thread-local storage. This is
581 * called once for each thread.
582 *
583 * Results:
584 * None.
585 *
586 * Side effects:
587 * Frees up all thread local storage.
588 *
589 *----------------------------------------------------------------------
590 */
591
592 void
593 TclpFinalizeThreadData(keyPtr)
594 Tcl_ThreadDataKey *keyPtr;
595 {
596 VOID *result;
597 pthread_key_t *pkeyPtr;
598
599 if (*keyPtr != NULL) {
600 pkeyPtr = *(pthread_key_t **)keyPtr;
601 result = (VOID *)pthread_getspecific(*pkeyPtr);
602 if (result != NULL) {
603 ckfree((char *)result);
604 pthread_setspecific(*pkeyPtr, (void *)NULL);
605 }
606 }
607 }
608
609 /*
610 *----------------------------------------------------------------------
611 *
612 * TclpFinalizeThreadDataKey --
613 *
614 * This procedure is invoked to clean up one key. This is a
615 * process-wide storage identifier. The thread finalization code
616 * cleans up the thread local storage itself.
617 *
618 * This assumes the master lock is held.
619 *
620 * Results:
621 * None.
622 *
623 * Side effects:
624 * The key is deallocated.
625 *
626 *----------------------------------------------------------------------
627 */
628
629 void
630 TclpFinalizeThreadDataKey(keyPtr)
631 Tcl_ThreadDataKey *keyPtr;
632 {
633 pthread_key_t *pkeyPtr;
634 if (*keyPtr != NULL) {
635 pkeyPtr = *(pthread_key_t **)keyPtr;
636 pthread_key_delete(*pkeyPtr);
637 ckfree((char *)pkeyPtr);
638 *keyPtr = NULL;
639 }
640 }
641
642
643 /*
644 *----------------------------------------------------------------------
645 *
646 * Tcl_ConditionWait --
647 *
648 * This procedure is invoked to wait on a condition variable.
649 * The mutex is automically released as part of the wait, and
650 * automatically grabbed when the condition is signaled.
651 *
652 * The mutex must be held when this procedure is called.
653 *
654 * Results:
655 * None.
656 *
657 * Side effects:
658 * May block the current thread. The mutex is aquired when
659 * this returns. Will allocate memory for a pthread_mutex_t
660 * and initialize this the first time this Tcl_Mutex is used.
661 *
662 *----------------------------------------------------------------------
663 */
664
665 void
666 Tcl_ConditionWait(condPtr, mutexPtr, timePtr)
667 Tcl_Condition *condPtr; /* Really (pthread_cond_t **) */
668 Tcl_Mutex *mutexPtr; /* Really (pthread_mutex_t **) */
669 Tcl_Time *timePtr; /* Timeout on waiting period */
670 {
671 pthread_cond_t *pcondPtr;
672 pthread_mutex_t *pmutexPtr;
673 struct timespec ptime;
674
675 if (*condPtr == NULL) {
676 MASTER_LOCK;
677
678 /*
679 * Double check inside mutex to avoid race,
680 * then initialize condition variable if necessary.
681 */
682
683 if (*condPtr == NULL) {
684 pcondPtr = (pthread_cond_t *)ckalloc(sizeof(pthread_cond_t));
685 pthread_cond_init(pcondPtr, NULL);
686 *condPtr = (Tcl_Condition)pcondPtr;
687 TclRememberCondition(condPtr);
688 }
689 MASTER_UNLOCK;
690 }
691 pmutexPtr = *((pthread_mutex_t **)mutexPtr);
692 pcondPtr = *((pthread_cond_t **)condPtr);
693 if (timePtr == NULL) {
694 pthread_cond_wait(pcondPtr, pmutexPtr);
695 } else {
696 Tcl_Time now;
697
698 /*
699 * Make sure to take into account the microsecond component of the
700 * current time, including possible overflow situations. [Bug #411603]
701 */
702
703 Tcl_GetTime(&now);
704 ptime.tv_sec = timePtr->sec + now.sec +
705 (timePtr->usec + now.usec) / 1000000;
706 ptime.tv_nsec = 1000 * ((timePtr->usec + now.usec) % 1000000);
707 pthread_cond_timedwait(pcondPtr, pmutexPtr, &ptime);
708 }
709 }
710
711 /*
712 *----------------------------------------------------------------------
713 *
714 * Tcl_ConditionNotify --
715 *
716 * This procedure is invoked to signal a condition variable.
717 *
718 * The mutex must be held during this call to avoid races,
719 * but this interface does not enforce that.
720 *
721 * Results:
722 * None.
723 *
724 * Side effects:
725 * May unblock another thread.
726 *
727 *----------------------------------------------------------------------
728 */
729
730 void
731 Tcl_ConditionNotify(condPtr)
732 Tcl_Condition *condPtr;
733 {
734 pthread_cond_t *pcondPtr = *((pthread_cond_t **)condPtr);
735 if (pcondPtr != NULL) {
736 pthread_cond_broadcast(pcondPtr);
737 } else {
738 /*
739 * Noone has used the condition variable, so there are no waiters.
740 */
741 }
742 }
743
744
745 /*
746 *----------------------------------------------------------------------
747 *
748 * TclpFinalizeCondition --
749 *
750 * This procedure is invoked to clean up a condition variable.
751 * This is only safe to call at the end of time.
752 *
753 * This assumes the Master Lock is held.
754 *
755 * Results:
756 * None.
757 *
758 * Side effects:
759 * The condition variable is deallocated.
760 *
761 *----------------------------------------------------------------------
762 */
763
764 void
765 TclpFinalizeCondition(condPtr)
766 Tcl_Condition *condPtr;
767 {
768 pthread_cond_t *pcondPtr = *(pthread_cond_t **)condPtr;
769 if (pcondPtr != NULL) {
770 pthread_cond_destroy(pcondPtr);
771 ckfree((char *)pcondPtr);
772 *condPtr = NULL;
773 }
774 }
775 #endif /* TCL_THREADS */
776
777 /*
778 *----------------------------------------------------------------------
779 *
780 * TclpReaddir, TclpLocaltime, TclpGmtime, TclpInetNtoa --
781 *
782 * These procedures replace core C versions to be used in a
783 * threaded environment.
784 *
785 * Results:
786 * See documentation of C functions.
787 *
788 * Side effects:
789 * See documentation of C functions.
790 *
791 *----------------------------------------------------------------------
792 */
793
794 #if defined(TCL_THREADS) && !defined(HAVE_READDIR_R)
795 TCL_DECLARE_MUTEX( rdMutex )
796 #undef readdir
797 #endif
798
799 Tcl_DirEntry *
800 TclpReaddir(DIR * dir)
801 {
802 Tcl_DirEntry *ent;
803 #ifdef TCL_THREADS
804 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
805
806 #ifdef HAVE_READDIR_R
807 ent = &tsdPtr->rdbuf.ent;
808 if (TclOSreaddir_r(dir, ent, &ent) != 0) {
809 ent = NULL;
810 }
811
812 #else /* !HAVE_READDIR_R */
813
814 Tcl_MutexLock(&rdMutex);
815 # ifdef HAVE_STRUCT_DIRENT64
816 ent = readdir64(dir);
817 # else /* !HAVE_STRUCT_DIRENT64 */
818 ent = readdir(dir);
819 # endif /* HAVE_STRUCT_DIRENT64 */
820 if (ent != NULL) {
821 memcpy((VOID *) &tsdPtr->rdbuf.ent, (VOID *) ent,
822 sizeof(tsdPtr->rdbuf));
823 ent = &tsdPtr->rdbuf.ent;
824 }
825 Tcl_MutexUnlock(&rdMutex);
826
827 #endif /* HAVE_READDIR_R */
828 #else
829 # ifdef HAVE_STRUCT_DIRENT64
830 ent = readdir64(dir);
831 # else /* !HAVE_STRUCT_DIRENT64 */
832 ent = readdir(dir);
833 # endif /* HAVE_STRUCT_DIRENT64 */
834 #endif
835 return ent;
836 }
837
838 #if defined(TCL_THREADS) && (!defined(HAVE_GMTIME_R) || !defined(HAVE_LOCALTIME_R))
839 TCL_DECLARE_MUTEX( tmMutex )
840 #undef localtime
841 #undef gmtime
842 #endif
843
844 struct tm *
845 TclpLocaltime(time_t * clock)
846 {
847 #ifdef TCL_THREADS
848 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
849
850 #ifdef HAVE_LOCALTIME_R
851 return localtime_r(clock, &tsdPtr->ltbuf);
852 #else
853 Tcl_MutexLock( &tmMutex );
854 memcpy( (VOID *) &tsdPtr->ltbuf, (VOID *) localtime( clock ), sizeof (struct tm) );
855 Tcl_MutexUnlock( &tmMutex );
856 return &tsdPtr->ltbuf;
857 #endif
858 #else
859 return localtime(clock);
860 #endif
861 }
862
863 struct tm *
864 TclpGmtime(time_t * clock)
865 {
866 #ifdef TCL_THREADS
867 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
868
869 #ifdef HAVE_GMTIME_R
870 return gmtime_r(clock, &tsdPtr->gtbuf);
871 #else
872 Tcl_MutexLock( &tmMutex );
873 memcpy( (VOID *) &tsdPtr->gtbuf, (VOID *) gmtime( clock ), sizeof (struct tm) );
874 Tcl_MutexUnlock( &tmMutex );
875 return &tsdPtr->gtbuf;
876 #endif
877 #else
878 return gmtime(clock);
879 #endif
880 }
881
882 char *
883 TclpInetNtoa(struct in_addr addr)
884 {
885 #ifdef TCL_THREADS
886 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
887 union {
888 unsigned long l;
889 unsigned char b[4];
890 } u;
891
892 u.l = (unsigned long) addr.s_addr;
893 sprintf(tsdPtr->nabuf, "%u.%u.%u.%u", u.b[0], u.b[1], u.b[2], u.b[3]);
894 return tsdPtr->nabuf;
895 #else
896 return inet_ntoa(addr);
897 #endif
898 }
899
900 #ifdef TCL_THREADS
901 /*
902 * Additions by AOL for specialized thread memory allocator.
903 */
904 #ifdef USE_THREAD_ALLOC
905 static int initialized = 0;
906 static pthread_key_t key;
907 static pthread_once_t once = PTHREAD_ONCE_INIT;
908
909 Tcl_Mutex *
910 TclpNewAllocMutex(void)
911 {
912 struct lock {
913 Tcl_Mutex tlock;
914 pthread_mutex_t plock;
915 } *lockPtr;
916
917 lockPtr = malloc(sizeof(struct lock));
918 if (lockPtr == NULL) {
919 panic("could not allocate lock");
920 }
921 lockPtr->tlock = (Tcl_Mutex) &lockPtr->plock;
922 pthread_mutex_init(&lockPtr->plock, NULL);
923 return &lockPtr->tlock;
924 }
925
926 static void
927 InitKey(void)
928 {
929 extern void TclFreeAllocCache(void *);
930
931 pthread_key_create(&key, TclFreeAllocCache);
932 initialized = 1;
933 }
934
935 void *
936 TclpGetAllocCache(void)
937 {
938 if (!initialized) {
939 pthread_once(&once, InitKey);
940 }
941 return pthread_getspecific(key);
942 }
943
944 void
945 TclpSetAllocCache(void *arg)
946 {
947 pthread_setspecific(key, arg);
948 }
949
950 #endif /* USE_THREAD_ALLOC */
951 #endif /* TCL_THREADS */