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 #define THREAD_SLOT_OFFSET_WORDS(c) \
27 (offsetof(struct thread,c)/(sizeof (struct thread *)))
29 union per_thread_data
{
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
41 #define THREAD_ALIGNMENT_BYTES BACKEND_PAGE_SIZE
42 #define CONTROL_STACK_ALIGNMENT_BYTES 16
46 #ifdef LISP_FEATURE_SB_THREAD
47 #define for_each_thread(th) for(th=all_threads;th;th=th->next)
49 /* there's some possibility a SSC could notice this never actually
51 #define for_each_thread(th) for(th=all_threads;th;th=0)
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
) {
62 ((union per_thread_data
*)thread
)
63 ->dynamic_values
[fixnum_value(sym
->tls_index
)];
64 if(r
!=NO_TLS_VALUE_MARKER_WIDETAG
) return r
;
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
)];
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
) {
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
)]
111 SetSymbolValue(tagged_symbol_pointer
,val
,thread
) ;
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
;
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
;
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;
144 #if defined(LISP_FEATURE_DARWIN) && defined(LISP_FEATURE_RESTORE_FS_SEGMENT_REGISTER_FROM_TLS)
146 struct thread
*th
= pthread_getspecific(specials
);
147 sel
.index
= th
->tls_cookie
;
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
;
155 struct thread
*th
= pthread_getspecific(specials
);
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. */
168 __asm__
__volatile__ ("movl %%fs:%c1,%0" : "=r" (me
)
169 : "i" (offsetof (struct thread
,this)));
173 #ifdef LISP_FEATURE_GCC_TLS
174 return current_thread
;
176 return pthread_getspecific(specials
);
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)
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)
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
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_ */