13 #include "validate.h" /* for CONTROL_STACK_SIZE etc */
17 #include "target-arch-os.h"
21 #include "genesis/cons.h"
22 #include "genesis/fdefn.h"
23 #include "interr.h" /* for lose() */
24 #include "gc-internal.h"
26 #define ALIEN_STACK_SIZE (1*1024*1024) /* 1Mb size chosen at random */
28 int dynamic_values_bytes=4096*sizeof(lispobj); /* same for all threads */
29 struct thread *all_threads;
30 volatile lispobj all_threads_lock;
31 extern struct interrupt_data * global_interrupt_data;
32 extern int linux_no_threads_p;
34 #ifdef LISP_FEATURE_SB_THREAD
35 /* When trying to get all_threads_lock one should make sure that
36 * sig_stop_for_gc is not blocked. Else there would be a possible
37 * deadlock: gc locks it, other thread blocks signals, gc sends stop
38 * request to other thread and waits, other thread blocks on lock. */
39 void check_sig_stop_for_gc_can_arrive_or_lose()
41 /* Get the current sigmask, by blocking the empty set. */
42 sigset_t empty,current;
44 thread_sigmask(SIG_BLOCK, &empty, ¤t);
45 if (sigismember(¤t,SIG_STOP_FOR_GC))
46 lose("SIG_STOP_FOR_GC is blocked\n");
47 if (SymbolValue(INTERRUPTS_ENABLED,arch_os_get_current_thread()) == NIL)
48 lose("interrupts disabled\n");
49 if (arch_pseudo_atomic_atomic(NULL))
50 lose("n pseudo atomic\n");
54 #define FSHOW_SIGNAL FSHOW
56 #define FSHOW_SIGNAL(args)
59 #define GET_ALL_THREADS_LOCK(name) \
61 sigset_t _newset,_oldset; \
62 sigemptyset(&_newset); \
63 sigaddset_blockable(&_newset); \
64 sigdelset(&_newset,SIG_STOP_FOR_GC); \
65 thread_sigmask(SIG_BLOCK, &_newset, &_oldset); \
66 check_sig_stop_for_gc_can_arrive_or_lose(); \
67 FSHOW_SIGNAL((stderr,"/%s:waiting on lock=%ld, thread=%ld\n",name, \
68 all_threads_lock,arch_os_get_current_thread()->os_thread)); \
69 get_spinlock(&all_threads_lock,(long)arch_os_get_current_thread()); \
70 FSHOW_SIGNAL((stderr,"/%s:got lock, thread=%ld\n", \
71 name,arch_os_get_current_thread()->os_thread));
73 #define RELEASE_ALL_THREADS_LOCK(name) \
74 FSHOW_SIGNAL((stderr,"/%s:released lock\n",name)); \
75 release_spinlock(&all_threads_lock); \
76 thread_sigmask(SIG_SETMASK,&_oldset,0); \
81 initial_thread_trampoline(struct thread *th)
84 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
88 function = th->unbound_marker;
89 th->unbound_marker = UNBOUND_MARKER_WIDETAG;
90 if(arch_os_thread_init(th)==0) return 1;
92 if(th->os_thread < 1) lose("th->os_thread not set up right");
93 th->state=STATE_RUNNING;
94 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
95 return call_into_lisp_first_time(function,args,0);
97 return funcall0(function);
101 #ifdef LISP_FEATURE_SB_THREAD
103 /* this is the first thing that runs in the child (which is why the
104 * silly calling convention). Basically it calls the user's requested
105 * lisp function after doing arch_os_thread_init and whatever other
106 * bookkeeping needs to be done
109 new_thread_trampoline(struct thread *th)
112 function = th->unbound_marker;
113 th->unbound_marker = UNBOUND_MARKER_WIDETAG;
114 if(arch_os_thread_init(th)==0) return 1;
116 /* wait here until our thread is linked into all_threads: see below */
117 while(th->os_thread<1) sched_yield();
119 th->state=STATE_RUNNING;
120 return funcall0(function);
122 #endif /* LISP_FEATURE_SB_THREAD */
124 /* this is called from any other thread to create the new one, and
125 * initialize all parts of it that can be initialized from another
129 struct thread * create_thread_struct(lispobj initial_function) {
130 union per_thread_data *per_thread;
131 struct thread *th=0; /* subdue gcc */
134 /* may as well allocate all the spaces at once: it saves us from
135 * having to decide what to do if only some of the allocations
137 spaces=os_validate(0,
138 THREAD_CONTROL_STACK_SIZE+
141 dynamic_values_bytes+
145 per_thread=(union per_thread_data *)
147 THREAD_CONTROL_STACK_SIZE+
152 memcpy(per_thread,arch_os_get_current_thread(),
153 dynamic_values_bytes);
155 #ifdef LISP_FEATURE_SB_THREAD
157 for(i=0;i<(dynamic_values_bytes/sizeof(lispobj));i++)
158 per_thread->dynamic_values[i]=UNBOUND_MARKER_WIDETAG;
159 if(SymbolValue(FREE_TLS_INDEX,0)==UNBOUND_MARKER_WIDETAG)
162 make_fixnum(MAX_INTERRUPTS+
163 sizeof(struct thread)/sizeof(lispobj)),
165 #define STATIC_TLS_INIT(sym,field) \
166 ((struct symbol *)(sym-OTHER_POINTER_LOWTAG))->tls_index= \
167 make_fixnum(THREAD_SLOT_OFFSET_WORDS(field))
169 STATIC_TLS_INIT(BINDING_STACK_START,binding_stack_start);
170 STATIC_TLS_INIT(BINDING_STACK_POINTER,binding_stack_pointer);
171 STATIC_TLS_INIT(CONTROL_STACK_START,control_stack_start);
172 STATIC_TLS_INIT(CONTROL_STACK_END,control_stack_end);
173 STATIC_TLS_INIT(ALIEN_STACK,alien_stack_pointer);
174 #if defined(LISP_FEATURE_X86) || defined (LISP_FEATURE_X86_64)
175 STATIC_TLS_INIT(PSEUDO_ATOMIC_ATOMIC,pseudo_atomic_atomic);
176 STATIC_TLS_INIT(PSEUDO_ATOMIC_INTERRUPTED,pseudo_atomic_interrupted);
178 #undef STATIC_TLS_INIT
182 th=&per_thread->thread;
183 th->control_stack_start = spaces;
184 th->binding_stack_start=
185 (lispobj*)((void*)th->control_stack_start+THREAD_CONTROL_STACK_SIZE);
186 th->control_stack_end = th->binding_stack_start;
187 th->alien_stack_start=
188 (lispobj*)((void*)th->binding_stack_start+BINDING_STACK_SIZE);
189 th->binding_stack_pointer=th->binding_stack_start;
192 th->interrupt_fun=NIL;
193 th->interrupt_fun_lock=0;
194 th->state=STATE_STARTING;
195 #ifdef LISP_FEATURE_STACK_GROWS_DOWNWARD_NOT_UPWARD
196 th->alien_stack_pointer=((void *)th->alien_stack_start
197 + ALIEN_STACK_SIZE-N_WORD_BYTES);
199 th->alien_stack_pointer=((void *)th->alien_stack_start);
201 #if defined(LISP_FEATURE_X86) || defined (LISP_FEATURE_X86_64)
202 th->pseudo_atomic_interrupted=0;
203 th->pseudo_atomic_atomic=0;
205 #ifdef LISP_FEATURE_GENCGC
206 gc_set_region_empty(&th->alloc_region);
209 #ifndef LISP_FEATURE_SB_THREAD
210 /* the tls-points-into-struct-thread trick is only good for threaded
211 * sbcl, because unithread sbcl doesn't have tls. So, we copy the
212 * appropriate values from struct thread here, and make sure that
213 * we use the appropriate SymbolValue macros to access any of the
214 * variable quantities from the C runtime. It's not quite OAOOM,
215 * it just feels like it */
216 SetSymbolValue(BINDING_STACK_START,(lispobj)th->binding_stack_start,th);
217 SetSymbolValue(CONTROL_STACK_START,(lispobj)th->control_stack_start,th);
218 SetSymbolValue(CONTROL_STACK_END,(lispobj)th->control_stack_end,th);
219 #if defined(LISP_FEATURE_X86) || defined (LISP_FEATURE_X86_64)
220 SetSymbolValue(BINDING_STACK_POINTER,(lispobj)th->binding_stack_pointer,th);
221 SetSymbolValue(ALIEN_STACK,(lispobj)th->alien_stack_pointer,th);
222 SetSymbolValue(PSEUDO_ATOMIC_ATOMIC,(lispobj)th->pseudo_atomic_atomic,th);
223 SetSymbolValue(PSEUDO_ATOMIC_INTERRUPTED,th->pseudo_atomic_interrupted,th);
225 current_binding_stack_pointer=th->binding_stack_pointer;
226 current_control_stack_pointer=th->control_stack_start;
229 bind_variable(CURRENT_CATCH_BLOCK,make_fixnum(0),th);
230 bind_variable(CURRENT_UNWIND_PROTECT_BLOCK,make_fixnum(0),th);
231 bind_variable(FREE_INTERRUPT_CONTEXT_INDEX,make_fixnum(0),th);
232 bind_variable(INTERRUPT_PENDING, NIL,th);
233 bind_variable(INTERRUPTS_ENABLED,T,th);
235 th->interrupt_data = (struct interrupt_data *)
236 os_validate(0,(sizeof (struct interrupt_data)));
238 memcpy(th->interrupt_data,
239 arch_os_get_current_thread()->interrupt_data,
240 sizeof (struct interrupt_data));
242 memcpy(th->interrupt_data,global_interrupt_data,
243 sizeof (struct interrupt_data));
245 th->unbound_marker=initial_function;
249 void link_thread(struct thread *th,os_thread_t kid_tid)
251 if (all_threads) all_threads->prev=th;
252 th->next=all_threads;
255 /* note that th->os_thread is 0 at this time. We rely on
256 * all_threads_lock to ensure that we don't have >1 thread with
257 * os_thread=0 on the list at once
259 protect_control_stack_guard_page(th,1);
260 /* child will not start until this is set */
261 th->os_thread=kid_tid;
264 void create_initial_thread(lispobj initial_function) {
265 struct thread *th=create_thread_struct(initial_function);
266 os_thread_t kid_tid=thread_self();
267 if(th && kid_tid>0) {
268 link_thread(th,kid_tid);
269 initial_thread_trampoline(all_threads); /* no return */
270 } else lose("can't create initial thread");
273 #ifdef LISP_FEATURE_SB_THREAD
275 boolean create_os_thread(struct thread *th,os_thread_t *kid_tid)
277 /* The new thread inherits the restrictive signal mask set here,
278 * and enables signals again when it is set up properly. */
280 sigset_t newset,oldset;
282 sigemptyset(&newset);
283 sigaddset_blockable(&newset);
284 thread_sigmask(SIG_BLOCK, &newset, &oldset);
286 if((pthread_attr_init(&attr)) ||
287 (pthread_attr_setstack(&attr,th->control_stack_start,
288 THREAD_CONTROL_STACK_SIZE-16)) ||
290 (kid_tid,&attr,(void *(*)(void *))new_thread_trampoline,th)))
292 thread_sigmask(SIG_SETMASK,&oldset,0);
296 struct thread *create_thread(lispobj initial_function) {
298 os_thread_t kid_tid=0;
301 if(linux_no_threads_p) return 0;
303 th=create_thread_struct(initial_function);
306 /* we must not be interrupted here after a successful
307 * create_os_thread, because the kid will be waiting for its
308 * thread struct to be linked */
309 GET_ALL_THREADS_LOCK("create_thread")
311 success=create_os_thread(th,&kid_tid);
313 link_thread(th,kid_tid);
315 os_invalidate((os_vm_address_t) th->control_stack_start,
317 * (th->control_stack_end-th->control_stack_start)) +
318 BINDING_STACK_SIZE+ALIEN_STACK_SIZE+dynamic_values_bytes+
321 RELEASE_ALL_THREADS_LOCK("create_thread")
330 #if defined LISP_FEATURE_SB_THREAD
331 /* This is not needed unless #+SB-THREAD, as there's a trivial null
332 * unithread definition. */
334 /* called from lisp from the thread object finalizer */
335 void reap_dead_thread(struct thread *th)
337 if(th->state!=STATE_DEAD)
338 lose("thread %lx is not joinable, state=%d\n",th,th->state);
339 #ifdef LISP_FEATURE_GENCGC
341 sigset_t newset,oldset;
342 sigemptyset(&newset);
343 sigaddset_blockable(&newset);
344 thread_sigmask(SIG_BLOCK, &newset, &oldset);
345 gc_alloc_update_page_tables(0, &th->alloc_region);
346 release_spinlock(&all_threads_lock);
347 thread_sigmask(SIG_SETMASK,&oldset,0);
350 GET_ALL_THREADS_LOCK("reap_dead_thread")
351 FSHOW((stderr,"/reap_dead_thread: reaping %ld\n",th->os_thread));
353 th->prev->next=th->next;
354 else all_threads=th->next;
356 th->next->prev=th->prev;
357 RELEASE_ALL_THREADS_LOCK("reap_dead_thread")
358 if(th->tls_cookie>=0) arch_os_thread_cleanup(th);
359 gc_assert(pthread_join(th->os_thread,NULL)==0);
360 os_invalidate((os_vm_address_t) th->control_stack_start,
362 * (th->control_stack_end-th->control_stack_start)) +
363 BINDING_STACK_SIZE+ALIEN_STACK_SIZE+dynamic_values_bytes+
367 int interrupt_thread(struct thread *th, lispobj function)
369 /* A thread may also become dead after this test. */
370 if ((th->state != STATE_DEAD)) {
371 /* In clone_threads, if A and B both interrupt C at
372 * approximately the same time, it does not matter: the
373 * second signal will be masked until the handler has
374 * returned from the first one. In pthreads though, we
375 * can't put the knowledge of what function to call into
376 * the siginfo, so we have to store it in the destination
377 * thread, and do it in such a way that A won't clobber
378 * B's interrupt. Hence this stupid linked list.
380 * This does depend on SIG_INTERRUPT_THREAD being queued
381 * (as POSIX RT signals are): we need to keep
382 * interrupt_fun data for exactly as many signals as are
383 * going to be received by the destination thread.
385 struct cons *c=alloc_cons(function,NIL);
387 /* interrupt_thread_handler locks this spinlock with
388 * interrupts blocked and it does so for the sake of
389 * arrange_return_to_lisp_function, so we must also block
391 sigset_t newset,oldset;
392 sigemptyset(&newset);
393 sigaddset_blockable(&newset);
394 thread_sigmask(SIG_BLOCK, &newset, &oldset);
395 get_spinlock(&th->interrupt_fun_lock,
396 (long)arch_os_get_current_thread());
397 kill_status=thread_kill(th->os_thread,SIG_INTERRUPT_THREAD);
399 ((struct cons *)native_pointer(c))->cdr=th->interrupt_fun;
402 release_spinlock(&th->interrupt_fun_lock);
403 thread_sigmask(SIG_SETMASK,&oldset,0);
404 return (kill_status ? -1 : 0);
406 errno=EPERM; return -1;
409 /* stopping the world is a two-stage process. From this thread we signal
410 * all the others with SIG_STOP_FOR_GC. The handler for this signal does
411 * the usual pseudo-atomic checks (we don't want to stop a thread while
412 * it's in the middle of allocation) then waits for another SIG_STOP_FOR_GC.
415 /* To avoid deadlocks when gc stops the world all clients of each
416 * mutex must enable or disable SIG_STOP_FOR_GC for the duration of
417 * holding the lock, but they must agree on which. */
418 void gc_stop_the_world()
420 struct thread *p,*th=arch_os_get_current_thread();
421 FSHOW_SIGNAL((stderr,"/gc_stop_the_world:waiting on lock, thread=%ld\n",
423 /* keep threads from starting while the world is stopped. */
424 get_spinlock(&all_threads_lock,(long)th);
425 FSHOW_SIGNAL((stderr,"/gc_stop_the_world:got lock, thread=%ld\n",
427 /* stop all other threads by sending them SIG_STOP_FOR_GC */
428 for(p=all_threads; p; p=p->next) {
429 while(p->state==STATE_STARTING) sched_yield();
430 if((p!=th) && (p->state==STATE_RUNNING)) {
431 FSHOW_SIGNAL((stderr,"/gc_stop_the_world:sending sig_stop to %ld\n",
433 if(thread_kill(p->os_thread,SIG_STOP_FOR_GC)==-1) {
434 /* we can't kill the thread; assume because it died
435 * since we last checked */
437 FSHOW_SIGNAL((stderr,"/gc_stop_the_world:assuming %ld dead\n",
442 FSHOW_SIGNAL((stderr,"/gc_stop_the_world:signals sent\n"));
443 /* wait for the running threads to stop or finish */
444 for(p=all_threads;p;) {
445 gc_assert(p->os_thread!=0);
446 gc_assert(p->state!=STATE_STARTING);
447 if((p==th) || (p->state==STATE_SUSPENDED) ||
448 (p->state==STATE_DEAD)) {
452 FSHOW_SIGNAL((stderr,"/gc_stop_the_world:end\n"));
455 void gc_start_the_world()
457 struct thread *p,*th=arch_os_get_current_thread();
458 /* if a resumed thread creates a new thread before we're done with
459 * this loop, the new thread will get consed on the front of
460 * all_threads, but it won't have been stopped so won't need
462 FSHOW_SIGNAL((stderr,"/gc_start_the_world:begin\n"));
463 for(p=all_threads;p;p=p->next) {
464 gc_assert(p->os_thread!=0);
465 if((p!=th) && (p->state!=STATE_DEAD)) {
466 if(p->state!=STATE_SUSPENDED) {
467 lose("gc_start_the_world: wrong thread state is %ld\n",
468 fixnum_value(p->state));
470 thread_kill(p->os_thread,SIG_STOP_FOR_GC);
473 /* we must wait for all threads to leave stopped state else we
474 * risk signal accumulation and lose any meaning of
476 for(p=all_threads;p;) {
477 if((p==th) || (p->state!=STATE_SUSPENDED)) {
481 release_spinlock(&all_threads_lock);
482 FSHOW_SIGNAL((stderr,"/gc_start_the_world:end\n"));