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"
21 #define STATE_RUNNING (make_fixnum(1))
22 #define STATE_SUSPENDED (make_fixnum(2))
23 #define STATE_DEAD (make_fixnum(3))
25 #define THREAD_SLOT_OFFSET_WORDS(c) \
26 (offsetof(struct thread,c)/(sizeof (struct thread *)))
28 union per_thread_data {
30 lispobj dynamic_values[1]; /* actually more like 4000 or so */
33 extern struct thread * volatile all_threads;
34 extern int dynamic_values_bytes;
36 #ifdef LISP_FEATURE_SB_THREAD
37 #define for_each_thread(th) for(th=all_threads;th;th=th->next)
39 /* there's some possibility a SSC could notice this never actually
41 #define for_each_thread(th) for(th=all_threads;th;th=0)
45 SymbolValue(u64 tagged_symbol_pointer, void *thread) {
46 struct symbol *sym= (struct symbol *)
47 (pointer_sized_uint_t)(tagged_symbol_pointer-OTHER_POINTER_LOWTAG);
48 #ifdef LISP_FEATURE_SB_THREAD
49 if(thread && sym->tls_index) {
51 ((union per_thread_data *)thread)
52 ->dynamic_values[fixnum_value(sym->tls_index)];
53 if(r!=NO_TLS_VALUE_MARKER_WIDETAG) return r;
60 SymbolTlValue(u64 tagged_symbol_pointer, void *thread) {
61 struct symbol *sym= (struct symbol *)
62 (pointer_sized_uint_t)(tagged_symbol_pointer-OTHER_POINTER_LOWTAG);
63 #ifdef LISP_FEATURE_SB_THREAD
64 return ((union per_thread_data *)thread)
65 ->dynamic_values[fixnum_value(sym->tls_index)];
72 SetSymbolValue(u64 tagged_symbol_pointer,lispobj val, 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 if(thread && sym->tls_index) {
77 lispobj *pr= &(((union per_thread_data *)thread)
78 ->dynamic_values[fixnum_value(sym->tls_index)]);
79 if(*pr!=NO_TLS_VALUE_MARKER_WIDETAG) {
88 SetTlSymbolValue(u64 tagged_symbol_pointer,lispobj val, void *thread) {
89 #ifdef LISP_FEATURE_SB_THREAD
90 struct symbol *sym= (struct symbol *)
91 (pointer_sized_uint_t)(tagged_symbol_pointer-OTHER_POINTER_LOWTAG);
92 ((union per_thread_data *)thread)
93 ->dynamic_values[fixnum_value(sym->tls_index)]
96 SetSymbolValue(tagged_symbol_pointer,val,thread) ;
101 os_context_t *get_interrupt_context_for_thread(struct thread *th)
103 return th->interrupt_contexts
104 [fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,th)-1)];
107 /* This is clearly per-arch and possibly even per-OS code, but we can't
108 * put it somewhere sensible like x86-linux-os.c because it needs too
109 * much stuff like struct thread and all_threads to be defined, which
110 * usually aren't by that time. So, it's here instead. Sorry */
112 static inline struct thread *arch_os_get_current_thread() {
113 #if defined(LISP_FEATURE_SB_THREAD)
114 #if defined(LISP_FEATURE_X86)
115 register struct thread *me=0;
117 #if defined(LISP_FEATURE_DARWIN) && defined(LISP_FEATURE_RESTORE_FS_SEGMENT_REGISTER_FROM_TLS)
119 struct thread *th = pthread_getspecific(specials);
120 sel.index = th->tls_cookie;
123 __asm__ __volatile__ ("movw %w0, %%fs" : : "r"(sel));
124 #elif defined(LISP_FEATURE_FREEBSD) && defined(LISP_FEATURE_RESTORE_TLS_SEGMENT_REGISTER_FROM_TLS)
125 struct thread *th = pthread_getspecific(specials);
126 unsigned int sel = LSEL(th->tls_cookie, SEL_UPL);
127 unsigned int fs = rfs();
129 /* Load FS only if it's necessary. Modifying a selector
130 * causes privilege checking and it takes long time. */
135 __asm__ __volatile__ ("movl %%fs:%c1,%0" : "=r" (me)
136 : "i" (offsetof (struct thread,this)));
140 return pthread_getspecific(specials);
147 #if defined(LISP_FEATURE_MACH_EXCEPTION_HANDLER)
148 #define THREAD_STRUCT_TO_EXCEPTION_PORT(th) ((mach_port_t) th)
149 #define EXCEPTION_PORT_TO_THREAD_STRUCT(th) ((struct thread *) th)
152 #if defined(LISP_FEATURE_SB_THREAD)
153 #define thread_self pthread_self
154 #define thread_kill pthread_kill
155 #define thread_sigmask pthread_sigmask
156 #define thread_mutex_lock(l) pthread_mutex_lock(l)
157 #define thread_mutex_unlock(l) pthread_mutex_unlock(l)
159 #define thread_self getpid
160 #define thread_kill kill
161 #define thread_sigmask sigprocmask
162 #define thread_mutex_lock(l) 0
163 #define thread_mutex_unlock(l) 0
166 extern void create_initial_thread(lispobj);
167 extern int kill_thread_safely(os_thread_t os_thread, int signo);
169 #endif /* _INCLUDE_THREAD_H_ */