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