1.0.23.40: export page sizes to C with LU suffix
[sbcl/tcr.git] / src / runtime / thread.h
blobc252afadc7074145d8863c64caba4dad7381dbf4
1 #if !defined(_INCLUDE_THREAD_H_)
2 #define _INCLUDE_THREAD_H_
4 #include <sys/types.h>
5 #include <unistd.h>
6 #include <stddef.h>
7 #include "sbcl.h"
8 #include "globals.h"
9 #include "runtime.h"
10 #include "os.h"
11 #include "interrupt.h"
12 #ifdef LISP_FEATURE_GENCGC
13 #include "gencgc-alloc-region.h"
14 #else
15 struct alloc_region { };
16 #endif
17 #include "genesis/symbol.h"
18 #include "genesis/static-symbols.h"
19 #include "genesis/thread.h"
20 #include "genesis/fdefn.h"
22 #define STATE_RUNNING (make_fixnum(1))
23 #define STATE_SUSPENDED (make_fixnum(2))
24 #define STATE_DEAD (make_fixnum(3))
26 #define THREAD_SLOT_OFFSET_WORDS(c) \
27 (offsetof(struct thread,c)/(sizeof (struct thread *)))
29 union per_thread_data {
30 struct thread thread;
31 lispobj dynamic_values[1]; /* actually more like 4000 or so */
34 extern struct thread * volatile all_threads;
35 extern int dynamic_values_bytes;
37 #if defined(LISP_FEATURE_DARWIN)
38 #define CONTROL_STACK_ALIGNMENT_BYTES 8192 /* darwin wants page-aligned stacks */
39 #define THREAD_ALIGNMENT_BYTES CONTROL_STACK_ALIGNMENT_BYTES
40 #else
41 #define THREAD_ALIGNMENT_BYTES BACKEND_PAGE_BYTES
42 #define CONTROL_STACK_ALIGNMENT_BYTES 16
43 #endif
46 #ifdef LISP_FEATURE_SB_THREAD
47 #define for_each_thread(th) for(th=all_threads;th;th=th->next)
48 #else
49 /* there's some possibility a SSC could notice this never actually
50 * loops */
51 #define for_each_thread(th) for(th=all_threads;th;th=0)
52 #endif
54 static inline lispobj
55 SymbolValue(u64 tagged_symbol_pointer, void *thread)
57 struct symbol *sym= (struct symbol *)
58 (pointer_sized_uint_t)(tagged_symbol_pointer-OTHER_POINTER_LOWTAG);
59 #ifdef LISP_FEATURE_SB_THREAD
60 if(thread && sym->tls_index) {
61 lispobj r=
62 ((union per_thread_data *)thread)
63 ->dynamic_values[fixnum_value(sym->tls_index)];
64 if(r!=NO_TLS_VALUE_MARKER_WIDETAG) return r;
66 #endif
67 return sym->value;
70 static inline lispobj
71 SymbolTlValue(u64 tagged_symbol_pointer, void *thread)
73 struct symbol *sym= (struct symbol *)
74 (pointer_sized_uint_t)(tagged_symbol_pointer-OTHER_POINTER_LOWTAG);
75 #ifdef LISP_FEATURE_SB_THREAD
76 return ((union per_thread_data *)thread)
77 ->dynamic_values[fixnum_value(sym->tls_index)];
78 #else
79 return sym->value;
80 #endif
83 static inline void
84 SetSymbolValue(u64 tagged_symbol_pointer,lispobj val, void *thread)
86 struct symbol *sym= (struct symbol *)
87 (pointer_sized_uint_t)(tagged_symbol_pointer-OTHER_POINTER_LOWTAG);
88 #ifdef LISP_FEATURE_SB_THREAD
89 if(thread && sym->tls_index) {
90 lispobj *pr= &(((union per_thread_data *)thread)
91 ->dynamic_values[fixnum_value(sym->tls_index)]);
92 if(*pr!=NO_TLS_VALUE_MARKER_WIDETAG) {
93 *pr=val;
94 return;
97 #endif
98 sym->value = val;
101 static inline void
102 SetTlSymbolValue(u64 tagged_symbol_pointer,lispobj val, void *thread)
104 #ifdef LISP_FEATURE_SB_THREAD
105 struct symbol *sym= (struct symbol *)
106 (pointer_sized_uint_t)(tagged_symbol_pointer-OTHER_POINTER_LOWTAG);
107 ((union per_thread_data *)thread)
108 ->dynamic_values[fixnum_value(sym->tls_index)]
109 =val;
110 #else
111 SetSymbolValue(tagged_symbol_pointer,val,thread) ;
112 #endif
115 /* This only works for static symbols. */
116 static inline lispobj
117 StaticSymbolFunction(lispobj sym)
119 return ((struct fdefn *)native_pointer(SymbolValue(sym, 0)))->fun;
122 static inline
123 os_context_t *get_interrupt_context_for_thread(struct thread *th)
125 return th->interrupt_contexts
126 [fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,th)-1)];
129 #if defined(LISP_FEATURE_SB_THREAD) && defined(LISP_FEATURE_GCC_TLS)
130 extern __thread struct thread *current_thread;
131 #endif
133 /* This is clearly per-arch and possibly even per-OS code, but we can't
134 * put it somewhere sensible like x86-linux-os.c because it needs too
135 * much stuff like struct thread and all_threads to be defined, which
136 * usually aren't by that time. So, it's here instead. Sorry */
138 static inline struct thread *arch_os_get_current_thread(void)
140 #if defined(LISP_FEATURE_SB_THREAD)
141 #if defined(LISP_FEATURE_X86)
142 register struct thread *me=0;
143 if(all_threads) {
144 #if defined(LISP_FEATURE_DARWIN) && defined(LISP_FEATURE_RESTORE_FS_SEGMENT_REGISTER_FROM_TLS)
145 sel_t sel;
146 struct thread *th = pthread_getspecific(specials);
147 sel.index = th->tls_cookie;
148 sel.rpl = USER_PRIV;
149 sel.ti = SEL_LDT;
150 __asm__ __volatile__ ("movw %w0, %%fs" : : "r"(sel));
151 #elif defined(LISP_FEATURE_FREEBSD)
152 #ifdef LISP_FEATURE_GCC_TLS
153 struct thread *th = current_thread;
154 #else
155 struct thread *th = pthread_getspecific(specials);
156 #endif
157 #ifdef LISP_FEATURE_RESTORE_TLS_SEGMENT_REGISTER_FROM_TLS
158 unsigned int sel = LSEL(th->tls_cookie, SEL_UPL);
159 unsigned int fs = rfs();
161 /* Load FS only if it's necessary. Modifying a selector
162 * causes privilege checking and it takes long time. */
163 if (fs != sel)
164 load_fs(sel);
165 #endif
166 return th;
167 #endif
168 __asm__ __volatile__ ("movl %%fs:%c1,%0" : "=r" (me)
169 : "i" (offsetof (struct thread,this)));
171 return me;
172 #else
173 #ifdef LISP_FEATURE_GCC_TLS
174 return current_thread;
175 #else
176 return pthread_getspecific(specials);
177 #endif
178 #endif /* x86 */
179 #else
180 return all_threads;
181 #endif
184 #if defined(LISP_FEATURE_MACH_EXCEPTION_HANDLER)
185 #define THREAD_STRUCT_TO_EXCEPTION_PORT(th) ((mach_port_t) th)
186 #define EXCEPTION_PORT_TO_THREAD_STRUCT(th) ((struct thread *) th)
187 #endif
189 #if defined(LISP_FEATURE_SB_THREAD)
190 #define thread_self pthread_self
191 #define thread_kill pthread_kill
192 #define thread_sigmask pthread_sigmask
193 #define thread_mutex_lock(l) pthread_mutex_lock(l)
194 #define thread_mutex_unlock(l) pthread_mutex_unlock(l)
195 #else
196 #define thread_self getpid
197 #define thread_kill kill
198 #define thread_sigmask sigprocmask
199 #define thread_mutex_lock(l) 0
200 #define thread_mutex_unlock(l) 0
201 #endif
203 extern void create_initial_thread(lispobj);
204 extern int kill_thread_safely(os_thread_t os_thread, int signo);
206 #endif /* _INCLUDE_THREAD_H_ */