1 #if !defined(_INCLUDE_THREAD_H_)
2 #define _INCLUDE_THREAD_H_
11 #include "interrupt.h"
12 #ifdef LISP_FEATURE_GENCGC
13 #include "gencgc-alloc-region.h"
15 struct alloc_region
{ };
17 #include "genesis/symbol.h"
18 #include "genesis/static-symbols.h"
19 #include "genesis/thread.h"
21 #define STATE_RUNNING (make_fixnum(1))
22 #define STATE_SUSPENDED (make_fixnum(2))
23 #define STATE_DEAD (make_fixnum(3))
25 #define THREAD_SLOT_OFFSET_WORDS(c) \
26 (offsetof(struct thread,c)/(sizeof (struct thread *)))
28 union per_thread_data
{
30 lispobj dynamic_values
[1]; /* actually more like 4000 or so */
33 extern struct thread
* volatile all_threads
;
34 extern int dynamic_values_bytes
;
36 #ifdef LISP_FEATURE_SB_THREAD
37 #define for_each_thread(th) for(th=all_threads;th;th=th->next)
39 /* there's some possibility a SSC could notice this never actually
41 #define for_each_thread(th) for(th=all_threads;th;th=0)
45 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
) {
52 ((union per_thread_data
*)thread
)
53 ->dynamic_values
[fixnum_value(sym
->tls_index
)];
54 if(r
!=NO_TLS_VALUE_MARKER_WIDETAG
) return r
;
61 SymbolTlValue(u64 tagged_symbol_pointer
, void *thread
)
63 struct symbol
*sym
= (struct symbol
*)
64 (pointer_sized_uint_t
)(tagged_symbol_pointer
-OTHER_POINTER_LOWTAG
);
65 #ifdef LISP_FEATURE_SB_THREAD
66 return ((union per_thread_data
*)thread
)
67 ->dynamic_values
[fixnum_value(sym
->tls_index
)];
74 SetSymbolValue(u64 tagged_symbol_pointer
,lispobj val
, void *thread
)
76 struct symbol
*sym
= (struct symbol
*)
77 (pointer_sized_uint_t
)(tagged_symbol_pointer
-OTHER_POINTER_LOWTAG
);
78 #ifdef LISP_FEATURE_SB_THREAD
79 if(thread
&& sym
->tls_index
) {
80 lispobj
*pr
= &(((union per_thread_data
*)thread
)
81 ->dynamic_values
[fixnum_value(sym
->tls_index
)]);
82 if(*pr
!=NO_TLS_VALUE_MARKER_WIDETAG
) {
92 SetTlSymbolValue(u64 tagged_symbol_pointer
,lispobj val
, void *thread
)
94 #ifdef LISP_FEATURE_SB_THREAD
95 struct symbol
*sym
= (struct symbol
*)
96 (pointer_sized_uint_t
)(tagged_symbol_pointer
-OTHER_POINTER_LOWTAG
);
97 ((union per_thread_data
*)thread
)
98 ->dynamic_values
[fixnum_value(sym
->tls_index
)]
101 SetSymbolValue(tagged_symbol_pointer
,val
,thread
) ;
106 os_context_t
*get_interrupt_context_for_thread(struct thread
*th
)
108 return th
->interrupt_contexts
109 [fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX
,th
)-1)];
112 /* This is clearly per-arch and possibly even per-OS code, but we can't
113 * put it somewhere sensible like x86-linux-os.c because it needs too
114 * much stuff like struct thread and all_threads to be defined, which
115 * usually aren't by that time. So, it's here instead. Sorry */
117 static inline struct thread
*arch_os_get_current_thread(void)
119 #if defined(LISP_FEATURE_SB_THREAD)
120 #if defined(LISP_FEATURE_X86)
121 register struct thread
*me
=0;
123 #if defined(LISP_FEATURE_DARWIN) && defined(LISP_FEATURE_RESTORE_FS_SEGMENT_REGISTER_FROM_TLS)
125 struct thread
*th
= pthread_getspecific(specials
);
126 sel
.index
= th
->tls_cookie
;
129 __asm__
__volatile__ ("movw %w0, %%fs" : : "r"(sel
));
130 #elif defined(LISP_FEATURE_FREEBSD) && defined(LISP_FEATURE_RESTORE_TLS_SEGMENT_REGISTER_FROM_TLS)
131 struct thread
*th
= pthread_getspecific(specials
);
132 unsigned int sel
= LSEL(th
->tls_cookie
, SEL_UPL
);
133 unsigned int fs
= rfs();
135 /* Load FS only if it's necessary. Modifying a selector
136 * causes privilege checking and it takes long time. */
141 __asm__
__volatile__ ("movl %%fs:%c1,%0" : "=r" (me
)
142 : "i" (offsetof (struct thread
,this)));
146 return pthread_getspecific(specials
);
153 #if defined(LISP_FEATURE_MACH_EXCEPTION_HANDLER)
154 #define THREAD_STRUCT_TO_EXCEPTION_PORT(th) ((mach_port_t) th)
155 #define EXCEPTION_PORT_TO_THREAD_STRUCT(th) ((struct thread *) th)
158 #if defined(LISP_FEATURE_SB_THREAD)
159 #define thread_self pthread_self
160 #define thread_kill pthread_kill
161 #define thread_sigmask pthread_sigmask
162 #define thread_mutex_lock(l) pthread_mutex_lock(l)
163 #define thread_mutex_unlock(l) pthread_mutex_unlock(l)
165 #define thread_self getpid
166 #define thread_kill kill
167 #define thread_sigmask sigprocmask
168 #define thread_mutex_lock(l) 0
169 #define thread_mutex_unlock(l) 0
172 extern void create_initial_thread(lispobj
);
173 extern int kill_thread_safely(os_thread_t os_thread
, int signo
);
175 #endif /* _INCLUDE_THREAD_H_ */