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