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