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