1 #if !defined(_INCLUDE_THREAD_H_)
2 #define _INCLUDE_THREAD_H_
11 #include "interrupt.h"
12 #ifdef LISP_FEATURE_GENCGC
13 #include "gencgc-alloc-region.h"
15 struct alloc_region { };
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 #ifdef LISP_FEATURE_SB_THREAD
28 /* Only access thread state with blockables blocked. */
30 thread_state(struct thread *thread)
33 pthread_mutex_lock(thread->state_lock);
34 state = thread->state;
35 pthread_mutex_unlock(thread->state_lock);
40 set_thread_state(struct thread *thread, lispobj state)
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);
49 wait_for_thread_state_change(struct thread *thread, lispobj state)
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);
57 extern pthread_key_t lisp_thread;
60 extern int kill_safely(os_thread_t os_thread, int signal);
61 extern void kill_a_lisp_thread(int signal);
63 #define THREAD_SLOT_OFFSET_WORDS(c) \
64 (offsetof(struct thread,c)/(sizeof (struct thread *)))
66 union per_thread_data {
68 lispobj dynamic_values[1]; /* actually more like 4000 or so */
71 extern struct thread *all_threads;
72 extern int dynamic_values_bytes;
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
78 #define THREAD_ALIGNMENT_BYTES BACKEND_PAGE_BYTES
79 #define CONTROL_STACK_ALIGNMENT_BYTES 16
83 #ifdef LISP_FEATURE_SB_THREAD
84 #define for_each_thread(th) for(th=all_threads;th;th=th->next)
86 /* there's some possibility a SSC could notice this never actually
88 #define for_each_thread(th) for(th=all_threads;th;th=0)
91 static inline lispobj *
92 SymbolValueAddress(u64 tagged_symbol_pointer, void *thread)
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;
106 static inline lispobj
107 SymbolValue(u64 tagged_symbol_pointer, void *thread)
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) {
114 ((union per_thread_data *)thread)
115 ->dynamic_values[fixnum_value(sym->tls_index)];
116 if(r!=NO_TLS_VALUE_MARKER_WIDETAG) return r;
122 static inline lispobj
123 SymbolTlValue(u64 tagged_symbol_pointer, void *thread)
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)];
136 SetSymbolValue(u64 tagged_symbol_pointer,lispobj val, void *thread)
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) {
154 SetTlSymbolValue(u64 tagged_symbol_pointer,lispobj val, void *thread)
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)]
163 SetSymbolValue(tagged_symbol_pointer,val,thread) ;
167 /* This only works for static symbols. */
168 static inline lispobj
169 StaticSymbolFunction(lispobj sym)
171 return ((struct fdefn *)native_pointer(SymbolValue(sym, 0)))->fun;
174 #if defined(LISP_FEATURE_SB_THREAD) && defined(LISP_FEATURE_GCC_TLS)
175 extern __thread struct thread *current_thread;
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 */
183 static inline struct thread *arch_os_get_current_thread(void)
185 #if defined(LISP_FEATURE_SB_THREAD)
186 #if defined(LISP_FEATURE_X86)
187 register struct thread *me=0;
189 #if defined(LISP_FEATURE_DARWIN) && defined(LISP_FEATURE_RESTORE_FS_SEGMENT_REGISTER_FROM_TLS)
191 struct thread *th = pthread_getspecific(specials);
192 sel.index = th->tls_cookie;
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;
200 struct thread *th = pthread_getspecific(specials);
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();
206 /* Load FS only if it's necessary. Modifying a selector
207 * causes privilege checking and it takes long time. */
213 __asm__ __volatile__ ("movl %%fs:%c1,%0" : "=r" (me)
214 : "i" (offsetof (struct thread,this)));
218 #ifdef LISP_FEATURE_GCC_TLS
219 return current_thread;
221 return pthread_getspecific(specials);
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)
234 extern void create_initial_thread(lispobj);
236 #endif /* _INCLUDE_THREAD_H_ */