2 * Copyright (c) 1994 by Xerox Corporation. All rights reserved.
3 * Copyright (c) 1996 by Silicon Graphics. All rights reserved.
4 * Copyright (c) 1998 by Fergus Henderson. All rights reserved.
5 * Copyright (c) 2000-2001 by Hewlett-Packard Company. All rights reserved.
7 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
8 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
10 * Permission is hereby granted to use or copy this program
11 * for any purpose, provided the above notices are retained on all copies.
12 * Permission to modify the code and to distribute modified code is granted,
13 * provided the above notices are retained, and a notice that the code was
14 * modified is included with the above copyright notice.
17 * Support code for LinuxThreads, the clone()-based kernel
18 * thread package for Linux which is included in libc6.
20 * This code relies on implementation details of LinuxThreads,
21 * (i.e. properties not guaranteed by the Pthread standard),
22 * though this version now does less of that than the other Pthreads
25 * Note that there is a lot of code duplication between linux_threads.c
26 * and thread support for some of the other Posix platforms; any changes
27 * made here may need to be reflected there too.
30 * Linux_threads.c now also includes some code to support HPUX and
31 * OSF1 (Compaq Tru64 Unix, really). The OSF1 support is not yet
32 * functional. The OSF1 code is based on Eric Benson's
33 * patch, though that was originally against hpux_irix_threads. The code
34 * here is completely untested. With 0.0000001% probability, it might
37 * Eric also suggested an alternate basis for a lock implementation in
39 * + #elif defined(OSF1)
40 * + unsigned long GC_allocate_lock = 0;
41 * + msemaphore GC_allocate_semaphore;
42 * + # define GC_TRY_LOCK() \
43 * + ((msem_lock(&GC_allocate_semaphore, MSEM_IF_NOWAIT) == 0) \
44 * + ? (GC_allocate_lock = 1) \
46 * + # define GC_LOCK_TAKEN GC_allocate_lock
49 /* #define DEBUG_THREADS 1 */
51 /* ANSI C requires that a compilation unit contains something */
53 # if defined(GC_LINUX_THREADS) || defined(LINUX_THREADS) \
54 || defined(GC_HPUX_THREADS) || defined(HPUX_THREADS) \
55 || defined(GC_OSF1_THREADS) || defined(OSF1_THREADS) \
57 # include "private/gc_priv.h"
58 # ifdef THREAD_LOCAL_ALLOC
59 # if !defined(USE_PTHREAD_SPECIFIC) && !defined(USE_HPUX_TLS)
60 # include "private/specific.h"
62 # if defined(USE_PTHREAD_SPECIFIC)
63 # define GC_getspecific pthread_getspecific
64 # define GC_setspecific pthread_setspecific
65 # define GC_key_create pthread_key_create
66 typedef pthread_key_t GC_key_t;
68 # if defined(USE_HPUX_TLS)
69 # define GC_getspecific(x) (x)
70 # define GC_setspecific(key, v) ((key) = (v), 0)
71 # define GC_key_create(key, d) 0
72 typedef void * GC_key_t;
81 # include <sys/mman.h>
82 # include <sys/time.h>
83 # include <semaphore.h>
85 # include <sys/types.h>
86 # include <sys/stat.h>
94 # define WRAP_FUNC(f) __wrap_##f
95 # define REAL_FUNC(f) __real_##f
97 # define WRAP_FUNC(f) GC_##f
98 # define REAL_FUNC(f) f
99 # undef pthread_create
100 # undef pthread_sigmask
102 # undef pthread_detach
109 void GC_print_sig_mask()
114 if (pthread_sigmask(SIG_BLOCK, NULL, &blocked) != 0)
115 ABORT("pthread_sigmask");
116 GC_printf0("Blocked: ");
117 for (i = 1; i <= MAXSIG; i++) {
118 if (sigismember(&blocked, i)) { GC_printf1("%ld ",(long) i); }
125 /* We use the allocation lock to protect thread-related data structures. */
127 /* The set of all known threads. We intercept thread creation and */
129 /* Protected by allocation/GC lock. */
130 /* Some of this should be declared volatile, but that's inconsistent */
131 /* with some library routine declarations. */
132 typedef struct GC_Thread_Rep {
133 struct GC_Thread_Rep * next; /* More recently allocated threads */
134 /* with a given pthread id come */
135 /* first. (All but the first are */
136 /* guaranteed to be dead, but we may */
137 /* not yet have registered the join.) */
140 # define FINISHED 1 /* Thread has exited. */
141 # define DETACHED 2 /* Thread is intended to be detached. */
142 # define MAIN_THREAD 4 /* True for the original thread only. */
143 short thread_blocked; /* Protected by GC lock. */
144 /* Treated as a boolean value. If set, */
145 /* thread will acquire GC lock before */
146 /* doing any pointer manipulations, and */
147 /* has set its sp value. Thus it does */
148 /* not need to be sent a signal to stop */
150 ptr_t stack_end; /* Cold end of the stack. */
151 ptr_t stack_ptr; /* Valid only when stopped. */
153 ptr_t backing_store_end;
154 ptr_t backing_store_ptr;
157 void * status; /* The value returned from the thread. */
158 /* Used only to avoid premature */
159 /* reclamation of any data it might */
161 # ifdef THREAD_LOCAL_ALLOC
162 # if CPP_WORDSZ == 64 && defined(ALIGN_DOUBLE)
163 # define GRANULARITY 16
164 # define NFREELISTS 48
166 # define GRANULARITY 8
167 # define NFREELISTS 64
169 /* The ith free list corresponds to size (i+1)*GRANULARITY */
170 # define INDEX_FROM_BYTES(n) (ADD_SLOP(n) - 1)/GRANULARITY
171 # define BYTES_FROM_INDEX(i) (((i) + 1) * GRANULARITY - EXTRA_BYTES)
172 # define SMALL_ENOUGH(bytes) (ADD_SLOP(bytes) <= NFREELISTS*GRANULARITY)
173 ptr_t ptrfree_freelists[NFREELISTS];
174 ptr_t normal_freelists[NFREELISTS];
175 # ifdef GC_GCJ_SUPPORT
176 ptr_t gcj_freelists[NFREELISTS];
178 /* Free lists contain either a pointer or a small count */
179 /* reflecting the number of granules allocated at that */
181 /* 0 ==> thread-local allocation in use, free list */
183 /* > 0, <= DIRECT_GRANULES ==> Using global allocation, */
184 /* too few objects of this size have been */
185 /* allocated by this thread. */
186 /* >= HBLKSIZE => pointer to nonempty free list. */
187 /* > DIRECT_GRANULES, < HBLKSIZE ==> transition to */
188 /* local alloc, equivalent to 0. */
189 # define DIRECT_GRANULES (HBLKSIZE/GRANULARITY)
190 /* Don't use local free lists for up to this much */
195 GC_thread GC_lookup_thread(pthread_t id);
197 static GC_bool fully_initialized = FALSE;
199 # if defined(__GNUC__)
200 void GC_full_init() __attribute__ ((constructor));
205 # if defined(THREAD_LOCAL_ALLOC) && !defined(DBG_HDRS_ALL)
207 /* We don't really support thread-local allocation with DBG_HDRS_ALL */
212 GC_key_t GC_thread_key;
214 static GC_bool keys_initialized;
216 /* Recover the contents of the freelist array fl into the global one gfl.*/
217 /* Note that the indexing scheme differs, in that gfl has finer size */
218 /* resolution, even if not all entries are used. */
219 /* We hold the allocator lock. */
220 static void return_freelists(ptr_t *fl, ptr_t *gfl)
226 for (i = 0; i < NFREELISTS; ++i) {
227 nwords = (i + 1) * (GRANULARITY/sizeof(word));
230 if ((word)q < HBLKSIZE) continue;
231 if (gfl[nwords] == 0) {
235 for (; (word)q >= HBLKSIZE; qptr = &(obj_link(q)), q = *qptr);
240 /* Clear fl[i], since the thread structure may hang around. */
241 /* Do it in a way that is likely to trap if we access it. */
242 fl[i] = (ptr_t)HBLKSIZE;
246 /* Each thread structure must be initialized. */
247 /* This call must be made from the new thread. */
248 /* Caller holds allocation lock. */
249 void GC_init_thread_local(GC_thread p)
253 if (!keys_initialized) {
254 if (0 != GC_key_create(&GC_thread_key, 0)) {
255 ABORT("Failed to create key for local allocator");
257 keys_initialized = TRUE;
259 if (0 != GC_setspecific(GC_thread_key, p)) {
260 ABORT("Failed to set thread specific allocation pointers");
262 for (i = 0; i < NFREELISTS; ++i) {
263 p -> ptrfree_freelists[i] = (ptr_t)1;
264 p -> normal_freelists[i] = (ptr_t)1;
265 # ifdef GC_GCJ_SUPPORT
266 p -> gcj_freelists[i] = (ptr_t)1;
271 #ifdef GC_GCJ_SUPPORT
272 extern ptr_t * GC_gcjobjfreelist;
275 /* We hold the allocator lock. */
276 void GC_destroy_thread_local(GC_thread p)
278 /* We currently only do this from the thread itself. */
279 GC_ASSERT(GC_getspecific(GC_thread_key) == (void *)p);
280 return_freelists(p -> ptrfree_freelists, GC_aobjfreelist);
281 return_freelists(p -> normal_freelists, GC_objfreelist);
282 # ifdef GC_GCJ_SUPPORT
283 return_freelists(p -> gcj_freelists, GC_gcjobjfreelist);
287 extern GC_PTR GC_generic_malloc_many();
289 GC_PTR GC_local_malloc(size_t bytes)
291 if (EXPECT(!SMALL_ENOUGH(bytes),0)) {
292 return(GC_malloc(bytes));
294 int index = INDEX_FROM_BYTES(bytes);
297 GC_key_t k = GC_thread_key;
300 # if defined(REDIRECT_MALLOC) && !defined(USE_PTHREAD_SPECIFIC) \
301 || !defined(__GNUC__)
302 if (EXPECT(0 == k, 0)) {
303 /* This can happen if we get called when the world is */
304 /* being initialized. Whether we can actually complete */
305 /* the initialization then is unclear. */
310 tsd = GC_getspecific(GC_thread_key);
311 # ifdef GC_ASSERTIONS
313 GC_ASSERT(tsd == (void *)GC_lookup_thread(pthread_self()));
316 my_fl = ((GC_thread)tsd) -> normal_freelists + index;
318 if (EXPECT((word)my_entry >= HBLKSIZE, 1)) {
319 ptr_t next = obj_link(my_entry);
320 GC_PTR result = (GC_PTR)my_entry;
322 obj_link(my_entry) = 0;
323 PREFETCH_FOR_WRITE(next);
325 } else if ((word)my_entry - 1 < DIRECT_GRANULES) {
326 *my_fl = my_entry + index + 1;
327 return GC_malloc(bytes);
329 my_entry = GC_generic_malloc_many(BYTES_FROM_INDEX(index),
332 if (my_entry == 0) return GC_oom_fn(bytes);
333 return GC_local_malloc(bytes);
338 GC_PTR GC_local_malloc_atomic(size_t bytes)
340 if (EXPECT(!SMALL_ENOUGH(bytes), 0)) {
341 return(GC_malloc_atomic(bytes));
343 int index = INDEX_FROM_BYTES(bytes);
344 ptr_t * my_fl = ((GC_thread)GC_getspecific(GC_thread_key))
345 -> ptrfree_freelists + index;
346 ptr_t my_entry = *my_fl;
347 if (EXPECT((word)my_entry >= HBLKSIZE, 1)) {
348 GC_PTR result = (GC_PTR)my_entry;
349 *my_fl = obj_link(my_entry);
351 } else if ((word)my_entry - 1 < DIRECT_GRANULES) {
352 *my_fl = my_entry + index + 1;
353 return GC_malloc_atomic(bytes);
355 my_entry = GC_generic_malloc_many(BYTES_FROM_INDEX(index),
358 if (my_entry == 0) return GC_oom_fn(bytes);
359 return GC_local_malloc_atomic(bytes);
364 #ifdef GC_GCJ_SUPPORT
366 #include "include/gc_gcj.h"
369 extern GC_bool GC_gcj_malloc_initialized;
372 extern int GC_gcj_kind;
374 GC_PTR GC_local_gcj_malloc(size_t bytes,
375 void * ptr_to_struct_containing_descr)
377 GC_ASSERT(GC_gcj_malloc_initialized);
378 if (EXPECT(!SMALL_ENOUGH(bytes), 0)) {
379 return GC_gcj_malloc(bytes, ptr_to_struct_containing_descr);
381 int index = INDEX_FROM_BYTES(bytes);
382 ptr_t * my_fl = ((GC_thread)GC_getspecific(GC_thread_key))
383 -> gcj_freelists + index;
384 ptr_t my_entry = *my_fl;
385 if (EXPECT((word)my_entry >= HBLKSIZE, 1)) {
386 GC_PTR result = (GC_PTR)my_entry;
387 GC_ASSERT(!GC_incremental);
388 /* We assert that any concurrent marker will stop us. */
389 /* Thus it is impossible for a mark procedure to see the */
390 /* allocation of the next object, but to see this object */
391 /* still containing a free list pointer. Otherwise the */
392 /* marker might find a random "mark descriptor". */
393 *my_fl = obj_link(my_entry);
394 *(void **)result = ptr_to_struct_containing_descr;
396 } else if ((word)my_entry - 1 < DIRECT_GRANULES) {
397 *my_fl = my_entry + index + 1;
398 return GC_gcj_malloc(bytes, ptr_to_struct_containing_descr);
400 my_entry = GC_generic_malloc_many(BYTES_FROM_INDEX(index),
403 if (my_entry == 0) return GC_oom_fn(bytes);
404 return GC_gcj_malloc(bytes, ptr_to_struct_containing_descr);
409 #endif /* GC_GCJ_SUPPORT */
411 # else /* !THREAD_LOCAL_ALLOC && !DBG_HDRS_ALL */
413 # define GC_destroy_thread_local(t)
415 # endif /* !THREAD_LOCAL_ALLOC */
418 * The only way to suspend threads given the pthread interface is to send
419 * signals. We can't use SIGSTOP directly, because we need to get the
420 * thread to save its stack pointer in the GC thread table before
421 * suspending. So we have to reserve a signal of our own for this.
422 * This means we have to intercept client calls to change the signal mask.
423 * The linuxthreads package already uses SIGUSR1 and SIGUSR2,
424 * so we need to reuse something else. I chose SIGPWR.
425 * (Perhaps SIGUNUSED would be a better choice.)
428 # if defined(HPUX_THREADS) || defined(GC_OSF1_THREADS)
429 # define SIG_SUSPEND _SIGRTMIN + 6
431 # define SIG_SUSPEND SIGPWR
435 #ifndef SIG_THR_RESTART
436 # if defined(HPUX_THREADS) || defined(GC_OSF1_THREADS)
437 # define SIG_THR_RESTART _SIGRTMIN + 5
439 # define SIG_THR_RESTART SIGXCPU
443 /* SPARC/Linux doesn't properly define SIGPWR in <signal.h>.
444 * It is aliased to SIGLOST in asm/signal.h, though. */
445 #if defined(SPARC) && !defined(SIGPWR)
446 # define SIGPWR SIGLOST
449 sem_t GC_suspend_ack_sem;
451 #if !defined(HPUX_THREADS) && !defined(GC_OSF1_THREADS)
453 To make sure that we're using LinuxThreads and not some other thread
454 package, we generate a dummy reference to `pthread_kill_other_threads_np'
455 (was `__pthread_initial_thread_bos' but that disappeared),
456 which is a symbol defined in LinuxThreads, but (hopefully) not in other
459 void (*dummy_var_to_force_linux_threads)() = pthread_kill_other_threads_np;
460 #endif /* !HPUX_THREADS */
462 #if defined(SPARC) || defined(IA64)
463 extern word GC_save_regs_in_stack();
466 long GC_nprocs = 1; /* Number of processors. We may not have */
467 /* access to all of them, but this is as good */
468 /* a guess as any ... */
473 # define MAX_MARKERS 16
476 static ptr_t marker_sp[MAX_MARKERS] = {0};
478 void * GC_mark_thread(void * id)
482 marker_sp[(word)id] = GC_approx_sp();
483 for (;; ++my_mark_no) {
484 /* GC_mark_no is passed only to allow GC_help_marker to terminate */
485 /* promptly. This is important if it were called from the signal */
486 /* handler or from the GC lock acquisition code. Under Linux, it's */
487 /* not safe to call it from a signal handler, since it uses mutexes */
488 /* and condition variables. Since it is called only here, the */
489 /* argument is unnecessary. */
490 if (my_mark_no < GC_mark_no || my_mark_no > GC_mark_no + 2) {
491 /* resynchronize if we get far off, e.g. because GC_mark_no */
493 my_mark_no = GC_mark_no;
495 # ifdef DEBUG_THREADS
496 GC_printf1("Starting mark helper for mark number %ld\n", my_mark_no);
498 GC_help_marker(my_mark_no);
502 extern long GC_markers; /* Number of mark threads we would */
503 /* like to have. Includes the */
504 /* initiating thread. */
506 pthread_t GC_mark_threads[MAX_MARKERS];
508 #define PTHREAD_CREATE REAL_FUNC(pthread_create)
510 static void start_mark_threads()
515 if (GC_markers > MAX_MARKERS) {
516 WARN("Limiting number of mark threads\n", 0);
517 GC_markers = MAX_MARKERS;
519 if (0 != pthread_attr_init(&attr)) ABORT("pthread_attr_init failed");
521 if (0 != pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED))
522 ABORT("pthread_attr_setdetachstate failed");
524 if (GC_print_stats) {
525 GC_printf1("Starting %ld marker threads\n", GC_markers - 1);
528 for (i = 0; i < GC_markers - 1; ++i) {
529 if (0 != PTHREAD_CREATE(GC_mark_threads + i, &attr,
530 GC_mark_thread, (void *)(word)i)) {
531 WARN("Marker thread creation failed, errno = %ld.\n", errno);
536 #else /* !PARALLEL_MARK */
538 static __inline__ void start_mark_threads()
542 #endif /* !PARALLEL_MARK */
544 void GC_suspend_handler(int sig)
547 pthread_t my_thread = pthread_self();
553 # ifdef PARALLEL_MARK
554 word my_mark_no = GC_mark_no;
555 /* Marker can't proceed until we acknowledge. Thus this is */
556 /* guaranteed to be the mark_no correspending to our */
557 /* suspension, i.e. the marker can't have incremented it yet. */
560 if (sig != SIG_SUSPEND) ABORT("Bad signal in suspend_handler");
563 GC_printf1("Suspending 0x%x\n", my_thread);
566 me = GC_lookup_thread(my_thread);
567 /* The lookup here is safe, since I'm doing this on behalf */
568 /* of a thread which holds the allocation lock in order */
569 /* to stop the world. Thus concurrent modification of the */
570 /* data structure is impossible. */
572 me -> stack_ptr = (ptr_t)GC_save_regs_in_stack();
574 me -> stack_ptr = (ptr_t)(&dummy);
577 me -> backing_store_ptr = (ptr_t)GC_save_regs_in_stack();
580 /* Tell the thread that wants to stop the world that this */
581 /* thread has been stopped. Note that sem_post() is */
582 /* the only async-signal-safe primitive in LinuxThreads. */
583 sem_post(&GC_suspend_ack_sem);
585 /* Wait until that thread tells us to restart by sending */
586 /* this thread a SIG_THR_RESTART signal. */
587 /* SIG_THR_RESTART should be masked at this point. Thus there */
589 if (sigfillset(&mask) != 0) ABORT("sigfillset() failed");
590 if (sigdelset(&mask, SIG_THR_RESTART) != 0) ABORT("sigdelset() failed");
592 if (sigdelset(&mask, SIGINT) != 0) ABORT("sigdelset() failed");
593 if (sigdelset(&mask, SIGQUIT) != 0) ABORT("sigdelset() failed");
594 if (sigdelset(&mask, SIGTERM) != 0) ABORT("sigdelset() failed");
595 if (sigdelset(&mask, SIGABRT) != 0) ABORT("sigdelset() failed");
599 sigsuspend(&mask); /* Wait for signal */
600 } while (me->signal != SIG_THR_RESTART);
603 GC_printf1("Continuing 0x%x\n", my_thread);
607 void GC_restart_handler(int sig)
611 if (sig != SIG_THR_RESTART) ABORT("Bad signal in suspend_handler");
613 /* Let the GC_suspend_handler() know that we got a SIG_THR_RESTART. */
614 /* The lookup here is safe, since I'm doing this on behalf */
615 /* of a thread which holds the allocation lock in order */
616 /* to stop the world. Thus concurrent modification of the */
617 /* data structure is impossible. */
618 me = GC_lookup_thread(pthread_self());
619 me->signal = SIG_THR_RESTART;
622 ** Note: even if we didn't do anything useful here,
623 ** it would still be necessary to have a signal handler,
624 ** rather than ignoring the signals, otherwise
625 ** the signals will not be delivered at all, and
626 ** will thus not interrupt the sigsuspend() above.
630 GC_printf1("In GC_restart_handler for 0x%x\n", pthread_self());
634 /* Defining INSTALL_LOOPING_SEGV_HANDLER causes SIGSEGV and SIGBUS to */
635 /* result in an infinite loop in a signal handler. This can be very */
636 /* useful for debugging, since (as of RH7) gdb still seems to have */
637 /* serious problems with threads. */
638 #ifdef INSTALL_LOOPING_SEGV_HANDLER
639 void GC_looping_handler(int sig)
641 GC_printf3("Signal %ld in thread %lx, pid %ld\n",
642 sig, pthread_self(), getpid());
647 GC_bool GC_thr_initialized = FALSE;
649 # define THREAD_TABLE_SZ 128 /* Must be power of 2 */
650 volatile GC_thread GC_threads[THREAD_TABLE_SZ];
652 void GC_push_thread_structures GC_PROTO((void))
654 GC_push_all((ptr_t)(GC_threads), (ptr_t)(GC_threads)+sizeof(GC_threads));
657 /* Add a thread to GC_threads. We assume it wasn't already there. */
658 /* Caller holds allocation lock. */
659 GC_thread GC_new_thread(pthread_t id)
661 int hv = ((word)id) % THREAD_TABLE_SZ;
663 static struct GC_Thread_Rep first_thread;
664 static GC_bool first_thread_used = FALSE;
666 if (!first_thread_used) {
667 result = &first_thread;
668 first_thread_used = TRUE;
670 result = (struct GC_Thread_Rep *)
671 GC_INTERNAL_MALLOC(sizeof(struct GC_Thread_Rep), NORMAL);
673 if (result == 0) return(0);
675 result -> next = GC_threads[hv];
676 GC_threads[hv] = result;
677 GC_ASSERT(result -> flags == 0 && result -> thread_blocked == 0);
681 /* Delete a thread from GC_threads. We assume it is there. */
682 /* (The code intentionally traps if it wasn't.) */
683 /* Caller holds allocation lock. */
684 void GC_delete_thread(pthread_t id)
686 int hv = ((word)id) % THREAD_TABLE_SZ;
687 register GC_thread p = GC_threads[hv];
688 register GC_thread prev = 0;
690 while (!pthread_equal(p -> id, id)) {
695 GC_threads[hv] = p -> next;
697 prev -> next = p -> next;
702 /* If a thread has been joined, but we have not yet */
703 /* been notified, then there may be more than one thread */
704 /* in the table with the same pthread id. */
705 /* This is OK, but we need a way to delete a specific one. */
706 void GC_delete_gc_thread(pthread_t id, GC_thread gc_id)
708 int hv = ((word)id) % THREAD_TABLE_SZ;
709 register GC_thread p = GC_threads[hv];
710 register GC_thread prev = 0;
717 GC_threads[hv] = p -> next;
719 prev -> next = p -> next;
724 /* Return a GC_thread corresponding to a given thread_t. */
725 /* Returns 0 if it's not there. */
726 /* Caller holds allocation lock or otherwise inhibits */
728 /* If there is more than one thread with the given id we */
729 /* return the most recent one. */
730 GC_thread GC_lookup_thread(pthread_t id)
732 int hv = ((word)id) % THREAD_TABLE_SZ;
733 register GC_thread p = GC_threads[hv];
735 while (p != 0 && !pthread_equal(p -> id, id)) p = p -> next;
739 /* There seems to be a very rare thread stopping problem. To help us */
740 /* debug that, we save the ids of the stopping thread. */
741 pthread_t GC_stopping_thread;
744 /* Caller holds allocation lock. */
747 pthread_t my_thread = pthread_self();
749 register GC_thread p;
750 register int n_live_threads = 0;
753 GC_stopping_thread = my_thread; /* debugging only. */
754 GC_stopping_pid = getpid(); /* debugging only. */
756 /* Make sure all free list construction has stopped before we start. */
757 /* No new construction can start, since free list construction is */
758 /* required to acquire and release the GC lock before it starts, */
759 /* and we have the lock. */
760 # ifdef PARALLEL_MARK
761 GC_acquire_mark_lock();
762 GC_ASSERT(GC_fl_builder_count == 0);
763 /* We should have previously waited for it to become zero. */
764 # endif /* PARALLEL_MARK */
765 for (i = 0; i < THREAD_TABLE_SZ; i++) {
766 for (p = GC_threads[i]; p != 0; p = p -> next) {
767 if (p -> id != my_thread) {
768 if (p -> flags & FINISHED) continue;
769 if (p -> thread_blocked) /* Will wait */ continue;
772 GC_printf1("Sending suspend signal to 0x%x\n", p -> id);
774 result = pthread_kill(p -> id, SIG_SUSPEND);
777 /* Not really there anymore. Possible? */
783 ABORT("pthread_kill failed");
788 for (i = 0; i < n_live_threads; i++) {
789 if (0 != sem_wait(&GC_suspend_ack_sem))
790 ABORT("sem_wait in handler failed");
792 # ifdef PARALLEL_MARK
793 GC_release_mark_lock();
796 GC_printf1("World stopped 0x%x\n", pthread_self());
800 /* Caller holds allocation lock, and has held it continuously since */
801 /* the world stopped. */
802 void GC_start_world()
804 pthread_t my_thread = pthread_self();
806 register GC_thread p;
807 register int n_live_threads = 0;
811 GC_printf0("World starting\n");
814 for (i = 0; i < THREAD_TABLE_SZ; i++) {
815 for (p = GC_threads[i]; p != 0; p = p -> next) {
816 if (p -> id != my_thread) {
817 if (p -> flags & FINISHED) continue;
818 if (p -> thread_blocked) continue;
821 GC_printf1("Sending restart signal to 0x%x\n", p -> id);
823 result = pthread_kill(p -> id, SIG_THR_RESTART);
826 /* Not really there anymore. Possible? */
832 ABORT("pthread_kill failed");
838 GC_printf0("World started\n");
840 GC_stopping_thread = 0; /* debugging only */
844 # define IF_IA64(x) x
848 /* We hold allocation lock. Should do exactly the right thing if the */
849 /* world is stopped. Should not fail if it isn't. */
850 void GC_push_all_stacks()
854 ptr_t sp = GC_approx_sp();
856 /* On IA64, we also need to scan the register backing store. */
857 IF_IA64(ptr_t bs_lo; ptr_t bs_hi;)
858 pthread_t me = pthread_self();
860 if (!GC_thr_initialized) GC_thr_init();
862 GC_printf1("Pushing stacks from thread 0x%lx\n", (unsigned long) me);
864 for (i = 0; i < THREAD_TABLE_SZ; i++) {
865 for (p = GC_threads[i]; p != 0; p = p -> next) {
866 if (p -> flags & FINISHED) continue;
867 if (pthread_equal(p -> id, me)) {
869 lo = (ptr_t)GC_save_regs_in_stack();
873 IF_IA64(bs_hi = (ptr_t)GC_save_regs_in_stack();)
876 IF_IA64(bs_hi = p -> backing_store_ptr;)
878 if ((p -> flags & MAIN_THREAD) == 0) {
880 IF_IA64(bs_lo = p -> backing_store_end);
882 /* The original stack. */
884 IF_IA64(bs_lo = BACKING_STORE_BASE;)
887 GC_printf3("Stack for thread 0x%lx = [%lx,%lx)\n",
888 (unsigned long) p -> id,
889 (unsigned long) lo, (unsigned long) hi);
891 if (0 == lo) ABORT("GC_push_all_stacks: sp not set!\n");
892 # ifdef STACK_GROWS_UP
893 /* We got them backwards! */
894 GC_push_all_stack(hi, lo);
896 GC_push_all_stack(lo, hi);
899 if (pthread_equal(p -> id, me)) {
900 GC_push_all_eager(bs_lo, bs_hi);
902 GC_push_all_stack(bs_lo, bs_hi);
909 #ifdef USE_PROC_FOR_LIBRARIES
910 int GC_segment_is_thread_stack(ptr_t lo, ptr_t hi)
915 # ifdef PARALLEL_MARK
916 for (i = 0; i < GC_markers; ++i) {
917 if (marker_sp[i] > lo & marker_sp[i] < hi) return 1;
920 for (i = 0; i < THREAD_TABLE_SZ; i++) {
921 for (p = GC_threads[i]; p != 0; p = p -> next) {
922 if (0 != p -> stack_end) {
923 # ifdef STACK_GROWS_UP
924 if (p -> stack_end >= lo && p -> stack_end < hi) return 1;
925 # else /* STACK_GROWS_DOWN */
926 if (p -> stack_end > lo && p -> stack_end <= hi) return 1;
933 #endif /* USE_PROC_FOR_LIBRARIES */
936 /* Return the number of processors, or i<= 0 if it can't be determined. */
939 /* Should be "return sysconf(_SC_NPROCESSORS_ONLN);" but that */
940 /* appears to be buggy in many cases. */
941 /* We look for lines "cpu<n>" in /proc/stat. */
942 # define STAT_BUF_SIZE 4096
943 # if defined(GC_USE_LD_WRAP)
944 # define STAT_READ __real_read
946 # define STAT_READ read
948 char stat_buf[STAT_BUF_SIZE];
952 /* Some old kernels only have a single "cpu nnnn ..." */
953 /* entry in /proc/stat. We identify those as */
957 f = open("/proc/stat", O_RDONLY);
958 if (f < 0 || (len = STAT_READ(f, stat_buf, STAT_BUF_SIZE)) < 100) {
959 WARN("Couldn't read /proc/stat\n", 0);
962 for (i = 0; i < len - 100; ++i) {
963 if (stat_buf[i] == '\n' && stat_buf[i+1] == 'c'
964 && stat_buf[i+2] == 'p' && stat_buf[i+3] == 'u') {
965 int cpu_no = atoi(stat_buf + i + 4);
966 if (cpu_no >= result) result = cpu_no + 1;
971 #endif /* LINUX_THREADS */
973 /* We hold the allocation lock. */
978 struct sigaction act;
980 if (GC_thr_initialized) return;
981 GC_thr_initialized = TRUE;
983 if (sem_init(&GC_suspend_ack_sem, 0, 0) != 0)
984 ABORT("sem_init failed");
986 act.sa_flags = SA_RESTART;
987 if (sigfillset(&act.sa_mask) != 0) {
988 ABORT("sigfillset() failed");
991 if (sigdelset(&act.sa_mask, SIGINT) != 0
992 || sigdelset(&act.sa_mask, SIGQUIT != 0)
993 || sigdelset(&act.sa_mask, SIGABRT != 0)
994 || sigdelset(&act.sa_mask, SIGTERM != 0)) {
995 ABORT("sigdelset() failed");
999 /* SIG_THR_RESTART is unmasked by the handler when necessary. */
1000 act.sa_handler = GC_suspend_handler;
1001 if (sigaction(SIG_SUSPEND, &act, NULL) != 0) {
1002 ABORT("Cannot set SIG_SUSPEND handler");
1005 act.sa_handler = GC_restart_handler;
1006 if (sigaction(SIG_THR_RESTART, &act, NULL) != 0) {
1007 ABORT("Cannot set SIG_THR_RESTART handler");
1009 # ifdef INSTALL_LOOPING_SEGV_HANDLER
1010 act.sa_handler = GC_looping_handler;
1011 if (sigaction(SIGSEGV, &act, NULL) != 0
1012 || sigaction(SIGBUS, &act, NULL) != 0) {
1013 ABORT("Cannot set SIGSEGV or SIGBUS looping handler");
1015 # endif /* INSTALL_LOOPING_SEGV_HANDLER */
1017 /* Add the initial thread, so we can stop it. */
1018 t = GC_new_thread(pthread_self());
1019 t -> stack_ptr = (ptr_t)(&dummy);
1020 t -> flags = DETACHED | MAIN_THREAD;
1022 /* Set GC_nprocs. */
1024 char * nprocs_string = GETENV("GC_NPROCS");
1026 if (nprocs_string != NULL) GC_nprocs = atoi(nprocs_string);
1028 if (GC_nprocs <= 0) {
1029 # if defined(HPUX_THREADS)
1030 GC_nprocs = pthread_num_processors_np();
1032 # if defined(OSF1_THREADS)
1035 # ifdef LINUX_THREADS
1036 GC_nprocs = GC_get_nprocs();
1039 if (GC_nprocs <= 0) {
1040 WARN("GC_get_nprocs() returned %ld\n", GC_nprocs);
1042 # ifdef PARALLEL_MARK
1046 # ifdef PARALLEL_MARK
1047 GC_markers = GC_nprocs;
1050 # ifdef PARALLEL_MARK
1052 if (GC_print_stats) {
1053 GC_printf2("Number of processors = %ld, "
1054 "number of marker threads = %ld\n", GC_nprocs, GC_markers);
1057 if (GC_markers == 1) {
1058 GC_parallel = FALSE;
1060 if (GC_print_stats) {
1061 GC_printf0("Single marker thread, turning off parallel marking\n");
1071 /* Perform all initializations, including those that */
1072 /* may require allocation. */
1073 /* Called as constructor without allocation lock. */
1074 /* Must be called before a second thread is created. */
1077 if (fully_initialized) return;
1078 if (!GC_is_initialized) GC_init();
1079 /* If we are using a parallel marker, start the helper threads. */
1080 # ifdef PARALLEL_MARK
1081 if (GC_parallel) start_mark_threads();
1083 /* Initialize thread local free lists if used. */
1084 # if defined(THREAD_LOCAL_ALLOC) && !defined(DBG_HDRS_ALL)
1086 GC_init_thread_local(GC_lookup_thread(pthread_self()));
1089 fully_initialized = TRUE;
1093 int WRAP_FUNC(pthread_sigmask)(int how, const sigset_t *set, sigset_t *oset)
1095 sigset_t fudged_set;
1097 if (set != NULL && (how == SIG_BLOCK || how == SIG_SETMASK)) {
1099 sigdelset(&fudged_set, SIG_SUSPEND);
1102 return(REAL_FUNC(pthread_sigmask)(how, set, oset));
1105 /* Wrappers for functions that are likely to block for an appreciable */
1106 /* length of time. Must be called in pairs, if at all. */
1107 /* Nothing much beyond the system call itself should be executed */
1108 /* between these. */
1110 void GC_start_blocking(void) {
1111 # define SP_SLOP 128
1114 me = GC_lookup_thread(pthread_self());
1115 GC_ASSERT(!(me -> thread_blocked));
1117 me -> stack_ptr = (ptr_t)GC_save_regs_in_stack();
1119 me -> stack_ptr = (ptr_t)GC_approx_sp();
1122 me -> backing_store_ptr = (ptr_t)GC_save_regs_in_stack() + SP_SLOP;
1124 /* Add some slop to the stack pointer, since the wrapped call may */
1125 /* end up pushing more callee-save registers. */
1126 # ifdef STACK_GROWS_UP
1127 me -> stack_ptr += SP_SLOP;
1129 me -> stack_ptr -= SP_SLOP;
1131 me -> thread_blocked = TRUE;
1135 GC_end_blocking(void) {
1137 LOCK(); /* This will block if the world is stopped. */
1138 me = GC_lookup_thread(pthread_self());
1139 GC_ASSERT(me -> thread_blocked);
1140 me -> thread_blocked = FALSE;
1144 /* A wrapper for the standard C sleep function */
1145 int WRAP_FUNC(sleep) (unsigned int seconds)
1149 GC_start_blocking();
1150 result = REAL_FUNC(sleep)(seconds);
1156 void *(*start_routine)(void *);
1159 sem_t registered; /* 1 ==> in our thread table, but */
1160 /* parent hasn't yet noticed. */
1163 /* Called at thread exit. */
1164 /* Never called for main thread. That's OK, since it */
1165 /* results in at most a tiny one-time leak. And */
1166 /* linuxthreads doesn't reclaim the main threads */
1167 /* resources or id anyway. */
1168 void GC_thread_exit_proc(void *arg)
1173 me = GC_lookup_thread(pthread_self());
1174 GC_destroy_thread_local(me);
1175 if (me -> flags & DETACHED) {
1176 GC_delete_thread(pthread_self());
1178 me -> flags |= FINISHED;
1180 # if defined(THREAD_LOCAL_ALLOC) && !defined(USE_PTHREAD_SPECIFIC) \
1181 && !defined(USE_HPUX_TLS) && !defined(DBG_HDRS_ALL)
1182 GC_remove_specific(GC_thread_key);
1184 if (GC_incremental && GC_collection_in_progress()) {
1185 int old_gc_no = GC_gc_no;
1187 /* Make sure that no part of our stack is still on the mark stack, */
1188 /* since it's about to be unmapped. */
1189 while (GC_incremental && GC_collection_in_progress()
1190 && old_gc_no == GC_gc_no) {
1192 GC_collect_a_little_inner(1);
1202 int WRAP_FUNC(pthread_join)(pthread_t thread, void **retval)
1205 GC_thread thread_gc_id;
1208 thread_gc_id = GC_lookup_thread(thread);
1209 /* This is guaranteed to be the intended one, since the thread id */
1210 /* cant have been recycled by pthreads. */
1212 result = REAL_FUNC(pthread_join)(thread, retval);
1215 /* Here the pthread thread id may have been recycled. */
1216 GC_delete_gc_thread(thread, thread_gc_id);
1223 WRAP_FUNC(pthread_detach)(pthread_t thread)
1226 GC_thread thread_gc_id;
1229 thread_gc_id = GC_lookup_thread(thread);
1231 result = REAL_FUNC(pthread_detach)(thread);
1234 thread_gc_id -> flags |= DETACHED;
1235 /* Here the pthread thread id may have been recycled. */
1236 if (thread_gc_id -> flags & FINISHED) {
1237 GC_delete_gc_thread(thread, thread_gc_id);
1244 void * GC_start_routine(void * arg)
1247 struct start_info * si = arg;
1250 pthread_t my_pthread;
1251 void *(*start)(void *);
1254 my_pthread = pthread_self();
1255 # ifdef DEBUG_THREADS
1256 GC_printf1("Starting thread 0x%lx\n", my_pthread);
1257 GC_printf1("pid = %ld\n", (long) getpid());
1258 GC_printf1("sp = 0x%lx\n", (long) &arg);
1261 me = GC_new_thread(my_pthread);
1262 me -> flags = si -> flags;
1263 me -> stack_ptr = 0;
1264 /* me -> stack_end = GC_linux_stack_base(); -- currently (11/99) */
1265 /* doesn't work because the stack base in /proc/self/stat is the */
1266 /* one for the main thread. There is a strong argument that that's */
1267 /* a kernel bug, but a pervasive one. */
1268 # ifdef STACK_GROWS_DOWN
1269 me -> stack_end = (ptr_t)(((word)(&dummy) + (GC_page_size - 1))
1270 & ~(GC_page_size - 1));
1271 me -> stack_ptr = me -> stack_end - 0x10;
1272 /* Needs to be plausible, since an asynchronous stack mark */
1273 /* should not crash. */
1275 me -> stack_end = (ptr_t)((word)(&dummy) & ~(GC_page_size - 1));
1276 me -> stack_ptr = me -> stack_end + 0x10;
1278 /* This is dubious, since we may be more than a page into the stack, */
1279 /* and hence skip some of it, though it's not clear that matters. */
1281 me -> backing_store_end = (ptr_t)
1282 (GC_save_regs_in_stack() & ~(GC_page_size - 1));
1283 /* This is also < 100% convincing. We should also read this */
1284 /* from /proc, but the hook to do so isn't there yet. */
1287 start = si -> start_routine;
1288 # ifdef DEBUG_THREADS
1289 GC_printf1("start_routine = 0x%lx\n", start);
1291 start_arg = si -> arg;
1292 sem_post(&(si -> registered)); /* Last action on si. */
1293 /* OK to deallocate. */
1294 pthread_cleanup_push(GC_thread_exit_proc, 0);
1295 # if defined(THREAD_LOCAL_ALLOC) && !defined(DBG_HDRS_ALL)
1297 GC_init_thread_local(me);
1300 result = (*start)(start_arg);
1302 GC_printf1("Finishing thread 0x%x\n", pthread_self());
1304 me -> status = result;
1305 me -> flags |= FINISHED;
1306 pthread_cleanup_pop(1);
1307 /* Cleanup acquires lock, ensuring that we can't exit */
1308 /* while a collection that thinks we're alive is trying to stop */
1314 WRAP_FUNC(pthread_create)(pthread_t *new_thread,
1315 const pthread_attr_t *attr,
1316 void *(*start_routine)(void *), void *arg)
1320 pthread_t my_new_thread;
1323 struct start_info * si;
1324 /* This is otherwise saved only in an area mmapped by the thread */
1325 /* library, which isn't visible to the collector. */
1328 si = (struct start_info *)GC_INTERNAL_MALLOC(sizeof(struct start_info), NORMAL);
1330 if (!fully_initialized) GC_full_init();
1331 if (0 == si) return(ENOMEM);
1332 sem_init(&(si -> registered), 0, 0);
1333 si -> start_routine = start_routine;
1336 if (!GC_thr_initialized) GC_thr_init();
1338 detachstate = PTHREAD_CREATE_JOINABLE;
1340 pthread_attr_getdetachstate(attr, &detachstate);
1342 if (PTHREAD_CREATE_DETACHED == detachstate) my_flags |= DETACHED;
1343 si -> flags = my_flags;
1345 # ifdef DEBUG_THREADS
1346 GC_printf1("About to start new thread from thread 0x%X\n",
1349 result = REAL_FUNC(pthread_create)(new_thread, attr, GC_start_routine, si);
1350 # ifdef DEBUG_THREADS
1351 GC_printf1("Started thread 0x%X\n", *new_thread);
1353 /* Wait until child has been added to the thread table. */
1354 /* This also ensures that we hold onto si until the child is done */
1355 /* with it. Thus it doesn't matter whether it is otherwise */
1356 /* visible to the collector. */
1357 while (0 != sem_wait(&(si -> registered))) {
1358 if (EINTR != errno) ABORT("sem_wait failed");
1360 sem_destroy(&(si -> registered));
1362 GC_INTERNAL_FREE(si);
1367 #ifdef GENERIC_COMPARE_AND_SWAP
1368 pthread_mutex_t GC_compare_and_swap_lock = PTHREAD_MUTEX_INITIALIZER;
1370 GC_bool GC_compare_and_exchange(volatile GC_word *addr,
1371 GC_word old, GC_word new_val)
1374 pthread_mutex_lock(&GC_compare_and_swap_lock);
1381 pthread_mutex_unlock(&GC_compare_and_swap_lock);
1385 GC_word GC_atomic_add(volatile GC_word *addr, GC_word how_much)
1388 pthread_mutex_lock(&GC_compare_and_swap_lock);
1390 *addr = old + how_much;
1391 pthread_mutex_unlock(&GC_compare_and_swap_lock);
1395 #endif /* GENERIC_COMPARE_AND_SWAP */
1396 /* Spend a few cycles in a way that can't introduce contention with */
1397 /* othre threads. */
1401 volatile word dummy = 0;
1403 for (i = 0; i < 10; ++i) {
1405 __asm__ __volatile__ (" " : : : "memory");
1407 /* Something that's unlikely to be optimized away. */
1413 #define SPIN_MAX 1024 /* Maximum number of calls to GC_pause before */
1416 VOLATILE GC_bool GC_collecting = 0;
1417 /* A hint that we're in the collector and */
1418 /* holding the allocation lock for an */
1419 /* extended period. */
1421 #if !defined(USE_SPIN_LOCK) || defined(PARALLEL_MARK)
1422 /* If we don't want to use the below spinlock implementation, either */
1423 /* because we don't have a GC_test_and_set implementation, or because */
1424 /* we don't want to risk sleeping, we can still try spinning on */
1425 /* pthread_mutex_trylock for a while. This appears to be very */
1426 /* beneficial in many cases. */
1427 /* I suspect that under high contention this is nearly always better */
1428 /* than the spin lock. But it's a bit slower on a uniprocessor. */
1429 /* Hence we still default to the spin lock. */
1430 /* This is also used to acquire the mark lock for the parallel */
1433 /* Here we use a strict exponential backoff scheme. I don't know */
1434 /* whether that's better or worse than the above. We eventually */
1435 /* yield by calling pthread_mutex_lock(); it never makes sense to */
1436 /* explicitly sleep. */
1438 void GC_generic_lock(pthread_mutex_t * lock)
1440 unsigned pause_length = 1;
1443 if (0 == pthread_mutex_trylock(lock)) return;
1444 for (; pause_length <= SPIN_MAX; pause_length <<= 1) {
1445 for (i = 0; i < pause_length; ++i) {
1448 switch(pthread_mutex_trylock(lock)) {
1454 ABORT("Unexpected error from pthread_mutex_trylock");
1457 pthread_mutex_lock(lock);
1460 #endif /* !USE_SPIN_LOCK || PARALLEL_MARK */
1462 #if defined(USE_SPIN_LOCK)
1464 /* Reasonably fast spin locks. Basically the same implementation */
1465 /* as STL alloc.h. This isn't really the right way to do this. */
1466 /* but until the POSIX scheduling mess gets straightened out ... */
1468 volatile unsigned int GC_allocate_lock = 0;
1473 # define low_spin_max 30 /* spin cycles if we suspect uniprocessor */
1474 # define high_spin_max SPIN_MAX /* spin cycles for multiprocessor */
1475 static unsigned spin_max = low_spin_max;
1476 unsigned my_spin_max;
1477 static unsigned last_spins = 0;
1478 unsigned my_last_spins;
1481 if (!GC_test_and_set(&GC_allocate_lock)) {
1484 my_spin_max = spin_max;
1485 my_last_spins = last_spins;
1486 for (i = 0; i < my_spin_max; i++) {
1487 if (GC_collecting || GC_nprocs == 1) goto yield;
1488 if (i < my_last_spins/2 || GC_allocate_lock) {
1492 if (!GC_test_and_set(&GC_allocate_lock)) {
1495 * Spinning worked. Thus we're probably not being scheduled
1496 * against the other process with which we were contending.
1497 * Thus it makes sense to spin longer the next time.
1500 spin_max = high_spin_max;
1504 /* We are probably being scheduled against the other process. Sleep. */
1505 spin_max = low_spin_max;
1508 if (!GC_test_and_set(&GC_allocate_lock)) {
1511 # define SLEEP_THRESHOLD 12
1512 /* nanosleep(<= 2ms) just spins under Linux. We */
1513 /* want to be careful to avoid that behavior. */
1514 if (i < SLEEP_THRESHOLD) {
1520 /* Don't wait for more than about 15msecs, even */
1521 /* under extreme contention. */
1523 ts.tv_nsec = 1 << i;
1529 #else /* !USE_SPINLOCK */
1533 if (1 == GC_nprocs || GC_collecting) {
1534 pthread_mutex_lock(&GC_allocate_ml);
1536 GC_generic_lock(&GC_allocate_ml);
1540 #endif /* !USE_SPINLOCK */
1542 #ifdef PARALLEL_MARK
1544 #ifdef GC_ASSERTIONS
1545 pthread_t GC_mark_lock_holder = NO_THREAD;
1549 /* Ugly workaround for a linux threads bug in the final versions */
1550 /* of glibc2.1. Pthread_mutex_trylock sets the mutex owner */
1551 /* field even when it fails to acquire the mutex. This causes */
1552 /* pthread_cond_wait to die. Remove for glibc2.2. */
1553 /* According to the man page, we should use */
1554 /* PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP, but that isn't actually */
1556 static pthread_mutex_t mark_mutex =
1557 {0, 0, 0, PTHREAD_MUTEX_ERRORCHECK_NP, {0, 0}};
1559 static pthread_mutex_t mark_mutex = PTHREAD_MUTEX_INITIALIZER;
1562 static pthread_cond_t mark_cv = PTHREAD_COND_INITIALIZER;
1564 static pthread_cond_t builder_cv = PTHREAD_COND_INITIALIZER;
1566 void GC_acquire_mark_lock()
1569 if (pthread_mutex_lock(&mark_mutex) != 0) {
1570 ABORT("pthread_mutex_lock failed");
1573 GC_generic_lock(&mark_mutex);
1574 # ifdef GC_ASSERTIONS
1575 GC_mark_lock_holder = pthread_self();
1579 void GC_release_mark_lock()
1581 GC_ASSERT(GC_mark_lock_holder == pthread_self());
1582 # ifdef GC_ASSERTIONS
1583 GC_mark_lock_holder = NO_THREAD;
1585 if (pthread_mutex_unlock(&mark_mutex) != 0) {
1586 ABORT("pthread_mutex_unlock failed");
1590 void GC_wait_marker()
1592 GC_ASSERT(GC_mark_lock_holder == pthread_self());
1593 # ifdef GC_ASSERTIONS
1594 GC_mark_lock_holder = NO_THREAD;
1596 if (pthread_cond_wait(&mark_cv, &mark_mutex) != 0) {
1597 ABORT("pthread_cond_wait failed");
1599 GC_ASSERT(GC_mark_lock_holder == NO_THREAD);
1600 # ifdef GC_ASSERTIONS
1601 GC_mark_lock_holder = pthread_self();
1605 void GC_wait_builder()
1607 GC_ASSERT(GC_mark_lock_holder == pthread_self());
1608 # ifdef GC_ASSERTIONS
1609 GC_mark_lock_holder = NO_THREAD;
1611 if (pthread_cond_wait(&builder_cv, &mark_mutex) != 0) {
1612 ABORT("pthread_cond_wait failed");
1614 GC_ASSERT(GC_mark_lock_holder == NO_THREAD);
1615 # ifdef GC_ASSERTIONS
1616 GC_mark_lock_holder = pthread_self();
1620 void GC_notify_all_marker()
1622 if (pthread_cond_broadcast(&mark_cv) != 0) {
1623 ABORT("pthread_cond_broadcast failed");
1627 void GC_notify_all_builder()
1629 GC_ASSERT(GC_mark_lock_holder == pthread_self());
1630 if (pthread_cond_broadcast(&builder_cv) != 0) {
1631 ABORT("pthread_cond_broadcast failed");
1635 void GC_wait_for_reclaim()
1637 GC_acquire_mark_lock();
1638 while (GC_fl_builder_count > 0) {
1641 GC_release_mark_lock();
1643 #endif /* PARALLEL_MARK */
1645 # endif /* LINUX_THREADS */