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