2 * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
3 * Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
4 * Copyright (c) 1996-1999 by Silicon Graphics. All rights reserved.
5 * Copyright (c) 1999 by Hewlett-Packard Company. All rights reserved.
8 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
9 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
11 * Permission is hereby granted to use or copy this program
12 * for any purpose, provided the above notices are retained on all copies.
13 * Permission to modify the code and to distribute modified code is granted,
14 * provided the above notices are retained, and a notice that the code was
15 * modified is included with the above copyright notice.
22 * Mutual exclusion between allocator/collector routines.
23 * Needed if there is more than one allocator thread.
24 * FASTLOCK() is assumed to try to acquire the lock in a cheap and
25 * dirty way that is acceptable for a few instructions, e.g. by
26 * inhibiting preemption. This is assumed to have succeeded only
27 * if a subsequent call to FASTLOCK_SUCCEEDED() returns TRUE.
28 * FASTUNLOCK() is called whether or not FASTLOCK_SUCCEEDED().
29 * If signals cannot be tolerated with the FASTLOCK held, then
30 * FASTLOCK should disable signals. The code executed under
31 * FASTLOCK is otherwise immune to interruption, provided it is
33 * DCL_LOCK_STATE declares any local variables needed by LOCK and UNLOCK
34 * and/or DISABLE_SIGNALS and ENABLE_SIGNALS and/or FASTLOCK.
35 * (There is currently no equivalent for FASTLOCK.)
37 * In the PARALLEL_MARK case, we also need to define a number of
38 * other inline finctions here:
39 * GC_bool GC_compare_and_exchange( volatile GC_word *addr,
40 * GC_word old, GC_word new )
41 * GC_word GC_atomic_add( volatile GC_word *addr, GC_word how_much )
42 * void GC_memory_barrier( )
46 void GC_noop1 GC_PROTO((word));
47 # ifdef PCR_OBSOLETE /* Faster, but broken with multiple lwp's */
48 # include "th/PCR_Th.h"
49 # include "th/PCR_ThCrSec.h"
50 extern struct PCR_Th_MLRep GC_allocate_ml;
51 # define DCL_LOCK_STATE PCR_sigset_t GC_old_sig_mask
52 # define LOCK() PCR_Th_ML_Acquire(&GC_allocate_ml)
53 # define UNLOCK() PCR_Th_ML_Release(&GC_allocate_ml)
54 # define UNLOCK() PCR_Th_ML_Release(&GC_allocate_ml)
55 # define FASTLOCK() PCR_ThCrSec_EnterSys()
56 /* Here we cheat (a lot): */
57 # define FASTLOCK_SUCCEEDED() (*(int *)(&GC_allocate_ml) == 0)
58 /* TRUE if nobody currently holds the lock */
59 # define FASTUNLOCK() PCR_ThCrSec_ExitSys()
62 # include <base/PCR_Base.h>
63 # include <th/PCR_Th.h>
64 extern PCR_Th_ML GC_allocate_ml;
65 # define DCL_LOCK_STATE \
66 PCR_ERes GC_fastLockRes; PCR_sigset_t GC_old_sig_mask
67 # define LOCK() PCR_Th_ML_Acquire(&GC_allocate_ml)
68 # define UNLOCK() PCR_Th_ML_Release(&GC_allocate_ml)
69 # define FASTLOCK() (GC_fastLockRes = PCR_Th_ML_Try(&GC_allocate_ml))
70 # define FASTLOCK_SUCCEEDED() (GC_fastLockRes == PCR_ERes_okay)
71 # define FASTUNLOCK() {\
72 if( FASTLOCK_SUCCEEDED() ) PCR_Th_ML_Release(&GC_allocate_ml); }
75 extern GC_word RT0u__inCritical;
76 # define LOCK() RT0u__inCritical++
77 # define UNLOCK() RT0u__inCritical--
79 # ifdef GC_SOLARIS_THREADS
82 extern mutex_t GC_allocate_ml;
83 # define LOCK() mutex_lock(&GC_allocate_ml);
84 # define UNLOCK() mutex_unlock(&GC_allocate_ml);
87 /* Try to define GC_TEST_AND_SET and a matching GC_CLEAR for spin lock */
88 /* acquisition and release. We need this for correct operation of the */
92 inline static int GC_test_and_set(volatile unsigned int *addr) {
94 /* Note: the "xchg" instruction does not need a "lock" prefix */
95 __asm__ __volatile__("xchgl %0, %1"
96 : "=r"(oldval), "=m"(*(addr))
97 : "0"(1), "m"(*(addr)) : "memory");
100 # define GC_TEST_AND_SET_DEFINED
103 # if defined(__INTEL_COMPILER)
104 # include <ia64intrin.h>
106 inline static int GC_test_and_set(volatile unsigned int *addr) {
108 # ifndef __INTEL_COMPILER
109 __asm__ __volatile__("xchg4 %0=%1,%2"
110 : "=r"(oldval), "=m"(*addr)
111 : "r"(n), "1"(*addr) : "memory");
113 oldval = _InterlockedExchange(addr, n);
117 # define GC_TEST_AND_SET_DEFINED
118 /* Should this handle post-increment addressing?? */
119 inline static void GC_clear(volatile unsigned int *addr) {
120 # ifndef __INTEL_COMPILER
121 __asm__ __volatile__("st4.rel %0=r0" : "=m" (*addr) : : "memory");
123 // there is no st4 but I can use xchg I hope
124 _InterlockedExchange(addr, 0);
127 # define GC_CLEAR_DEFINED
130 inline static int GC_test_and_set(volatile unsigned int *addr) {
133 __asm__ __volatile__("ldstub %1,%0"
134 : "=r"(oldval), "=m"(*addr)
135 : "m"(*addr) : "memory");
138 # define GC_TEST_AND_SET_DEFINED
141 /* Contributed by Tony Mantler. I'm not sure how well it was */
143 inline static int GC_test_and_set(volatile unsigned int *addr) {
144 char oldval; /* this must be no longer than 8 bits */
146 /* The return value is semi-phony. */
147 /* 'tas' sets bit 7 while the return */
148 /* value pretends bit 0 was set */
149 __asm__ __volatile__(
150 "tas %1@; sne %0; negb %0"
152 : "a" (addr) : "memory");
155 # define GC_TEST_AND_SET_DEFINED
157 # if defined(POWERPC)
158 inline static int GC_test_and_set(volatile unsigned int *addr) {
160 int temp = 1; /* locked value */
162 __asm__ __volatile__(
163 "1:\tlwarx %0,0,%3\n" /* load and reserve */
164 "\tcmpwi %0, 0\n" /* if load is */
165 "\tbne 2f\n" /* non-zero, return already set */
166 "\tstwcx. %2,0,%1\n" /* else store conditional */
167 "\tbne- 1b\n" /* retry if lost reservation */
168 "\tsync\n" /* import barrier */
169 "2:\t\n" /* oldval is zero if we set */
170 : "=&r"(oldval), "=p"(addr)
171 : "r"(temp), "1"(addr)
175 # define GC_TEST_AND_SET_DEFINED
176 inline static void GC_clear(volatile unsigned int *addr) {
177 __asm__ __volatile__("eieio" : : : "memory");
180 # define GC_CLEAR_DEFINED
183 inline static int GC_test_and_set(volatile unsigned int * addr)
185 unsigned long oldvalue;
188 __asm__ __volatile__(
202 ".section .text2,\"ax\"\n"
206 :"=&r" (temp), "=m" (*addr), "=&r" (oldvalue)
207 :"Ir" (1), "m" (*addr)
212 # define GC_TEST_AND_SET_DEFINED
213 inline static void GC_clear(volatile unsigned int *addr) {
214 __asm__ __volatile__("mb" : : : "memory");
217 # define GC_CLEAR_DEFINED
220 inline static int GC_test_and_set(volatile unsigned int *addr) {
222 /* SWP on ARM is very similar to XCHG on x86. Doesn't lock the
223 * bus because there are no SMP ARM machines. If/when there are,
224 * this code will likely need to be updated. */
225 /* See linuxthreads/sysdeps/arm/pt-machine.h in glibc-2.1 */
226 __asm__ __volatile__("swp %0, %1, [%2]"
232 # define GC_TEST_AND_SET_DEFINED
235 inline static int GC_test_and_set(volatile unsigned int *addr) {
237 __asm__ __volatile__ (
239 "0: cs %0,%1,0(%2)\n"
242 : "d" (1), "a" (addr)
247 # endif /* __GNUC__ */
248 # if (defined(ALPHA) && !defined(__GNUC__))
250 --> We currently assume that if gcc is not used, we are
251 --> running under Tru64.
253 # include <machine/builtins.h>
255 # define GC_test_and_set(addr) __ATOMIC_EXCH_LONG(addr, 1)
256 # define GC_TEST_AND_SET_DEFINED
257 # define GC_clear(addr) { asm("mb"); *(volatile unsigned *)addr = 0; }
258 # define GC_CLEAR_DEFINED
260 # if defined(MSWIN32)
261 # define GC_test_and_set(addr) InterlockedExchange((LPLONG)addr,1)
262 # define GC_TEST_AND_SET_DEFINED
266 # include <sys/tas.h>
267 # define GC_test_and_set(addr) _test_and_set((int *) addr,1)
268 # define GC_TEST_AND_SET_DEFINED
269 # elif __mips < 3 || !(defined (_ABIN32) || defined(_ABI64)) \
270 || !defined(_COMPILER_VERSION) || _COMPILER_VERSION < 700
272 # define GC_test_and_set(addr) _test_and_set((void *)addr,1)
274 # define GC_test_and_set(addr) test_and_set((void *)addr,1)
277 # define GC_test_and_set(addr) __test_and_set32((void *)addr,1)
278 # define GC_clear(addr) __lock_release(addr);
279 # define GC_CLEAR_DEFINED
281 # define GC_TEST_AND_SET_DEFINED
284 # include <sys/atomic_op.h>
285 # if (defined(_POWER) || defined(_POWERPC))
286 # if defined(__GNUC__)
287 inline static void GC_memsync() {
288 __asm__ __volatile__ ("sync" : : : "memory");
292 # define inline __inline
294 # pragma mc_func GC_memsync { \
295 "7c0004ac" /* sync (same opcode used for dcs)*/ \
299 # error dont know how to memsync
301 inline static int GC_test_and_set(volatile unsigned int * addr) {
303 if (compare_and_swap((void *)addr, &oldvalue, 1)) {
308 # define GC_TEST_AND_SET_DEFINED
309 inline static void GC_clear(volatile unsigned int *addr) {
313 # define GC_CLEAR_DEFINED
316 # if 0 /* defined(HP_PA) */
317 /* The official recommendation seems to be to not use ldcw from */
318 /* user mode. Since multithreaded incremental collection doesn't */
319 /* work anyway on HP_PA, this shouldn't be a major loss. */
321 /* "set" means 0 and "clear" means 1 here. */
322 # define GC_test_and_set(addr) !GC_test_and_clear(addr);
323 # define GC_TEST_AND_SET_DEFINED
324 # define GC_clear(addr) GC_noop1((word)(addr)); *(volatile unsigned int *)addr = 1;
325 /* The above needs a memory barrier! */
326 # define GC_CLEAR_DEFINED
328 # if defined(GC_TEST_AND_SET_DEFINED) && !defined(GC_CLEAR_DEFINED)
330 inline static void GC_clear(volatile unsigned int *addr) {
331 /* Try to discourage gcc from moving anything past this. */
332 __asm__ __volatile__(" " : : : "memory");
336 /* The function call in the following should prevent the */
337 /* compiler from moving assignments to below the UNLOCK. */
338 # define GC_clear(addr) GC_noop1((word)(addr)); \
339 *((volatile unsigned int *)(addr)) = 0;
341 # define GC_CLEAR_DEFINED
342 # endif /* !GC_CLEAR_DEFINED */
344 # if !defined(GC_TEST_AND_SET_DEFINED)
345 # define USE_PTHREAD_LOCKS
348 # if defined(GC_PTHREADS) && !defined(GC_SOLARIS_THREADS) \
349 && !defined(GC_IRIX_THREADS) && !defined(GC_WIN32_THREADS)
350 # define NO_THREAD (pthread_t)(-1)
351 # include <pthread.h>
352 # if defined(PARALLEL_MARK)
353 /* We need compare-and-swap to update mark bits, where it's */
354 /* performance critical. If USE_MARK_BYTES is defined, it is */
355 /* no longer needed for this purpose. However we use it in */
356 /* either case to implement atomic fetch-and-add, though that's */
357 /* less performance critical, and could perhaps be done with */
359 # if defined(GENERIC_COMPARE_AND_SWAP)
360 /* Probably not useful, except for debugging. */
361 /* We do use GENERIC_COMPARE_AND_SWAP on PA_RISC, but we */
362 /* minimize its use. */
363 extern pthread_mutex_t GC_compare_and_swap_lock;
365 /* Note that if GC_word updates are not atomic, a concurrent */
366 /* reader should acquire GC_compare_and_swap_lock. On */
367 /* currently supported platforms, such updates are atomic. */
368 extern GC_bool GC_compare_and_exchange(volatile GC_word *addr,
369 GC_word old, GC_word new_val);
370 # endif /* GENERIC_COMPARE_AND_SWAP */
372 # if !defined(GENERIC_COMPARE_AND_SWAP)
373 /* Returns TRUE if the comparison succeeded. */
374 inline static GC_bool GC_compare_and_exchange(volatile GC_word *addr,
379 __asm__ __volatile__("lock; cmpxchgl %2, %0; setz %1"
380 : "+m"(*(addr)), "=r"(result)
381 : "r" (new_val), "a"(old) : "memory");
382 return (GC_bool) result;
384 # endif /* !GENERIC_COMPARE_AND_SWAP */
385 inline static void GC_memory_barrier()
387 /* We believe the processor ensures at least processor */
388 /* consistent ordering. Thus a compiler barrier */
389 /* should suffice. */
390 __asm__ __volatile__("" : : : "memory");
394 # if defined(POWERPC)
395 # if !defined(GENERIC_COMPARE_AND_SWAP)
396 /* Returns TRUE if the comparison succeeded. */
397 inline static GC_bool GC_compare_and_exchange(volatile GC_word *addr,
398 GC_word old, GC_word new_val)
401 __asm__ __volatile__(
402 "1:\tlwarx %0,0,%5\n"
412 : "=&r" (dummy), "=r" (result), "=p" (addr)
413 : "r" (new_val), "r" (old), "2"(addr)
415 return (GC_bool) result;
417 # endif /* !GENERIC_COMPARE_AND_SWAP */
418 inline static void GC_memory_barrier()
420 __asm__ __volatile__("sync" : : : "memory");
422 # endif /* POWERPC */
425 # if !defined(GENERIC_COMPARE_AND_SWAP)
426 inline static GC_bool GC_compare_and_exchange(volatile GC_word *addr,
427 GC_word old, GC_word new_val)
429 unsigned long oldval;
430 __asm__ __volatile__("mov ar.ccv=%4 ;; cmpxchg8.rel %0=%1,%2,ar.ccv"
431 : "=r"(oldval), "=m"(*addr)
432 : "r"(new_val), "1"(*addr), "r"(old) : "memory");
433 return (oldval == old);
435 # endif /* !GENERIC_COMPARE_AND_SWAP */
437 /* Shouldn't be needed; we use volatile stores instead. */
438 inline static void GC_memory_barrier()
440 __asm__ __volatile__("mf" : : : "memory");
445 # if !defined(GENERIC_COMPARE_AND_SWAP)
446 # if defined(__GNUC__)
447 inline static GC_bool GC_compare_and_exchange(volatile GC_word *addr,
448 GC_word old, GC_word new_val)
450 unsigned long was_equal;
453 __asm__ __volatile__(
462 :"=&r" (temp), "=m" (*addr), "=&r" (was_equal)
463 : "r" (new_val), "Ir" (old)
467 # else /* !__GNUC__ */
468 inline static GC_bool GC_compare_and_exchange(volatile GC_word *addr,
469 GC_word old, GC_word new_val)
471 return __CMP_STORE_QUAD(addr, old, new_val, addr);
473 # endif /* !__GNUC__ */
474 # endif /* !GENERIC_COMPARE_AND_SWAP */
476 inline static void GC_memory_barrier()
478 __asm__ __volatile__("mb" : : : "memory");
481 # define GC_memory_barrier() asm("mb")
482 # endif /* !__GNUC__ */
485 # if !defined(GENERIC_COMPARE_AND_SWAP)
486 inline static GC_bool GC_compare_and_exchange(volatile C_word *addr,
487 GC_word old, GC_word new_val)
490 __asm__ __volatile__ (
498 : "=&d" (retval), "+d" (old)
499 : "d" (new_val), "a" (addr)
505 # if !defined(GENERIC_COMPARE_AND_SWAP)
506 /* Returns the original value of *addr. */
507 inline static GC_word GC_atomic_add(volatile GC_word *addr,
513 } while (!GC_compare_and_exchange(addr, old, old+how_much));
516 # else /* GENERIC_COMPARE_AND_SWAP */
517 /* So long as a GC_word can be atomically updated, it should */
518 /* be OK to read *addr without a lock. */
519 extern GC_word GC_atomic_add(volatile GC_word *addr, GC_word how_much);
520 # endif /* GENERIC_COMPARE_AND_SWAP */
522 # endif /* PARALLEL_MARK */
524 # if !defined(THREAD_LOCAL_ALLOC) && !defined(USE_PTHREAD_LOCKS)
525 /* In the THREAD_LOCAL_ALLOC case, the allocation lock tends to */
526 /* be held for long periods, if it is held at all. Thus spinning */
527 /* and sleeping for fixed periods are likely to result in */
528 /* significant wasted time. We thus rely mostly on queued locks. */
529 # define USE_SPIN_LOCK
530 extern volatile unsigned int GC_allocate_lock;
531 extern void GC_lock(void);
532 /* Allocation lock holder. Only set if acquired by client through */
533 /* GC_call_with_alloc_lock. */
534 # ifdef GC_ASSERTIONS
536 { if (GC_test_and_set(&GC_allocate_lock)) GC_lock(); \
539 { GC_ASSERT(I_HOLD_LOCK()); UNSET_LOCK_HOLDER(); \
540 GC_clear(&GC_allocate_lock); }
543 { if (GC_test_and_set(&GC_allocate_lock)) GC_lock(); }
545 GC_clear(&GC_allocate_lock)
546 # endif /* !GC_ASSERTIONS */
548 /* Another alternative for OSF1 might be: */
549 # include <sys/mman.h>
550 extern msemaphore GC_allocate_semaphore;
551 # define LOCK() { if (msem_lock(&GC_allocate_semaphore, MSEM_IF_NOWAIT) \
552 != 0) GC_lock(); else GC_allocate_lock = 1; }
553 /* The following is INCORRECT, since the memory model is too weak. */
554 /* Is this true? Presumably msem_unlock has the right semantics? */
556 # define UNLOCK() { GC_allocate_lock = 0; \
557 msem_unlock(&GC_allocate_semaphore, 0); }
559 # else /* THREAD_LOCAL_ALLOC || USE_PTHREAD_LOCKS */
560 # ifndef USE_PTHREAD_LOCKS
561 # define USE_PTHREAD_LOCKS
563 # endif /* THREAD_LOCAL_ALLOC */
564 # ifdef USE_PTHREAD_LOCKS
565 # include <pthread.h>
566 extern pthread_mutex_t GC_allocate_ml;
567 # ifdef GC_ASSERTIONS
572 { GC_ASSERT(I_HOLD_LOCK()); UNSET_LOCK_HOLDER(); \
573 pthread_mutex_unlock(&GC_allocate_ml); }
574 # else /* !GC_ASSERTIONS */
575 # if defined(NO_PTHREAD_TRYLOCK)
576 # define LOCK() GC_lock();
577 # else /* !defined(NO_PTHREAD_TRYLOCK) */
579 { if (0 != pthread_mutex_trylock(&GC_allocate_ml)) GC_lock(); }
581 # define UNLOCK() pthread_mutex_unlock(&GC_allocate_ml)
582 # endif /* !GC_ASSERTIONS */
583 # endif /* USE_PTHREAD_LOCKS */
584 # define SET_LOCK_HOLDER() GC_lock_holder = pthread_self()
585 # define UNSET_LOCK_HOLDER() GC_lock_holder = NO_THREAD
586 # define I_HOLD_LOCK() (pthread_equal(GC_lock_holder, pthread_self()))
587 extern VOLATILE GC_bool GC_collecting;
588 # define ENTER_GC() GC_collecting = 1;
589 # define EXIT_GC() GC_collecting = 0;
590 extern void GC_lock(void);
591 extern pthread_t GC_lock_holder;
592 # ifdef GC_ASSERTIONS
593 extern pthread_t GC_mark_lock_holder;
595 # endif /* GC_PTHREADS with linux_threads.c implementation */
596 # if defined(GC_IRIX_THREADS)
597 # include <pthread.h>
598 /* This probably should never be included, but I can't test */
599 /* on Irix anymore. */
602 extern volatile unsigned int GC_allocate_lock;
603 /* This is not a mutex because mutexes that obey the (optional) */
604 /* POSIX scheduling rules are subject to convoys in high contention */
605 /* applications. This is basically a spin lock. */
606 extern pthread_t GC_lock_holder;
607 extern void GC_lock(void);
608 /* Allocation lock holder. Only set if acquired by client through */
609 /* GC_call_with_alloc_lock. */
610 # define SET_LOCK_HOLDER() GC_lock_holder = pthread_self()
611 # define NO_THREAD (pthread_t)(-1)
612 # define UNSET_LOCK_HOLDER() GC_lock_holder = NO_THREAD
613 # define I_HOLD_LOCK() (pthread_equal(GC_lock_holder, pthread_self()))
614 # define LOCK() { if (GC_test_and_set(&GC_allocate_lock)) GC_lock(); }
615 # define UNLOCK() GC_clear(&GC_allocate_lock);
616 extern VOLATILE GC_bool GC_collecting;
617 # define ENTER_GC() \
621 # define EXIT_GC() GC_collecting = 0;
622 # endif /* GC_IRIX_THREADS */
623 # if defined(GC_WIN32_THREADS)
624 # if defined(GC_PTHREADS)
625 # include <pthread.h>
626 extern pthread_mutex_t GC_allocate_ml;
627 # define LOCK() pthread_mutex_lock(&GC_allocate_ml)
628 # define UNLOCK() pthread_mutex_unlock(&GC_allocate_ml)
630 # include <windows.h>
631 GC_API CRITICAL_SECTION GC_allocate_ml;
632 # define LOCK() EnterCriticalSection(&GC_allocate_ml);
633 # define UNLOCK() LeaveCriticalSection(&GC_allocate_ml);
636 # ifndef SET_LOCK_HOLDER
637 # define SET_LOCK_HOLDER()
638 # define UNSET_LOCK_HOLDER()
639 # define I_HOLD_LOCK() FALSE
640 /* Used on platforms were locks can be reacquired, */
641 /* so it doesn't matter if we lie. */
643 # else /* !THREADS */
646 # endif /* !THREADS */
647 # ifndef SET_LOCK_HOLDER
648 # define SET_LOCK_HOLDER()
649 # define UNSET_LOCK_HOLDER()
650 # define I_HOLD_LOCK() FALSE
651 /* Used on platforms were locks can be reacquired, */
652 /* so it doesn't matter if we lie. */
659 # ifndef DCL_LOCK_STATE
660 # define DCL_LOCK_STATE
663 # define FASTLOCK() LOCK()
664 # define FASTLOCK_SUCCEEDED() TRUE
665 # define FASTUNLOCK() UNLOCK()
668 #endif /* GC_LOCKS_H */