0.8.4.3
[sbcl.git] / src / runtime / thread.h
1
2 #if !defined(_INCLUDE_THREAD_H_)
3 #define _INCLUDE_THREAD_H_
4
5 #include <sys/types.h>
6 #include <unistd.h>
7 #include <stddef.h>
8 #include "runtime.h"
9 #include "sbcl.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 THREAD_SLOT_OFFSET_WORDS(c) \
22  (offsetof(struct thread,c)/(sizeof (struct thread *)))
23
24 union per_thread_data {
25     struct thread thread;
26     lispobj dynamic_values[1];  /* actually more like 4000 or so */
27 };
28
29 extern struct thread *all_threads;
30 extern int dynamic_values_bytes;
31 extern struct thread *find_thread_by_pid(pid_t pid);
32
33 #ifdef LISP_FEATURE_SB_THREAD
34 #define for_each_thread(th) for(th=all_threads;th;th=th->next)
35 #else
36 /* there's some possibility a SSC could notice this never actually
37  * loops  */
38 #define for_each_thread(th) for(th=all_threads;th;th=0)
39 #endif
40
41 static inline lispobj SymbolValue(u32 tagged_symbol_pointer, void *thread) {
42     struct symbol *sym= (struct symbol *)
43         (pointer_sized_uint_t)(tagged_symbol_pointer-OTHER_POINTER_LOWTAG);
44 #ifdef LISP_FEATURE_SB_THREAD
45     if(thread && sym->tls_index) {
46         lispobj r=
47             ((union per_thread_data *)thread)
48             ->dynamic_values[fixnum_value(sym->tls_index)];
49         if(r!=UNBOUND_MARKER_WIDETAG) return r;
50     }
51 #endif
52     return sym->value;
53 }
54 static inline lispobj SymbolTlValue(u32 tagged_symbol_pointer, void *thread) {
55     struct symbol *sym= (struct symbol *)
56         (pointer_sized_uint_t)(tagged_symbol_pointer-OTHER_POINTER_LOWTAG);
57 #ifdef LISP_FEATURE_SB_THREAD
58     return ((union per_thread_data *)thread)
59         ->dynamic_values[fixnum_value(sym->tls_index)];
60 #else
61     return sym->value;
62 #endif
63 }
64
65 static inline void SetSymbolValue(u32 tagged_symbol_pointer,lispobj val, void *thread) {
66     struct symbol *sym= (struct symbol *)
67         (pointer_sized_uint_t)(tagged_symbol_pointer-OTHER_POINTER_LOWTAG);
68 #ifdef LISP_FEATURE_SB_THREAD
69     if(thread && sym->tls_index) {
70         lispobj *pr= &(((union per_thread_data *)thread)
71                        ->dynamic_values[fixnum_value(sym->tls_index)]);
72         if(*pr!= UNBOUND_MARKER_WIDETAG) {
73             *pr=val;
74             return;
75         }
76     }
77 #endif
78     sym->value = val;
79 }
80 static inline void SetTlSymbolValue(u32 tagged_symbol_pointer,lispobj val, void *thread) {
81 #ifdef LISP_FEATURE_SB_THREAD
82     struct symbol *sym= (struct symbol *)
83         (pointer_sized_uint_t)(tagged_symbol_pointer-OTHER_POINTER_LOWTAG);
84     ((union per_thread_data *)thread)
85         ->dynamic_values[fixnum_value(sym->tls_index)]
86         =val;
87 #else
88     SetSymbolValue(tagged_symbol_pointer,val,thread) ;
89 #endif
90 }
91
92 static inline os_context_t *get_interrupt_context_for_thread(struct thread *th)
93 {
94     return th->interrupt_contexts
95         [fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,th)-1)];
96 }
97
98 /* This is clearly per-arch and possibly even per-OS code, but we can't 
99  * put it somewhere sensible like x86-linux-os.c because it needs too
100  * much stuff like struct thread and all_threads to be defined, which
101  * usually aren't by that time.  So, it's here instead.  Sorry */
102
103 inline static struct thread *arch_os_get_current_thread() {
104 #if defined(LISP_FEATURE_SB_THREAD) && defined (LISP_FEATURE_X86)
105     register struct thread *me=0;
106     if(all_threads)
107         __asm__ __volatile__ ("movl %%fs:%c1,%0" : "=r" (me)
108                  : "i" (offsetof (struct thread,this)));
109     return me;
110 #else
111     return all_threads;
112 #endif
113 }
114
115
116 int arch_os_thread_init(struct thread *thread);
117 extern struct thread *arch_os_get_current_thread();
118
119 #endif /* _INCLUDE_THREAD_H_ */