Try to make the :lurking-threads test more robust.
[sbcl.git] / src / runtime / runtime.h
blob639ffc5f9b9f682831ed4d0c28a87d589b962054
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 void gc_state_lock();
71 void gc_state_wait(gc_phase_t);
72 int gc_cycle_active(void);
73 void gc_state_unlock();
75 #define WITH_GC_STATE_LOCK \
76 gc_state_lock(); \
77 RUN_BODY_ONCE(gc_state_lock, gc_state_unlock())
79 #endif
82 * The next few defines serve as configuration -- edit them inline if
83 * you are a developer and want to affect FSHOW behaviour.
86 /* Block blockable interrupts for each SHOW, if not 0.
87 * (On Windows, this setting has no effect.)
89 * In principle, this is a "configuration option", but I am not aware of
90 * any reason why or when it would be advantageous to disable it. */
91 #define QSHOW_SIGNAL_SAFE 1
93 /* Enable extra-verbose low-level debugging output for signals? (You
94 * probably don't want this unless you're trying to debug very early
95 * cold boot on a new machine, or one where you've just messed up
96 * signal handling.)
98 * Note: It may be that doing this is fundamentally unsound, since it
99 * causes output from signal handlers, and the i/o libraries aren't
100 * necessarily reentrant. But it can still be very convenient for
101 * figuring out what's going on when you have a signal handling
102 * problem.
104 * Possible values are:
105 * 0 -- Never show signal-related output. There is absolutely no
106 * run-time overhead from FSHOW_SIGNAL in this case.
108 * 1 -- (recommended)
109 * Show signal-related output only if selected at run-time
110 * (otherwise almost no run-time overhead).
112 * 2 -- Unconditionally show signal-related output.
113 * Very significant overhead.
115 * For reasons of tradition, we default to 0 on POSIX and 1 on Windows
116 * through :SB-QSHOW.
118 * With option 1, set up environment variable SBCL_DYNDEBUG to include
119 * "fshow" or "fshow_signal" before starting SBCL to enable output.
121 * There is no particular advantage to option 2 except that you do not
122 * need to set environment variables in this case.
124 #ifdef LISP_FEATURE_SB_QSHOW
125 # define QSHOW_SIGNALS 1
126 #else
127 # define QSHOW_SIGNALS 0
128 #endif
130 /* Enable low-level debugging output, if not zero. Defaults to enabled
131 * if QSHOW_SIGNALS, disabled otherwise. Change it to 1 or 2 if you want
132 * low-level debugging output but not the whole signal mess. */
133 #define QSHOW QSHOW_SIGNALS
136 * Configuration options end here -- the following defines do not
137 * generally need customization.
140 #define odxprint(topic, fmt, ...) \
141 do \
142 if (dyndebug_config.dyndebug_##topic) \
143 odxprint_fun(fmt "\n", ##__VA_ARGS__); \
144 while (0)
146 void odxprint_fun(const char *fmt, ...);
147 void fshow_fun(void *ignored, const char *fmt, ...);
149 /* Flags defined in a structure to avoid code duplication between
150 * declaration and definition. */
151 extern struct dyndebug_config {
152 int dyndebug_fshow;
153 int dyndebug_fshow_signal;
154 int dyndebug_gencgc_verbose;
155 int dyndebug_safepoints;
156 int dyndebug_seh;
157 int dyndebug_misc;
158 int dyndebug_pagefaults;
159 int dyndebug_backtrace_when_lost;
160 int dyndebug_sleep_when_lost;
161 int dyndebug_io;
162 int dyndebug_runtime_link;
163 } dyndebug_config;
165 #ifdef LISP_FEATURE_GENCGC
166 extern int gencgc_verbose;
167 #endif
169 void dyndebug_init(void);
171 #if QSHOW_SIGNAL_SAFE == 1 && !defined(LISP_FEATURE_WIN32)
173 extern sigset_t blockable_sigset;
175 #define QSHOW_BLOCK \
176 sigset_t oldset; \
177 thread_sigmask(SIG_BLOCK, &blockable_sigset, &oldset)
178 #define QSHOW_UNBLOCK thread_sigmask(SIG_SETMASK,&oldset,0)
179 #else
180 #define QSHOW_BLOCK
181 #define QSHOW_UNBLOCK
182 #endif
184 /* The following macros duplicate the expansion of odxprint, because the
185 * extra level of parentheses around `args' prevents us from
186 * implementing FSHOW in terms of odxprint directly. (They also differ
187 * in a newline.)
190 #if QSHOW
191 # define FSHOW(args) \
192 do if (dyndebug_config.dyndebug_fshow) fshow_fun args; while (0)
193 # define SHOW(string) FSHOW((stderr, "/%s\n", string))
194 #else
195 # define FSHOW(args)
196 # define SHOW(string)
197 #endif
199 #if QSHOW_SIGNALS
200 # define FSHOW_SIGNAL(args) \
201 do if (dyndebug_config.dyndebug_fshow_signal) fshow_fun args; while (0)
202 #else
203 # define FSHOW_SIGNAL(args)
204 #endif
206 /* KLUDGE: These are in theory machine-dependent and OS-dependent, but
207 * in practice the "foo int" definitions work for all the machines
208 * that SBCL runs on as of 0.6.7. If we port to the Alpha or some
209 * other non-32-bit machine we'll probably need real machine-dependent
210 * and OS-dependent definitions again. */
211 /* even on alpha, int happens to be 4 bytes. long is longer. */
212 /* FIXME: these names really shouldn't reflect their length and this
213 is not quite right for some of the FFI stuff */
214 #if defined(LISP_FEATURE_WIN32)&&defined(LISP_FEATURE_X86_64)
215 typedef unsigned long long u64;
216 typedef signed long long s64;
217 #else
218 typedef unsigned long u64;
219 typedef signed long s64;
220 #endif
221 typedef unsigned int u32;
222 typedef signed int s32;
224 #ifdef _WIN64
225 #define AMD64_SYSV_ABI __attribute__((sysv_abi))
226 #else
227 #define AMD64_SYSV_ABI
228 #endif
230 #include <sys/types.h>
232 #if defined(LISP_FEATURE_SB_THREAD)
233 typedef pthread_t os_thread_t;
234 #else
235 typedef pid_t os_thread_t;
236 #endif
238 #ifndef LISP_FEATURE_ALPHA
239 typedef uintptr_t uword_t;
240 typedef intptr_t sword_t;
241 #else
242 /* The alpha32 port uses non-intptr-sized words */
243 typedef u32 uword_t;
244 typedef s32 sword_t;
245 #endif
247 /* FIXME: we do things this way because of the alpha32 port. once
248 alpha64 has arrived, all this nastiness can go away */
249 #if 64 == N_WORD_BITS
250 #define LOW_WORD(c) ((uintptr_t)c)
251 #define OBJ_FMTX "lx"
252 typedef uintptr_t lispobj;
253 #else
254 #define OBJ_FMTX "x"
255 #define LOW_WORD(c) ((long)(c) & 0xFFFFFFFFL)
256 /* fake it on alpha32 */
257 typedef unsigned int lispobj;
258 #endif
260 static inline int
261 lowtag_of(lispobj obj)
263 return obj & LOWTAG_MASK;
266 static inline int
267 widetag_of(lispobj obj)
269 return obj & WIDETAG_MASK;
272 static inline uword_t
273 HeaderValue(lispobj obj)
275 return obj >> N_WIDETAG_BITS;
278 #ifdef LISP_FEATURE_IMMOBILE_SPACE
279 #define HEADER_VALUE_MASKED(x) (HeaderValue(x) & SHORT_HEADER_MAX_WORDS)
280 #else
281 #define HEADER_VALUE_MASKED(x) HeaderValue(x)
282 #endif
283 static inline uword_t instance_length(lispobj header)
285 return HEADER_VALUE_MASKED(header);
287 /* Define an assignable instance_layout() macro taking a native pointer */
288 #ifndef LISP_FEATURE_COMPACT_INSTANCE_HEADER
289 # define instance_layout(instance_ptr) (instance_ptr)[1]
290 #elif defined(LISP_FEATURE_64_BIT) && defined(LISP_FEATURE_LITTLE_ENDIAN)
291 // so that this stays an lvalue, it can't be cast to lispobj
292 # define instance_layout(instance_ptr) ((uint32_t*)(instance_ptr))[1]
293 #else
294 # error "No instance_layout() defined"
295 #endif
297 /* Is the Lisp object obj something with pointer nature (as opposed to
298 * e.g. a fixnum or character or unbound marker)? */
299 static inline int
300 is_lisp_pointer(lispobj obj)
302 #if N_WORD_BITS == 64
303 return (obj & 3) == 3;
304 #else
305 return obj & 1;
306 #endif
309 #include "fixnump.h"
311 /* Is the Lisp object obj something with immediate nature (e.g. a
312 * fixnum or character or unbound marker)? */
313 static inline int
314 is_lisp_immediate(lispobj obj)
316 return (fixnump(obj)
317 || (widetag_of(obj) == CHARACTER_WIDETAG)
318 #if N_WORD_BITS == 64
319 || (widetag_of(obj) == SINGLE_FLOAT_WIDETAG)
320 #endif
321 || (widetag_of(obj) == UNBOUND_MARKER_WIDETAG));
324 /* Convert from a lispobj with type bits to a native (ordinary
325 * C/assembly) pointer to the beginning of the object. */
326 static inline lispobj *
327 native_pointer(lispobj obj)
329 return (lispobj *) ((uintptr_t) (obj & ~LOWTAG_MASK));
332 /* inverse operation: create a suitably tagged lispobj from a native
333 * pointer or integer.*/
334 static inline lispobj
335 make_lispobj(void *o, int low_tag)
337 return LOW_WORD(o) | low_tag;
340 #define MAKE_FIXNUM(n) (n << N_FIXNUM_TAG_BITS)
341 static inline lispobj
342 make_fixnum(uword_t n) // '<<' on negatives is _technically_ undefined behavior
344 return MAKE_FIXNUM(n);
347 static inline sword_t
348 fixnum_value(lispobj n)
350 return (sword_t)n >> N_FIXNUM_TAG_BITS;
353 static inline uword_t
354 code_header_words(lispobj header) // given header = code->header
356 return HeaderValue(header) & SHORT_HEADER_MAX_WORDS;
359 #include "align.h"
360 static inline sword_t
361 code_instruction_words(lispobj n) // given n = code->code_size
363 /* Convert bytes into words, double-word aligned. */
364 return ALIGN_UP(fixnum_value(n), 2*N_WORD_BYTES) >> WORD_SHIFT;
366 #undef HEADER_VALUE_MASKED
368 #if defined(LISP_FEATURE_WIN32)
369 /* KLUDGE: Avoid double definition of boolean by rpcndr.h included via
370 * shlobj.h.
372 * FIXME: We should probably arrange to use the rpcndr.h boolean on Windows,
373 * or get rid of our own boolean type. If the boolean type is only used in
374 * the runtime, and never passed to Lisp, then it doesn't matter which one
375 * we use.
377 #define boolean rpcndr_boolean
378 #include <shlobj.h>
379 #undef boolean
380 #endif
381 typedef int boolean;
383 static inline boolean
384 other_immediate_lowtag_p(lispobj header)
386 /* These lowtags are spaced 4 apart throughout the lowtag space. */
387 return (lowtag_of(header) & 3) == OTHER_IMMEDIATE_0_LOWTAG;
390 static inline int
391 is_cons_half(lispobj obj)
393 /* A word that satisfies other_immediate_lowtag_p is a headered object
394 * and can not be half of a cons, except that widetags which satisfy
395 * other_immediate and are Lisp immediates can be half of a cons */
396 return !other_immediate_lowtag_p(obj)
397 #if N_WORD_BITS == 64
398 || ((uword_t)IMMEDIATE_WIDETAGS_MASK >> (widetag_of(obj) >> 2)) & 1;
399 #else
400 /* The above bit-shifting approach is not applicable
401 * since we can't employ a 64-bit unsigned integer constant. */
402 || widetag_of(obj) == CHARACTER_WIDETAG
403 || widetag_of(obj) == UNBOUND_MARKER_WIDETAG;
404 #endif
407 /* KLUDGE: As far as I can tell there's no ANSI C way of saying
408 * "this function never returns". This is the way that you do it
409 * in GCC later than version 2.5 or so. */
410 #if defined(__GNUC__)
411 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5)
412 #define never_returns __attribute__ ((noreturn))
413 #else
414 #define never_returns
415 #endif
416 #else
417 #define never_returns
418 #endif
420 extern void *successful_malloc (size_t size);
421 extern char *copied_string (char *string);
423 #if defined(LISP_FEATURE_SB_THREAD) && !defined(LISP_FEATURE_SB_SAFEPOINT)
424 # define THREADS_USING_GCSIGNAL 1
425 #endif
427 /* Now that SPARC has precise GENCGC, several places that used to be
428 * #ifdef PCC need adjustment. Clearly, "PPC or SPARC" is as unhelpful
429 * a test as its reverse, "x86 or x86-64". However, the feature
430 * commonly used to differentiate between those two worlds is
431 * C_STACK_IS_CONTROL_STACK, and clearly (or at least in my humble
432 * opinion), at some point we'd like to have precise GC on x86 while
433 * still sharing the C stack, so stack usage ought not imply GC
434 * conservativeness. So let's have a helper feature that makes the code
435 * a bit more future-proof, even if it is itself currently defined in
436 * the naive way: */
437 #if defined(LISP_FEATURE_GENCGC) && !defined(LISP_FEATURE_C_STACK_IS_CONTROL_STACK)
438 # define GENCGC_IS_PRECISE 1
439 #else
440 # define GENCGC_IS_PRECISE 0
441 #endif
443 void *os_dlsym_default(char *name);
445 struct lisp_startup_options {
446 boolean noinform;
448 extern struct lisp_startup_options lisp_startup_options;
450 /* Even with just -O1, gcc optimizes the jumps in this "loop" away
451 * entirely, giving the ability to define WITH-FOO-style macros. */
452 #define RUN_BODY_ONCE(prefix, finally_do) \
453 int prefix##done = 0; \
454 for (; !prefix##done; finally_do, prefix##done = 1)
456 // casting to void is no longer enough to suppress a warning about unused
457 // results of libc functions declared with warn_unused_result.
458 // from http://git.savannah.gnu.org/cgit/gnulib.git/tree/lib/ignore-value.h
459 #if 3 < __GNUC__ + (4 <= __GNUC_MINOR__)
460 # define ignore_value(x) \
461 (__extension__ ({ __typeof__ (x) __x = (x); (void) __x; }))
462 #else
463 # define ignore_value(x) ((void) (x))
464 #endif
466 #endif /* _SBCL_RUNTIME_H_ */