1.0.25.24: x86/x86-64 runtime pseudo atomic fixes
[sbcl.git] / src / runtime / thread.h
1 #if !defined(_INCLUDE_THREAD_H_)
2 #define _INCLUDE_THREAD_H_
3
4 #include <sys/types.h>
5 #include <unistd.h>
6 #include <stddef.h>
7 #include "sbcl.h"
8 #include "globals.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"
20 #include "genesis/fdefn.h"
21
22 #define STATE_RUNNING (make_fixnum(1))
23 #define STATE_SUSPENDED (make_fixnum(2))
24 #define STATE_DEAD (make_fixnum(3))
25
26 #define THREAD_SLOT_OFFSET_WORDS(c) \
27  (offsetof(struct thread,c)/(sizeof (struct thread *)))
28
29 union per_thread_data {
30     struct thread thread;
31     lispobj dynamic_values[1];  /* actually more like 4000 or so */
32 };
33
34 extern struct thread * volatile all_threads;
35 extern int dynamic_values_bytes;
36
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
40 #else
41 #define THREAD_ALIGNMENT_BYTES BACKEND_PAGE_BYTES
42 #define CONTROL_STACK_ALIGNMENT_BYTES 16
43 #endif
44
45
46 #ifdef LISP_FEATURE_SB_THREAD
47 #define for_each_thread(th) for(th=all_threads;th;th=th->next)
48 #else
49 /* there's some possibility a SSC could notice this never actually
50  * loops  */
51 #define for_each_thread(th) for(th=all_threads;th;th=0)
52 #endif
53
54 static inline lispobj *
55 SymbolValueAddress(u64 tagged_symbol_pointer, void *thread)
56 {
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) {
61         lispobj *r = &(((union per_thread_data *)thread)
62                        ->dynamic_values[fixnum_value(sym->tls_index)]);
63         if((*r)!=NO_TLS_VALUE_MARKER_WIDETAG) return r;
64     }
65 #endif
66     return &sym->value;
67 }
68
69 static inline lispobj
70 SymbolValue(u64 tagged_symbol_pointer, void *thread)
71 {
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 r=
77             ((union per_thread_data *)thread)
78             ->dynamic_values[fixnum_value(sym->tls_index)];
79         if(r!=NO_TLS_VALUE_MARKER_WIDETAG) return r;
80     }
81 #endif
82     return sym->value;
83 }
84
85 static inline lispobj
86 SymbolTlValue(u64 tagged_symbol_pointer, void *thread)
87 {
88     struct symbol *sym= (struct symbol *)
89         (pointer_sized_uint_t)(tagged_symbol_pointer-OTHER_POINTER_LOWTAG);
90 #ifdef LISP_FEATURE_SB_THREAD
91     return ((union per_thread_data *)thread)
92         ->dynamic_values[fixnum_value(sym->tls_index)];
93 #else
94     return sym->value;
95 #endif
96 }
97
98 static inline void
99 SetSymbolValue(u64 tagged_symbol_pointer,lispobj val, void *thread)
100 {
101     struct symbol *sym= (struct symbol *)
102         (pointer_sized_uint_t)(tagged_symbol_pointer-OTHER_POINTER_LOWTAG);
103 #ifdef LISP_FEATURE_SB_THREAD
104     if(thread && sym->tls_index) {
105         lispobj *pr= &(((union per_thread_data *)thread)
106                        ->dynamic_values[fixnum_value(sym->tls_index)]);
107         if(*pr!=NO_TLS_VALUE_MARKER_WIDETAG) {
108             *pr=val;
109             return;
110         }
111     }
112 #endif
113     sym->value = val;
114 }
115
116 static inline void
117 SetTlSymbolValue(u64 tagged_symbol_pointer,lispobj val, void *thread)
118 {
119 #ifdef LISP_FEATURE_SB_THREAD
120     struct symbol *sym= (struct symbol *)
121         (pointer_sized_uint_t)(tagged_symbol_pointer-OTHER_POINTER_LOWTAG);
122     ((union per_thread_data *)thread)
123         ->dynamic_values[fixnum_value(sym->tls_index)]
124         =val;
125 #else
126     SetSymbolValue(tagged_symbol_pointer,val,thread) ;
127 #endif
128 }
129
130 /* This only works for static symbols. */
131 static inline lispobj
132 StaticSymbolFunction(lispobj sym)
133 {
134     return ((struct fdefn *)native_pointer(SymbolValue(sym, 0)))->fun;
135 }
136
137 static inline
138 os_context_t *get_interrupt_context_for_thread(struct thread *th)
139 {
140     return th->interrupt_contexts
141         [fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,th)-1)];
142 }
143
144 #if defined(LISP_FEATURE_SB_THREAD) && defined(LISP_FEATURE_GCC_TLS)
145 extern __thread struct thread *current_thread;
146 #endif
147
148 /* This is clearly per-arch and possibly even per-OS code, but we can't
149  * put it somewhere sensible like x86-linux-os.c because it needs too
150  * much stuff like struct thread and all_threads to be defined, which
151  * usually aren't by that time.  So, it's here instead.  Sorry */
152
153 static inline struct thread *arch_os_get_current_thread(void)
154 {
155 #if defined(LISP_FEATURE_SB_THREAD)
156 #if defined(LISP_FEATURE_X86)
157     register struct thread *me=0;
158     if(all_threads) {
159 #if defined(LISP_FEATURE_DARWIN) && defined(LISP_FEATURE_RESTORE_FS_SEGMENT_REGISTER_FROM_TLS)
160         sel_t sel;
161         struct thread *th = pthread_getspecific(specials);
162         sel.index = th->tls_cookie;
163         sel.rpl = USER_PRIV;
164         sel.ti = SEL_LDT;
165         __asm__ __volatile__ ("movw %w0, %%fs" : : "r"(sel));
166 #elif defined(LISP_FEATURE_FREEBSD)
167 #ifdef LISP_FEATURE_GCC_TLS
168         struct thread *th = current_thread;
169 #else
170         struct thread *th = pthread_getspecific(specials);
171 #endif
172 #ifdef LISP_FEATURE_RESTORE_TLS_SEGMENT_REGISTER_FROM_TLS
173         unsigned int sel = LSEL(th->tls_cookie, SEL_UPL);
174         unsigned int fs = rfs();
175
176         /* Load FS only if it's necessary.  Modifying a selector
177          * causes privilege checking and it takes long time. */
178         if (fs != sel)
179             load_fs(sel);
180 #endif
181         return th;
182 #endif
183         __asm__ __volatile__ ("movl %%fs:%c1,%0" : "=r" (me)
184                  : "i" (offsetof (struct thread,this)));
185     }
186     return me;
187 #else
188 #ifdef LISP_FEATURE_GCC_TLS
189     return current_thread;
190 #else
191     return pthread_getspecific(specials);
192 #endif
193 #endif /* x86 */
194 #else
195      return all_threads;
196 #endif
197 }
198
199 #if defined(LISP_FEATURE_MACH_EXCEPTION_HANDLER)
200 #define THREAD_STRUCT_TO_EXCEPTION_PORT(th) ((mach_port_t) th)
201 #define EXCEPTION_PORT_TO_THREAD_STRUCT(th) ((struct thread *) th)
202 #endif
203
204 extern void create_initial_thread(lispobj);
205 extern int kill_thread_safely(os_thread_t os_thread, int signo);
206
207 #endif /* _INCLUDE_THREAD_H_ */