De-flake 'traceroot' test
[sbcl.git] / src / runtime / runtime.h
blob390915857fe6b9cbb73fcd48c1de2c060bd16a77
1 /*
2 * This software is part of the SBCL system. See the README file for
3 * more information.
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.
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_
18 #if defined(LISP_FEATURE_WIN32) && defined(LISP_FEATURE_SB_THREAD)
19 # include "pthreads_win32.h"
20 #else
21 # include <signal.h>
22 # ifdef LISP_FEATURE_SB_THREAD
23 # include <pthread.h>
24 # endif
25 #endif
27 #include <stdint.h>
29 #if defined(LISP_FEATURE_SB_THREAD)
30 #define thread_self() pthread_self()
31 #define thread_kill pthread_kill
33 #ifdef LISP_FEATURE_WIN32
34 #define thread_sigmask _sbcl_pthread_sigmask
35 #else
36 #define thread_sigmask pthread_sigmask
37 #endif
39 #define thread_mutex_lock(l) pthread_mutex_lock(l)
40 #define thread_mutex_unlock(l) pthread_mutex_unlock(l)
41 #else
42 #define thread_self() 0
43 #define thread_kill kill_safely
44 #define thread_sigmask sigprocmask
45 #define thread_mutex_lock(l) 0
46 #define thread_mutex_unlock(l) 0
47 #endif
49 #if defined(LISP_FEATURE_WIN32) && defined(LISP_FEATURE_SB_THREAD)
50 void os_preinit();
51 #endif
53 void os_link_runtime();
55 #if defined(LISP_FEATURE_SB_SAFEPOINT)
57 typedef enum {
58 GC_NONE=0,
59 GC_FLIGHT,
60 GC_MESSAGE,
61 GC_INVOKED,
62 GC_QUIET,
63 GC_SETTLED,
64 GC_COLLECT,
65 GC_NPHASES
66 } gc_phase_t;
68 void map_gc_page();
69 void unmap_gc_page();
70 int check_pending_interrupts();
71 void gc_state_lock();
72 void gc_state_wait(gc_phase_t);
73 void gc_state_unlock();
75 #endif
78 * The next few defines serve as configuration -- edit them inline if
79 * you are a developer and want to affect FSHOW behaviour.
82 /* Block blockable interrupts for each SHOW, if not 0.
83 * (On Windows, this setting has no effect.)
85 * In principle, this is a "configuration option", but I am not aware of
86 * any reason why or when it would be advantageous to disable it. */
87 #define QSHOW_SIGNAL_SAFE 1
89 /* Enable extra-verbose low-level debugging output for signals? (You
90 * probably don't want this unless you're trying to debug very early
91 * cold boot on a new machine, or one where you've just messed up
92 * signal handling.)
94 * Note: It may be that doing this is fundamentally unsound, since it
95 * causes output from signal handlers, and the i/o libraries aren't
96 * necessarily reentrant. But it can still be very convenient for
97 * figuring out what's going on when you have a signal handling
98 * problem.
100 * Possible values are:
101 * 0 -- Never show signal-related output. There is absolutely no
102 * run-time overhead from FSHOW_SIGNAL in this case.
104 * 1 -- (recommended)
105 * Show signal-related output only if selected at run-time
106 * (otherwise almost no run-time overhead).
108 * 2 -- Unconditionally show signal-related output.
109 * Very significant overhead.
111 * For reasons of tradition, we default to 0 on POSIX and 1 on Windows
112 * through :SB-QSHOW.
114 * With option 1, set up environment variable SBCL_DYNDEBUG to include
115 * "fshow" or "fshow_signal" before starting SBCL to enable output.
117 * There is no particular advantage to option 2 except that you do not
118 * need to set environment variables in this case.
120 #ifdef LISP_FEATURE_SB_QSHOW
121 # define QSHOW_SIGNALS 1
122 #else
123 # define QSHOW_SIGNALS 0
124 #endif
126 /* Enable low-level debugging output, if not zero. Defaults to enabled
127 * if QSHOW_SIGNALS, disabled otherwise. Change it to 1 or 2 if you want
128 * low-level debugging output but not the whole signal mess. */
129 #define QSHOW QSHOW_SIGNALS
132 * Configuration options end here -- the following defines do not
133 * generally need customization.
136 #define odxprint(topic, fmt, ...) \
137 do \
138 if (dyndebug_config.dyndebug_##topic) \
139 odxprint_fun(fmt "\n", ##__VA_ARGS__); \
140 while (0)
142 void odxprint_fun(const char *fmt, ...);
143 void fshow_fun(void *ignored, const char *fmt, ...);
145 /* Flags defined in a structure to avoid code duplication between
146 * declaration and definition. */
147 extern struct dyndebug_config {
148 int dyndebug_fshow;
149 int dyndebug_fshow_signal;
150 int dyndebug_gencgc_verbose;
151 int dyndebug_safepoints;
152 int dyndebug_seh;
153 int dyndebug_misc;
154 int dyndebug_pagefaults;
155 int dyndebug_backtrace_when_lost;
156 int dyndebug_sleep_when_lost;
157 int dyndebug_io;
158 int dyndebug_runtime_link;
159 } dyndebug_config;
161 #ifdef LISP_FEATURE_GENCGC
162 extern int gencgc_verbose;
163 #endif
165 void dyndebug_init(void);
167 #if QSHOW_SIGNAL_SAFE == 1 && !defined(LISP_FEATURE_WIN32)
169 extern sigset_t blockable_sigset;
171 #define QSHOW_BLOCK \
172 sigset_t oldset; \
173 thread_sigmask(SIG_BLOCK, &blockable_sigset, &oldset)
174 #define QSHOW_UNBLOCK thread_sigmask(SIG_SETMASK,&oldset,0)
175 #else
176 #define QSHOW_BLOCK
177 #define QSHOW_UNBLOCK
178 #endif
180 /* The following macros duplicate the expansion of odxprint, because the
181 * extra level of parentheses around `args' prevents us from
182 * implementing FSHOW in terms of odxprint directly. (They also differ
183 * in a newline.)
186 #if QSHOW
187 # define FSHOW(args) \
188 do if (dyndebug_config.dyndebug_fshow) fshow_fun args; while (0)
189 # define SHOW(string) FSHOW((stderr, "/%s\n", string))
190 #else
191 # define FSHOW(args)
192 # define SHOW(string)
193 #endif
195 #if QSHOW_SIGNALS
196 # define FSHOW_SIGNAL(args) \
197 do if (dyndebug_config.dyndebug_fshow_signal) fshow_fun args; while (0)
198 #else
199 # define FSHOW_SIGNAL(args)
200 #endif
202 /* KLUDGE: These are in theory machine-dependent and OS-dependent, but
203 * in practice the "foo int" definitions work for all the machines
204 * that SBCL runs on as of 0.6.7. If we port to the Alpha or some
205 * other non-32-bit machine we'll probably need real machine-dependent
206 * and OS-dependent definitions again. */
207 /* even on alpha, int happens to be 4 bytes. long is longer. */
208 /* FIXME: these names really shouldn't reflect their length and this
209 is not quite right for some of the FFI stuff */
210 #if defined(LISP_FEATURE_WIN32)&&defined(LISP_FEATURE_X86_64)
211 typedef unsigned long long u64;
212 typedef signed long long s64;
213 #else
214 typedef unsigned long u64;
215 typedef signed long s64;
216 #endif
217 typedef unsigned int u32;
218 typedef signed int s32;
220 #ifdef _WIN64
221 #define AMD64_SYSV_ABI __attribute__((sysv_abi))
222 #else
223 #define AMD64_SYSV_ABI
224 #endif
226 #include <sys/types.h>
228 #if defined(LISP_FEATURE_SB_THREAD)
229 typedef pthread_t os_thread_t;
230 #else
231 typedef pid_t os_thread_t;
232 #endif
234 #ifndef LISP_FEATURE_ALPHA
235 typedef uintptr_t uword_t;
236 typedef intptr_t sword_t;
237 #else
238 /* The alpha32 port uses non-intptr-sized words */
239 typedef u32 uword_t;
240 typedef s32 sword_t;
241 #endif
243 /* FIXME: we do things this way because of the alpha32 port. once
244 alpha64 has arrived, all this nastiness can go away */
245 #if 64 == N_WORD_BITS
246 #define LOW_WORD(c) ((uintptr_t)c)
247 #define OBJ_FMTX "lx"
248 typedef uintptr_t lispobj;
249 #else
250 #define OBJ_FMTX "x"
251 #define LOW_WORD(c) ((long)(c) & 0xFFFFFFFFL)
252 /* fake it on alpha32 */
253 typedef unsigned int lispobj;
254 #endif
256 static inline int
257 lowtag_of(lispobj obj)
259 return obj & LOWTAG_MASK;
262 static inline int
263 widetag_of(lispobj obj)
265 return obj & WIDETAG_MASK;
268 static inline uword_t
269 HeaderValue(lispobj obj)
271 return obj >> N_WIDETAG_BITS;
274 #ifdef LISP_FEATURE_IMMOBILE_SPACE
275 #define HEADER_VALUE_MASKED(x) (HeaderValue(x) & SHORT_HEADER_MAX_WORDS)
276 #else
277 #define HEADER_VALUE_MASKED(x) HeaderValue(x)
278 #endif
279 static inline uword_t instance_length(lispobj header)
281 return HEADER_VALUE_MASKED(header);
283 static inline lispobj instance_layout(lispobj* instance_ptr) // native ptr
285 #ifdef LISP_FEATURE_COMPACT_INSTANCE_HEADER
286 return instance_ptr[0] >> 32; // the high half of the header is the layout
287 #else
288 return instance_ptr[1]; // the word following the header is the layout
289 #endif
292 static inline struct cons *
293 CONS(lispobj obj)
295 return (struct cons *)(obj - LIST_POINTER_LOWTAG);
298 static inline struct symbol *
299 SYMBOL(lispobj obj)
301 return (struct symbol *)(obj - OTHER_POINTER_LOWTAG);
304 static inline struct fdefn *
305 FDEFN(lispobj obj)
307 return (struct fdefn *)(obj - OTHER_POINTER_LOWTAG);
310 /* Is the Lisp object obj something with pointer nature (as opposed to
311 * e.g. a fixnum or character or unbound marker)? */
312 static inline int
313 is_lisp_pointer(lispobj obj)
315 #if N_WORD_BITS == 64
316 return (obj & 3) == 3;
317 #else
318 return obj & 1;
319 #endif
322 #include "fixnump.h"
324 /* Is the Lisp object obj something with immediate nature (e.g. a
325 * fixnum or character or unbound marker)? */
326 static inline int
327 is_lisp_immediate(lispobj obj)
329 return (fixnump(obj)
330 || (widetag_of(obj) == CHARACTER_WIDETAG)
331 #if N_WORD_BITS == 64
332 || (widetag_of(obj) == SINGLE_FLOAT_WIDETAG)
333 #endif
334 || (widetag_of(obj) == UNBOUND_MARKER_WIDETAG));
337 /* Convert from a lispobj with type bits to a native (ordinary
338 * C/assembly) pointer to the beginning of the object. */
339 static inline lispobj *
340 native_pointer(lispobj obj)
342 return (lispobj *) ((uintptr_t) (obj & ~LOWTAG_MASK));
345 /* inverse operation: create a suitably tagged lispobj from a native
346 * pointer or integer.*/
347 static inline lispobj
348 make_lispobj(void *o, int low_tag)
350 return LOW_WORD(o) | low_tag;
353 #define MAKE_FIXNUM(n) (n << N_FIXNUM_TAG_BITS)
354 static inline lispobj
355 make_fixnum(sword_t n)
357 return MAKE_FIXNUM(n);
360 static inline sword_t
361 fixnum_value(lispobj n)
363 return n >> N_FIXNUM_TAG_BITS;
366 static inline uword_t
367 code_header_words(lispobj header) // given header = code->header
369 return HeaderValue(header) & SHORT_HEADER_MAX_WORDS;
372 static inline sword_t
373 code_instruction_words(lispobj n) // given n = code->code_size
375 /* Convert bytes into words, double-word aligned. */
376 sword_t x = ((n >> N_FIXNUM_TAG_BITS) + LOWTAG_MASK) & ~LOWTAG_MASK;
378 return x >> WORD_SHIFT;
380 #undef HEADER_VALUE_MASKED
382 #if defined(LISP_FEATURE_WIN32)
383 /* KLUDGE: Avoid double definition of boolean by rpcndr.h included via
384 * shlobj.h.
386 * FIXME: We should probably arrange to use the rpcndr.h boolean on Windows,
387 * or get rid of our own boolean type. If the boolean type is only used in
388 * the runtime, and never passed to Lisp, then it doesn't matter which one
389 * we use.
391 #define boolean rpcndr_boolean
392 #include <shlobj.h>
393 #undef boolean
394 #endif
395 typedef int boolean;
397 static inline boolean
398 other_immediate_lowtag_p(lispobj header)
400 /* These lowtags are spaced 4 apart throughout the lowtag space. */
401 return (lowtag_of(header) & 3) == OTHER_IMMEDIATE_0_LOWTAG;
404 static inline int
405 is_cons_half(lispobj obj)
407 /* A word that satisfies other_immediate_lowtag_p is a headered object
408 * and can not be half of a cons, except that widetags which satisfy
409 * other_immediate and are Lisp immediates can be half of a cons */
410 return !other_immediate_lowtag_p(obj)
411 #if N_WORD_BITS == 64
412 || ((uword_t)IMMEDIATE_WIDETAGS_MASK >> (widetag_of(obj) >> 2)) & 1;
413 #else
414 /* The above bit-shifting approach is not applicable
415 * since we can't employ a 64-bit unsigned integer constant. */
416 || widetag_of(obj) == CHARACTER_WIDETAG
417 || widetag_of(obj) == UNBOUND_MARKER_WIDETAG;
418 #endif
421 /* KLUDGE: As far as I can tell there's no ANSI C way of saying
422 * "this function never returns". This is the way that you do it
423 * in GCC later than version 2.5 or so. */
424 #if defined(__GNUC__)
425 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5)
426 #define never_returns __attribute__ ((noreturn))
427 #else
428 #define never_returns
429 #endif
430 #else
431 #define never_returns
432 #endif
434 extern void *successful_malloc (size_t size);
435 extern char *copied_string (char *string);
437 #if defined(LISP_FEATURE_SB_THREAD) && !defined(LISP_FEATURE_SB_SAFEPOINT)
438 # define THREADS_USING_GCSIGNAL 1
439 #endif
441 /* Now that SPARC has precise GENCGC, several places that used to be
442 * #ifdef PCC need adjustment. Clearly, "PPC or SPARC" is as unhelpful
443 * a test as its reverse, "x86 or x86-64". However, the feature
444 * commonly used to differentiate between those two worlds is
445 * C_STACK_IS_CONTROL_STACK, and clearly (or at least in my humble
446 * opinion), at some point we'd like to have precise GC on x86 while
447 * still sharing the C stack, so stack usage ought not imply GC
448 * conservativeness. So let's have a helper feature that makes the code
449 * a bit more future-proof, even if it is itself currently defined in
450 * the naive way: */
451 #if defined(LISP_FEATURE_GENCGC) && !defined(LISP_FEATURE_C_STACK_IS_CONTROL_STACK)
452 # define GENCGC_IS_PRECISE 1
453 #endif
455 void *os_dlsym_default(char *name);
457 struct lisp_startup_options {
458 boolean noinform;
461 /* Even with just -O1, gcc optimizes the jumps in this "loop" away
462 * entirely, giving the ability to define WITH-FOO-style macros. */
463 #define RUN_BODY_ONCE(prefix, finally_do) \
464 int prefix##done = 0; \
465 for (; !prefix##done; finally_do, prefix##done = 1)
467 #endif /* _SBCL_RUNTIME_H_ */