0.9.5.20:
[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 SymbolValue(u64 tagged_symbol_pointer, void *thread) {
45     struct symbol *sym= (struct symbol *)
46         (pointer_sized_uint_t)(tagged_symbol_pointer-OTHER_POINTER_LOWTAG);
47 #ifdef LISP_FEATURE_SB_THREAD
48     if(thread && sym->tls_index) {
49         lispobj r=
50             ((union per_thread_data *)thread)
51             ->dynamic_values[fixnum_value(sym->tls_index)];
52         if(r!=NO_TLS_VALUE_MARKER_WIDETAG) return r;
53     }
54 #endif
55     return sym->value;
56 }
57 static inline lispobj SymbolTlValue(u64 tagged_symbol_pointer, void *thread) {
58     struct symbol *sym= (struct symbol *)
59         (pointer_sized_uint_t)(tagged_symbol_pointer-OTHER_POINTER_LOWTAG);
60 #ifdef LISP_FEATURE_SB_THREAD
61     return ((union per_thread_data *)thread)
62         ->dynamic_values[fixnum_value(sym->tls_index)];
63 #else
64     return sym->value;
65 #endif
66 }
67
68 static inline void SetSymbolValue(u64 tagged_symbol_pointer,lispobj val, void *thread) {
69     struct symbol *sym= (struct symbol *)
70         (pointer_sized_uint_t)(tagged_symbol_pointer-OTHER_POINTER_LOWTAG);
71 #ifdef LISP_FEATURE_SB_THREAD
72     if(thread && sym->tls_index) {
73         lispobj *pr= &(((union per_thread_data *)thread)
74                        ->dynamic_values[fixnum_value(sym->tls_index)]);
75         if(*pr!=NO_TLS_VALUE_MARKER_WIDETAG) {
76             *pr=val;
77             return;
78         }
79     }
80 #endif
81     sym->value = val;
82 }
83 static inline void SetTlSymbolValue(u64 tagged_symbol_pointer,lispobj val, void *thread) {
84 #ifdef LISP_FEATURE_SB_THREAD
85     struct symbol *sym= (struct symbol *)
86         (pointer_sized_uint_t)(tagged_symbol_pointer-OTHER_POINTER_LOWTAG);
87     ((union per_thread_data *)thread)
88         ->dynamic_values[fixnum_value(sym->tls_index)]
89         =val;
90 #else
91     SetSymbolValue(tagged_symbol_pointer,val,thread) ;
92 #endif
93 }
94
95 static inline os_context_t *get_interrupt_context_for_thread(struct thread *th)
96 {
97     return th->interrupt_contexts
98         [fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,th)-1)];
99 }
100
101 /* This is clearly per-arch and possibly even per-OS code, but we can't
102  * put it somewhere sensible like x86-linux-os.c because it needs too
103  * much stuff like struct thread and all_threads to be defined, which
104  * usually aren't by that time.  So, it's here instead.  Sorry */
105
106 static inline struct thread *arch_os_get_current_thread() {
107 #if defined(LISP_FEATURE_SB_THREAD)
108 #if defined(LISP_FEATURE_X86)
109     register struct thread *me=0;
110     if(all_threads)
111         __asm__ __volatile__ ("movl %%fs:%c1,%0" : "=r" (me)
112                  : "i" (offsetof (struct thread,this)));
113     return me;
114 #else
115     return pthread_getspecific(specials);
116 #endif /* x86 */
117 #else
118      return all_threads;
119 #endif
120 }
121
122 #if defined(LISP_FEATURE_SB_THREAD)
123 #define thread_self pthread_self
124 #define thread_kill pthread_kill
125 #define thread_sigmask pthread_sigmask
126 #else
127 #define thread_self getpid
128 #define thread_kill kill
129 #define thread_sigmask sigprocmask
130 #endif
131
132 extern void create_initial_thread(lispobj);
133
134 #endif /* _INCLUDE_THREAD_H_ */