1.0.9.62: Performance and stability improvement of threading on FreeBSD
[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 #if defined(LISP_FEATURE_SB_THREAD) && defined(LISP_FEATURE_GCC_TLS)
113 extern __thread struct thread *current_thread;
114 #endif
115
116 /* This is clearly per-arch and possibly even per-OS code, but we can't
117  * put it somewhere sensible like x86-linux-os.c because it needs too
118  * much stuff like struct thread and all_threads to be defined, which
119  * usually aren't by that time.  So, it's here instead.  Sorry */
120
121 static inline struct thread *arch_os_get_current_thread(void)
122 {
123 #if defined(LISP_FEATURE_SB_THREAD)
124 #if defined(LISP_FEATURE_X86)
125     register struct thread *me=0;
126     if(all_threads) {
127 #if defined(LISP_FEATURE_DARWIN) && defined(LISP_FEATURE_RESTORE_FS_SEGMENT_REGISTER_FROM_TLS)
128         sel_t sel;
129         struct thread *th = pthread_getspecific(specials);
130         sel.index = th->tls_cookie;
131         sel.rpl = USER_PRIV;
132         sel.ti = SEL_LDT;
133         __asm__ __volatile__ ("movw %w0, %%fs" : : "r"(sel));
134 #elif defined(LISP_FEATURE_FREEBSD)
135 #ifdef LISP_FEATURE_GCC_TLS
136         struct thread *th = current_thread;
137 #else
138         struct thread *th = pthread_getspecific(specials);
139 #endif
140 #ifdef LISP_FEATURE_RESTORE_TLS_SEGMENT_REGISTER_FROM_TLS
141         unsigned int sel = LSEL(th->tls_cookie, SEL_UPL);
142         unsigned int fs = rfs();
143
144         /* Load FS only if it's necessary.  Modifying a selector
145          * causes privilege checking and it takes long time. */
146         if (fs != sel)
147             load_fs(sel);
148 #endif
149         return th;
150 #endif
151         __asm__ __volatile__ ("movl %%fs:%c1,%0" : "=r" (me)
152                  : "i" (offsetof (struct thread,this)));
153     }
154     return me;
155 #else
156 #ifdef LISP_FEATURE_GCC_TLS
157     return current_thread;
158 #else
159     return pthread_getspecific(specials);
160 #endif
161 #endif /* x86 */
162 #else
163      return all_threads;
164 #endif
165 }
166
167 #if defined(LISP_FEATURE_MACH_EXCEPTION_HANDLER)
168 #define THREAD_STRUCT_TO_EXCEPTION_PORT(th) ((mach_port_t) th)
169 #define EXCEPTION_PORT_TO_THREAD_STRUCT(th) ((struct thread *) th)
170 #endif
171
172 #if defined(LISP_FEATURE_SB_THREAD)
173 #define thread_self pthread_self
174 #define thread_kill pthread_kill
175 #define thread_sigmask pthread_sigmask
176 #define thread_mutex_lock(l) pthread_mutex_lock(l)
177 #define thread_mutex_unlock(l) pthread_mutex_unlock(l)
178 #else
179 #define thread_self getpid
180 #define thread_kill kill
181 #define thread_sigmask sigprocmask
182 #define thread_mutex_lock(l) 0
183 #define thread_mutex_unlock(l) 0
184 #endif
185
186 extern void create_initial_thread(lispobj);
187 extern int kill_thread_safely(os_thread_t os_thread, int signo);
188
189 #endif /* _INCLUDE_THREAD_H_ */