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
* In the PARALLEL_MARK case, we also need to define a number of
27
* other inline finctions here:
28
* GC_bool GC_compare_and_exchange( volatile GC_word *addr,
29
* GC_word old, GC_word new )
30
* GC_word GC_atomic_add( volatile GC_word *addr, GC_word how_much )
31
* void GC_memory_barrier( )
33
* Note that I_HOLD_LOCK and I_DONT_HOLD_LOCK are used only positively
34
* in assertions, and may return TRUE in the "dont know" case.
37
# include <atomic_ops.h>
41
# include <base/PCR_Base.h>
42
# include <th/PCR_Th.h>
43
extern PCR_Th_ML GC_allocate_ml;
44
# define DCL_LOCK_STATE \
45
PCR_ERes GC_fastLockRes; PCR_sigset_t GC_old_sig_mask
46
# define LOCK() PCR_Th_ML_Acquire(&GC_allocate_ml)
47
# define UNLOCK() PCR_Th_ML_Release(&GC_allocate_ml)
50
# if !defined(AO_HAVE_test_and_set_acquire)
51
# define USE_PTHREAD_LOCKS
54
# if defined(GC_WIN32_THREADS) && defined(GC_PTHREADS)
55
# define USE_PTHREAD_LOCKS
58
# if defined(GC_WIN32_THREADS) && !defined(USE_PTHREAD_LOCKS)
60
# define NO_THREAD (DWORD)(-1)
61
extern DWORD GC_lock_holder;
62
GC_API CRITICAL_SECTION GC_allocate_ml;
64
# define UNCOND_LOCK() \
65
{ EnterCriticalSection(&GC_allocate_ml); \
67
# define UNCOND_UNLOCK() \
68
{ GC_ASSERT(I_HOLD_LOCK()); UNSET_LOCK_HOLDER(); \
69
LeaveCriticalSection(&GC_allocate_ml); }
71
# define UNCOND_LOCK() EnterCriticalSection(&GC_allocate_ml);
72
# define UNCOND_UNLOCK() LeaveCriticalSection(&GC_allocate_ml);
73
# endif /* !GC_ASSERTIONS */
74
# define SET_LOCK_HOLDER() GC_lock_holder = GetCurrentThreadId()
75
# define UNSET_LOCK_HOLDER() GC_lock_holder = NO_THREAD
76
# define I_HOLD_LOCK() (!GC_need_to_lock \
77
|| GC_lock_holder == GetCurrentThreadId())
78
# define I_DONT_HOLD_LOCK() (!GC_need_to_lock \
79
|| GC_lock_holder != GetCurrentThreadId())
80
# elif defined(GC_PTHREADS)
81
# define NO_THREAD (pthread_t)(-1)
84
# if !defined(THREAD_LOCAL_ALLOC) && !defined(USE_PTHREAD_LOCKS)
85
/* In the THREAD_LOCAL_ALLOC case, the allocation lock tends to */
86
/* be held for long periods, if it is held at all. Thus spinning */
87
/* and sleeping for fixed periods are likely to result in */
88
/* significant wasted time. We thus rely mostly on queued locks. */
89
# define USE_SPIN_LOCK
90
extern volatile AO_TS_t GC_allocate_lock;
91
extern void GC_lock(void);
92
/* Allocation lock holder. Only set if acquired by client through */
93
/* GC_call_with_alloc_lock. */
95
# define UNCOND_LOCK() \
96
{ if (AO_test_and_set_acquire(&GC_allocate_lock) == AO_TS_SET) \
99
# define UNCOND_UNLOCK() \
100
{ GC_ASSERT(I_HOLD_LOCK()); UNSET_LOCK_HOLDER(); \
101
AO_CLEAR(&GC_allocate_lock); }
103
# define UNCOND_LOCK() \
104
{ if (AO_test_and_set_acquire(&GC_allocate_lock) == AO_TS_SET) \
106
# define UNCOND_UNLOCK() \
107
AO_CLEAR(&GC_allocate_lock)
108
# endif /* !GC_ASSERTIONS */
109
# else /* THREAD_LOCAL_ALLOC || USE_PTHREAD_LOCKS */
110
# ifndef USE_PTHREAD_LOCKS
111
# define USE_PTHREAD_LOCKS
113
# endif /* THREAD_LOCAL_ALLOC || USE_PTHREAD_LOCK */
114
# ifdef USE_PTHREAD_LOCKS
115
# include <pthread.h>
116
extern pthread_mutex_t GC_allocate_ml;
117
# ifdef GC_ASSERTIONS
118
# define UNCOND_LOCK() \
121
# define UNCOND_UNLOCK() \
122
{ GC_ASSERT(I_HOLD_LOCK()); UNSET_LOCK_HOLDER(); \
123
pthread_mutex_unlock(&GC_allocate_ml); }
124
# else /* !GC_ASSERTIONS */
125
# if defined(NO_PTHREAD_TRYLOCK)
126
# define UNCOND_LOCK() GC_lock();
127
# else /* !defined(NO_PTHREAD_TRYLOCK) */
128
# define UNCOND_LOCK() \
129
{ if (0 != pthread_mutex_trylock(&GC_allocate_ml)) GC_lock(); }
131
# define UNCOND_UNLOCK() pthread_mutex_unlock(&GC_allocate_ml)
132
# endif /* !GC_ASSERTIONS */
133
# endif /* USE_PTHREAD_LOCKS */
134
# define SET_LOCK_HOLDER() GC_lock_holder = pthread_self()
135
# define UNSET_LOCK_HOLDER() GC_lock_holder = NO_THREAD
136
# define I_HOLD_LOCK() (!GC_need_to_lock \
137
|| pthread_equal(GC_lock_holder, pthread_self()))
138
# define I_DONT_HOLD_LOCK() (!GC_need_to_lock \
139
|| !pthread_equal(GC_lock_holder, pthread_self()))
140
extern volatile GC_bool GC_collecting;
141
# define ENTER_GC() GC_collecting = 1;
142
# define EXIT_GC() GC_collecting = 0;
143
extern void GC_lock(void);
144
extern pthread_t GC_lock_holder;
145
# ifdef GC_ASSERTIONS
146
extern pthread_t GC_mark_lock_holder;
148
# endif /* GC_PTHREADS with linux_threads.c implementation */
151
# else /* !THREADS */
154
# define SET_LOCK_HOLDER()
155
# define UNSET_LOCK_HOLDER()
156
# define I_HOLD_LOCK() TRUE
157
# define I_DONT_HOLD_LOCK() TRUE
158
/* Used only in positive assertions or to test whether */
159
/* we still need to acaquire the lock. TRUE works in */
161
# endif /* !THREADS */
163
#if defined(UNCOND_LOCK) && !defined(LOCK)
164
GC_API GC_bool GC_need_to_lock;
165
/* At least two thread running; need to lock. */
166
# define LOCK() if (GC_need_to_lock) { UNCOND_LOCK(); }
167
# define UNLOCK() if (GC_need_to_lock) { UNCOND_UNLOCK(); }
175
# ifndef DCL_LOCK_STATE
176
# define DCL_LOCK_STATE
179
#endif /* GC_LOCKS_H */