Add a declaration
[sbcl.git] / src / runtime / runtime.h
blobee05fc5f7f1ab948c048dc9885c0a3fc502c2e62
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 #if defined(LISP_FEATURE_SB_SAFEPOINT)
55 typedef enum {
56 GC_NONE=0,
57 GC_FLIGHT,
58 GC_MESSAGE,
59 GC_INVOKED,
60 GC_QUIET,
61 GC_SETTLED,
62 GC_COLLECT,
63 GC_NPHASES
64 } gc_phase_t;
66 void map_gc_page();
67 void unmap_gc_page();
68 int check_pending_interrupts();
69 void gc_state_lock();
70 void gc_state_wait(gc_phase_t);
71 void gc_state_unlock();
73 #endif
76 * The next few defines serve as configuration -- edit them inline if
77 * you are a developer and want to affect FSHOW behaviour.
80 /* Block blockable interrupts for each SHOW, if not 0.
81 * (On Windows, this setting has no effect.)
83 * In principle, this is a "configuration option", but I am not aware of
84 * any reason why or when it would be advantageous to disable it. */
85 #define QSHOW_SIGNAL_SAFE 1
87 /* Enable extra-verbose low-level debugging output for signals? (You
88 * probably don't want this unless you're trying to debug very early
89 * cold boot on a new machine, or one where you've just messed up
90 * signal handling.)
92 * Note: It may be that doing this is fundamentally unsound, since it
93 * causes output from signal handlers, and the i/o libraries aren't
94 * necessarily reentrant. But it can still be very convenient for
95 * figuring out what's going on when you have a signal handling
96 * problem.
98 * Possible values are:
99 * 0 -- Never show signal-related output. There is absolutely no
100 * run-time overhead from FSHOW_SIGNAL in this case.
102 * 1 -- (recommended)
103 * Show signal-related output only if selected at run-time
104 * (otherwise almost no run-time overhead).
106 * 2 -- Unconditionally show signal-related output.
107 * Very significant overhead.
109 * For reasons of tradition, we default to 0 on POSIX and 1 on Windows
110 * through :SB-QSHOW.
112 * With option 1, set up environment variable SBCL_DYNDEBUG to include
113 * "fshow" or "fshow_signal" before starting SBCL to enable output.
115 * There is no particular advantage to option 2 except that you do not
116 * need to set environment variables in this case.
118 #ifdef LISP_FEATURE_SB_QSHOW
119 # define QSHOW_SIGNALS 1
120 #else
121 # define QSHOW_SIGNALS 0
122 #endif
124 /* Enable low-level debugging output, if not zero. Defaults to enabled
125 * if QSHOW_SIGNALS, disabled otherwise. Change it to 1 or 2 if you want
126 * low-level debugging output but not the whole signal mess. */
127 #define QSHOW QSHOW_SIGNALS
130 * Configuration options end here -- the following defines do not
131 * generally need customization.
134 #define odxprint(topic, fmt, ...) \
135 do \
136 if (dyndebug_config.dyndebug_##topic) \
137 odxprint_fun(fmt "\n", ##__VA_ARGS__); \
138 while (0)
140 void odxprint_fun(const char *fmt, ...);
141 void fshow_fun(void *ignored, const char *fmt, ...);
143 /* Flags defined in a structure to avoid code duplication between
144 * declaration and definition. */
145 extern struct dyndebug_config {
146 int dyndebug_fshow;
147 int dyndebug_fshow_signal;
148 int dyndebug_gencgc_verbose;
149 int dyndebug_safepoints;
150 int dyndebug_seh;
151 int dyndebug_misc;
152 int dyndebug_pagefaults;
153 int dyndebug_backtrace_when_lost;
154 int dyndebug_sleep_when_lost;
155 int dyndebug_io;
156 int dyndebug_runtime_link;
157 } dyndebug_config;
159 #ifdef LISP_FEATURE_GENCGC
160 extern int gencgc_verbose;
161 #endif
163 void dyndebug_init(void);
165 #if QSHOW_SIGNAL_SAFE == 1 && !defined(LISP_FEATURE_WIN32)
167 extern sigset_t blockable_sigset;
169 #define QSHOW_BLOCK \
170 sigset_t oldset; \
171 thread_sigmask(SIG_BLOCK, &blockable_sigset, &oldset)
172 #define QSHOW_UNBLOCK thread_sigmask(SIG_SETMASK,&oldset,0)
173 #else
174 #define QSHOW_BLOCK
175 #define QSHOW_UNBLOCK
176 #endif
178 /* The following macros duplicate the expansion of odxprint, because the
179 * extra level of parentheses around `args' prevents us from
180 * implementing FSHOW in terms of odxprint directly. (They also differ
181 * in a newline.)
184 #if QSHOW
185 # define FSHOW(args) \
186 do if (dyndebug_config.dyndebug_fshow) fshow_fun args; while (0)
187 # define SHOW(string) FSHOW((stderr, "/%s\n", string))
188 #else
189 # define FSHOW(args)
190 # define SHOW(string)
191 #endif
193 #if QSHOW_SIGNALS
194 # define FSHOW_SIGNAL(args) \
195 do if (dyndebug_config.dyndebug_fshow_signal) fshow_fun args; while (0)
196 #else
197 # define FSHOW_SIGNAL(args)
198 #endif
200 /* KLUDGE: These are in theory machine-dependent and OS-dependent, but
201 * in practice the "foo int" definitions work for all the machines
202 * that SBCL runs on as of 0.6.7. If we port to the Alpha or some
203 * other non-32-bit machine we'll probably need real machine-dependent
204 * and OS-dependent definitions again. */
205 /* even on alpha, int happens to be 4 bytes. long is longer. */
206 /* FIXME: these names really shouldn't reflect their length and this
207 is not quite right for some of the FFI stuff */
208 #if defined(LISP_FEATURE_WIN32)&&defined(LISP_FEATURE_X86_64)
209 typedef unsigned long long u64;
210 typedef signed long long s64;
211 #else
212 typedef unsigned long u64;
213 typedef signed long s64;
214 #endif
215 typedef unsigned int u32;
216 typedef signed int s32;
218 /* this is an integral type the same length as a machine pointer */
219 typedef uintptr_t pointer_sized_uint_t;
221 #ifdef _WIN64
222 #define AMD64_SYSV_ABI __attribute__((sysv_abi))
223 #else
224 #define AMD64_SYSV_ABI
225 #endif
227 #include <sys/types.h>
229 #if defined(LISP_FEATURE_SB_THREAD)
230 typedef pthread_t os_thread_t;
231 #else
232 typedef pid_t os_thread_t;
233 #endif
235 typedef uintptr_t uword_t;
236 typedef intptr_t sword_t;
238 /* FIXME: we do things this way because of the alpha32 port. once
239 alpha64 has arrived, all this nastiness can go away */
240 #if 64 == N_WORD_BITS
241 #define LOW_WORD(c) ((pointer_sized_uint_t)c)
242 #define OBJ_FMTX "lx"
243 typedef uintptr_t lispobj;
244 #else
245 #define OBJ_FMTX "x"
246 #define LOW_WORD(c) ((long)(c) & 0xFFFFFFFFL)
247 /* fake it on alpha32 */
248 typedef unsigned int lispobj;
249 #endif
251 static inline int
252 lowtag_of(lispobj obj)
254 return obj & LOWTAG_MASK;
257 static inline int
258 widetag_of(lispobj obj)
260 return obj & WIDETAG_MASK;
263 static inline uword_t
264 HeaderValue(lispobj obj)
266 return obj >> N_WIDETAG_BITS;
269 static inline uword_t instance_length(lispobj header)
271 return (header >> N_WIDETAG_BITS);
273 static inline lispobj instance_layout(lispobj* instance_ptr) // native ptr
275 return instance_ptr[1]; // the word following the header is the layout
278 static inline struct cons *
279 CONS(lispobj obj)
281 return (struct cons *)(obj - LIST_POINTER_LOWTAG);
284 static inline struct symbol *
285 SYMBOL(lispobj obj)
287 return (struct symbol *)(obj - OTHER_POINTER_LOWTAG);
290 static inline struct fdefn *
291 FDEFN(lispobj obj)
293 return (struct fdefn *)(obj - OTHER_POINTER_LOWTAG);
296 /* Is the Lisp object obj something with pointer nature (as opposed to
297 * e.g. a fixnum or character or unbound marker)? */
298 static inline int
299 is_lisp_pointer(lispobj obj)
301 #if N_WORD_BITS == 64
302 return (obj & 3) == 3;
303 #else
304 return obj & 1;
305 #endif
308 #include "fixnump.h"
310 /* Is the Lisp object obj something with immediate nature (e.g. a
311 * fixnum or character or unbound marker)? */
312 static inline int
313 is_lisp_immediate(lispobj obj)
315 return (fixnump(obj)
316 || (widetag_of(obj) == CHARACTER_WIDETAG)
317 #if N_WORD_BITS == 64
318 || (widetag_of(obj) == SINGLE_FLOAT_WIDETAG)
319 #endif
320 || (widetag_of(obj) == UNBOUND_MARKER_WIDETAG));
323 /* Convert from a lispobj with type bits to a native (ordinary
324 * C/assembly) pointer to the beginning of the object. */
325 static inline lispobj *
326 native_pointer(lispobj obj)
328 return (lispobj *) ((pointer_sized_uint_t) (obj & ~LOWTAG_MASK));
331 /* inverse operation: create a suitably tagged lispobj from a native
332 * pointer or integer.*/
333 static inline lispobj
334 make_lispobj(void *o, int low_tag)
336 return LOW_WORD(o) | low_tag;
339 #define MAKE_FIXNUM(n) (n << N_FIXNUM_TAG_BITS)
340 static inline lispobj
341 make_fixnum(sword_t n)
343 return MAKE_FIXNUM(n);
346 static inline sword_t
347 fixnum_value(lispobj n)
349 return n >> N_FIXNUM_TAG_BITS;
352 static inline sword_t
353 fixnum_word_value(lispobj n)
355 /* Convert bytes into words, double-word aligned. */
356 sword_t x = ((n >> N_FIXNUM_TAG_BITS) + LOWTAG_MASK) & ~LOWTAG_MASK;
358 return x >> WORD_SHIFT;
361 #if defined(LISP_FEATURE_WIN32)
362 /* KLUDGE: Avoid double definition of boolean by rpcndr.h included via
363 * shlobj.h.
365 * FIXME: We should probably arrange to use the rpcndr.h boolean on Windows,
366 * or get rid of our own boolean type. If the boolean type is only used in
367 * the runtime, and never passed to Lisp, then it doesn't matter which one
368 * we use.
370 #define boolean rpcndr_boolean
371 #include <shlobj.h>
372 #undef boolean
373 #endif
374 typedef int boolean;
376 static inline boolean
377 other_immediate_lowtag_p(lispobj header)
379 /* These lowtags are spaced 4 apart throughout the lowtag space. */
380 return (lowtag_of(header) & 3) == OTHER_IMMEDIATE_0_LOWTAG;
383 /* KLUDGE: As far as I can tell there's no ANSI C way of saying
384 * "this function never returns". This is the way that you do it
385 * in GCC later than version 2.5 or so. */
386 #if defined(__GNUC__)
387 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5)
388 #define never_returns __attribute__ ((noreturn))
389 #else
390 #define never_returns
391 #endif
392 #else
393 #define never_returns
394 #endif
396 extern void *successful_malloc (size_t size);
397 extern char *copied_string (char *string);
399 #if defined(LISP_FEATURE_SB_THREAD) && !defined(LISP_FEATURE_SB_SAFEPOINT)
400 # define THREADS_USING_GCSIGNAL 1
401 #endif
403 /* Now that SPARC has precise GENCGC, several places that used to be
404 * #ifdef PCC need adjustment. Clearly, "PPC or SPARC" is as unhelpful
405 * a test as its reverse, "x86 or x86-64". However, the feature
406 * commonly used to differentiate between those two worlds is
407 * C_STACK_IS_CONTROL_STACK, and clearly (or at least in my humble
408 * opinion), at some point we'd like to have precise GC on x86 while
409 * still sharing the C stack, so stack usage ought not imply GC
410 * conservativeness. So let's have a helper feature that makes the code
411 * a bit more future-proof, even if it is itself currently defined in
412 * the naive way: */
413 #if defined(LISP_FEATURE_GENCGC) && !defined(LISP_FEATURE_C_STACK_IS_CONTROL_STACK)
414 # define GENCGC_IS_PRECISE 1
415 #endif
417 void *os_dlsym_default(char *name);
419 #endif /* _SBCL_RUNTIME_H_ */