0.9.1.59:
[sbcl/eslaughter.git] / src / runtime / thread.h
blob870411c747a1222279eab92f332b4d68ee11f390
2 #if !defined(_INCLUDE_THREAD_H_)
3 #define _INCLUDE_THREAD_H_
5 #include <sys/types.h>
6 #include <unistd.h>
7 #include <stddef.h>
8 #include "sbcl.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"
21 #define STATE_RUNNING (make_fixnum(0))
22 #define STATE_STOPPING (make_fixnum(1))
23 #define STATE_STOPPED (make_fixnum(2))
24 #define STATE_DEAD (make_fixnum(3))
25 #define STATE_STARTING (make_fixnum(4))
27 #define THREAD_SLOT_OFFSET_WORDS(c) \
28 (offsetof(struct thread,c)/(sizeof (struct thread *)))
30 union per_thread_data {
31 struct thread thread;
32 lispobj dynamic_values[1]; /* actually more like 4000 or so */
35 extern struct thread *all_threads;
36 extern int dynamic_values_bytes;
37 extern struct thread *find_thread_by_os_thread(os_thread_t tid);
39 #ifdef LISP_FEATURE_SB_THREAD
40 #define for_each_thread(th) for(th=all_threads;th;th=th->next)
41 #else
42 /* there's some possibility a SSC could notice this never actually
43 * loops */
44 #define for_each_thread(th) for(th=all_threads;th;th=0)
45 #endif
47 static inline lispobj 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) {
52 lispobj r=
53 ((union per_thread_data *)thread)
54 ->dynamic_values[fixnum_value(sym->tls_index)];
55 if(r!=UNBOUND_MARKER_WIDETAG) return r;
57 #endif
58 return sym->value;
60 static inline lispobj 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)];
66 #else
67 return sym->value;
68 #endif
71 static inline void SetSymbolValue(u64 tagged_symbol_pointer,lispobj val, void *thread) {
72 struct symbol *sym= (struct symbol *)
73 (pointer_sized_uint_t)(tagged_symbol_pointer-OTHER_POINTER_LOWTAG);
74 #ifdef LISP_FEATURE_SB_THREAD
75 if(thread && sym->tls_index) {
76 lispobj *pr= &(((union per_thread_data *)thread)
77 ->dynamic_values[fixnum_value(sym->tls_index)]);
78 if(*pr!= UNBOUND_MARKER_WIDETAG) {
79 *pr=val;
80 return;
83 #endif
84 sym->value = val;
86 static inline void SetTlSymbolValue(u64 tagged_symbol_pointer,lispobj val, void *thread) {
87 #ifdef LISP_FEATURE_SB_THREAD
88 struct symbol *sym= (struct symbol *)
89 (pointer_sized_uint_t)(tagged_symbol_pointer-OTHER_POINTER_LOWTAG);
90 ((union per_thread_data *)thread)
91 ->dynamic_values[fixnum_value(sym->tls_index)]
92 =val;
93 #else
94 SetSymbolValue(tagged_symbol_pointer,val,thread) ;
95 #endif
98 static inline os_context_t *get_interrupt_context_for_thread(struct thread *th)
100 return th->interrupt_contexts
101 [fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,th)-1)];
104 /* This is clearly per-arch and possibly even per-OS code, but we can't
105 * put it somewhere sensible like x86-linux-os.c because it needs too
106 * much stuff like struct thread and all_threads to be defined, which
107 * usually aren't by that time. So, it's here instead. Sorry */
109 static inline struct thread *arch_os_get_current_thread() {
110 #if defined(LISP_FEATURE_SB_THREAD)
111 #if defined(LISP_FEATURE_X86)
112 register struct thread *me=0;
113 if(all_threads)
114 __asm__ __volatile__ ("movl %%fs:%c1,%0" : "=r" (me)
115 : "i" (offsetof (struct thread,this)));
116 return me;
117 #else
118 return pthread_getspecific(specials);
119 #endif /* x86 */
120 #else
121 return all_threads;
122 #endif
125 #if defined(LISP_FEATURE_SB_THREAD)
126 #define thread_self pthread_self
127 #define thread_kill pthread_kill
128 #define thread_sigmask pthread_sigmask
129 #else
130 #define thread_self getpid
131 #define thread_kill kill
132 #define thread_sigmask sigprocmask
133 #endif
135 extern void create_initial_thread(lispobj);
137 #endif /* _INCLUDE_THREAD_H_ */