Debian Codegraph @

external/bdwgc/include/private/gc_locks.h

raw
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 # ifdef PCR
32 # include <base/PCR_Base.h>
33 # include <th/PCR_Th.h>
34 # endif
35
36 EXTERN_C_BEGIN
37
38 # ifdef PCR
39 GC_EXTERN PCR_Th_ML GC_allocate_ml;
40 # if defined(CPPCHECK)
41 # define DCL_LOCK_STATE /* empty */
42 # else
43 # define DCL_LOCK_STATE \
44 PCR_ERes GC_fastLockRes; PCR_sigset_t GC_old_sig_mask
45 # endif
46 # define UNCOND_LOCK() PCR_Th_ML_Acquire(&GC_allocate_ml)
47 # define UNCOND_UNLOCK() PCR_Th_ML_Release(&GC_allocate_ml)
48 # endif
49
50 # if (!defined(AO_HAVE_test_and_set_acquire) || defined(GC_RTEMS_PTHREADS) \
51 || defined(SN_TARGET_PS3) \
52 || defined(GC_WIN32_THREADS) || defined(LINT2)) && defined(GC_PTHREADS)
53 # define USE_PTHREAD_LOCKS
54 # undef USE_SPIN_LOCK
55 # endif
56
57 # if defined(GC_WIN32_THREADS) && !defined(USE_PTHREAD_LOCKS)
58 # ifndef WIN32_LEAN_AND_MEAN
59 # define WIN32_LEAN_AND_MEAN 1
60 # endif
61 # define NOSERVICE
62 EXTERN_C_END
63 # include <windows.h>
64 EXTERN_C_BEGIN
65 # define NO_THREAD (DWORD)(-1)
66 GC_EXTERN CRITICAL_SECTION GC_allocate_ml;
67 # ifdef GC_ASSERTIONS
68 GC_EXTERN DWORD GC_lock_holder;
69 # define SET_LOCK_HOLDER() GC_lock_holder = GetCurrentThreadId()
70 # define UNSET_LOCK_HOLDER() GC_lock_holder = NO_THREAD
71 # define I_HOLD_LOCK() (!GC_need_to_lock \
72 || GC_lock_holder == GetCurrentThreadId())
73 # ifdef THREAD_SANITIZER
74 # define I_DONT_HOLD_LOCK() TRUE /* Conservatively say yes */
75 # else
76 # define I_DONT_HOLD_LOCK() (!GC_need_to_lock \
77 || GC_lock_holder != GetCurrentThreadId())
78 # endif
79 # define UNCOND_LOCK() \
80 { GC_ASSERT(I_DONT_HOLD_LOCK()); \
81 EnterCriticalSection(&GC_allocate_ml); \
82 SET_LOCK_HOLDER(); }
83 # define UNCOND_UNLOCK() \
84 { GC_ASSERT(I_HOLD_LOCK()); UNSET_LOCK_HOLDER(); \
85 LeaveCriticalSection(&GC_allocate_ml); }
86 # else
87 # define UNCOND_LOCK() EnterCriticalSection(&GC_allocate_ml)
88 # define UNCOND_UNLOCK() LeaveCriticalSection(&GC_allocate_ml)
89 # endif /* !GC_ASSERTIONS */
90 # elif defined(GC_PTHREADS)
91 EXTERN_C_END
92 # include <pthread.h>
93 EXTERN_C_BEGIN
94 /* Posix allows pthread_t to be a struct, though it rarely is. */
95 /* Unfortunately, we need to use a pthread_t to index a data */
96 /* structure. It also helps if comparisons don't involve a */
97 /* function call. Hence we introduce platform-dependent macros */
98 /* to compare pthread_t ids and to map them to integers. */
99 /* The mapping to integers does not need to result in different */
100 /* integers for each thread, though that should be true as much */
101 /* as possible. */
102 /* Refine to exclude platforms on which pthread_t is struct. */
103 # if !defined(GC_WIN32_PTHREADS)
104 # define NUMERIC_THREAD_ID(id) ((unsigned long)(id))
105 # define THREAD_EQUAL(id1, id2) ((id1) == (id2))
106 # define NUMERIC_THREAD_ID_UNIQUE
107 # elif defined(__WINPTHREADS_VERSION_MAJOR) /* winpthreads */
108 # define NUMERIC_THREAD_ID(id) ((unsigned long)(id))
109 # define THREAD_EQUAL(id1, id2) ((id1) == (id2))
110 # ifndef _WIN64
111 /* NUMERIC_THREAD_ID is 32-bit and not unique on Win64. */
112 # define NUMERIC_THREAD_ID_UNIQUE
113 # endif
114 # else /* pthreads-win32 */
115 # define NUMERIC_THREAD_ID(id) ((unsigned long)(word)(id.p))
116 /* Using documented internal details of pthreads-win32 library. */
117 /* Faster than pthread_equal(). Should not change with */
118 /* future versions of pthreads-win32 library. */
119 # define THREAD_EQUAL(id1, id2) ((id1.p == id2.p) && (id1.x == id2.x))
120 # undef NUMERIC_THREAD_ID_UNIQUE
121 /* Generic definitions based on pthread_equal() always work but */
122 /* will result in poor performance (as NUMERIC_THREAD_ID is */
123 /* defined to just a constant) and weak assertion checking. */
124 # endif
125 # define NO_THREAD ((unsigned long)(-1l))
126 /* != NUMERIC_THREAD_ID(pthread_self()) for any thread */
127
128 # ifdef SN_TARGET_PSP2
129 EXTERN_C_END
130 # include "psp2-support.h"
131 EXTERN_C_BEGIN
132 GC_EXTERN WapiMutex GC_allocate_ml_PSP2;
133 # define UNCOND_LOCK() { int res; GC_ASSERT(I_DONT_HOLD_LOCK()); \
134 res = PSP2_MutexLock(&GC_allocate_ml_PSP2); \
135 GC_ASSERT(0 == res); (void)res; \
136 SET_LOCK_HOLDER(); }
137 # define UNCOND_UNLOCK() { int res; GC_ASSERT(I_HOLD_LOCK()); \
138 UNSET_LOCK_HOLDER(); \
139 res = PSP2_MutexUnlock(&GC_allocate_ml_PSP2); \
140 GC_ASSERT(0 == res); (void)res; }
141
142 # elif (!defined(THREAD_LOCAL_ALLOC) || defined(USE_SPIN_LOCK)) \
143 && !defined(USE_PTHREAD_LOCKS)
144 /* In the THREAD_LOCAL_ALLOC case, the allocation lock tends to */
145 /* be held for long periods, if it is held at all. Thus spinning */
146 /* and sleeping for fixed periods are likely to result in */
147 /* significant wasted time. We thus rely mostly on queued locks. */
148 # undef USE_SPIN_LOCK
149 # define USE_SPIN_LOCK
150 GC_EXTERN volatile AO_TS_t GC_allocate_lock;
151 GC_INNER void GC_lock(void);
152 /* Allocation lock holder. Only set if acquired by client through */
153 /* GC_call_with_alloc_lock. */
154 # ifdef GC_ASSERTIONS
155 # define UNCOND_LOCK() \
156 { GC_ASSERT(I_DONT_HOLD_LOCK()); \
157 if (AO_test_and_set_acquire(&GC_allocate_lock) == AO_TS_SET) \
158 GC_lock(); \
159 SET_LOCK_HOLDER(); }
160 # define UNCOND_UNLOCK() \
161 { GC_ASSERT(I_HOLD_LOCK()); UNSET_LOCK_HOLDER(); \
162 AO_CLEAR(&GC_allocate_lock); }
163 # else
164 # define UNCOND_LOCK() \
165 { if (AO_test_and_set_acquire(&GC_allocate_lock) == AO_TS_SET) \
166 GC_lock(); }
167 # define UNCOND_UNLOCK() AO_CLEAR(&GC_allocate_lock)
168 # endif /* !GC_ASSERTIONS */
169 # else /* THREAD_LOCAL_ALLOC || USE_PTHREAD_LOCKS */
170 # ifndef USE_PTHREAD_LOCKS
171 # define USE_PTHREAD_LOCKS
172 # endif
173 # endif /* THREAD_LOCAL_ALLOC || USE_PTHREAD_LOCKS */
174 # ifdef USE_PTHREAD_LOCKS
175 EXTERN_C_END
176 # include <pthread.h>
177 EXTERN_C_BEGIN
178 GC_EXTERN pthread_mutex_t GC_allocate_ml;
179 # ifdef GC_ASSERTIONS
180 # define UNCOND_LOCK() { GC_ASSERT(I_DONT_HOLD_LOCK()); \
181 GC_lock(); SET_LOCK_HOLDER(); }
182 # define UNCOND_UNLOCK() \
183 { GC_ASSERT(I_HOLD_LOCK()); UNSET_LOCK_HOLDER(); \
184 pthread_mutex_unlock(&GC_allocate_ml); }
185 # else /* !GC_ASSERTIONS */
186 # if defined(NO_PTHREAD_TRYLOCK)
187 # define UNCOND_LOCK() pthread_mutex_lock(&GC_allocate_ml)
188 # else
189 # define UNCOND_LOCK() \
190 { if (0 != pthread_mutex_trylock(&GC_allocate_ml)) \
191 GC_lock(); }
192 # endif
193 # define UNCOND_UNLOCK() pthread_mutex_unlock(&GC_allocate_ml)
194 # endif /* !GC_ASSERTIONS */
195 # endif /* USE_PTHREAD_LOCKS */
196 # ifdef GC_ASSERTIONS
197 GC_EXTERN unsigned long GC_lock_holder;
198 # define SET_LOCK_HOLDER() \
199 GC_lock_holder = NUMERIC_THREAD_ID(pthread_self())
200 # define UNSET_LOCK_HOLDER() GC_lock_holder = NO_THREAD
201 # define I_HOLD_LOCK() \
202 (!GC_need_to_lock \
203 || GC_lock_holder == NUMERIC_THREAD_ID(pthread_self()))
204 # if !defined(NUMERIC_THREAD_ID_UNIQUE) || defined(THREAD_SANITIZER)
205 # define I_DONT_HOLD_LOCK() TRUE /* Conservatively say yes */
206 # else
207 # define I_DONT_HOLD_LOCK() \
208 (!GC_need_to_lock \
209 || GC_lock_holder != NUMERIC_THREAD_ID(pthread_self()))
210 # endif
211 # endif /* GC_ASSERTIONS */
212 # ifndef GC_WIN32_THREADS
213 GC_EXTERN volatile GC_bool GC_collecting;
214 # ifdef AO_HAVE_char_store
215 # define ENTER_GC() AO_char_store((unsigned char*)&GC_collecting, TRUE)
216 # define EXIT_GC() AO_char_store((unsigned char*)&GC_collecting, FALSE)
217 # else
218 # define ENTER_GC() (void)(GC_collecting = TRUE)
219 # define EXIT_GC() (void)(GC_collecting = FALSE)
220 # endif
221 # endif
222 GC_INNER void GC_lock(void);
223 # endif /* GC_PTHREADS */
224 # if defined(GC_ALWAYS_MULTITHREADED) \
225 && (defined(USE_PTHREAD_LOCKS) || defined(USE_SPIN_LOCK))
226 # define GC_need_to_lock TRUE
227 # define set_need_to_lock() (void)0
228 # else
229 # if defined(GC_ALWAYS_MULTITHREADED) && !defined(CPPCHECK)
230 # error Runtime initialization of GC lock is needed!
231 # endif
232 # undef GC_ALWAYS_MULTITHREADED
233 GC_EXTERN GC_bool GC_need_to_lock;
234 # ifdef THREAD_SANITIZER
235 /* To workaround TSan false positive (e.g., when */
236 /* GC_pthread_create is called from multiple threads in */
237 /* parallel), do not set GC_need_to_lock if it is already set. */
238 # define set_need_to_lock() \
239 (void)(*(GC_bool volatile *)&GC_need_to_lock \
240 ? FALSE \
241 : (GC_need_to_lock = TRUE))
242 # else
243 # define set_need_to_lock() (void)(GC_need_to_lock = TRUE)
244 /* We are multi-threaded now. */
245 # endif
246 # endif
247
248 #if !defined(UNCOND_LOCK)
249 extern void GC_lock(void);
250 extern void GC_unlock(void);
251 # define UNCOND_LOCK() GC_lock()
252 # define UNCOND_UNLOCK() GC_unlock()
253 #endif
254
255 EXTERN_C_END
256
257 # else /* !THREADS */
258 # define LOCK() (void)0
259 # define UNLOCK() (void)0
260 # ifdef GC_ASSERTIONS
261 # define I_HOLD_LOCK() TRUE
262 # define I_DONT_HOLD_LOCK() TRUE
263 /* Used only in positive assertions or to test whether */
264 /* we still need to acquire the lock. TRUE works in */
265 /* either case. */
266 # endif
267 # endif /* !THREADS */
268
269 #if defined(UNCOND_LOCK) && !defined(LOCK)
270 # if (defined(LINT2) && defined(USE_PTHREAD_LOCKS)) \
271 || defined(GC_ALWAYS_MULTITHREADED)
272 /* Instruct code analysis tools not to care about GC_need_to_lock */
273 /* influence to LOCK/UNLOCK semantic. */
274 # define LOCK() UNCOND_LOCK()
275 # define UNLOCK() UNCOND_UNLOCK()
276 # else
277 /* At least two thread running; need to lock. */
278 # define LOCK() do { if (GC_need_to_lock) UNCOND_LOCK(); } while (0)
279 # define UNLOCK() do { if (GC_need_to_lock) UNCOND_UNLOCK(); } while (0)
280 # endif
281 #endif
282
283 # ifndef ENTER_GC
284 # define ENTER_GC()
285 # define EXIT_GC()
286 # endif
287
288 # ifndef DCL_LOCK_STATE
289 # define DCL_LOCK_STATE
290 # endif
291
292 #endif /* GC_LOCKS_H */