1.0.5.37: cleanup a small thinko from previous x86-assem.S refactoring
[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
21 #define STATE_RUNNING (make_fixnum(1))
22 #define STATE_SUSPENDED (make_fixnum(2))
23 #define STATE_DEAD (make_fixnum(3))
24
25 #define THREAD_SLOT_OFFSET_WORDS(c) \
26  (offsetof(struct thread,c)/(sizeof (struct thread *)))
27
28 union per_thread_data {
29     struct thread thread;
30     lispobj dynamic_values[1];  /* actually more like 4000 or so */
31 };
32
33 extern struct thread * volatile all_threads;
34 extern int dynamic_values_bytes;
35
36 #ifdef LISP_FEATURE_SB_THREAD
37 #define for_each_thread(th) for(th=all_threads;th;th=th->next)
38 #else
39 /* there's some possibility a SSC could notice this never actually
40  * loops  */
41 #define for_each_thread(th) for(th=all_threads;th;th=0)
42 #endif
43
44 static inline lispobj
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) {
50         lispobj r=
51             ((union per_thread_data *)thread)
52             ->dynamic_values[fixnum_value(sym->tls_index)];
53         if(r!=NO_TLS_VALUE_MARKER_WIDETAG) return r;
54     }
55 #endif
56     return sym->value;
57 }
58
59 static inline lispobj
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)];
66 #else
67     return sym->value;
68 #endif
69 }
70
71 static inline void
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) {
80             *pr=val;
81             return;
82         }
83     }
84 #endif
85     sym->value = val;
86 }
87 static inline void
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)]
94         =val;
95 #else
96     SetSymbolValue(tagged_symbol_pointer,val,thread) ;
97 #endif
98 }
99
100 static inline
101 os_context_t *get_interrupt_context_for_thread(struct thread *th)
102 {
103     return th->interrupt_contexts
104         [fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,th)-1)];
105 }
106
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 */
111
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;
116     if(all_threads) {
117 #if defined(LISP_FEATURE_DARWIN) && defined(LISP_FEATURE_RESTORE_FS_SEGMENT_REGISTER_FROM_TLS)
118         sel_t sel;
119         struct thread *th = pthread_getspecific(specials);
120         sel.index = th->tls_cookie;
121         sel.rpl = USER_PRIV;
122         sel.ti = SEL_LDT;
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();
128
129         /* Load FS only if it's necessary.  Modifying a selector
130          * causes privilege checking and it takes long time. */
131         if (fs != sel)
132             load_fs(sel);
133         return th;
134 #endif
135         __asm__ __volatile__ ("movl %%fs:%c1,%0" : "=r" (me)
136                  : "i" (offsetof (struct thread,this)));
137     }
138     return me;
139 #else
140     return pthread_getspecific(specials);
141 #endif /* x86 */
142 #else
143      return all_threads;
144 #endif
145 }
146
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)
150 #endif
151
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)
158 #else
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
164 #endif
165
166 extern void create_initial_thread(lispobj);
167 extern int kill_thread_safely(os_thread_t os_thread, int signo);
168
169 #endif /* _INCLUDE_THREAD_H_ */