implemented Setup.hs to build boehm cpp libs and install them;
[hs-boehmgc.git] / gc-7.2 / include / private / gc_locks.h
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 #  if defined(GC_PTHREADS) && !defined(GC_WIN32_THREADS)
32 #    include "atomic_ops.h"
33 #  endif
34
35    GC_API void GC_CALL GC_noop1(word);
36 #  ifdef PCR
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)
44 #  endif
45
46 #  if !defined(AO_HAVE_test_and_set_acquire) && defined(GC_PTHREADS)
47 #    define USE_PTHREAD_LOCKS
48 #  endif
49
50 #  if defined(GC_WIN32_THREADS) && defined(GC_PTHREADS)
51 #    define USE_PTHREAD_LOCKS
52 #  endif
53
54 #  if defined(GC_RTEMS_PTHREADS)
55 #    define USE_PTHREAD_LOCKS
56 #  endif
57
58 #  if defined(GC_WIN32_THREADS) && !defined(USE_PTHREAD_LOCKS)
59 #    ifndef WIN32_LEAN_AND_MEAN
60 #      define WIN32_LEAN_AND_MEAN 1
61 #    endif
62 #    define NOSERVICE
63 #    include <windows.h>
64 #    define NO_THREAD (DWORD)(-1)
65      GC_EXTERN DWORD GC_lock_holder;
66      GC_EXTERN CRITICAL_SECTION GC_allocate_ml;
67 #    ifdef GC_ASSERTIONS
68 #        define UNCOND_LOCK() \
69                 { EnterCriticalSection(&GC_allocate_ml); \
70                   SET_LOCK_HOLDER(); }
71 #        define UNCOND_UNLOCK() \
72                 { GC_ASSERT(I_HOLD_LOCK()); UNSET_LOCK_HOLDER(); \
73                   LeaveCriticalSection(&GC_allocate_ml); }
74 #    else
75 #      define UNCOND_LOCK() EnterCriticalSection(&GC_allocate_ml)
76 #      define UNCOND_UNLOCK() LeaveCriticalSection(&GC_allocate_ml)
77 #    endif /* !GC_ASSERTIONS */
78 #    define SET_LOCK_HOLDER() GC_lock_holder = GetCurrentThreadId()
79 #    define UNSET_LOCK_HOLDER() GC_lock_holder = NO_THREAD
80 #    define I_HOLD_LOCK() (!GC_need_to_lock \
81                            || GC_lock_holder == GetCurrentThreadId())
82 #    define I_DONT_HOLD_LOCK() (!GC_need_to_lock \
83                            || GC_lock_holder != GetCurrentThreadId())
84 #  elif defined(SN_TARGET_PS3)
85 #    include <pthread.h>
86      GC_EXTERN pthread_mutex_t GC_allocate_ml;
87 #    define LOCK() pthread_mutex_lock(&GC_allocate_ml)
88 #    define UNLOCK() pthread_mutex_unlock(&GC_allocate_ml)
89 #  elif defined(GC_PTHREADS)
90 #    include <pthread.h>
91
92      /* Posix allows pthread_t to be a struct, though it rarely is.     */
93      /* Unfortunately, we need to use a pthread_t to index a data       */
94      /* structure.  It also helps if comparisons don't involve a        */
95      /* function call.  Hence we introduce platform-dependent macros    */
96      /* to compare pthread_t ids and to map them to integers.           */
97      /* the mapping to integers does not need to result in different    */
98      /* integers for each thread, though that should be true as much    */
99      /* as possible.                                                    */
100      /* Refine to exclude platforms on which pthread_t is struct */
101 #    if !defined(GC_WIN32_PTHREADS)
102 #      define NUMERIC_THREAD_ID(id) ((unsigned long)(id))
103 #      define THREAD_EQUAL(id1, id2) ((id1) == (id2))
104 #      define NUMERIC_THREAD_ID_UNIQUE
105 #    else
106 #      define NUMERIC_THREAD_ID(id) ((unsigned long)(id.p))
107        /* Using documented internal details of win32-pthread library.   */
108        /* Faster than pthread_equal(). Should not change with           */
109        /* future versions of win32-pthread library.                     */
110 #      define THREAD_EQUAL(id1, id2) ((id1.p == id2.p) && (id1.x == id2.x))
111 #      undef NUMERIC_THREAD_ID_UNIQUE
112        /* Generic definitions based on pthread_equal() always work but  */
113        /* will result in poor performance (as NUMERIC_THREAD_ID is      */
114        /* defined to just a constant) and weak assertion checking.      */
115 #    endif
116 #    define NO_THREAD ((unsigned long)(-1l))
117                 /* != NUMERIC_THREAD_ID(pthread_self()) for any thread */
118
119 #    if !defined(THREAD_LOCAL_ALLOC) && !defined(USE_PTHREAD_LOCKS)
120       /* In the THREAD_LOCAL_ALLOC case, the allocation lock tends to   */
121       /* be held for long periods, if it is held at all.  Thus spinning */
122       /* and sleeping for fixed periods are likely to result in         */
123       /* significant wasted time.  We thus rely mostly on queued locks. */
124 #     define USE_SPIN_LOCK
125       GC_EXTERN volatile AO_TS_t GC_allocate_lock;
126       GC_INNER void GC_lock(void);
127         /* Allocation lock holder.  Only set if acquired by client through */
128         /* GC_call_with_alloc_lock.                                        */
129 #     ifdef GC_ASSERTIONS
130 #        define UNCOND_LOCK() \
131               { if (AO_test_and_set_acquire(&GC_allocate_lock) == AO_TS_SET) \
132                   GC_lock(); \
133                 SET_LOCK_HOLDER(); }
134 #        define UNCOND_UNLOCK() \
135               { GC_ASSERT(I_HOLD_LOCK()); UNSET_LOCK_HOLDER(); \
136                 AO_CLEAR(&GC_allocate_lock); }
137 #     else
138 #        define UNCOND_LOCK() \
139               { if (AO_test_and_set_acquire(&GC_allocate_lock) == AO_TS_SET) \
140                   GC_lock(); }
141 #        define UNCOND_UNLOCK() AO_CLEAR(&GC_allocate_lock)
142 #     endif /* !GC_ASSERTIONS */
143 #    else /* THREAD_LOCAL_ALLOC  || USE_PTHREAD_LOCKS */
144 #      ifndef USE_PTHREAD_LOCKS
145 #        define USE_PTHREAD_LOCKS
146 #      endif
147 #    endif /* THREAD_LOCAL_ALLOC || USE_PTHREAD_LOCK */
148 #    ifdef USE_PTHREAD_LOCKS
149 #      include <pthread.h>
150        GC_EXTERN pthread_mutex_t GC_allocate_ml;
151 #      ifdef GC_ASSERTIONS
152 #        define UNCOND_LOCK() { GC_lock(); SET_LOCK_HOLDER(); }
153 #        define UNCOND_UNLOCK() \
154                 { GC_ASSERT(I_HOLD_LOCK()); UNSET_LOCK_HOLDER(); \
155                   pthread_mutex_unlock(&GC_allocate_ml); }
156 #      else /* !GC_ASSERTIONS */
157 #        if defined(NO_PTHREAD_TRYLOCK)
158 #          define UNCOND_LOCK() GC_lock()
159 #        else /* !defined(NO_PTHREAD_TRYLOCK) */
160 #        define UNCOND_LOCK() \
161            { if (0 != pthread_mutex_trylock(&GC_allocate_ml)) \
162                GC_lock(); }
163 #        endif
164 #        define UNCOND_UNLOCK() pthread_mutex_unlock(&GC_allocate_ml)
165 #      endif /* !GC_ASSERTIONS */
166 #    endif /* USE_PTHREAD_LOCKS */
167 #    define SET_LOCK_HOLDER() \
168                 GC_lock_holder = NUMERIC_THREAD_ID(pthread_self())
169 #    define UNSET_LOCK_HOLDER() GC_lock_holder = NO_THREAD
170 #    define I_HOLD_LOCK() \
171                 (!GC_need_to_lock || \
172                  GC_lock_holder == NUMERIC_THREAD_ID(pthread_self()))
173 #    ifndef NUMERIC_THREAD_ID_UNIQUE
174 #      define I_DONT_HOLD_LOCK() 1  /* Conservatively say yes */
175 #    else
176 #      define I_DONT_HOLD_LOCK() \
177                 (!GC_need_to_lock \
178                  || GC_lock_holder != NUMERIC_THREAD_ID(pthread_self()))
179 #    endif
180      GC_EXTERN volatile GC_bool GC_collecting;
181 #    define ENTER_GC() GC_collecting = 1;
182 #    define EXIT_GC() GC_collecting = 0;
183      GC_INNER void GC_lock(void);
184      GC_EXTERN unsigned long GC_lock_holder;
185 #    if defined(GC_ASSERTIONS) && defined(PARALLEL_MARK)
186        GC_EXTERN unsigned long GC_mark_lock_holder;
187 #    endif
188 #  endif /* GC_PTHREADS with linux_threads.c implementation */
189    GC_EXTERN GC_bool GC_need_to_lock;
190
191 # else /* !THREADS */
192 #   define LOCK()
193 #   define UNLOCK()
194 #   define SET_LOCK_HOLDER()
195 #   define UNSET_LOCK_HOLDER()
196 #   define I_HOLD_LOCK() TRUE
197 #   define I_DONT_HOLD_LOCK() TRUE
198                 /* Used only in positive assertions or to test whether  */
199                 /* we still need to acquire the lock.  TRUE works in    */
200                 /* either case.                                         */
201 # endif /* !THREADS */
202
203 #if defined(UNCOND_LOCK) && !defined(LOCK)
204                 /* At least two thread running; need to lock.   */
205 #    define LOCK() { if (GC_need_to_lock) UNCOND_LOCK(); }
206 #    define UNLOCK() { if (GC_need_to_lock) UNCOND_UNLOCK(); }
207 #endif
208
209 # ifndef ENTER_GC
210 #   define ENTER_GC()
211 #   define EXIT_GC()
212 # endif
213
214 # ifndef DCL_LOCK_STATE
215 #   define DCL_LOCK_STATE
216 # endif
217
218 #endif /* GC_LOCKS_H */