186f29680d117c69e19ba013f17baeb4628a6922
[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 /*
39  * The next few defines serve as configuration -- edit them inline if
40  * you are a developer and want to affect FSHOW behaviour.
41  */
42
43 /* Block blockable interrupts for each SHOW, if not 0.
44  * (On Windows, this setting has no effect.)
45  *
46  * In principle, this is a "configuration option", but I am not aware of
47  * any reason why or when it would be advantageous to disable it. */
48 #define QSHOW_SIGNAL_SAFE 1
49
50 /* Enable extra-verbose low-level debugging output for signals? (You
51  * probably don't want this unless you're trying to debug very early
52  * cold boot on a new machine, or one where you've just messed up
53  * signal handling.)
54  *
55  * Note: It may be that doing this is fundamentally unsound, since it
56  * causes output from signal handlers, and the i/o libraries aren't
57  * necessarily reentrant. But it can still be very convenient for
58  * figuring out what's going on when you have a signal handling
59  * problem.
60  *
61  * Possible values are:
62  *   0 -- Never show signal-related output.  There is absolutely no
63  *        run-time overhead from FSHOW_SIGNAL in this case.
64  *
65  *   1 -- (recommended)
66  *        Show signal-related output only if selected at run-time
67  *        (otherwise almost no run-time overhead).
68  *
69  *   2 -- Unconditionally show signal-related output.
70  *        Very significant overhead.
71  *
72  * For reasons of tradition, we default to 0 on POSIX and 1 on Windows
73  * through :SB-QSHOW.
74  *
75  * With option 1, set up environment variable SBCL_DYNDEBUG to include
76  * "fshow" or "fshow_signal" before starting SBCL to enable output.
77  *
78  * There is no particular advantage to option 2 except that you do not
79  * need to set environment variables in this case.
80  */
81 #ifdef LISP_FEATURE_SB_QSHOW
82 # define QSHOW_SIGNALS 1
83 #else
84 # define QSHOW_SIGNALS 0
85 #endif
86
87 /* Enable low-level debugging output, if not zero. Defaults to enabled
88  * if QSHOW_SIGNALS, disabled otherwise. Change it to 1 or 2 if you want
89  * low-level debugging output but not the whole signal mess. */
90 #define QSHOW QSHOW_SIGNALS
91
92 /*
93  * Configuration options end here -- the following defines do not
94  * generally need customization.
95  */
96
97 #define odxprint(topic, fmt, ...)                       \
98     do                                                  \
99         if (dyndebug_config.dyndebug_##topic)           \
100             odxprint_fun(fmt "\n", ##__VA_ARGS__);      \
101     while (0)
102
103 void odxprint_fun(const char *fmt, ...);
104 void fshow_fun(void *ignored, const char *fmt, ...);
105
106 /* Flags defined in a structure to avoid code duplication between
107  * declaration and definition. */
108 extern struct dyndebug_config {
109     int dyndebug_fshow;
110     int dyndebug_fshow_signal;
111     int dyndebug_gencgc_verbose;
112     int dyndebug_safepoints;
113     int dyndebug_seh;
114     int dyndebug_misc;
115     int dyndebug_pagefaults;
116     int dyndebug_backtrace_when_lost;
117     int dyndebug_sleep_when_lost;
118 } dyndebug_config;
119
120 #ifdef LISP_FEATURE_GENCGC
121 extern int gencgc_verbose;
122 #endif
123
124 void dyndebug_init(void);
125
126 #if QSHOW_SIGNAL_SAFE == 1 && !defined(LISP_FEATURE_WIN32)
127
128 #include <signal.h>
129 extern sigset_t blockable_sigset;
130
131 #define QSHOW_BLOCK                                             \
132         sigset_t oldset;                                        \
133         thread_sigmask(SIG_BLOCK, &blockable_sigset, &oldset)
134 #define QSHOW_UNBLOCK thread_sigmask(SIG_SETMASK,&oldset,0)
135 #else
136 #define QSHOW_BLOCK
137 #define QSHOW_UNBLOCK
138 #endif
139
140 /* The following macros duplicate the expansion of odxprint, because the
141  * extra level of parentheses around `args' prevents us from
142  * implementing FSHOW in terms of odxprint directly.  (They also differ
143  * in a newline.)
144  */
145
146 #if QSHOW
147 # define FSHOW(args) \
148     do if (dyndebug_config.dyndebug_fshow) fshow_fun args; while (0)
149 # define SHOW(string) FSHOW((stderr, "/%s\n", string))
150 #else
151 # define FSHOW(args)
152 # define SHOW(string)
153 #endif
154
155 #if QSHOW_SIGNALS
156 # define FSHOW_SIGNAL(args)                                             \
157     do if (dyndebug_config.dyndebug_fshow_signal) fshow_fun args; while (0)
158 #else
159 # define FSHOW_SIGNAL(args)
160 #endif
161
162 /* KLUDGE: These are in theory machine-dependent and OS-dependent, but
163  * in practice the "foo int" definitions work for all the machines
164  * that SBCL runs on as of 0.6.7. If we port to the Alpha or some
165  * other non-32-bit machine we'll probably need real machine-dependent
166  * and OS-dependent definitions again. */
167 /* even on alpha, int happens to be 4 bytes.  long is longer. */
168 /* FIXME: these names really shouldn't reflect their length and this
169    is not quite right for some of the FFI stuff */
170 typedef unsigned long u64;
171 typedef signed long s64;
172 typedef unsigned int u32;
173 typedef signed int s32;
174
175 /* this is an integral type the same length as a machine pointer */
176 typedef unsigned long pointer_sized_uint_t ;
177
178 #include <sys/types.h>
179
180 #if defined(LISP_FEATURE_SB_THREAD)
181 #include <pthread.h>
182 typedef pthread_t os_thread_t;
183 #else
184 typedef pid_t os_thread_t;
185 #endif
186
187 /* FIXME: we do things this way because of the alpha32 port.  once
188    alpha64 has arrived, all this nastiness can go away */
189 #if 64 == N_WORD_BITS
190 #define LOW_WORD(c) ((pointer_sized_uint_t)c)
191 #define OBJ_FMTX "lx"
192 typedef unsigned long lispobj;
193 #else
194 #define OBJ_FMTX "x"
195 #define LOW_WORD(c) ((long)(c) & 0xFFFFFFFFL)
196 /* fake it on alpha32 */
197 typedef unsigned int lispobj;
198 #endif
199
200 static inline int
201 lowtag_of(lispobj obj)
202 {
203     return obj & LOWTAG_MASK;
204 }
205
206 static inline int
207 widetag_of(lispobj obj)
208 {
209     return obj & WIDETAG_MASK;
210 }
211
212 static inline unsigned long
213 HeaderValue(lispobj obj)
214 {
215   return obj >> N_WIDETAG_BITS;
216 }
217
218 static inline struct cons *
219 CONS(lispobj obj)
220 {
221   return (struct cons *)(obj - LIST_POINTER_LOWTAG);
222 }
223
224 static inline struct symbol *
225 SYMBOL(lispobj obj)
226 {
227   return (struct symbol *)(obj - OTHER_POINTER_LOWTAG);
228 }
229
230 static inline struct fdefn *
231 FDEFN(lispobj obj)
232 {
233   return (struct fdefn *)(obj - OTHER_POINTER_LOWTAG);
234 }
235
236 /* Is the Lisp object obj something with pointer nature (as opposed to
237  * e.g. a fixnum or character or unbound marker)? */
238 static inline int
239 is_lisp_pointer(lispobj obj)
240 {
241 #if N_WORD_BITS == 64
242     return (obj & 3) == 3;
243 #else
244     return obj & 1;
245 #endif
246 }
247
248 #include "fixnump.h"
249
250 /* Is the Lisp object obj something with immediate nature (e.g. a
251  * fixnum or character or unbound marker)? */
252 static inline int
253 is_lisp_immediate(lispobj obj)
254 {
255     return (fixnump(obj)
256             || (widetag_of(obj) == CHARACTER_WIDETAG)
257 #if N_WORD_BITS == 64
258             || (widetag_of(obj) == SINGLE_FLOAT_WIDETAG)
259 #endif
260             || (widetag_of(obj) == UNBOUND_MARKER_WIDETAG));
261 }
262
263 /* Convert from a lispobj with type bits to a native (ordinary
264  * C/assembly) pointer to the beginning of the object. */
265 static inline lispobj *
266 native_pointer(lispobj obj)
267 {
268     return (lispobj *) ((pointer_sized_uint_t) (obj & ~LOWTAG_MASK));
269 }
270
271 /* inverse operation: create a suitably tagged lispobj from a native
272  * pointer or integer.*/
273 static inline lispobj
274 make_lispobj(void *o, int low_tag)
275 {
276     return LOW_WORD(o) | low_tag;
277 }
278
279 #define MAKE_FIXNUM(n) (n << N_FIXNUM_TAG_BITS)
280 static inline lispobj
281 make_fixnum(long n)
282 {
283     return MAKE_FIXNUM(n);
284 }
285
286 static inline long
287 fixnum_value(lispobj n)
288 {
289     return n >> N_FIXNUM_TAG_BITS;
290 }
291
292 #if defined(LISP_FEATURE_WIN32)
293 /* KLUDGE: Avoid double definition of boolean by rpcndr.h included via
294  * shlobj.h.
295  *
296  * FIXME: We should probably arrange to use the rpcndr.h boolean on Windows,
297  * or get rid of our own boolean type.  If the boolean type is only used in
298  * the runtime, and never passed to Lisp, then it doesn't matter which one
299  * we use.
300  */
301 #define boolean rpcndr_boolean
302 #include <shlobj.h>
303 #undef boolean
304 #endif
305 typedef int boolean;
306
307 static inline boolean
308 other_immediate_lowtag_p(lispobj header)
309 {
310     /* These lowtags are spaced 4 apart throughout the lowtag space. */
311     return (lowtag_of(header) & 3) == OTHER_IMMEDIATE_0_LOWTAG;
312 }
313
314 /* KLUDGE: As far as I can tell there's no ANSI C way of saying
315  * "this function never returns". This is the way that you do it
316  * in GCC later than version 2.5 or so. */
317 #if defined(__GNUC__)
318 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5)
319 #define never_returns __attribute__ ((noreturn))
320 #else
321 #define never_returns
322 #endif
323 #else
324 #define never_returns
325 #endif
326
327 extern void *successful_malloc (size_t size);
328 extern char *copied_string (char *string);
329
330 #if defined(LISP_FEATURE_SB_THREAD) && !defined(LISP_FEATURE_SB_SAFEPOINT)
331 # define THREADS_USING_GCSIGNAL 1
332 #endif
333
334 #endif /* _SBCL_RUNTIME_H_ */