1.0.28.37: resignal signals received in foreign threads
[sbcl.git] / src / runtime / thread.h
1 #if !defined(_INCLUDE_THREAD_H_)
2 #define _INCLUDE_THREAD_H_
3
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"
21
22 #define STATE_RUNNING (make_fixnum(1))
23 #define STATE_SUSPENDED (make_fixnum(2))
24 #define STATE_DEAD (make_fixnum(3))
25
26 #ifdef LISP_FEATURE_SB_THREAD
27
28 /* Only access thread state with blockables blocked. */
29 static inline lispobj
30 thread_state(struct thread *thread)
31 {
32     lispobj state;
33     pthread_mutex_lock(thread->state_lock);
34     state = thread->state;
35     pthread_mutex_unlock(thread->state_lock);
36     return state;
37 }
38
39 static inline void
40 set_thread_state(struct thread *thread, lispobj state)
41 {
42     pthread_mutex_lock(thread->state_lock);
43     thread->state = state;
44     pthread_cond_broadcast(thread->state_cond);
45     pthread_mutex_unlock(thread->state_lock);
46 }
47
48 static inline void
49 wait_for_thread_state_change(struct thread *thread, lispobj state)
50 {
51     pthread_mutex_lock(thread->state_lock);
52     while (thread->state == state)
53         pthread_cond_wait(thread->state_cond, thread->state_lock);
54     pthread_mutex_unlock(thread->state_lock);
55 }
56
57 #endif
58
59 extern int kill_safely(os_thread_t os_thread, int signal);
60 extern void kill_a_lisp_thread(int signal);
61
62 #define THREAD_SLOT_OFFSET_WORDS(c) \
63  (offsetof(struct thread,c)/(sizeof (struct thread *)))
64
65 union per_thread_data {
66     struct thread thread;
67     lispobj dynamic_values[1];  /* actually more like 4000 or so */
68 };
69
70 extern struct thread *all_threads;
71 extern int dynamic_values_bytes;
72 extern pthread_key_t lisp_thread;
73
74 #if defined(LISP_FEATURE_DARWIN)
75 #define CONTROL_STACK_ALIGNMENT_BYTES 8192 /* darwin wants page-aligned stacks */
76 #define THREAD_ALIGNMENT_BYTES CONTROL_STACK_ALIGNMENT_BYTES
77 #else
78 #define THREAD_ALIGNMENT_BYTES BACKEND_PAGE_BYTES
79 #define CONTROL_STACK_ALIGNMENT_BYTES 16
80 #endif
81
82
83 #ifdef LISP_FEATURE_SB_THREAD
84 #define for_each_thread(th) for(th=all_threads;th;th=th->next)
85 #else
86 /* there's some possibility a SSC could notice this never actually
87  * loops  */
88 #define for_each_thread(th) for(th=all_threads;th;th=0)
89 #endif
90
91 static inline lispobj *
92 SymbolValueAddress(u64 tagged_symbol_pointer, void *thread)
93 {
94     struct symbol *sym= (struct symbol *)
95         (pointer_sized_uint_t)(tagged_symbol_pointer-OTHER_POINTER_LOWTAG);
96 #ifdef LISP_FEATURE_SB_THREAD
97     if(thread && sym->tls_index) {
98         lispobj *r = &(((union per_thread_data *)thread)
99                        ->dynamic_values[fixnum_value(sym->tls_index)]);
100         if((*r)!=NO_TLS_VALUE_MARKER_WIDETAG) return r;
101     }
102 #endif
103     return &sym->value;
104 }
105
106 static inline lispobj
107 SymbolValue(u64 tagged_symbol_pointer, void *thread)
108 {
109     struct symbol *sym= (struct symbol *)
110         (pointer_sized_uint_t)(tagged_symbol_pointer-OTHER_POINTER_LOWTAG);
111 #ifdef LISP_FEATURE_SB_THREAD
112     if(thread && sym->tls_index) {
113         lispobj r=
114             ((union per_thread_data *)thread)
115             ->dynamic_values[fixnum_value(sym->tls_index)];
116         if(r!=NO_TLS_VALUE_MARKER_WIDETAG) return r;
117     }
118 #endif
119     return sym->value;
120 }
121
122 static inline lispobj
123 SymbolTlValue(u64 tagged_symbol_pointer, void *thread)
124 {
125     struct symbol *sym= (struct symbol *)
126         (pointer_sized_uint_t)(tagged_symbol_pointer-OTHER_POINTER_LOWTAG);
127 #ifdef LISP_FEATURE_SB_THREAD
128     return ((union per_thread_data *)thread)
129         ->dynamic_values[fixnum_value(sym->tls_index)];
130 #else
131     return sym->value;
132 #endif
133 }
134
135 static inline void
136 SetSymbolValue(u64 tagged_symbol_pointer,lispobj val, void *thread)
137 {
138     struct symbol *sym= (struct symbol *)
139         (pointer_sized_uint_t)(tagged_symbol_pointer-OTHER_POINTER_LOWTAG);
140 #ifdef LISP_FEATURE_SB_THREAD
141     if(thread && sym->tls_index) {
142         lispobj *pr= &(((union per_thread_data *)thread)
143                        ->dynamic_values[fixnum_value(sym->tls_index)]);
144         if(*pr!=NO_TLS_VALUE_MARKER_WIDETAG) {
145             *pr=val;
146             return;
147         }
148     }
149 #endif
150     sym->value = val;
151 }
152
153 static inline void
154 SetTlSymbolValue(u64 tagged_symbol_pointer,lispobj val, void *thread)
155 {
156 #ifdef LISP_FEATURE_SB_THREAD
157     struct symbol *sym= (struct symbol *)
158         (pointer_sized_uint_t)(tagged_symbol_pointer-OTHER_POINTER_LOWTAG);
159     ((union per_thread_data *)thread)
160         ->dynamic_values[fixnum_value(sym->tls_index)]
161         =val;
162 #else
163     SetSymbolValue(tagged_symbol_pointer,val,thread) ;
164 #endif
165 }
166
167 /* This only works for static symbols. */
168 static inline lispobj
169 StaticSymbolFunction(lispobj sym)
170 {
171     return ((struct fdefn *)native_pointer(SymbolValue(sym, 0)))->fun;
172 }
173
174 #if defined(LISP_FEATURE_SB_THREAD) && defined(LISP_FEATURE_GCC_TLS)
175 extern __thread struct thread *current_thread;
176 #endif
177
178 /* This is clearly per-arch and possibly even per-OS code, but we can't
179  * put it somewhere sensible like x86-linux-os.c because it needs too
180  * much stuff like struct thread and all_threads to be defined, which
181  * usually aren't by that time.  So, it's here instead.  Sorry */
182
183 static inline struct thread *arch_os_get_current_thread(void)
184 {
185 #if defined(LISP_FEATURE_SB_THREAD)
186 #if defined(LISP_FEATURE_X86)
187     register struct thread *me=0;
188     if(all_threads) {
189 #if defined(LISP_FEATURE_DARWIN) && defined(LISP_FEATURE_RESTORE_FS_SEGMENT_REGISTER_FROM_TLS)
190         sel_t sel;
191         struct thread *th = pthread_getspecific(specials);
192         sel.index = th->tls_cookie;
193         sel.rpl = USER_PRIV;
194         sel.ti = SEL_LDT;
195         __asm__ __volatile__ ("movw %w0, %%fs" : : "r"(sel));
196 #elif defined(LISP_FEATURE_FREEBSD)
197 #ifdef LISP_FEATURE_GCC_TLS
198         struct thread *th = current_thread;
199 #else
200         struct thread *th = pthread_getspecific(specials);
201 #endif
202 #ifdef LISP_FEATURE_RESTORE_TLS_SEGMENT_REGISTER_FROM_TLS
203         unsigned int sel = LSEL(th->tls_cookie, SEL_UPL);
204         unsigned int fs = rfs();
205
206         /* Load FS only if it's necessary.  Modifying a selector
207          * causes privilege checking and it takes long time. */
208         if (fs != sel)
209             load_fs(sel);
210 #endif
211         return th;
212 #endif
213         __asm__ __volatile__ ("movl %%fs:%c1,%0" : "=r" (me)
214                  : "i" (offsetof (struct thread,this)));
215     }
216     return me;
217 #else
218 #ifdef LISP_FEATURE_GCC_TLS
219     return current_thread;
220 #else
221     return pthread_getspecific(specials);
222 #endif
223 #endif /* x86 */
224 #else
225      return all_threads;
226 #endif
227 }
228
229 #if defined(LISP_FEATURE_MACH_EXCEPTION_HANDLER)
230 #define THREAD_STRUCT_TO_EXCEPTION_PORT(th) ((mach_port_t) th)
231 #define EXCEPTION_PORT_TO_THREAD_STRUCT(th) ((struct thread *) th)
232 #endif
233
234 extern void create_initial_thread(lispobj);
235
236 #endif /* _INCLUDE_THREAD_H_ */