1.0.12.19: runtime cleanups by Daniel Lowe
[sbcl/simd.git] / src / runtime / thread.h
blob8134ff01f8de73211fd2859fbf54f3862b48bfb1
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 #ifdef LISP_FEATURE_SB_THREAD
38 #define for_each_thread(th) for(th=all_threads;th;th=th->next)
39 #else
40 /* there's some possibility a SSC could notice this never actually
41 * loops */
42 #define for_each_thread(th) for(th=all_threads;th;th=0)
43 #endif
45 static inline lispobj
46 SymbolValue(u64 tagged_symbol_pointer, void *thread)
48 struct symbol *sym= (struct symbol *)
49 (pointer_sized_uint_t)(tagged_symbol_pointer-OTHER_POINTER_LOWTAG);
50 #ifdef LISP_FEATURE_SB_THREAD
51 if(thread && sym->tls_index) {
52 lispobj r=
53 ((union per_thread_data *)thread)
54 ->dynamic_values[fixnum_value(sym->tls_index)];
55 if(r!=NO_TLS_VALUE_MARKER_WIDETAG) return r;
57 #endif
58 return sym->value;
61 static inline lispobj
62 SymbolTlValue(u64 tagged_symbol_pointer, void *thread)
64 struct symbol *sym= (struct symbol *)
65 (pointer_sized_uint_t)(tagged_symbol_pointer-OTHER_POINTER_LOWTAG);
66 #ifdef LISP_FEATURE_SB_THREAD
67 return ((union per_thread_data *)thread)
68 ->dynamic_values[fixnum_value(sym->tls_index)];
69 #else
70 return sym->value;
71 #endif
74 static inline void
75 SetSymbolValue(u64 tagged_symbol_pointer,lispobj val, void *thread)
77 struct symbol *sym= (struct symbol *)
78 (pointer_sized_uint_t)(tagged_symbol_pointer-OTHER_POINTER_LOWTAG);
79 #ifdef LISP_FEATURE_SB_THREAD
80 if(thread && sym->tls_index) {
81 lispobj *pr= &(((union per_thread_data *)thread)
82 ->dynamic_values[fixnum_value(sym->tls_index)]);
83 if(*pr!=NO_TLS_VALUE_MARKER_WIDETAG) {
84 *pr=val;
85 return;
88 #endif
89 sym->value = val;
92 static inline void
93 SetTlSymbolValue(u64 tagged_symbol_pointer,lispobj val, void *thread)
95 #ifdef LISP_FEATURE_SB_THREAD
96 struct symbol *sym= (struct symbol *)
97 (pointer_sized_uint_t)(tagged_symbol_pointer-OTHER_POINTER_LOWTAG);
98 ((union per_thread_data *)thread)
99 ->dynamic_values[fixnum_value(sym->tls_index)]
100 =val;
101 #else
102 SetSymbolValue(tagged_symbol_pointer,val,thread) ;
103 #endif
106 /* This only works for static symbols. */
107 static inline lispobj
108 StaticSymbolFunction(lispobj sym)
110 return ((struct fdefn *)native_pointer(SymbolValue(sym, 0)))->fun;
113 static inline
114 os_context_t *get_interrupt_context_for_thread(struct thread *th)
116 return th->interrupt_contexts
117 [fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,th)-1)];
120 #if defined(LISP_FEATURE_SB_THREAD) && defined(LISP_FEATURE_GCC_TLS)
121 extern __thread struct thread *current_thread;
122 #endif
124 /* This is clearly per-arch and possibly even per-OS code, but we can't
125 * put it somewhere sensible like x86-linux-os.c because it needs too
126 * much stuff like struct thread and all_threads to be defined, which
127 * usually aren't by that time. So, it's here instead. Sorry */
129 static inline struct thread *arch_os_get_current_thread(void)
131 #if defined(LISP_FEATURE_SB_THREAD)
132 #if defined(LISP_FEATURE_X86)
133 register struct thread *me=0;
134 if(all_threads) {
135 #if defined(LISP_FEATURE_DARWIN) && defined(LISP_FEATURE_RESTORE_FS_SEGMENT_REGISTER_FROM_TLS)
136 sel_t sel;
137 struct thread *th = pthread_getspecific(specials);
138 sel.index = th->tls_cookie;
139 sel.rpl = USER_PRIV;
140 sel.ti = SEL_LDT;
141 __asm__ __volatile__ ("movw %w0, %%fs" : : "r"(sel));
142 #elif defined(LISP_FEATURE_FREEBSD)
143 #ifdef LISP_FEATURE_GCC_TLS
144 struct thread *th = current_thread;
145 #else
146 struct thread *th = pthread_getspecific(specials);
147 #endif
148 #ifdef LISP_FEATURE_RESTORE_TLS_SEGMENT_REGISTER_FROM_TLS
149 unsigned int sel = LSEL(th->tls_cookie, SEL_UPL);
150 unsigned int fs = rfs();
152 /* Load FS only if it's necessary. Modifying a selector
153 * causes privilege checking and it takes long time. */
154 if (fs != sel)
155 load_fs(sel);
156 #endif
157 return th;
158 #endif
159 __asm__ __volatile__ ("movl %%fs:%c1,%0" : "=r" (me)
160 : "i" (offsetof (struct thread,this)));
162 return me;
163 #else
164 #ifdef LISP_FEATURE_GCC_TLS
165 return current_thread;
166 #else
167 return pthread_getspecific(specials);
168 #endif
169 #endif /* x86 */
170 #else
171 return all_threads;
172 #endif
175 #if defined(LISP_FEATURE_MACH_EXCEPTION_HANDLER)
176 #define THREAD_STRUCT_TO_EXCEPTION_PORT(th) ((mach_port_t) th)
177 #define EXCEPTION_PORT_TO_THREAD_STRUCT(th) ((struct thread *) th)
178 #endif
180 #if defined(LISP_FEATURE_SB_THREAD)
181 #define thread_self pthread_self
182 #define thread_kill pthread_kill
183 #define thread_sigmask pthread_sigmask
184 #define thread_mutex_lock(l) pthread_mutex_lock(l)
185 #define thread_mutex_unlock(l) pthread_mutex_unlock(l)
186 #else
187 #define thread_self getpid
188 #define thread_kill kill
189 #define thread_sigmask sigprocmask
190 #define thread_mutex_lock(l) 0
191 #define thread_mutex_unlock(l) 0
192 #endif
194 extern void create_initial_thread(lispobj);
195 extern int kill_thread_safely(os_thread_t os_thread, int signo);
197 #endif /* _INCLUDE_THREAD_H_ */