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