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-2004 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.
29 /* DG/UX ix86 support <takis@xfree86.org> */
31 * Linux_threads.c now also includes some code to support HPUX and
32 * OSF1 (Compaq Tru64 Unix, really). The OSF1 support is based on Eric Benson's
35 * Eric also suggested an alternate basis for a lock implementation in
37 * + #elif defined(OSF1)
38 * + unsigned long GC_allocate_lock = 0;
39 * + msemaphore GC_allocate_semaphore;
40 * + # define GC_TRY_LOCK() \
41 * + ((msem_lock(&GC_allocate_semaphore, MSEM_IF_NOWAIT) == 0) \
42 * + ? (GC_allocate_lock = 1) \
44 * + # define GC_LOCK_TAKEN GC_allocate_lock
49 /*#define DEBUG_THREADS 1*/
50 /*#define GC_ASSERTIONS*/
52 # include "private/pthread_support.h"
54 # if defined(GC_PTHREADS) && !defined(GC_SOLARIS_THREADS) \
55 && !defined(GC_WIN32_THREADS)
57 # if defined(GC_HPUX_THREADS) && !defined(USE_PTHREAD_SPECIFIC) \
58 && !defined(USE_COMPILER_TLS)
60 # define USE_PTHREAD_SPECIFIC
61 /* Empirically, as of gcc 3.3, USE_COMPILER_TLS doesn't work. */
63 # define USE_COMPILER_TLS
67 # if defined USE_HPUX_TLS
68 --> Macro replaced by USE_COMPILER_TLS
71 # if (defined(GC_DGUX386_THREADS) || defined(GC_OSF1_THREADS) || \
72 defined(GC_DARWIN_THREADS) || defined(GC_AIX_THREADS) || \
73 defined(GC_NETBSD_THREADS)) \
74 && !defined(USE_PTHREAD_SPECIFIC)
75 # define USE_PTHREAD_SPECIFIC
78 # if defined(GC_DGUX386_THREADS) && !defined(_POSIX4A_DRAFT10_SOURCE)
79 # define _POSIX4A_DRAFT10_SOURCE 1
82 # if defined(GC_DGUX386_THREADS) && !defined(_USING_POSIX4A_DRAFT10)
83 # define _USING_POSIX4A_DRAFT10 1
86 # ifdef THREAD_LOCAL_ALLOC
87 # if !defined(USE_PTHREAD_SPECIFIC) && !defined(USE_COMPILER_TLS)
88 # include "private/specific.h"
90 # if defined(USE_PTHREAD_SPECIFIC)
91 # define GC_getspecific pthread_getspecific
92 # define GC_setspecific pthread_setspecific
93 # define GC_key_create pthread_key_create
94 typedef pthread_key_t GC_key_t;
96 # if defined(USE_COMPILER_TLS)
97 # define GC_getspecific(x) (x)
98 # define GC_setspecific(key, v) ((key) = (v), 0)
99 # define GC_key_create(key, d) 0
100 typedef void * GC_key_t;
104 # include <pthread.h>
109 # include <sys/mman.h>
110 # include <sys/time.h>
111 # include <sys/types.h>
112 # include <sys/stat.h>
116 #if defined(GC_DARWIN_THREADS)
117 # include "private/darwin_semaphore.h"
119 # include <semaphore.h>
120 #endif /* !GC_DARWIN_THREADS */
122 #if defined(GC_DARWIN_THREADS) || defined(GC_FREEBSD_THREADS)
123 # include <sys/sysctl.h>
124 #endif /* GC_DARWIN_THREADS */
126 #if defined(GC_NETBSD_THREADS)
127 # include <sys/param.h>
128 # include <sys/sysctl.h>
129 #endif /* GC_NETBSD_THREADS */
131 #if defined(GC_DGUX386_THREADS)
132 # include <sys/dg_sys_info.h>
133 # include <sys/_int_psem.h>
134 /* sem_t is an uint in DG/UX */
135 typedef unsigned int sem_t;
136 #endif /* GC_DGUX386_THREADS */
142 #ifdef GC_USE_LD_WRAP
143 # define WRAP_FUNC(f) __wrap_##f
144 # define REAL_FUNC(f) __real_##f
146 # define WRAP_FUNC(f) GC_##f
147 # if !defined(GC_DGUX386_THREADS)
148 # define REAL_FUNC(f) f
149 # else /* GC_DGUX386_THREADS */
150 # define REAL_FUNC(f) __d10_##f
151 # endif /* GC_DGUX386_THREADS */
152 # undef pthread_create
153 # if !defined(GC_DARWIN_THREADS)
154 # undef pthread_sigmask
157 # undef pthread_detach
158 # if defined(GC_OSF1_THREADS) && defined(_PTHREAD_USE_MANGLED_NAMES_) \
159 && !defined(_PTHREAD_USE_PTDNAM_)
160 /* Restore the original mangled names on Tru64 UNIX. */
161 # define pthread_create __pthread_create
162 # define pthread_join __pthread_join
163 # define pthread_detach __pthread_detach
169 static GC_bool parallel_initialized = FALSE;
171 void GC_init_parallel();
173 # if defined(THREAD_LOCAL_ALLOC) && !defined(DBG_HDRS_ALL)
175 /* We don't really support thread-local allocation with DBG_HDRS_ALL */
177 #ifdef USE_COMPILER_TLS
180 GC_key_t GC_thread_key;
182 static GC_bool keys_initialized;
184 /* Recover the contents of the freelist array fl into the global one gfl.*/
185 /* Note that the indexing scheme differs, in that gfl has finer size */
186 /* resolution, even if not all entries are used. */
187 /* We hold the allocator lock. */
188 static void return_freelists(ptr_t *fl, ptr_t *gfl)
194 for (i = 1; i < NFREELISTS; ++i) {
195 nwords = i * (GRANULARITY/sizeof(word));
198 if ((word)q >= HBLKSIZE) {
199 if (gfl[nwords] == 0) {
203 for (; (word)q >= HBLKSIZE; qptr = &(obj_link(q)), q = *qptr);
209 /* Clear fl[i], since the thread structure may hang around. */
210 /* Do it in a way that is likely to trap if we access it. */
211 fl[i] = (ptr_t)HBLKSIZE;
215 /* We statically allocate a single "size 0" object. It is linked to */
216 /* itself, and is thus repeatedly reused for all size 0 allocation */
217 /* requests. (Size 0 gcj allocation requests are incorrect, and */
218 /* we arrange for those to fault asap.) */
219 static ptr_t size_zero_object = (ptr_t)(&size_zero_object);
221 /* Each thread structure must be initialized. */
222 /* This call must be made from the new thread. */
223 /* Caller holds allocation lock. */
224 void GC_init_thread_local(GC_thread p)
228 if (!keys_initialized) {
229 if (0 != GC_key_create(&GC_thread_key, 0)) {
230 ABORT("Failed to create key for local allocator");
232 keys_initialized = TRUE;
234 if (0 != GC_setspecific(GC_thread_key, p)) {
235 ABORT("Failed to set thread specific allocation pointers");
237 for (i = 1; i < NFREELISTS; ++i) {
238 p -> ptrfree_freelists[i] = (ptr_t)1;
239 p -> normal_freelists[i] = (ptr_t)1;
240 # ifdef GC_GCJ_SUPPORT
241 p -> gcj_freelists[i] = (ptr_t)1;
244 /* Set up the size 0 free lists. */
245 p -> ptrfree_freelists[0] = (ptr_t)(&size_zero_object);
246 p -> normal_freelists[0] = (ptr_t)(&size_zero_object);
247 # ifdef GC_GCJ_SUPPORT
248 p -> gcj_freelists[0] = (ptr_t)(-1);
252 #ifdef GC_GCJ_SUPPORT
253 extern ptr_t * GC_gcjobjfreelist;
256 /* We hold the allocator lock. */
257 void GC_destroy_thread_local(GC_thread p)
259 /* We currently only do this from the thread itself or from */
260 /* the fork handler for a child process. */
262 GC_ASSERT(GC_getspecific(GC_thread_key) == (void *)p);
264 return_freelists(p -> ptrfree_freelists, GC_aobjfreelist);
265 return_freelists(p -> normal_freelists, GC_objfreelist);
266 # ifdef GC_GCJ_SUPPORT
267 return_freelists(p -> gcj_freelists, GC_gcjobjfreelist);
271 extern GC_PTR GC_generic_malloc_many();
273 GC_PTR GC_local_malloc(size_t bytes)
275 if (EXPECT(!SMALL_ENOUGH(bytes),0)) {
276 return(GC_malloc(bytes));
278 int index = INDEX_FROM_BYTES(bytes);
281 # if defined(REDIRECT_MALLOC) && !defined(USE_PTHREAD_SPECIFIC)
282 GC_key_t k = GC_thread_key;
286 # if defined(REDIRECT_MALLOC) && !defined(USE_PTHREAD_SPECIFIC)
287 if (EXPECT(0 == k, 0)) {
288 /* This can happen if we get called when the world is */
289 /* being initialized. Whether we can actually complete */
290 /* the initialization then is unclear. */
295 tsd = GC_getspecific(GC_thread_key);
296 # ifdef GC_ASSERTIONS
298 GC_ASSERT(tsd == (void *)GC_lookup_thread(pthread_self()));
301 my_fl = ((GC_thread)tsd) -> normal_freelists + index;
303 if (EXPECT((word)my_entry >= HBLKSIZE, 1)) {
304 ptr_t next = obj_link(my_entry);
305 GC_PTR result = (GC_PTR)my_entry;
307 obj_link(my_entry) = 0;
308 PREFETCH_FOR_WRITE(next);
310 } else if ((word)my_entry - 1 < DIRECT_GRANULES) {
311 *my_fl = my_entry + index + 1;
312 return GC_malloc(bytes);
314 GC_generic_malloc_many(BYTES_FROM_INDEX(index), NORMAL, my_fl);
315 if (*my_fl == 0) return GC_oom_fn(bytes);
316 return GC_local_malloc(bytes);
321 GC_PTR GC_local_malloc_atomic(size_t bytes)
323 if (EXPECT(!SMALL_ENOUGH(bytes), 0)) {
324 return(GC_malloc_atomic(bytes));
326 int index = INDEX_FROM_BYTES(bytes);
327 ptr_t * my_fl = ((GC_thread)GC_getspecific(GC_thread_key))
328 -> ptrfree_freelists + index;
329 ptr_t my_entry = *my_fl;
331 if (EXPECT((word)my_entry >= HBLKSIZE, 1)) {
332 GC_PTR result = (GC_PTR)my_entry;
333 *my_fl = obj_link(my_entry);
335 } else if ((word)my_entry - 1 < DIRECT_GRANULES) {
336 *my_fl = my_entry + index + 1;
337 return GC_malloc_atomic(bytes);
339 GC_generic_malloc_many(BYTES_FROM_INDEX(index), PTRFREE, my_fl);
340 /* *my_fl is updated while the collector is excluded; */
341 /* the free list is always visible to the collector as */
343 if (*my_fl == 0) return GC_oom_fn(bytes);
344 return GC_local_malloc_atomic(bytes);
349 #ifdef GC_GCJ_SUPPORT
351 #include "include/gc_gcj.h"
354 extern GC_bool GC_gcj_malloc_initialized;
357 extern int GC_gcj_kind;
359 GC_PTR GC_local_gcj_malloc(size_t bytes,
360 void * ptr_to_struct_containing_descr)
362 GC_ASSERT(GC_gcj_malloc_initialized);
363 if (EXPECT(!SMALL_ENOUGH(bytes), 0)) {
364 return GC_gcj_malloc(bytes, ptr_to_struct_containing_descr);
366 int index = INDEX_FROM_BYTES(bytes);
367 ptr_t * my_fl = ((GC_thread)GC_getspecific(GC_thread_key))
368 -> gcj_freelists + index;
369 ptr_t my_entry = *my_fl;
370 if (EXPECT((word)my_entry >= HBLKSIZE, 1)) {
371 GC_PTR result = (GC_PTR)my_entry;
372 GC_ASSERT(!GC_incremental);
373 /* We assert that any concurrent marker will stop us. */
374 /* Thus it is impossible for a mark procedure to see the */
375 /* allocation of the next object, but to see this object */
376 /* still containing a free list pointer. Otherwise the */
377 /* marker might find a random "mark descriptor". */
378 *(volatile ptr_t *)my_fl = obj_link(my_entry);
379 /* We must update the freelist before we store the pointer. */
380 /* Otherwise a GC at this point would see a corrupted */
382 /* A memory barrier is probably never needed, since the */
383 /* action of stopping this thread will cause prior writes */
385 GC_ASSERT(((void * volatile *)result)[1] == 0);
386 *(void * volatile *)result = ptr_to_struct_containing_descr;
388 } else if ((word)my_entry - 1 < DIRECT_GRANULES) {
389 if (!GC_incremental) *my_fl = my_entry + index + 1;
390 /* In the incremental case, we always have to take this */
391 /* path. Thus we leave the counter alone. */
392 return GC_gcj_malloc(bytes, ptr_to_struct_containing_descr);
394 GC_generic_malloc_many(BYTES_FROM_INDEX(index), GC_gcj_kind, my_fl);
395 if (*my_fl == 0) return GC_oom_fn(bytes);
396 return GC_local_gcj_malloc(bytes, ptr_to_struct_containing_descr);
401 #endif /* GC_GCJ_SUPPORT */
403 # else /* !THREAD_LOCAL_ALLOC && !DBG_HDRS_ALL */
405 # define GC_destroy_thread_local(t)
407 # endif /* !THREAD_LOCAL_ALLOC */
411 To make sure that we're using LinuxThreads and not some other thread
412 package, we generate a dummy reference to `pthread_kill_other_threads_np'
413 (was `__pthread_initial_thread_bos' but that disappeared),
414 which is a symbol defined in LinuxThreads, but (hopefully) not in other
417 We no longer do this, since this code is now portable enough that it might
418 actually work for something else.
420 void (*dummy_var_to_force_linux_threads)() = pthread_kill_other_threads_np;
423 long GC_nprocs = 1; /* Number of processors. We may not have */
424 /* access to all of them, but this is as good */
425 /* a guess as any ... */
430 # define MAX_MARKERS 16
433 static ptr_t marker_sp[MAX_MARKERS] = {0};
435 void * GC_mark_thread(void * id)
439 marker_sp[(word)id] = GC_approx_sp();
440 for (;; ++my_mark_no) {
441 /* GC_mark_no is passed only to allow GC_help_marker to terminate */
442 /* promptly. This is important if it were called from the signal */
443 /* handler or from the GC lock acquisition code. Under Linux, it's */
444 /* not safe to call it from a signal handler, since it uses mutexes */
445 /* and condition variables. Since it is called only here, the */
446 /* argument is unnecessary. */
447 if (my_mark_no < GC_mark_no || my_mark_no > GC_mark_no + 2) {
448 /* resynchronize if we get far off, e.g. because GC_mark_no */
450 my_mark_no = GC_mark_no;
452 # ifdef DEBUG_THREADS
453 GC_printf1("Starting mark helper for mark number %ld\n", my_mark_no);
455 GC_help_marker(my_mark_no);
459 extern long GC_markers; /* Number of mark threads we would */
460 /* like to have. Includes the */
461 /* initiating thread. */
463 pthread_t GC_mark_threads[MAX_MARKERS];
465 #define PTHREAD_CREATE REAL_FUNC(pthread_create)
467 static void start_mark_threads()
472 if (GC_markers > MAX_MARKERS) {
473 WARN("Limiting number of mark threads\n", 0);
474 GC_markers = MAX_MARKERS;
476 if (0 != pthread_attr_init(&attr)) ABORT("pthread_attr_init failed");
478 if (0 != pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED))
479 ABORT("pthread_attr_setdetachstate failed");
481 # if defined(HPUX) || defined(GC_DGUX386_THREADS)
482 /* Default stack size is usually too small: fix it. */
483 /* Otherwise marker threads or GC may run out of */
485 # define MIN_STACK_SIZE (8*HBLKSIZE*sizeof(word))
490 if (pthread_attr_getstacksize(&attr, &old_size) != 0)
491 ABORT("pthread_attr_getstacksize failed\n");
492 if (old_size < MIN_STACK_SIZE) {
493 if (pthread_attr_setstacksize(&attr, MIN_STACK_SIZE) != 0)
494 ABORT("pthread_attr_setstacksize failed\n");
497 # endif /* HPUX || GC_DGUX386_THREADS */
499 if (GC_print_stats) {
500 GC_printf1("Starting %ld marker threads\n", GC_markers - 1);
503 for (i = 0; i < GC_markers - 1; ++i) {
504 if (0 != PTHREAD_CREATE(GC_mark_threads + i, &attr,
505 GC_mark_thread, (void *)(word)i)) {
506 WARN("Marker thread creation failed, errno = %ld.\n", errno);
511 #else /* !PARALLEL_MARK */
513 static __inline__ void start_mark_threads()
517 #endif /* !PARALLEL_MARK */
519 GC_bool GC_thr_initialized = FALSE;
521 volatile GC_thread GC_threads[THREAD_TABLE_SZ];
523 void GC_push_thread_structures GC_PROTO((void))
525 GC_push_all((ptr_t)(GC_threads), (ptr_t)(GC_threads)+sizeof(GC_threads));
526 # if defined(THREAD_LOCAL_ALLOC) && !defined(DBG_HDRS_ALL)
527 GC_push_all((ptr_t)(&GC_thread_key),
528 (ptr_t)(&GC_thread_key)+sizeof(&GC_thread_key));
532 #ifdef THREAD_LOCAL_ALLOC
533 /* We must explicitly mark ptrfree and gcj free lists, since the free */
534 /* list links wouldn't otherwise be found. We also set them in the */
535 /* normal free lists, since that involves touching less memory than if */
536 /* we scanned them normally. */
537 void GC_mark_thread_local_free_lists(void)
543 for (i = 0; i < THREAD_TABLE_SZ; ++i) {
544 for (p = GC_threads[i]; 0 != p; p = p -> next) {
545 for (j = 1; j < NFREELISTS; ++j) {
546 q = p -> ptrfree_freelists[j];
547 if ((word)q > HBLKSIZE) GC_set_fl_marks(q);
548 q = p -> normal_freelists[j];
549 if ((word)q > HBLKSIZE) GC_set_fl_marks(q);
550 # ifdef GC_GCJ_SUPPORT
551 q = p -> gcj_freelists[j];
552 if ((word)q > HBLKSIZE) GC_set_fl_marks(q);
553 # endif /* GC_GCJ_SUPPORT */
558 #endif /* THREAD_LOCAL_ALLOC */
560 static struct GC_Thread_Rep first_thread;
562 /* Add a thread to GC_threads. We assume it wasn't already there. */
563 /* Caller holds allocation lock. */
564 GC_thread GC_new_thread(pthread_t id)
566 int hv = ((word)id) % THREAD_TABLE_SZ;
568 static GC_bool first_thread_used = FALSE;
570 if (!first_thread_used) {
571 result = &first_thread;
572 first_thread_used = TRUE;
574 result = (struct GC_Thread_Rep *)
575 GC_INTERNAL_MALLOC(sizeof(struct GC_Thread_Rep), NORMAL);
577 if (result == 0) return(0);
579 result -> next = GC_threads[hv];
580 GC_threads[hv] = result;
581 GC_ASSERT(result -> flags == 0 && result -> thread_blocked == 0);
585 /* Delete a thread from GC_threads. We assume it is there. */
586 /* (The code intentionally traps if it wasn't.) */
587 /* Caller holds allocation lock. */
588 void GC_delete_thread(pthread_t id)
590 int hv = ((word)id) % THREAD_TABLE_SZ;
591 register GC_thread p = GC_threads[hv];
592 register GC_thread prev = 0;
594 while (!pthread_equal(p -> id, id)) {
599 GC_threads[hv] = p -> next;
601 prev -> next = p -> next;
604 #ifdef GC_DARWIN_THREADS
605 mach_port_deallocate(mach_task_self(), p->stop_info.mach_thread);
611 /* If a thread has been joined, but we have not yet */
612 /* been notified, then there may be more than one thread */
613 /* in the table with the same pthread id. */
614 /* This is OK, but we need a way to delete a specific one. */
615 void GC_delete_gc_thread(pthread_t id, GC_thread gc_id)
617 int hv = ((word)id) % THREAD_TABLE_SZ;
618 register GC_thread p = GC_threads[hv];
619 register GC_thread prev = 0;
626 GC_threads[hv] = p -> next;
628 prev -> next = p -> next;
631 #ifdef GC_DARWIN_THREADS
632 mach_port_deallocate(mach_task_self(), p->stop_info.mach_thread);
638 /* Return a GC_thread corresponding to a given pthread_t. */
639 /* Returns 0 if it's not there. */
640 /* Caller holds allocation lock or otherwise inhibits */
642 /* If there is more than one thread with the given id we */
643 /* return the most recent one. */
644 GC_thread GC_lookup_thread(pthread_t id)
646 int hv = ((word)id) % THREAD_TABLE_SZ;
647 register GC_thread p = GC_threads[hv];
649 while (p != 0 && !pthread_equal(p -> id, id)) p = p -> next;
654 /* Remove all entries from the GC_threads table, except the */
655 /* one for the current thread. We need to do this in the child */
656 /* process after a fork(), since only the current thread */
657 /* survives in the child. */
658 void GC_remove_all_threads_but_me(void)
660 pthread_t self = pthread_self();
662 GC_thread p, next, me;
664 for (hv = 0; hv < THREAD_TABLE_SZ; ++hv) {
666 for (p = GC_threads[hv]; 0 != p; p = next) {
668 if (p -> id == self) {
672 # ifdef THREAD_LOCAL_ALLOC
673 if (!(p -> flags & FINISHED)) {
674 GC_destroy_thread_local(p);
676 # endif /* THREAD_LOCAL_ALLOC */
677 if (p != &first_thread) GC_INTERNAL_FREE(p);
683 #endif /* HANDLE_FORK */
685 #ifdef USE_PROC_FOR_LIBRARIES
686 int GC_segment_is_thread_stack(ptr_t lo, ptr_t hi)
691 # ifdef PARALLEL_MARK
692 for (i = 0; i < GC_markers; ++i) {
693 if (marker_sp[i] > lo & marker_sp[i] < hi) return 1;
696 for (i = 0; i < THREAD_TABLE_SZ; i++) {
697 for (p = GC_threads[i]; p != 0; p = p -> next) {
698 if (0 != p -> stack_end) {
699 # ifdef STACK_GROWS_UP
700 if (p -> stack_end >= lo && p -> stack_end < hi) return 1;
701 # else /* STACK_GROWS_DOWN */
702 if (p -> stack_end > lo && p -> stack_end <= hi) return 1;
709 #endif /* USE_PROC_FOR_LIBRARIES */
711 #ifdef GC_LINUX_THREADS
712 /* Return the number of processors, or i<= 0 if it can't be determined. */
715 /* Should be "return sysconf(_SC_NPROCESSORS_ONLN);" but that */
716 /* appears to be buggy in many cases. */
717 /* We look for lines "cpu<n>" in /proc/stat. */
718 # define STAT_BUF_SIZE 4096
719 # define STAT_READ read
720 /* If read is wrapped, this may need to be redefined to call */
722 char stat_buf[STAT_BUF_SIZE];
725 /* Some old kernels only have a single "cpu nnnn ..." */
726 /* entry in /proc/stat. We identify those as */
730 f = open("/proc/stat", O_RDONLY);
731 if (f < 0 || (len = STAT_READ(f, stat_buf, STAT_BUF_SIZE)) < 100) {
732 WARN("Couldn't read /proc/stat\n", 0);
735 for (i = 0; i < len - 100; ++i) {
736 if (stat_buf[i] == '\n' && stat_buf[i+1] == 'c'
737 && stat_buf[i+2] == 'p' && stat_buf[i+3] == 'u') {
738 int cpu_no = atoi(stat_buf + i + 4);
739 if (cpu_no >= result) result = cpu_no + 1;
745 #endif /* GC_LINUX_THREADS */
747 /* We hold the GC lock. Wait until an in-progress GC has finished. */
748 /* Repeatedly RELEASES GC LOCK in order to wait. */
749 /* If wait_for_all is true, then we exit with the GC lock held and no */
750 /* collection in progress; otherwise we just wait for the current GC */
752 extern GC_bool GC_collection_in_progress();
753 void GC_wait_for_gc_completion(GC_bool wait_for_all)
755 if (GC_incremental && GC_collection_in_progress()) {
756 int old_gc_no = GC_gc_no;
758 /* Make sure that no part of our stack is still on the mark stack, */
759 /* since it's about to be unmapped. */
760 while (GC_incremental && GC_collection_in_progress()
761 && (wait_for_all || old_gc_no == GC_gc_no)) {
763 GC_in_thread_creation = TRUE;
764 GC_collect_a_little_inner(1);
765 GC_in_thread_creation = FALSE;
775 /* Procedures called before and after a fork. The goal here is to make */
776 /* it safe to call GC_malloc() in a forked child. It's unclear that is */
777 /* attainable, since the single UNIX spec seems to imply that one */
778 /* should only call async-signal-safe functions, and we probably can't */
779 /* quite guarantee that. But we give it our best shot. (That same */
780 /* spec also implies that it's not safe to call the system malloc */
781 /* between fork() and exec(). Thus we're doing no worse than it. */
783 /* Called before a fork() */
784 void GC_fork_prepare_proc(void)
786 /* Acquire all relevant locks, so that after releasing the locks */
787 /* the child will see a consistent state in which monitor */
788 /* invariants hold. Unfortunately, we can't acquire libc locks */
789 /* we might need, and there seems to be no guarantee that libc */
790 /* must install a suitable fork handler. */
791 /* Wait for an ongoing GC to finish, since we can't finish it in */
792 /* the (one remaining thread in) the child. */
794 # if defined(PARALLEL_MARK) || defined(THREAD_LOCAL_ALLOC)
795 GC_wait_for_reclaim();
797 GC_wait_for_gc_completion(TRUE);
798 # if defined(PARALLEL_MARK) || defined(THREAD_LOCAL_ALLOC)
799 GC_acquire_mark_lock();
803 /* Called in parent after a fork() */
804 void GC_fork_parent_proc(void)
806 # if defined(PARALLEL_MARK) || defined(THREAD_LOCAL_ALLOC)
807 GC_release_mark_lock();
812 /* Called in child after a fork() */
813 void GC_fork_child_proc(void)
815 /* Clean up the thread table, so that just our thread is left. */
816 # if defined(PARALLEL_MARK) || defined(THREAD_LOCAL_ALLOC)
817 GC_release_mark_lock();
819 GC_remove_all_threads_but_me();
820 # ifdef PARALLEL_MARK
821 /* Turn off parallel marking in the child, since we are probably */
822 /* just going to exec, and we would have to restart mark threads. */
825 # endif /* PARALLEL_MARK */
828 #endif /* HANDLE_FORK */
830 #if defined(GC_DGUX386_THREADS)
831 /* Return the number of processors, or i<= 0 if it can't be determined. */
834 /* <takis@XFree86.Org> */
836 struct dg_sys_info_pm_info pm_sysinfo;
839 status = dg_sys_info((long int *) &pm_sysinfo,
840 DG_SYS_INFO_PM_INFO_TYPE, DG_SYS_INFO_PM_CURRENT_VERSION);
842 /* set -1 for error */
846 numCpus = pm_sysinfo.idle_vp_count;
848 # ifdef DEBUG_THREADS
849 GC_printf1("Number of active CPUs in this system: %d\n", numCpus);
853 #endif /* GC_DGUX386_THREADS */
855 #if defined(GC_NETBSD_THREADS)
856 static int get_ncpu(void)
858 int mib[] = {CTL_HW,HW_NCPU};
860 size_t len = sizeof(res);
862 sysctl(mib, sizeof(mib)/sizeof(int), &res, &len, NULL, 0);
865 #endif /* GC_NETBSD_THREADS */
867 /* We hold the allocation lock. */
870 # ifndef GC_DARWIN_THREADS
875 if (GC_thr_initialized) return;
876 GC_thr_initialized = TRUE;
879 /* Prepare for a possible fork. */
880 pthread_atfork(GC_fork_prepare_proc, GC_fork_parent_proc,
882 # endif /* HANDLE_FORK */
883 /* Add the initial thread, so we can stop it. */
884 t = GC_new_thread(pthread_self());
885 # ifdef GC_DARWIN_THREADS
886 t -> stop_info.mach_thread = mach_thread_self();
888 t -> stop_info.stack_ptr = (ptr_t)(&dummy);
890 t -> flags = DETACHED | MAIN_THREAD;
896 char * nprocs_string = GETENV("GC_NPROCS");
898 if (nprocs_string != NULL) GC_nprocs = atoi(nprocs_string);
900 if (GC_nprocs <= 0) {
901 # if defined(GC_HPUX_THREADS)
902 GC_nprocs = pthread_num_processors_np();
904 # if defined(GC_OSF1_THREADS) || defined(GC_AIX_THREADS)
905 GC_nprocs = sysconf(_SC_NPROCESSORS_ONLN);
906 if (GC_nprocs <= 0) GC_nprocs = 1;
908 # if defined(GC_IRIX_THREADS)
909 GC_nprocs = sysconf(_SC_NPROC_ONLN);
910 if (GC_nprocs <= 0) GC_nprocs = 1;
912 # if defined(GC_NETBSD_THREADS)
913 GC_nprocs = get_ncpu();
915 # if defined(GC_DARWIN_THREADS) || defined(GC_FREEBSD_THREADS)
917 size_t len = sizeof(ncpus);
918 sysctl((int[2]) {CTL_HW, HW_NCPU}, 2, &ncpus, &len, NULL, 0);
921 # if defined(GC_LINUX_THREADS) || defined(GC_DGUX386_THREADS)
922 GC_nprocs = GC_get_nprocs();
925 if (GC_nprocs <= 0) {
926 WARN("GC_get_nprocs() returned %ld\n", GC_nprocs);
928 # ifdef PARALLEL_MARK
932 # ifdef PARALLEL_MARK
934 char * markers_string = GETENV("GC_MARKERS");
935 if (markers_string != NULL) {
936 GC_markers = atoi(markers_string);
938 GC_markers = GC_nprocs;
943 # ifdef PARALLEL_MARK
945 if (GC_print_stats) {
946 GC_printf2("Number of processors = %ld, "
947 "number of marker threads = %ld\n", GC_nprocs, GC_markers);
950 if (GC_markers == 1) {
953 if (GC_print_stats) {
954 GC_printf0("Single marker thread, turning off parallel marking\n");
959 /* Disable true incremental collection, but generational is OK. */
960 GC_time_limit = GC_TIME_UNLIMITED;
962 /* If we are using a parallel marker, actually start helper threads. */
963 if (GC_parallel) start_mark_threads();
968 /* Perform all initializations, including those that */
969 /* may require allocation. */
970 /* Called without allocation lock. */
971 /* Must be called before a second thread is created. */
972 /* Called without allocation lock. */
973 void GC_init_parallel()
975 if (parallel_initialized) return;
976 parallel_initialized = TRUE;
978 /* GC_init() calls us back, so set flag first. */
979 if (!GC_is_initialized) GC_init();
980 /* Initialize thread local free lists if used. */
981 # if defined(THREAD_LOCAL_ALLOC) && !defined(DBG_HDRS_ALL)
983 GC_init_thread_local(GC_lookup_thread(pthread_self()));
989 #if !defined(GC_DARWIN_THREADS)
990 int WRAP_FUNC(pthread_sigmask)(int how, const sigset_t *set, sigset_t *oset)
994 if (set != NULL && (how == SIG_BLOCK || how == SIG_SETMASK)) {
996 sigdelset(&fudged_set, SIG_SUSPEND);
999 return(REAL_FUNC(pthread_sigmask)(how, set, oset));
1001 #endif /* !GC_DARWIN_THREADS */
1003 /* Wrappers for functions that are likely to block for an appreciable */
1004 /* length of time. Must be called in pairs, if at all. */
1005 /* Nothing much beyond the system call itself should be executed */
1006 /* between these. */
1008 void GC_start_blocking(void) {
1009 # define SP_SLOP 128
1012 me = GC_lookup_thread(pthread_self());
1013 GC_ASSERT(!(me -> thread_blocked));
1015 me -> stop_info.stack_ptr = (ptr_t)GC_save_regs_in_stack();
1017 # ifndef GC_DARWIN_THREADS
1018 me -> stop_info.stack_ptr = (ptr_t)GC_approx_sp();
1022 me -> backing_store_ptr = (ptr_t)GC_save_regs_in_stack() + SP_SLOP;
1024 /* Add some slop to the stack pointer, since the wrapped call may */
1025 /* end up pushing more callee-save registers. */
1026 # ifndef GC_DARWIN_THREADS
1027 # ifdef STACK_GROWS_UP
1028 me -> stop_info.stack_ptr += SP_SLOP;
1030 me -> stop_info.stack_ptr -= SP_SLOP;
1033 me -> thread_blocked = TRUE;
1037 void GC_end_blocking(void) {
1039 LOCK(); /* This will block if the world is stopped. */
1040 me = GC_lookup_thread(pthread_self());
1041 GC_ASSERT(me -> thread_blocked);
1042 me -> thread_blocked = FALSE;
1046 #if defined(GC_DGUX386_THREADS)
1047 #define __d10_sleep sleep
1048 #endif /* GC_DGUX386_THREADS */
1050 /* A wrapper for the standard C sleep function */
1051 int WRAP_FUNC(sleep) (unsigned int seconds)
1055 GC_start_blocking();
1056 result = REAL_FUNC(sleep)(seconds);
1062 void *(*start_routine)(void *);
1065 sem_t registered; /* 1 ==> in our thread table, but */
1066 /* parent hasn't yet noticed. */
1069 /* Called at thread exit. */
1070 /* Never called for main thread. That's OK, since it */
1071 /* results in at most a tiny one-time leak. And */
1072 /* linuxthreads doesn't reclaim the main threads */
1073 /* resources or id anyway. */
1074 void GC_thread_exit_proc(void *arg)
1079 me = GC_lookup_thread(pthread_self());
1080 GC_destroy_thread_local(me);
1081 if (me -> flags & DETACHED) {
1082 GC_delete_thread(pthread_self());
1084 me -> flags |= FINISHED;
1086 # if defined(THREAD_LOCAL_ALLOC) && !defined(USE_PTHREAD_SPECIFIC) \
1087 && !defined(USE_COMPILER_TLS) && !defined(DBG_HDRS_ALL)
1088 GC_remove_specific(GC_thread_key);
1090 /* The following may run the GC from "nonexistent" thread. */
1091 GC_wait_for_gc_completion(FALSE);
1095 int WRAP_FUNC(pthread_join)(pthread_t thread, void **retval)
1098 GC_thread thread_gc_id;
1101 thread_gc_id = GC_lookup_thread(thread);
1102 /* This is guaranteed to be the intended one, since the thread id */
1103 /* cant have been recycled by pthreads. */
1105 result = REAL_FUNC(pthread_join)(thread, retval);
1106 # if defined (GC_FREEBSD_THREADS)
1107 /* On FreeBSD, the wrapped pthread_join() sometimes returns (what
1108 appears to be) a spurious EINTR which caused the test and real code
1109 to gratuitously fail. Having looked at system pthread library source
1110 code, I see how this return code may be generated. In one path of
1111 code, pthread_join() just returns the errno setting of the thread
1112 being joined. This does not match the POSIX specification or the
1113 local man pages thus I have taken the liberty to catch this one
1114 spurious return value properly conditionalized on GC_FREEBSD_THREADS. */
1115 if (result == EINTR) result = 0;
1119 /* Here the pthread thread id may have been recycled. */
1120 GC_delete_gc_thread(thread, thread_gc_id);
1127 WRAP_FUNC(pthread_detach)(pthread_t thread)
1130 GC_thread thread_gc_id;
1133 thread_gc_id = GC_lookup_thread(thread);
1135 result = REAL_FUNC(pthread_detach)(thread);
1138 thread_gc_id -> flags |= DETACHED;
1139 /* Here the pthread thread id may have been recycled. */
1140 if (thread_gc_id -> flags & FINISHED) {
1141 GC_delete_gc_thread(thread, thread_gc_id);
1148 GC_bool GC_in_thread_creation = FALSE;
1150 void * GC_start_routine(void * arg)
1153 struct start_info * si = arg;
1156 pthread_t my_pthread;
1157 void *(*start)(void *);
1160 my_pthread = pthread_self();
1161 # ifdef DEBUG_THREADS
1162 GC_printf1("Starting thread 0x%lx\n", my_pthread);
1163 GC_printf1("pid = %ld\n", (long) getpid());
1164 GC_printf1("sp = 0x%lx\n", (long) &arg);
1167 GC_in_thread_creation = TRUE;
1168 me = GC_new_thread(my_pthread);
1169 GC_in_thread_creation = FALSE;
1170 #ifdef GC_DARWIN_THREADS
1171 me -> stop_info.mach_thread = mach_thread_self();
1173 me -> stop_info.stack_ptr = 0;
1175 me -> flags = si -> flags;
1176 /* me -> stack_end = GC_linux_stack_base(); -- currently (11/99) */
1177 /* doesn't work because the stack base in /proc/self/stat is the */
1178 /* one for the main thread. There is a strong argument that that's */
1179 /* a kernel bug, but a pervasive one. */
1180 # ifdef STACK_GROWS_DOWN
1181 me -> stack_end = (ptr_t)(((word)(&dummy) + (GC_page_size - 1))
1182 & ~(GC_page_size - 1));
1183 # ifndef GC_DARWIN_THREADS
1184 me -> stop_info.stack_ptr = me -> stack_end - 0x10;
1186 /* Needs to be plausible, since an asynchronous stack mark */
1187 /* should not crash. */
1189 me -> stack_end = (ptr_t)((word)(&dummy) & ~(GC_page_size - 1));
1190 me -> stop_info.stack_ptr = me -> stack_end + 0x10;
1192 /* This is dubious, since we may be more than a page into the stack, */
1193 /* and hence skip some of it, though it's not clear that matters. */
1195 me -> backing_store_end = (ptr_t)
1196 (GC_save_regs_in_stack() & ~(GC_page_size - 1));
1197 /* This is also < 100% convincing. We should also read this */
1198 /* from /proc, but the hook to do so isn't there yet. */
1201 start = si -> start_routine;
1202 # ifdef DEBUG_THREADS
1203 GC_printf1("start_routine = 0x%lx\n", start);
1205 start_arg = si -> arg;
1206 sem_post(&(si -> registered)); /* Last action on si. */
1207 /* OK to deallocate. */
1208 pthread_cleanup_push(GC_thread_exit_proc, 0);
1209 # if defined(THREAD_LOCAL_ALLOC) && !defined(DBG_HDRS_ALL)
1211 GC_init_thread_local(me);
1214 result = (*start)(start_arg);
1216 GC_printf1("Finishing thread 0x%x\n", pthread_self());
1218 me -> status = result;
1219 pthread_cleanup_pop(1);
1220 /* Cleanup acquires lock, ensuring that we can't exit */
1221 /* while a collection that thinks we're alive is trying to stop */
1227 WRAP_FUNC(pthread_create)(pthread_t *new_thread,
1228 const pthread_attr_t *attr,
1229 void *(*start_routine)(void *), void *arg)
1234 struct start_info * si;
1235 /* This is otherwise saved only in an area mmapped by the thread */
1236 /* library, which isn't visible to the collector. */
1238 /* We resist the temptation to muck with the stack size here, */
1239 /* even if the default is unreasonably small. That's the client's */
1240 /* responsibility. */
1243 si = (struct start_info *)GC_INTERNAL_MALLOC(sizeof(struct start_info),
1246 if (!parallel_initialized) GC_init_parallel();
1247 if (0 == si) return(ENOMEM);
1248 sem_init(&(si -> registered), 0, 0);
1249 si -> start_routine = start_routine;
1252 if (!GC_thr_initialized) GC_thr_init();
1253 # ifdef GC_ASSERTIONS
1257 pthread_attr_t my_attr;
1258 pthread_attr_init(&my_attr);
1259 pthread_attr_getstacksize(&my_attr, &stack_size);
1261 pthread_attr_getstacksize(attr, &stack_size);
1263 # ifdef PARALLEL_MARK
1264 GC_ASSERT(stack_size >= (8*HBLKSIZE*sizeof(word)));
1266 /* FreeBSD-5.3/Alpha: default pthread stack is 64K, */
1267 /* HBLKSIZE=8192, sizeof(word)=8 */
1268 GC_ASSERT(stack_size >= 65536);
1270 /* Our threads may need to do some work for the GC. */
1271 /* Ridiculously small threads won't work, and they */
1272 /* probably wouldn't work anyway. */
1276 detachstate = PTHREAD_CREATE_JOINABLE;
1278 pthread_attr_getdetachstate(attr, &detachstate);
1280 if (PTHREAD_CREATE_DETACHED == detachstate) my_flags |= DETACHED;
1281 si -> flags = my_flags;
1283 # ifdef DEBUG_THREADS
1284 GC_printf1("About to start new thread from thread 0x%X\n",
1288 result = REAL_FUNC(pthread_create)(new_thread, attr, GC_start_routine, si);
1290 # ifdef DEBUG_THREADS
1291 GC_printf1("Started thread 0x%X\n", *new_thread);
1293 /* Wait until child has been added to the thread table. */
1294 /* This also ensures that we hold onto si until the child is done */
1295 /* with it. Thus it doesn't matter whether it is otherwise */
1296 /* visible to the collector. */
1298 while (0 != sem_wait(&(si -> registered))) {
1299 if (EINTR != errno) ABORT("sem_wait failed");
1302 sem_destroy(&(si -> registered));
1304 GC_INTERNAL_FREE(si);
1310 #ifdef GENERIC_COMPARE_AND_SWAP
1311 pthread_mutex_t GC_compare_and_swap_lock = PTHREAD_MUTEX_INITIALIZER;
1313 GC_bool GC_compare_and_exchange(volatile GC_word *addr,
1314 GC_word old, GC_word new_val)
1317 pthread_mutex_lock(&GC_compare_and_swap_lock);
1324 pthread_mutex_unlock(&GC_compare_and_swap_lock);
1328 GC_word GC_atomic_add(volatile GC_word *addr, GC_word how_much)
1331 pthread_mutex_lock(&GC_compare_and_swap_lock);
1333 *addr = old + how_much;
1334 pthread_mutex_unlock(&GC_compare_and_swap_lock);
1338 #endif /* GENERIC_COMPARE_AND_SWAP */
1339 /* Spend a few cycles in a way that can't introduce contention with */
1340 /* othre threads. */
1344 # if !defined(__GNUC__) || defined(__INTEL_COMPILER)
1345 volatile word dummy = 0;
1348 for (i = 0; i < 10; ++i) {
1349 # if defined(__GNUC__) && !defined(__INTEL_COMPILER)
1350 __asm__ __volatile__ (" " : : : "memory");
1352 /* Something that's unlikely to be optimized away. */
1358 #define SPIN_MAX 128 /* Maximum number of calls to GC_pause before */
1361 VOLATILE GC_bool GC_collecting = 0;
1362 /* A hint that we're in the collector and */
1363 /* holding the allocation lock for an */
1364 /* extended period. */
1366 #if !defined(USE_SPIN_LOCK) || defined(PARALLEL_MARK)
1367 /* If we don't want to use the below spinlock implementation, either */
1368 /* because we don't have a GC_test_and_set implementation, or because */
1369 /* we don't want to risk sleeping, we can still try spinning on */
1370 /* pthread_mutex_trylock for a while. This appears to be very */
1371 /* beneficial in many cases. */
1372 /* I suspect that under high contention this is nearly always better */
1373 /* than the spin lock. But it's a bit slower on a uniprocessor. */
1374 /* Hence we still default to the spin lock. */
1375 /* This is also used to acquire the mark lock for the parallel */
1378 /* Here we use a strict exponential backoff scheme. I don't know */
1379 /* whether that's better or worse than the above. We eventually */
1380 /* yield by calling pthread_mutex_lock(); it never makes sense to */
1381 /* explicitly sleep. */
1385 unsigned long GC_spin_count = 0;
1386 unsigned long GC_block_count = 0;
1387 unsigned long GC_unlocked_count = 0;
1390 void GC_generic_lock(pthread_mutex_t * lock)
1392 #ifndef NO_PTHREAD_TRYLOCK
1393 unsigned pause_length = 1;
1396 if (0 == pthread_mutex_trylock(lock)) {
1398 ++GC_unlocked_count;
1402 for (; pause_length <= SPIN_MAX; pause_length <<= 1) {
1403 for (i = 0; i < pause_length; ++i) {
1406 switch(pthread_mutex_trylock(lock)) {
1415 ABORT("Unexpected error from pthread_mutex_trylock");
1418 #endif /* !NO_PTHREAD_TRYLOCK */
1422 pthread_mutex_lock(lock);
1425 #endif /* !USE_SPIN_LOCK || PARALLEL_MARK */
1427 #if defined(USE_SPIN_LOCK)
1429 /* Reasonably fast spin locks. Basically the same implementation */
1430 /* as STL alloc.h. This isn't really the right way to do this. */
1431 /* but until the POSIX scheduling mess gets straightened out ... */
1433 volatile unsigned int GC_allocate_lock = 0;
1438 # define low_spin_max 30 /* spin cycles if we suspect uniprocessor */
1439 # define high_spin_max SPIN_MAX /* spin cycles for multiprocessor */
1440 static unsigned spin_max = low_spin_max;
1441 unsigned my_spin_max;
1442 static unsigned last_spins = 0;
1443 unsigned my_last_spins;
1446 if (!GC_test_and_set(&GC_allocate_lock)) {
1449 my_spin_max = spin_max;
1450 my_last_spins = last_spins;
1451 for (i = 0; i < my_spin_max; i++) {
1452 if (GC_collecting || GC_nprocs == 1) goto yield;
1453 if (i < my_last_spins/2 || GC_allocate_lock) {
1457 if (!GC_test_and_set(&GC_allocate_lock)) {
1460 * Spinning worked. Thus we're probably not being scheduled
1461 * against the other process with which we were contending.
1462 * Thus it makes sense to spin longer the next time.
1465 spin_max = high_spin_max;
1469 /* We are probably being scheduled against the other process. Sleep. */
1470 spin_max = low_spin_max;
1473 if (!GC_test_and_set(&GC_allocate_lock)) {
1476 # define SLEEP_THRESHOLD 12
1477 /* Under Linux very short sleeps tend to wait until */
1478 /* the current time quantum expires. On old Linux */
1479 /* kernels nanosleep(<= 2ms) just spins under Linux. */
1480 /* (Under 2.4, this happens only for real-time */
1481 /* processes.) We want to minimize both behaviors */
1483 if (i < SLEEP_THRESHOLD) {
1489 /* Don't wait for more than about 15msecs, even */
1490 /* under extreme contention. */
1492 ts.tv_nsec = 1 << i;
1498 #else /* !USE_SPINLOCK */
1501 #ifndef NO_PTHREAD_TRYLOCK
1502 if (1 == GC_nprocs || GC_collecting) {
1503 pthread_mutex_lock(&GC_allocate_ml);
1505 GC_generic_lock(&GC_allocate_ml);
1507 #else /* !NO_PTHREAD_TRYLOCK */
1508 pthread_mutex_lock(&GC_allocate_ml);
1509 #endif /* !NO_PTHREAD_TRYLOCK */
1512 #endif /* !USE_SPINLOCK */
1514 #if defined(PARALLEL_MARK) || defined(THREAD_LOCAL_ALLOC)
1516 #ifdef GC_ASSERTIONS
1517 pthread_t GC_mark_lock_holder = NO_THREAD;
1521 /* Ugly workaround for a linux threads bug in the final versions */
1522 /* of glibc2.1. Pthread_mutex_trylock sets the mutex owner */
1523 /* field even when it fails to acquire the mutex. This causes */
1524 /* pthread_cond_wait to die. Remove for glibc2.2. */
1525 /* According to the man page, we should use */
1526 /* PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP, but that isn't actually */
1528 static pthread_mutex_t mark_mutex =
1529 {0, 0, 0, PTHREAD_MUTEX_ERRORCHECK_NP, {0, 0}};
1531 static pthread_mutex_t mark_mutex = PTHREAD_MUTEX_INITIALIZER;
1534 static pthread_cond_t builder_cv = PTHREAD_COND_INITIALIZER;
1536 void GC_acquire_mark_lock()
1539 if (pthread_mutex_lock(&mark_mutex) != 0) {
1540 ABORT("pthread_mutex_lock failed");
1543 GC_generic_lock(&mark_mutex);
1544 # ifdef GC_ASSERTIONS
1545 GC_mark_lock_holder = pthread_self();
1549 void GC_release_mark_lock()
1551 GC_ASSERT(GC_mark_lock_holder == pthread_self());
1552 # ifdef GC_ASSERTIONS
1553 GC_mark_lock_holder = NO_THREAD;
1555 if (pthread_mutex_unlock(&mark_mutex) != 0) {
1556 ABORT("pthread_mutex_unlock failed");
1560 /* Collector must wait for a freelist builders for 2 reasons: */
1561 /* 1) Mark bits may still be getting examined without lock. */
1562 /* 2) Partial free lists referenced only by locals may not be scanned */
1563 /* correctly, e.g. if they contain "pointer-free" objects, since the */
1564 /* free-list link may be ignored. */
1565 void GC_wait_builder()
1567 GC_ASSERT(GC_mark_lock_holder == pthread_self());
1568 # ifdef GC_ASSERTIONS
1569 GC_mark_lock_holder = NO_THREAD;
1571 if (pthread_cond_wait(&builder_cv, &mark_mutex) != 0) {
1572 ABORT("pthread_cond_wait failed");
1574 GC_ASSERT(GC_mark_lock_holder == NO_THREAD);
1575 # ifdef GC_ASSERTIONS
1576 GC_mark_lock_holder = pthread_self();
1580 void GC_wait_for_reclaim()
1582 GC_acquire_mark_lock();
1583 while (GC_fl_builder_count > 0) {
1586 GC_release_mark_lock();
1589 void GC_notify_all_builder()
1591 GC_ASSERT(GC_mark_lock_holder == pthread_self());
1592 if (pthread_cond_broadcast(&builder_cv) != 0) {
1593 ABORT("pthread_cond_broadcast failed");
1597 #endif /* PARALLEL_MARK || THREAD_LOCAL_ALLOC */
1599 #ifdef PARALLEL_MARK
1601 static pthread_cond_t mark_cv = PTHREAD_COND_INITIALIZER;
1603 void GC_wait_marker()
1605 GC_ASSERT(GC_mark_lock_holder == pthread_self());
1606 # ifdef GC_ASSERTIONS
1607 GC_mark_lock_holder = NO_THREAD;
1609 if (pthread_cond_wait(&mark_cv, &mark_mutex) != 0) {
1610 ABORT("pthread_cond_wait failed");
1612 GC_ASSERT(GC_mark_lock_holder == NO_THREAD);
1613 # ifdef GC_ASSERTIONS
1614 GC_mark_lock_holder = pthread_self();
1618 void GC_notify_all_marker()
1620 if (pthread_cond_broadcast(&mark_cv) != 0) {
1621 ABORT("pthread_cond_broadcast failed");
1625 #endif /* PARALLEL_MARK */
1627 # endif /* GC_LINUX_THREADS and friends */