Add safepoint mechanism
[sbcl.git] / src / runtime / runtime.h
1 /*
2  * This software is part of the SBCL system. See the README file for
3  * more information.
4  *
5  * This software is derived from the CMU CL system, which was
6  * written at Carnegie Mellon University and released into the
7  * public domain. The software is in the public domain and is
8  * provided with absolutely no warranty. See the COPYING and CREDITS
9  * files for more information.
10  */
11
12 /* FIXME: Aren't symbols with underscore prefixes supposed to be
13  * reserved for system libraries? Perhaps rename stuff like this
14  * to names like INCLUDED_SBCL_RUNTIME_H. */
15 #ifndef _SBCL_RUNTIME_H_
16 #define _SBCL_RUNTIME_H_
17
18 #if defined(LISP_FEATURE_SB_THREAD)
19 #define thread_self() pthread_self()
20 #define thread_kill pthread_kill
21 #define thread_sigmask pthread_sigmask
22 #define thread_mutex_lock(l) pthread_mutex_lock(l)
23 #define thread_mutex_unlock(l) pthread_mutex_unlock(l)
24 #else
25 #define thread_self() 0
26 #define thread_kill kill_safely
27 #define thread_sigmask sigprocmask
28 #define thread_mutex_lock(l) 0
29 #define thread_mutex_unlock(l) 0
30 #endif
31
32 #if defined(LISP_FEATURE_SB_SAFEPOINT)
33 void map_gc_page();
34 void unmap_gc_page();
35 int check_pending_interrupts();
36 #endif
37
38 /* Block blockable interrupts for each SHOW, if not 0. */
39 #define QSHOW_SIGNAL_SAFE 1
40 /* Enable extra-verbose low-level debugging output for signals? (You
41  * probably don't want this unless you're trying to debug very early
42  * cold boot on a new machine, or one where you've just messed up
43  * signal handling.)
44  *
45  * Note: It may be that doing this is fundamentally unsound, since it
46  * causes output from signal handlers, and the i/o libraries aren't
47  * necessarily reentrant. But it can still be very convenient for
48  * figuring out what's going on when you have a signal handling
49  * problem. */
50 #define QSHOW_SIGNALS 0
51 /* Enable low-level debugging output, if not zero. Defaults to enabled
52  * if QSHOW_SIGNALS, disabled otherwise. Change it to 1 if you want
53  * low-level debugging output but not the whole signal mess. */
54 #define QSHOW QSHOW_SIGNALS
55
56 #if QSHOW
57
58 #if QSHOW_SIGNAL_SAFE == 1 && !defined(LISP_FEATURE_WIN32)
59
60 #include <signal.h>
61 extern sigset_t blockable_sigset;
62
63 #define QSHOW_BLOCK                                             \
64         sigset_t oldset;                                        \
65         thread_sigmask(SIG_BLOCK, &blockable_sigset, &oldset);
66 #define QSHOW_UNBLOCK thread_sigmask(SIG_SETMASK,&oldset,0);
67 #else
68 #define QSHOW_BLOCK
69 #define QSHOW_UNBLOCK
70 #endif
71
72 #ifdef LISP_FEATURE_SB_THREAD
73 #define QSHOW_PREFIX fprintf(stderr, "%p ", pthread_self());
74 #else
75 #define QSHOW_PREFIX
76 #endif
77
78 #define FSHOW(args)                                             \
79     do {                                                        \
80         QSHOW_BLOCK                                             \
81         QSHOW_PREFIX                                            \
82         fprintf args;                                           \
83         QSHOW_UNBLOCK                                           \
84     } while (0)
85 #define SHOW(string) FSHOW((stderr, "/%s\n", string))
86
87 #else
88
89 #define FSHOW(args)
90 #define SHOW(string)
91
92 #endif
93
94 #if QSHOW_SIGNALS
95 #define FSHOW_SIGNAL FSHOW
96 #else
97 #define FSHOW_SIGNAL(args)
98 #endif
99
100 /* KLUDGE: These are in theory machine-dependent and OS-dependent, but
101  * in practice the "foo int" definitions work for all the machines
102  * that SBCL runs on as of 0.6.7. If we port to the Alpha or some
103  * other non-32-bit machine we'll probably need real machine-dependent
104  * and OS-dependent definitions again. */
105 /* even on alpha, int happens to be 4 bytes.  long is longer. */
106 /* FIXME: these names really shouldn't reflect their length and this
107    is not quite right for some of the FFI stuff */
108 typedef unsigned long u64;
109 typedef signed long s64;
110 typedef unsigned int u32;
111 typedef signed int s32;
112
113 /* this is an integral type the same length as a machine pointer */
114 typedef unsigned long pointer_sized_uint_t ;
115
116 #include <sys/types.h>
117
118 #if defined(LISP_FEATURE_SB_THREAD)
119 #include <pthread.h>
120 typedef pthread_t os_thread_t;
121 #else
122 typedef pid_t os_thread_t;
123 #endif
124
125 /* FIXME: we do things this way because of the alpha32 port.  once
126    alpha64 has arrived, all this nastiness can go away */
127 #if 64 == N_WORD_BITS
128 #define LOW_WORD(c) ((pointer_sized_uint_t)c)
129 #define OBJ_FMTX "lx"
130 typedef unsigned long lispobj;
131 #else
132 #define OBJ_FMTX "x"
133 #define LOW_WORD(c) ((long)(c) & 0xFFFFFFFFL)
134 /* fake it on alpha32 */
135 typedef unsigned int lispobj;
136 #endif
137
138 static inline int
139 lowtag_of(lispobj obj)
140 {
141     return obj & LOWTAG_MASK;
142 }
143
144 static inline int
145 widetag_of(lispobj obj)
146 {
147     return obj & WIDETAG_MASK;
148 }
149
150 static inline unsigned long
151 HeaderValue(lispobj obj)
152 {
153   return obj >> N_WIDETAG_BITS;
154 }
155
156 static inline struct cons *
157 CONS(lispobj obj)
158 {
159   return (struct cons *)(obj - LIST_POINTER_LOWTAG);
160 }
161
162 static inline struct symbol *
163 SYMBOL(lispobj obj)
164 {
165   return (struct symbol *)(obj - OTHER_POINTER_LOWTAG);
166 }
167
168 static inline struct fdefn *
169 FDEFN(lispobj obj)
170 {
171   return (struct fdefn *)(obj - OTHER_POINTER_LOWTAG);
172 }
173
174 /* Is the Lisp object obj something with pointer nature (as opposed to
175  * e.g. a fixnum or character or unbound marker)? */
176 static inline int
177 is_lisp_pointer(lispobj obj)
178 {
179 #if N_WORD_BITS == 64
180     return (obj & 3) == 3;
181 #else
182     return obj & 1;
183 #endif
184 }
185
186 #include "fixnump.h"
187
188 /* Is the Lisp object obj something with immediate nature (e.g. a
189  * fixnum or character or unbound marker)? */
190 static inline int
191 is_lisp_immediate(lispobj obj)
192 {
193     return (fixnump(obj)
194             || (widetag_of(obj) == CHARACTER_WIDETAG)
195 #if N_WORD_BITS == 64
196             || (widetag_of(obj) == SINGLE_FLOAT_WIDETAG)
197 #endif
198             || (widetag_of(obj) == UNBOUND_MARKER_WIDETAG));
199 }
200
201 /* Convert from a lispobj with type bits to a native (ordinary
202  * C/assembly) pointer to the beginning of the object. */
203 static inline lispobj *
204 native_pointer(lispobj obj)
205 {
206     return (lispobj *) ((pointer_sized_uint_t) (obj & ~LOWTAG_MASK));
207 }
208
209 /* inverse operation: create a suitably tagged lispobj from a native
210  * pointer or integer.*/
211 static inline lispobj
212 make_lispobj(void *o, int low_tag)
213 {
214     return LOW_WORD(o) | low_tag;
215 }
216
217 #define MAKE_FIXNUM(n) (n << N_FIXNUM_TAG_BITS)
218 static inline lispobj
219 make_fixnum(long n)
220 {
221     return MAKE_FIXNUM(n);
222 }
223
224 static inline long
225 fixnum_value(lispobj n)
226 {
227     return n >> N_FIXNUM_TAG_BITS;
228 }
229
230 #if defined(LISP_FEATURE_WIN32)
231 /* KLUDGE: Avoid double definition of boolean by rpcndr.h included via
232  * shlobj.h.
233  *
234  * FIXME: We should probably arrange to use the rpcndr.h boolean on Windows,
235  * or get rid of our own boolean type.  If the boolean type is only used in
236  * the runtime, and never passed to Lisp, then it doesn't matter which one
237  * we use.
238  */
239 #define boolean rpcndr_boolean
240 #include <shlobj.h>
241 #undef boolean
242 #endif
243 typedef int boolean;
244
245 static inline boolean
246 other_immediate_lowtag_p(lispobj header)
247 {
248     /* These lowtags are spaced 4 apart throughout the lowtag space. */
249     return (lowtag_of(header) & 3) == OTHER_IMMEDIATE_0_LOWTAG;
250 }
251
252 /* KLUDGE: As far as I can tell there's no ANSI C way of saying
253  * "this function never returns". This is the way that you do it
254  * in GCC later than version 2.5 or so. */
255 #if defined(__GNUC__)
256 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5)
257 #define never_returns __attribute__ ((noreturn))
258 #else
259 #define never_returns
260 #endif
261 #else
262 #define never_returns
263 #endif
264
265 extern void *successful_malloc (size_t size);
266 extern char *copied_string (char *string);
267
268 #if defined(LISP_FEATURE_SB_THREAD) && !defined(LISP_FEATURE_SB_SAFEPOINT)
269 # define THREADS_USING_GCSIGNAL 1
270 #endif
271
272 #endif /* _SBCL_RUNTIME_H_ */