Merged revisions 7501-7598 via svnmerge from
[cacao.git] / src / mm / boehm-gc / include / private / gc_locks.h
1 /* 
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.
6  *
7  *
8  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
9  * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
10  *
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.
16  */
17
18 #ifndef GC_LOCKS_H
19 #define GC_LOCKS_H
20
21 /*
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
32  * not restarted.
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.)
36  *
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( )
43  *   
44  */  
45 # ifdef THREADS
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()
60 #  endif
61 #  ifdef PCR
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); }
73 #  endif
74 #  ifdef SRC_M3
75      extern GC_word RT0u__inCritical;
76 #    define LOCK() RT0u__inCritical++
77 #    define UNLOCK() RT0u__inCritical--
78 #  endif
79 #  ifdef GC_SOLARIS_THREADS
80 #    include <thread.h>
81 #    include <signal.h>
82      extern mutex_t GC_allocate_ml;
83 #    define LOCK() mutex_lock(&GC_allocate_ml);
84 #    define UNLOCK() mutex_unlock(&GC_allocate_ml);
85 #  endif
86
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  */
89 /* incremental GC.                                                      */
90 #  ifdef __GNUC__
91 #    if defined(I386)
92        inline static int GC_test_and_set(volatile unsigned int *addr) {
93           int oldval;
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");
98           return oldval;
99        }
100 #      define GC_TEST_AND_SET_DEFINED
101 #    endif
102 #    if defined(IA64)
103 #      if defined(__INTEL_COMPILER)
104 #        include <ia64intrin.h>
105 #      endif
106        inline static int GC_test_and_set(volatile unsigned int *addr) {
107           long oldval, n = 1;
108 #       ifndef __INTEL_COMPILER
109           __asm__ __volatile__("xchg4 %0=%1,%2"
110                 : "=r"(oldval), "=m"(*addr)
111                 : "r"(n), "1"(*addr) : "memory");
112 #       else
113           oldval = _InterlockedExchange(addr, n);
114 #       endif
115           return oldval;
116        }
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");
122 #       else
123          /* there is no st4 but I can use xchg I hope */
124          _InterlockedExchange(addr, 0);
125 #       endif
126        }
127 #      define GC_CLEAR_DEFINED
128 #    endif
129 #    ifdef SPARC
130        inline static int GC_test_and_set(volatile unsigned int *addr) {
131          int oldval;
132
133          __asm__ __volatile__("ldstub %1,%0"
134          : "=r"(oldval), "=m"(*addr)
135          : "m"(*addr) : "memory");
136          return oldval;
137        }
138 #      define GC_TEST_AND_SET_DEFINED
139 #    endif
140 /* tbfg */
141 #if 0           
142 #    ifdef M68K
143        /* Contributed by Tony Mantler.  I'm not sure how well it was    */
144        /* tested.                                                       */
145        inline static int GC_test_and_set(volatile unsigned int *addr) {
146           char oldval; /* this must be no longer than 8 bits */
147
148           /* The return value is semi-phony. */
149           /* 'tas' sets bit 7 while the return */
150           /* value pretends bit 0 was set */
151           __asm__ __volatile__(
152                  "tas %1@; sne %0; negb %0"
153                  : "=d" (oldval)
154                  : "a" (addr) : "memory");
155           return oldval;
156        }
157 #      define GC_TEST_AND_SET_DEFINED
158 #    endif
159 #endif
160 #    if defined(POWERPC)
161         inline static int GC_test_and_set(volatile unsigned int *addr) {
162           int oldval;
163           int temp = 1; /* locked value */
164
165           __asm__ __volatile__(
166                "1:\tlwarx %0,0,%3\n"   /* load and reserve               */
167                "\tcmpwi %0, 0\n"       /* if load is                     */
168                "\tbne 2f\n"            /*   non-zero, return already set */
169                "\tstwcx. %2,0,%1\n"    /* else store conditional         */
170                "\tbne- 1b\n"           /* retry if lost reservation      */
171                "\tsync\n"              /* import barrier                 */
172                "2:\t\n"                /* oldval is zero if we set       */
173               : "=&r"(oldval), "=p"(addr)
174               : "r"(temp), "1"(addr)
175               : "cr0","memory");
176           return oldval;
177         }
178 #     define GC_TEST_AND_SET_DEFINED
179       inline static void GC_clear(volatile unsigned int *addr) {
180         __asm__ __volatile__("lwsync" : : : "memory");
181         *(addr) = 0;
182       }
183 #     define GC_CLEAR_DEFINED
184 #    endif
185 #    if defined(ALPHA) 
186         inline static int GC_test_and_set(volatile unsigned int * addr)
187         {
188           unsigned long oldvalue;
189           unsigned long temp;
190
191           __asm__ __volatile__(
192                              "1:     ldl_l %0,%1\n"
193                              "       and %0,%3,%2\n"
194                              "       bne %2,2f\n"
195                              "       xor %0,%3,%0\n"
196                              "       stl_c %0,%1\n"
197 #       ifdef __ELF__
198                              "       beq %0,3f\n"
199 #       else
200                              "       beq %0,1b\n"
201 #       endif
202                              "       mb\n"
203                              "2:\n"
204 #       ifdef __ELF__
205                              ".section .text2,\"ax\"\n"
206                              "3:     br 1b\n"
207                              ".previous"
208 #       endif
209                              :"=&r" (temp), "=m" (*addr), "=&r" (oldvalue)
210                              :"Ir" (1), "m" (*addr)
211                              :"memory");
212
213           return oldvalue;
214         }
215 #       define GC_TEST_AND_SET_DEFINED
216         inline static void GC_clear(volatile unsigned int *addr) {
217           __asm__ __volatile__("mb" : : : "memory");
218           *(addr) = 0;
219         }
220 #       define GC_CLEAR_DEFINED
221 #    endif /* ALPHA */
222 #    ifdef ARM32
223         inline static int GC_test_and_set(volatile unsigned int *addr) {
224           int oldval;
225           /* SWP on ARM is very similar to XCHG on x86.                 */
226           /* The first operand is the result, the second the value      */
227           /* to be stored.  Both registers must be different from addr. */
228           /* Make the address operand an early clobber output so it     */
229           /* doesn't overlap with the other operands.  The early clobber*/
230           /* on oldval is neccessary to prevent the compiler allocating */
231           /* them to the same register if they are both unused.         */
232           __asm__ __volatile__("swp %0, %2, [%3]"
233                              : "=&r"(oldval), "=&r"(addr)
234                              : "r"(1), "1"(addr)
235                              : "memory");
236           return oldval;
237         }
238 #       define GC_TEST_AND_SET_DEFINED
239 #    endif /* ARM32 */
240 #    ifdef CRIS
241         inline static int GC_test_and_set(volatile unsigned int *addr) {
242           /* Ripped from linuxthreads/sysdeps/cris/pt-machine.h.        */
243           /* Included with Hans-Peter Nilsson's permission.             */
244           register unsigned long int ret;
245
246           /* Note the use of a dummy output of *addr to expose the write.
247            * The memory barrier is to stop *other* writes being moved past
248            * this code.
249            */
250             __asm__ __volatile__("clearf\n"
251                                  "0:\n\t"
252                                  "movu.b [%2],%0\n\t"
253                                  "ax\n\t"
254                                  "move.b %3,[%2]\n\t"
255                                  "bwf 0b\n\t"
256                                  "clearf"
257                                  : "=&r" (ret), "=m" (*addr)
258                                  : "r" (addr), "r" ((int) 1), "m" (*addr)
259                                  : "memory");
260             return ret;
261         }
262 #       define GC_TEST_AND_SET_DEFINED
263 #    endif /* CRIS */
264 #    ifdef S390
265        inline static int GC_test_and_set(volatile unsigned int *addr) {
266          int ret;
267          __asm__ __volatile__ (
268           "     l     %0,0(%2)\n"
269           "0:   cs    %0,%1,0(%2)\n"
270           "     jl    0b"
271           : "=&d" (ret)
272           : "d" (1), "a" (addr)
273           : "cc", "memory");
274          return ret;
275        }
276 #    endif
277 #  endif /* __GNUC__ */
278 #  if (defined(ALPHA) && !defined(__GNUC__))
279 #    ifndef OSF1
280         --> We currently assume that if gcc is not used, we are
281         --> running under Tru64.
282 #    endif
283 #    include <machine/builtins.h>
284 #    include <c_asm.h>
285 #    define GC_test_and_set(addr) __ATOMIC_EXCH_LONG(addr, 1)
286 #    define GC_TEST_AND_SET_DEFINED
287 #    define GC_clear(addr) { asm("mb"); *(volatile unsigned *)addr = 0; }
288 #    define GC_CLEAR_DEFINED
289 #  endif
290 #  if defined(MSWIN32)
291 #    define GC_test_and_set(addr) InterlockedExchange((LPLONG)addr,1)
292 #    define GC_TEST_AND_SET_DEFINED
293 #  endif
294 #  ifdef MIPS
295 #    ifdef LINUX
296 #      include <sys/tas.h>
297 #      define GC_test_and_set(addr) _test_and_set((int *) addr,1)
298 #      define GC_TEST_AND_SET_DEFINED
299 #    elif __mips < 3 || !(defined (_ABIN32) || defined(_ABI64)) \
300         || !defined(_COMPILER_VERSION) || _COMPILER_VERSION < 700
301 #        ifdef __GNUC__
302 #          define GC_test_and_set(addr) _test_and_set((void *)addr,1)
303 #        else
304 #          define GC_test_and_set(addr) test_and_set((void *)addr,1)
305 #        endif
306 #    else
307 #        include <sgidefs.h>
308 #        include <mutex.h>
309 #        define GC_test_and_set(addr) __test_and_set32((void *)addr,1)
310 #        define GC_clear(addr) __lock_release(addr);
311 #        define GC_CLEAR_DEFINED
312 #    endif
313 #    define GC_TEST_AND_SET_DEFINED
314 #  endif /* MIPS */
315 #  if defined(_AIX)
316 #    include <sys/atomic_op.h>
317 #    if (defined(_POWER) || defined(_POWERPC)) 
318 #      if defined(__GNUC__)  
319          inline static void GC_memsync() {
320            __asm__ __volatile__ ("sync" : : : "memory");
321          }
322 #      else
323 #        ifndef inline
324 #          define inline __inline
325 #        endif
326 #        pragma mc_func GC_memsync { \
327            "7c0004ac" /* sync (same opcode used for dcs)*/ \
328          }
329 #      endif
330 #    else 
331 #    error dont know how to memsync
332 #    endif
333      inline static int GC_test_and_set(volatile unsigned int * addr) {
334           int oldvalue = 0;
335           if (compare_and_swap((void *)addr, &oldvalue, 1)) {
336             GC_memsync();
337             return 0;
338           } else return 1;
339      }
340 #    define GC_TEST_AND_SET_DEFINED
341      inline static void GC_clear(volatile unsigned int *addr) {
342           GC_memsync();
343           *(addr) = 0;
344      }
345 #    define GC_CLEAR_DEFINED
346
347 #  endif
348 #  if 0 /* defined(HP_PA) */
349      /* The official recommendation seems to be to not use ldcw from    */
350      /* user mode.  Since multithreaded incremental collection doesn't  */
351      /* work anyway on HP_PA, this shouldn't be a major loss.           */
352
353      /* "set" means 0 and "clear" means 1 here.         */
354 #    define GC_test_and_set(addr) !GC_test_and_clear(addr);
355 #    define GC_TEST_AND_SET_DEFINED
356 #    define GC_clear(addr) GC_noop1((word)(addr)); *(volatile unsigned int *)addr = 1;
357         /* The above needs a memory barrier! */
358 #    define GC_CLEAR_DEFINED
359 #  endif
360 #  if defined(GC_TEST_AND_SET_DEFINED) && !defined(GC_CLEAR_DEFINED)
361 #    ifdef __GNUC__
362        inline static void GC_clear(volatile unsigned int *addr) {
363          /* Try to discourage gcc from moving anything past this. */
364          __asm__ __volatile__(" " : : : "memory");
365          *(addr) = 0;
366        }
367 #    else
368             /* The function call in the following should prevent the    */
369             /* compiler from moving assignments to below the UNLOCK.    */
370 #      define GC_clear(addr) GC_noop1((word)(addr)); \
371                              *((volatile unsigned int *)(addr)) = 0;
372 #    endif
373 #    define GC_CLEAR_DEFINED
374 #  endif /* !GC_CLEAR_DEFINED */
375
376 #  if !defined(GC_TEST_AND_SET_DEFINED)
377 #    define USE_PTHREAD_LOCKS
378 #  endif
379
380 #  if defined(GC_PTHREADS) && !defined(GC_SOLARIS_THREADS) \
381       && !defined(GC_WIN32_THREADS)
382 #    define NO_THREAD (pthread_t)(-1)
383 #    include <pthread.h>
384 #    if defined(PARALLEL_MARK) 
385       /* We need compare-and-swap to update mark bits, where it's       */
386       /* performance critical.  If USE_MARK_BYTES is defined, it is     */
387       /* no longer needed for this purpose.  However we use it in       */
388       /* either case to implement atomic fetch-and-add, though that's   */
389       /* less performance critical, and could perhaps be done with      */
390       /* a lock.                                                        */
391 #     if defined(GENERIC_COMPARE_AND_SWAP)
392         /* Probably not useful, except for debugging.   */
393         /* We do use GENERIC_COMPARE_AND_SWAP on PA_RISC, but we        */
394         /* minimize its use.                                            */
395         extern pthread_mutex_t GC_compare_and_swap_lock;
396
397         /* Note that if GC_word updates are not atomic, a concurrent    */
398         /* reader should acquire GC_compare_and_swap_lock.  On          */
399         /* currently supported platforms, such updates are atomic.      */
400         extern GC_bool GC_compare_and_exchange(volatile GC_word *addr,
401                                                GC_word old, GC_word new_val);
402 #     endif /* GENERIC_COMPARE_AND_SWAP */
403 #     if defined(I386)
404 #      if !defined(GENERIC_COMPARE_AND_SWAP)
405          /* Returns TRUE if the comparison succeeded. */
406          inline static GC_bool GC_compare_and_exchange(volatile GC_word *addr,
407                                                        GC_word old,
408                                                        GC_word new_val) 
409          {
410            char result;
411            __asm__ __volatile__("lock; cmpxchgl %2, %0; setz %1"
412                 : "+m"(*(addr)), "=r"(result)
413                 : "r" (new_val), "a"(old) : "memory");
414            return (GC_bool) result;
415          }
416 #      endif /* !GENERIC_COMPARE_AND_SWAP */
417        inline static void GC_memory_barrier()
418        {
419          /* We believe the processor ensures at least processor */
420          /* consistent ordering.  Thus a compiler barrier       */
421          /* should suffice.                                     */
422          __asm__ __volatile__("" : : : "memory");
423        }
424 #     endif /* I386 */
425
426 #     if defined(POWERPC)
427 #      if !defined(GENERIC_COMPARE_AND_SWAP)
428 #       if CPP_WORDSZ == 64
429         /* Returns TRUE if the comparison succeeded. */
430         inline static GC_bool GC_compare_and_exchange(volatile GC_word *addr,
431             GC_word old, GC_word new_val) 
432         {
433             unsigned long result, dummy;
434             __asm__ __volatile__(
435                 "1:\tldarx %0,0,%5\n"
436                   "\tcmpd %0,%4\n"
437                   "\tbne  2f\n"
438                   "\tstdcx. %3,0,%2\n"
439                   "\tbne- 1b\n"
440                   "\tsync\n"
441                   "\tli %1, 1\n"
442                   "\tb 3f\n"
443                 "2:\tli %1, 0\n"
444                 "3:\t\n"
445                 :  "=&r" (dummy), "=r" (result), "=p" (addr)
446                 :  "r" (new_val), "r" (old), "2"(addr)
447                 : "cr0","memory");
448             return (GC_bool) result;
449         }
450 #       else
451         /* Returns TRUE if the comparison succeeded. */
452         inline static GC_bool GC_compare_and_exchange(volatile GC_word *addr,
453             GC_word old, GC_word new_val) 
454         {
455             int result, dummy;
456             __asm__ __volatile__(
457                 "1:\tlwarx %0,0,%5\n"
458                   "\tcmpw %0,%4\n"
459                   "\tbne  2f\n"
460                   "\tstwcx. %3,0,%2\n"
461                   "\tbne- 1b\n"
462                   "\tsync\n"
463                   "\tli %1, 1\n"
464                   "\tb 3f\n"
465                 "2:\tli %1, 0\n"
466                 "3:\t\n"
467                 :  "=&r" (dummy), "=r" (result), "=p" (addr)
468                 :  "r" (new_val), "r" (old), "2"(addr)
469                 : "cr0","memory");
470             return (GC_bool) result;
471         }
472 #       endif
473 #      endif /* !GENERIC_COMPARE_AND_SWAP */
474         inline static void GC_memory_barrier()
475         {
476             __asm__ __volatile__("sync" : : : "memory");
477         }
478 #     endif /* POWERPC */
479
480 #     if defined(IA64)
481 #      if !defined(GENERIC_COMPARE_AND_SWAP)
482          inline static GC_bool GC_compare_and_exchange(volatile GC_word *addr,
483                                                        GC_word old, GC_word new_val) 
484          {
485           unsigned long oldval;
486 #         if CPP_WORDSZ == 32
487             __asm__ __volatile__(
488                   "addp4 %0=0,%1\n"
489                   "mov ar.ccv=%3 ;; cmpxchg4.rel %0=[%0],%2,ar.ccv"
490                   : "=&r"(oldval)
491                   : "r"(addr), "r"(new_val), "r"(old) : "memory");
492 #         else
493             __asm__ __volatile__(
494                   "mov ar.ccv=%3 ;; cmpxchg8.rel %0=[%1],%2,ar.ccv"
495                   : "=r"(oldval)
496                   : "r"(addr), "r"(new_val), "r"(old) : "memory");
497 #         endif
498           return (oldval == old);
499          }
500 #      endif /* !GENERIC_COMPARE_AND_SWAP */
501 #      if 0
502         /* Shouldn't be needed; we use volatile stores instead. */
503         inline static void GC_memory_barrier()
504         {
505           __asm__ __volatile__("mf" : : : "memory");
506         }
507 #      endif /* 0 */
508 #     endif /* IA64 */
509 #     if defined(ALPHA)
510 #      if !defined(GENERIC_COMPARE_AND_SWAP)
511 #        if defined(__GNUC__)
512            inline static GC_bool GC_compare_and_exchange(volatile GC_word *addr,
513                                                          GC_word old, GC_word new_val) 
514            {
515              unsigned long was_equal;
516              unsigned long temp;
517
518              __asm__ __volatile__(
519                              "1:     ldq_l %0,%1\n"
520                              "       cmpeq %0,%4,%2\n"
521                              "       mov %3,%0\n"
522                              "       beq %2,2f\n"
523                              "       stq_c %0,%1\n"
524                              "       beq %0,1b\n"
525                              "2:\n"
526                              "       mb\n"
527                              :"=&r" (temp), "=m" (*addr), "=&r" (was_equal)
528                              : "r" (new_val), "Ir" (old)
529                              :"memory");
530              return was_equal;
531            }
532 #        else /* !__GNUC__ */
533            inline static GC_bool GC_compare_and_exchange(volatile GC_word *addr,
534                                                          GC_word old, GC_word new_val) 
535           {
536             return __CMP_STORE_QUAD(addr, old, new_val, addr);
537           }
538 #        endif /* !__GNUC__ */
539 #      endif /* !GENERIC_COMPARE_AND_SWAP */
540 #      ifdef __GNUC__
541          inline static void GC_memory_barrier()
542          {
543            __asm__ __volatile__("mb" : : : "memory");
544          }
545 #      else
546 #        define GC_memory_barrier() asm("mb")
547 #      endif /* !__GNUC__ */
548 #     endif /* ALPHA */
549 #     if defined(S390)
550 #      if !defined(GENERIC_COMPARE_AND_SWAP)
551          inline static GC_bool GC_compare_and_exchange(volatile C_word *addr,
552                                          GC_word old, GC_word new_val)
553          {
554            int retval;
555            __asm__ __volatile__ (
556 #            ifndef __s390x__
557                "     cs  %1,%2,0(%3)\n"
558 #            else
559                "     csg %1,%2,0(%3)\n"
560 #            endif
561              "     ipm %0\n"
562              "     srl %0,28\n"
563              : "=&d" (retval), "+d" (old)
564              : "d" (new_val), "a" (addr)
565              : "cc", "memory");
566            return retval == 0;
567          }
568 #      endif
569 #     endif
570 #     if !defined(GENERIC_COMPARE_AND_SWAP)
571         /* Returns the original value of *addr. */
572         inline static GC_word GC_atomic_add(volatile GC_word *addr,
573                                             GC_word how_much)
574         {
575           GC_word old;
576           do {
577             old = *addr;
578           } while (!GC_compare_and_exchange(addr, old, old+how_much));
579           return old;
580         }
581 #     else /* GENERIC_COMPARE_AND_SWAP */
582         /* So long as a GC_word can be atomically updated, it should    */
583         /* be OK to read *addr without a lock.                          */
584         extern GC_word GC_atomic_add(volatile GC_word *addr, GC_word how_much);
585 #     endif /* GENERIC_COMPARE_AND_SWAP */
586
587 #    endif /* PARALLEL_MARK */
588
589 #    if !defined(THREAD_LOCAL_ALLOC) && !defined(USE_PTHREAD_LOCKS)
590       /* In the THREAD_LOCAL_ALLOC case, the allocation lock tends to   */
591       /* be held for long periods, if it is held at all.  Thus spinning */
592       /* and sleeping for fixed periods are likely to result in         */
593       /* significant wasted time.  We thus rely mostly on queued locks. */
594 #     define USE_SPIN_LOCK
595       extern volatile unsigned int GC_allocate_lock;
596       extern void GC_lock(void);
597         /* Allocation lock holder.  Only set if acquired by client through */
598         /* GC_call_with_alloc_lock.                                        */
599 #     ifdef GC_ASSERTIONS
600 #        define LOCK() \
601                 { if (GC_test_and_set(&GC_allocate_lock)) GC_lock(); \
602                   SET_LOCK_HOLDER(); }
603 #        define UNLOCK() \
604                 { GC_ASSERT(I_HOLD_LOCK()); UNSET_LOCK_HOLDER(); \
605                   GC_clear(&GC_allocate_lock); }
606 #     else
607 #        define LOCK() \
608                 { if (GC_test_and_set(&GC_allocate_lock)) GC_lock(); }
609 #        define UNLOCK() \
610                 GC_clear(&GC_allocate_lock)
611 #     endif /* !GC_ASSERTIONS */
612 #     if 0
613         /* Another alternative for OSF1 might be:               */
614 #       include <sys/mman.h>
615         extern msemaphore GC_allocate_semaphore;
616 #       define LOCK() { if (msem_lock(&GC_allocate_semaphore, MSEM_IF_NOWAIT) \
617                             != 0) GC_lock(); else GC_allocate_lock = 1; }
618         /* The following is INCORRECT, since the memory model is too weak. */
619         /* Is this true?  Presumably msem_unlock has the right semantics?  */
620         /*              - HB                                               */
621 #       define UNLOCK() { GC_allocate_lock = 0; \
622                           msem_unlock(&GC_allocate_semaphore, 0); }
623 #     endif /* 0 */
624 #    else /* THREAD_LOCAL_ALLOC  || USE_PTHREAD_LOCKS */
625 #      ifndef USE_PTHREAD_LOCKS
626 #        define USE_PTHREAD_LOCKS
627 #      endif
628 #    endif /* THREAD_LOCAL_ALLOC */
629 #   ifdef USE_PTHREAD_LOCKS
630 #      include <pthread.h>
631        extern pthread_mutex_t GC_allocate_ml;
632 #      ifdef GC_ASSERTIONS
633 #        define LOCK() \
634                 { GC_lock(); \
635                   SET_LOCK_HOLDER(); }
636 #        define UNLOCK() \
637                 { GC_ASSERT(I_HOLD_LOCK()); UNSET_LOCK_HOLDER(); \
638                   pthread_mutex_unlock(&GC_allocate_ml); }
639 #      else /* !GC_ASSERTIONS */
640 #        if defined(NO_PTHREAD_TRYLOCK)
641 #          define LOCK() GC_lock();
642 #        else /* !defined(NO_PTHREAD_TRYLOCK) */
643 #        define LOCK() \
644            { if (0 != pthread_mutex_trylock(&GC_allocate_ml)) GC_lock(); }
645 #        endif
646 #        define UNLOCK() pthread_mutex_unlock(&GC_allocate_ml)
647 #      endif /* !GC_ASSERTIONS */
648 #   endif /* USE_PTHREAD_LOCKS */
649 #   define SET_LOCK_HOLDER() GC_lock_holder = pthread_self()
650 #   define UNSET_LOCK_HOLDER() GC_lock_holder = NO_THREAD
651 #   define I_HOLD_LOCK() (pthread_equal(GC_lock_holder, pthread_self()))
652     extern VOLATILE GC_bool GC_collecting;
653 #   define ENTER_GC() GC_collecting = 1;
654 #   define EXIT_GC() GC_collecting = 0;
655     extern void GC_lock(void);
656     extern pthread_t GC_lock_holder;
657 #   ifdef GC_ASSERTIONS
658       extern pthread_t GC_mark_lock_holder;
659 #   endif
660 #  endif /* GC_PTHREADS with linux_threads.c implementation */
661 #  if defined(GC_WIN32_THREADS)
662 #    if defined(GC_PTHREADS)
663 #      include <pthread.h>
664        extern pthread_mutex_t GC_allocate_ml;
665 #      define LOCK()   pthread_mutex_lock(&GC_allocate_ml)
666 #      define UNLOCK() pthread_mutex_unlock(&GC_allocate_ml)
667 #    else
668 #      include <windows.h>
669        GC_API CRITICAL_SECTION GC_allocate_ml;
670 #      define LOCK() EnterCriticalSection(&GC_allocate_ml);
671 #      define UNLOCK() LeaveCriticalSection(&GC_allocate_ml);
672 #    endif
673 #  endif
674 #  ifndef SET_LOCK_HOLDER
675 #      define SET_LOCK_HOLDER()
676 #      define UNSET_LOCK_HOLDER()
677 #      define I_HOLD_LOCK() FALSE
678                 /* Used on platforms were locks can be reacquired,      */
679                 /* so it doesn't matter if we lie.                      */
680 #  endif
681 # else /* !THREADS */
682 #    define LOCK()
683 #    define UNLOCK()
684 # endif /* !THREADS */
685 # ifndef SET_LOCK_HOLDER
686 #   define SET_LOCK_HOLDER()
687 #   define UNSET_LOCK_HOLDER()
688 #   define I_HOLD_LOCK() FALSE
689                 /* Used on platforms were locks can be reacquired,      */
690                 /* so it doesn't matter if we lie.                      */
691 # endif
692 # ifndef ENTER_GC
693 #   define ENTER_GC()
694 #   define EXIT_GC()
695 # endif
696
697 # ifndef DCL_LOCK_STATE
698 #   define DCL_LOCK_STATE
699 # endif
700 # ifndef FASTLOCK
701 #   define FASTLOCK() LOCK()
702 #   define FASTLOCK_SUCCEEDED() TRUE
703 #   define FASTUNLOCK() UNLOCK()
704 # endif
705
706 #endif /* GC_LOCKS_H */