Upgrade Boehm GC to 7.2alpha4.
[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  * DCL_LOCK_STATE declares any local variables needed by LOCK and UNLOCK.
25  *
26  * Note that I_HOLD_LOCK and I_DONT_HOLD_LOCK are used only positively
27  * in assertions, and may return TRUE in the "don't know" case.
28  */
29 # ifdef THREADS
30
31 #  if defined(GC_PTHREADS) && !defined(GC_WIN32_THREADS)
32 #    include "atomic_ops.h"
33 #  endif
34
35    GC_API void GC_CALL GC_noop1(word);
36 #  ifdef PCR
37 #    include <base/PCR_Base.h>
38 #    include <th/PCR_Th.h>
39      GC_EXTERN PCR_Th_ML GC_allocate_ml;
40 #    define DCL_LOCK_STATE \
41          PCR_ERes GC_fastLockRes; PCR_sigset_t GC_old_sig_mask
42 #    define UNCOND_LOCK() PCR_Th_ML_Acquire(&GC_allocate_ml)
43 #    define UNCOND_UNLOCK() PCR_Th_ML_Release(&GC_allocate_ml)
44 #  endif
45
46 #  if !defined(AO_HAVE_test_and_set_acquire) && defined(GC_PTHREADS)
47 #    define USE_PTHREAD_LOCKS
48 #  endif
49
50 #  if defined(GC_WIN32_THREADS) && defined(GC_PTHREADS)
51 #    define USE_PTHREAD_LOCKS
52 #  endif
53
54 #  if defined(GC_WIN32_THREADS) && !defined(USE_PTHREAD_LOCKS)
55 #    include <windows.h>
56 #    define NO_THREAD (DWORD)(-1)
57      GC_EXTERN DWORD GC_lock_holder;
58      GC_EXTERN CRITICAL_SECTION GC_allocate_ml;
59 #    ifdef GC_ASSERTIONS
60 #        define UNCOND_LOCK() \
61                 { EnterCriticalSection(&GC_allocate_ml); \
62                   SET_LOCK_HOLDER(); }
63 #        define UNCOND_UNLOCK() \
64                 { GC_ASSERT(I_HOLD_LOCK()); UNSET_LOCK_HOLDER(); \
65                   LeaveCriticalSection(&GC_allocate_ml); }
66 #    else
67 #      define UNCOND_LOCK() EnterCriticalSection(&GC_allocate_ml);
68 #      define UNCOND_UNLOCK() LeaveCriticalSection(&GC_allocate_ml);
69 #    endif /* !GC_ASSERTIONS */
70 #    define SET_LOCK_HOLDER() GC_lock_holder = GetCurrentThreadId()
71 #    define UNSET_LOCK_HOLDER() GC_lock_holder = NO_THREAD
72 #    define I_HOLD_LOCK() (!GC_need_to_lock \
73                            || GC_lock_holder == GetCurrentThreadId())
74 #    define I_DONT_HOLD_LOCK() (!GC_need_to_lock \
75                            || GC_lock_holder != GetCurrentThreadId())
76 #  elif defined(GC_PTHREADS)
77 #    include <pthread.h>
78
79      /* Posix allows pthread_t to be a struct, though it rarely is.     */
80      /* Unfortunately, we need to use a pthread_t to index a data       */
81      /* structure.  It also helps if comparisons don't involve a        */
82      /* function call.  Hence we introduce platform-dependent macros    */
83      /* to compare pthread_t ids and to map them to integers.           */
84      /* the mapping to integers does not need to result in different    */
85      /* integers for each thread, though that should be true as much    */
86      /* as possible.                                                    */
87      /* Refine to exclude platforms on which pthread_t is struct */
88 #    if !defined(GC_WIN32_PTHREADS)
89 #      define NUMERIC_THREAD_ID(id) ((unsigned long)(id))
90 #      define THREAD_EQUAL(id1, id2) ((id1) == (id2))
91 #      define NUMERIC_THREAD_ID_UNIQUE
92 #    else
93 #      if defined(GC_WIN32_PTHREADS)
94 #        define NUMERIC_THREAD_ID(id) ((unsigned long)(id.p))
95          /* Using documented internal details of win32_pthread library. */
96          /* Faster than pthread_equal(). Should not change with         */
97          /* future versions of win32_pthread library.                   */
98 #        define THREAD_EQUAL(id1, id2) ((id1.p == id2.p) && (id1.x == id2.x))
99 #        undef NUMERIC_THREAD_ID_UNIQUE
100 #      else
101          /* Generic definitions that always work, but will result in    */
102          /* poor performance and weak assertion checking.               */
103 #        define NUMERIC_THREAD_ID(id) 1l
104 #        define THREAD_EQUAL(id1, id2) pthread_equal(id1, id2)
105 #        undef NUMERIC_THREAD_ID_UNIQUE
106 #      endif
107 #    endif
108 #    define NO_THREAD ((unsigned long)(-1l))
109                 /* != NUMERIC_THREAD_ID(pthread_self()) for any thread */
110
111 #    if !defined(THREAD_LOCAL_ALLOC) && !defined(USE_PTHREAD_LOCKS)
112       /* In the THREAD_LOCAL_ALLOC case, the allocation lock tends to   */
113       /* be held for long periods, if it is held at all.  Thus spinning */
114       /* and sleeping for fixed periods are likely to result in         */
115       /* significant wasted time.  We thus rely mostly on queued locks. */
116 #     define USE_SPIN_LOCK
117       GC_EXTERN volatile AO_TS_t GC_allocate_lock;
118       GC_INNER void GC_lock(void);
119         /* Allocation lock holder.  Only set if acquired by client through */
120         /* GC_call_with_alloc_lock.                                        */
121 #     ifdef GC_ASSERTIONS
122 #        define UNCOND_LOCK() \
123                 { if (AO_test_and_set_acquire(&GC_allocate_lock) == AO_TS_SET) \
124                         GC_lock(); \
125                   SET_LOCK_HOLDER(); }
126 #        define UNCOND_UNLOCK() \
127                 { GC_ASSERT(I_HOLD_LOCK()); UNSET_LOCK_HOLDER(); \
128                   AO_CLEAR(&GC_allocate_lock); }
129 #     else
130 #        define UNCOND_LOCK() \
131                 { if (AO_test_and_set_acquire(&GC_allocate_lock) == AO_TS_SET) \
132                         GC_lock(); }
133 #        define UNCOND_UNLOCK() \
134                 AO_CLEAR(&GC_allocate_lock)
135 #     endif /* !GC_ASSERTIONS */
136 #    else /* THREAD_LOCAL_ALLOC  || USE_PTHREAD_LOCKS */
137 #      ifndef USE_PTHREAD_LOCKS
138 #        define USE_PTHREAD_LOCKS
139 #      endif
140 #    endif /* THREAD_LOCAL_ALLOC || USE_PTHREAD_LOCK */
141 #    ifdef USE_PTHREAD_LOCKS
142 #      include <pthread.h>
143        GC_EXTERN pthread_mutex_t GC_allocate_ml;
144 #      ifdef GC_ASSERTIONS
145 #        define UNCOND_LOCK() \
146                 { GC_lock(); \
147                   SET_LOCK_HOLDER(); }
148 #        define UNCOND_UNLOCK() \
149                 { GC_ASSERT(I_HOLD_LOCK()); UNSET_LOCK_HOLDER(); \
150                   pthread_mutex_unlock(&GC_allocate_ml); }
151 #      else /* !GC_ASSERTIONS */
152 #        if defined(NO_PTHREAD_TRYLOCK)
153 #          define UNCOND_LOCK() GC_lock();
154 #        else /* !defined(NO_PTHREAD_TRYLOCK) */
155 #        define UNCOND_LOCK() \
156            { if (0 != pthread_mutex_trylock(&GC_allocate_ml)) GC_lock(); }
157 #        endif
158 #        define UNCOND_UNLOCK() pthread_mutex_unlock(&GC_allocate_ml)
159 #      endif /* !GC_ASSERTIONS */
160 #    endif /* USE_PTHREAD_LOCKS */
161 #    define SET_LOCK_HOLDER() \
162                 GC_lock_holder = NUMERIC_THREAD_ID(pthread_self())
163 #    define UNSET_LOCK_HOLDER() GC_lock_holder = NO_THREAD
164 #    define I_HOLD_LOCK() \
165                 (!GC_need_to_lock || \
166                  GC_lock_holder == NUMERIC_THREAD_ID(pthread_self()))
167 #    ifndef NUMERIC_THREAD_ID_UNIQUE
168 #      define I_DONT_HOLD_LOCK() 1  /* Conservatively say yes */
169 #    else
170 #      define I_DONT_HOLD_LOCK() \
171                 (!GC_need_to_lock \
172                  || GC_lock_holder != NUMERIC_THREAD_ID(pthread_self()))
173 #    endif
174      GC_EXTERN volatile GC_bool GC_collecting;
175 #    define ENTER_GC() GC_collecting = 1;
176 #    define EXIT_GC() GC_collecting = 0;
177      GC_INNER void GC_lock(void);
178      GC_EXTERN unsigned long GC_lock_holder;
179 #    ifdef GC_ASSERTIONS
180        GC_EXTERN unsigned long GC_mark_lock_holder;
181 #    endif
182 #  endif /* GC_PTHREADS with linux_threads.c implementation */
183
184
185 # else /* !THREADS */
186 #   define LOCK()
187 #   define UNLOCK()
188 #   define SET_LOCK_HOLDER()
189 #   define UNSET_LOCK_HOLDER()
190 #   define I_HOLD_LOCK() TRUE
191 #   define I_DONT_HOLD_LOCK() TRUE
192                 /* Used only in positive assertions or to test whether  */
193                 /* we still need to acquire the lock.  TRUE works in    */
194                 /* either case.                                         */
195 # endif /* !THREADS */
196
197 #if defined(UNCOND_LOCK) && !defined(LOCK)
198      GC_EXTERN GC_bool GC_need_to_lock;
199                 /* At least two thread running; need to lock.   */
200 #    define LOCK() if (GC_need_to_lock) { UNCOND_LOCK(); }
201 #    define UNLOCK() if (GC_need_to_lock) { UNCOND_UNLOCK(); }
202 #endif
203
204 # ifndef ENTER_GC
205 #   define ENTER_GC()
206 #   define EXIT_GC()
207 # endif
208
209 # ifndef DCL_LOCK_STATE
210 #   define DCL_LOCK_STATE
211 # endif
212
213 #endif /* GC_LOCKS_H */