gc_locks.h 12 KB

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