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 #ifdef LISP_FEATURE_SB_THREAD
38 #define for_each_thread(th) for(th=all_threads;th;th=th->next)
40 /* there's some possibility a SSC could notice this never actually
42 #define for_each_thread(th) for(th=all_threads;th;th=0)
46 SymbolValue(u64 tagged_symbol_pointer, void *thread)
48 struct symbol *sym= (struct symbol *)
49 (pointer_sized_uint_t)(tagged_symbol_pointer-OTHER_POINTER_LOWTAG);
50 #ifdef LISP_FEATURE_SB_THREAD
51 if(thread && sym->tls_index) {
53 ((union per_thread_data *)thread)
54 ->dynamic_values[fixnum_value(sym->tls_index)];
55 if(r!=NO_TLS_VALUE_MARKER_WIDETAG) return r;
62 SymbolTlValue(u64 tagged_symbol_pointer, void *thread)
64 struct symbol *sym= (struct symbol *)
65 (pointer_sized_uint_t)(tagged_symbol_pointer-OTHER_POINTER_LOWTAG);
66 #ifdef LISP_FEATURE_SB_THREAD
67 return ((union per_thread_data *)thread)
68 ->dynamic_values[fixnum_value(sym->tls_index)];
75 SetSymbolValue(u64 tagged_symbol_pointer,lispobj val, void *thread)
77 struct symbol *sym= (struct symbol *)
78 (pointer_sized_uint_t)(tagged_symbol_pointer-OTHER_POINTER_LOWTAG);
79 #ifdef LISP_FEATURE_SB_THREAD
80 if(thread && sym->tls_index) {
81 lispobj *pr= &(((union per_thread_data *)thread)
82 ->dynamic_values[fixnum_value(sym->tls_index)]);
83 if(*pr!=NO_TLS_VALUE_MARKER_WIDETAG) {
93 SetTlSymbolValue(u64 tagged_symbol_pointer,lispobj val, void *thread)
95 #ifdef LISP_FEATURE_SB_THREAD
96 struct symbol *sym= (struct symbol *)
97 (pointer_sized_uint_t)(tagged_symbol_pointer-OTHER_POINTER_LOWTAG);
98 ((union per_thread_data *)thread)
99 ->dynamic_values[fixnum_value(sym->tls_index)]
102 SetSymbolValue(tagged_symbol_pointer,val,thread) ;
106 /* This only works for static symbols. */
107 static inline lispobj
108 StaticSymbolFunction(lispobj sym)
110 return ((struct fdefn *)native_pointer(SymbolValue(sym, 0)))->fun;
114 os_context_t *get_interrupt_context_for_thread(struct thread *th)
116 return th->interrupt_contexts
117 [fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,th)-1)];
120 #if defined(LISP_FEATURE_SB_THREAD) && defined(LISP_FEATURE_GCC_TLS)
121 extern __thread struct thread *current_thread;
124 /* This is clearly per-arch and possibly even per-OS code, but we can't
125 * put it somewhere sensible like x86-linux-os.c because it needs too
126 * much stuff like struct thread and all_threads to be defined, which
127 * usually aren't by that time. So, it's here instead. Sorry */
129 static inline struct thread *arch_os_get_current_thread(void)
131 #if defined(LISP_FEATURE_SB_THREAD)
132 #if defined(LISP_FEATURE_X86)
133 register struct thread *me=0;
135 #if defined(LISP_FEATURE_DARWIN) && defined(LISP_FEATURE_RESTORE_FS_SEGMENT_REGISTER_FROM_TLS)
137 struct thread *th = pthread_getspecific(specials);
138 sel.index = th->tls_cookie;
141 __asm__ __volatile__ ("movw %w0, %%fs" : : "r"(sel));
142 #elif defined(LISP_FEATURE_FREEBSD)
143 #ifdef LISP_FEATURE_GCC_TLS
144 struct thread *th = current_thread;
146 struct thread *th = pthread_getspecific(specials);
148 #ifdef LISP_FEATURE_RESTORE_TLS_SEGMENT_REGISTER_FROM_TLS
149 unsigned int sel = LSEL(th->tls_cookie, SEL_UPL);
150 unsigned int fs = rfs();
152 /* Load FS only if it's necessary. Modifying a selector
153 * causes privilege checking and it takes long time. */
159 __asm__ __volatile__ ("movl %%fs:%c1,%0" : "=r" (me)
160 : "i" (offsetof (struct thread,this)));
164 #ifdef LISP_FEATURE_GCC_TLS
165 return current_thread;
167 return pthread_getspecific(specials);
175 #if defined(LISP_FEATURE_MACH_EXCEPTION_HANDLER)
176 #define THREAD_STRUCT_TO_EXCEPTION_PORT(th) ((mach_port_t) th)
177 #define EXCEPTION_PORT_TO_THREAD_STRUCT(th) ((struct thread *) th)
180 #if defined(LISP_FEATURE_SB_THREAD)
181 #define thread_self pthread_self
182 #define thread_kill pthread_kill
183 #define thread_sigmask pthread_sigmask
184 #define thread_mutex_lock(l) pthread_mutex_lock(l)
185 #define thread_mutex_unlock(l) pthread_mutex_unlock(l)
187 #define thread_self getpid
188 #define thread_kill kill
189 #define thread_sigmask sigprocmask
190 #define thread_mutex_lock(l) 0
191 #define thread_mutex_unlock(l) 0
194 extern void create_initial_thread(lispobj);
195 extern int kill_thread_safely(os_thread_t os_thread, int signo);
197 #endif /* _INCLUDE_THREAD_H_ */