5d5eba9eae0130f58be68c418cecb5ea0581d8e5
[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 {
47     struct symbol *sym= (struct symbol *)
48         (pointer_sized_uint_t)(tagged_symbol_pointer-OTHER_POINTER_LOWTAG);
49 #ifdef LISP_FEATURE_SB_THREAD
50     if(thread && sym->tls_index) {
51         lispobj r=
52             ((union per_thread_data *)thread)
53             ->dynamic_values[fixnum_value(sym->tls_index)];
54         if(r!=NO_TLS_VALUE_MARKER_WIDETAG) return r;
55     }
56 #endif
57     return sym->value;
58 }
59
60 static inline lispobj
61 SymbolTlValue(u64 tagged_symbol_pointer, void *thread)
62 {
63     struct symbol *sym= (struct symbol *)
64         (pointer_sized_uint_t)(tagged_symbol_pointer-OTHER_POINTER_LOWTAG);
65 #ifdef LISP_FEATURE_SB_THREAD
66     return ((union per_thread_data *)thread)
67         ->dynamic_values[fixnum_value(sym->tls_index)];
68 #else
69     return sym->value;
70 #endif
71 }
72
73 static inline void
74 SetSymbolValue(u64 tagged_symbol_pointer,lispobj val, void *thread)
75 {
76     struct symbol *sym= (struct symbol *)
77         (pointer_sized_uint_t)(tagged_symbol_pointer-OTHER_POINTER_LOWTAG);
78 #ifdef LISP_FEATURE_SB_THREAD
79     if(thread && sym->tls_index) {
80         lispobj *pr= &(((union per_thread_data *)thread)
81                        ->dynamic_values[fixnum_value(sym->tls_index)]);
82         if(*pr!=NO_TLS_VALUE_MARKER_WIDETAG) {
83             *pr=val;
84             return;
85         }
86     }
87 #endif
88     sym->value = val;
89 }
90
91 static inline void
92 SetTlSymbolValue(u64 tagged_symbol_pointer,lispobj val, void *thread)
93 {
94 #ifdef LISP_FEATURE_SB_THREAD
95     struct symbol *sym= (struct symbol *)
96         (pointer_sized_uint_t)(tagged_symbol_pointer-OTHER_POINTER_LOWTAG);
97     ((union per_thread_data *)thread)
98         ->dynamic_values[fixnum_value(sym->tls_index)]
99         =val;
100 #else
101     SetSymbolValue(tagged_symbol_pointer,val,thread) ;
102 #endif
103 }
104
105 static inline
106 os_context_t *get_interrupt_context_for_thread(struct thread *th)
107 {
108     return th->interrupt_contexts
109         [fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,th)-1)];
110 }
111
112 /* This is clearly per-arch and possibly even per-OS code, but we can't
113  * put it somewhere sensible like x86-linux-os.c because it needs too
114  * much stuff like struct thread and all_threads to be defined, which
115  * usually aren't by that time.  So, it's here instead.  Sorry */
116
117 static inline struct thread *arch_os_get_current_thread(void)
118 {
119 #if defined(LISP_FEATURE_SB_THREAD)
120 #if defined(LISP_FEATURE_X86)
121     register struct thread *me=0;
122     if(all_threads) {
123 #if defined(LISP_FEATURE_DARWIN) && defined(LISP_FEATURE_RESTORE_FS_SEGMENT_REGISTER_FROM_TLS)
124         sel_t sel;
125         struct thread *th = pthread_getspecific(specials);
126         sel.index = th->tls_cookie;
127         sel.rpl = USER_PRIV;
128         sel.ti = SEL_LDT;
129         __asm__ __volatile__ ("movw %w0, %%fs" : : "r"(sel));
130 #elif defined(LISP_FEATURE_FREEBSD) && defined(LISP_FEATURE_RESTORE_TLS_SEGMENT_REGISTER_FROM_TLS)
131         struct thread *th = pthread_getspecific(specials);
132         unsigned int sel = LSEL(th->tls_cookie, SEL_UPL);
133         unsigned int fs = rfs();
134
135         /* Load FS only if it's necessary.  Modifying a selector
136          * causes privilege checking and it takes long time. */
137         if (fs != sel)
138             load_fs(sel);
139         return th;
140 #endif
141         __asm__ __volatile__ ("movl %%fs:%c1,%0" : "=r" (me)
142                  : "i" (offsetof (struct thread,this)));
143     }
144     return me;
145 #else
146     return pthread_getspecific(specials);
147 #endif /* x86 */
148 #else
149      return all_threads;
150 #endif
151 }
152
153 #if defined(LISP_FEATURE_MACH_EXCEPTION_HANDLER)
154 #define THREAD_STRUCT_TO_EXCEPTION_PORT(th) ((mach_port_t) th)
155 #define EXCEPTION_PORT_TO_THREAD_STRUCT(th) ((struct thread *) th)
156 #endif
157
158 #if defined(LISP_FEATURE_SB_THREAD)
159 #define thread_self pthread_self
160 #define thread_kill pthread_kill
161 #define thread_sigmask pthread_sigmask
162 #define thread_mutex_lock(l) pthread_mutex_lock(l)
163 #define thread_mutex_unlock(l) pthread_mutex_unlock(l)
164 #else
165 #define thread_self getpid
166 #define thread_kill kill
167 #define thread_sigmask sigprocmask
168 #define thread_mutex_lock(l) 0
169 #define thread_mutex_unlock(l) 0
170 #endif
171
172 extern void create_initial_thread(lispobj);
173 extern int kill_thread_safely(os_thread_t os_thread, int signo);
174
175 #endif /* _INCLUDE_THREAD_H_ */