0.9.5.43:
[sbcl/eslaughter.git] / src / runtime / thread.h
blob2815abd52f8830a1d59595d5dcf15c8ea6c1ce6e
1 #if !defined(_INCLUDE_THREAD_H_)
2 #define _INCLUDE_THREAD_H_
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"
21 #define STATE_STARTING (make_fixnum(0))
22 #define STATE_RUNNING (make_fixnum(1))
23 #define STATE_SUSPENDED (make_fixnum(2))
24 #define STATE_DEAD (make_fixnum(3))
26 #define THREAD_SLOT_OFFSET_WORDS(c) \
27 (offsetof(struct thread,c)/(sizeof (struct thread *)))
29 union per_thread_data {
30 struct thread thread;
31 lispobj dynamic_values[1]; /* actually more like 4000 or so */
34 extern struct thread * volatile all_threads;
35 extern int dynamic_values_bytes;
37 #ifdef LISP_FEATURE_SB_THREAD
38 #define for_each_thread(th) for(th=all_threads;th;th=th->next)
39 #else
40 /* there's some possibility a SSC could notice this never actually
41 * loops */
42 #define for_each_thread(th) for(th=all_threads;th;th=0)
43 #endif
45 static inline lispobj
46 SymbolValue(u64 tagged_symbol_pointer, void *thread) {
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;
56 #endif
57 return sym->value;
60 static inline lispobj
61 SymbolTlValue(u64 tagged_symbol_pointer, void *thread) {
62 struct symbol *sym= (struct symbol *)
63 (pointer_sized_uint_t)(tagged_symbol_pointer-OTHER_POINTER_LOWTAG);
64 #ifdef LISP_FEATURE_SB_THREAD
65 return ((union per_thread_data *)thread)
66 ->dynamic_values[fixnum_value(sym->tls_index)];
67 #else
68 return sym->value;
69 #endif
72 static inline void
73 SetSymbolValue(u64 tagged_symbol_pointer,lispobj val, void *thread) {
74 struct symbol *sym= (struct symbol *)
75 (pointer_sized_uint_t)(tagged_symbol_pointer-OTHER_POINTER_LOWTAG);
76 #ifdef LISP_FEATURE_SB_THREAD
77 if(thread && sym->tls_index) {
78 lispobj *pr= &(((union per_thread_data *)thread)
79 ->dynamic_values[fixnum_value(sym->tls_index)]);
80 if(*pr!=NO_TLS_VALUE_MARKER_WIDETAG) {
81 *pr=val;
82 return;
85 #endif
86 sym->value = val;
88 static inline void
89 SetTlSymbolValue(u64 tagged_symbol_pointer,lispobj val, void *thread) {
90 #ifdef LISP_FEATURE_SB_THREAD
91 struct symbol *sym= (struct symbol *)
92 (pointer_sized_uint_t)(tagged_symbol_pointer-OTHER_POINTER_LOWTAG);
93 ((union per_thread_data *)thread)
94 ->dynamic_values[fixnum_value(sym->tls_index)]
95 =val;
96 #else
97 SetSymbolValue(tagged_symbol_pointer,val,thread) ;
98 #endif
101 static inline
102 os_context_t *get_interrupt_context_for_thread(struct thread *th)
104 return th->interrupt_contexts
105 [fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,th)-1)];
108 /* This is clearly per-arch and possibly even per-OS code, but we can't
109 * put it somewhere sensible like x86-linux-os.c because it needs too
110 * much stuff like struct thread and all_threads to be defined, which
111 * usually aren't by that time. So, it's here instead. Sorry */
113 static inline struct thread *arch_os_get_current_thread() {
114 #if defined(LISP_FEATURE_SB_THREAD)
115 #if defined(LISP_FEATURE_X86)
116 register struct thread *me=0;
117 if(all_threads)
118 __asm__ __volatile__ ("movl %%fs:%c1,%0" : "=r" (me)
119 : "i" (offsetof (struct thread,this)));
120 return me;
121 #else
122 return pthread_getspecific(specials);
123 #endif /* x86 */
124 #else
125 return all_threads;
126 #endif
129 #if defined(LISP_FEATURE_SB_THREAD)
130 #define thread_self pthread_self
131 #define thread_kill pthread_kill
132 #define thread_sigmask pthread_sigmask
133 #define thread_mutex_lock(l) pthread_mutex_lock(l)
134 #define thread_mutex_unlock(l) pthread_mutex_unlock(l)
135 #else
136 #define thread_self getpid
137 #define thread_kill kill
138 #define thread_sigmask sigprocmask
139 #define thread_mutex_lock(l)
140 #define thread_mutex_unlock(l)
141 #endif
143 extern void create_initial_thread(lispobj);
145 #endif /* _INCLUDE_THREAD_H_ */