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