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.
8 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
9 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
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.
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.
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.
31 # if defined(GC_PTHREADS) && !defined(GC_WIN32_THREADS)
32 # include "atomic_ops.h"
35 GC_API void GC_CALL GC_noop1(word);
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)
46 # if !defined(AO_HAVE_test_and_set_acquire) && defined(GC_PTHREADS)
47 # define USE_PTHREAD_LOCKS
50 # if defined(GC_WIN32_THREADS) && defined(GC_PTHREADS)
51 # define USE_PTHREAD_LOCKS
54 # if defined(GC_WIN32_THREADS) && !defined(USE_PTHREAD_LOCKS)
56 # define NO_THREAD (DWORD)(-1)
57 GC_EXTERN DWORD GC_lock_holder;
58 GC_EXTERN CRITICAL_SECTION GC_allocate_ml;
60 # define UNCOND_LOCK() \
61 { EnterCriticalSection(&GC_allocate_ml); \
63 # define UNCOND_UNLOCK() \
64 { GC_ASSERT(I_HOLD_LOCK()); UNSET_LOCK_HOLDER(); \
65 LeaveCriticalSection(&GC_allocate_ml); }
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)
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 */
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
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
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
108 # define NO_THREAD ((unsigned long)(-1l))
109 /* != NUMERIC_THREAD_ID(pthread_self()) for any thread */
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) \
126 # define UNCOND_UNLOCK() \
127 { GC_ASSERT(I_HOLD_LOCK()); UNSET_LOCK_HOLDER(); \
128 AO_CLEAR(&GC_allocate_lock); }
130 # define UNCOND_LOCK() \
131 { if (AO_test_and_set_acquire(&GC_allocate_lock) == AO_TS_SET) \
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
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() \
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(); }
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 */
170 # define I_DONT_HOLD_LOCK() \
172 || GC_lock_holder != NUMERIC_THREAD_ID(pthread_self()))
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;
182 # endif /* GC_PTHREADS with linux_threads.c implementation */
185 # else /* !THREADS */
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 */
195 # endif /* !THREADS */
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(); }
209 # ifndef DCL_LOCK_STATE
210 # define DCL_LOCK_STATE
213 #endif /* GC_LOCKS_H */