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 volatile lispobj thread_start_lock;
32 extern struct interrupt_data * global_interrupt_data;
33 extern int linux_no_threads_p;
36 initial_thread_trampoline(struct thread *th)
39 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
43 function = th->unbound_marker;
44 th->unbound_marker = UNBOUND_MARKER_WIDETAG;
45 if(arch_os_thread_init(th)==0) return 1;
47 if(th->os_thread < 1) lose("th->os_thread not set up right");
48 th->state=STATE_RUNNING;
49 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
50 return call_into_lisp_first_time(function,args,0);
52 return funcall0(function);
56 #ifdef LISP_FEATURE_SB_THREAD
57 void mark_thread_dead(struct thread *th) {
58 funcall1(SymbolFunction(HANDLE_THREAD_EXIT),alloc_number(th->os_thread));
59 /* I hope it's safe for a thread to detach itself inside a
60 * cancellation cleanup */
61 pthread_detach(th->os_thread);
63 /* FIXME: if gc hits here it will rip the stack from under us */
66 /* this is the first thing that runs in the child (which is why the
67 * silly calling convention). Basically it calls the user's requested
68 * lisp function after doing arch_os_thread_init and whatever other
69 * bookkeeping needs to be done
72 new_thread_trampoline(struct thread *th)
75 function = th->unbound_marker;
76 th->unbound_marker = UNBOUND_MARKER_WIDETAG;
77 pthread_cleanup_push((void (*) (void *))mark_thread_dead,th);
78 if(arch_os_thread_init(th)==0) return 1;
80 /* wait here until our thread is linked into all_threads: see below */
81 while(th->os_thread<1) sched_yield();
83 th->state=STATE_RUNNING;
84 ret = funcall0(function);
86 pthread_cleanup_pop(1);
89 #endif /* LISP_FEATURE_SB_THREAD */
91 /* this is called from any other thread to create the new one, and
92 * initialize all parts of it that can be initialized from another
96 struct thread * create_thread_struct(lispobj initial_function) {
97 union per_thread_data *per_thread;
98 struct thread *th=0; /* subdue gcc */
101 /* may as well allocate all the spaces at once: it saves us from
102 * having to decide what to do if only some of the allocations
104 spaces=os_validate(0,
105 THREAD_CONTROL_STACK_SIZE+
108 dynamic_values_bytes+
112 per_thread=(union per_thread_data *)
114 THREAD_CONTROL_STACK_SIZE+
119 memcpy(per_thread,arch_os_get_current_thread(),
120 dynamic_values_bytes);
122 #ifdef LISP_FEATURE_SB_THREAD
124 for(i=0;i<(dynamic_values_bytes/sizeof(lispobj));i++)
125 per_thread->dynamic_values[i]=UNBOUND_MARKER_WIDETAG;
126 if(SymbolValue(FREE_TLS_INDEX,0)==UNBOUND_MARKER_WIDETAG)
129 make_fixnum(MAX_INTERRUPTS+
130 sizeof(struct thread)/sizeof(lispobj)),
132 #define STATIC_TLS_INIT(sym,field) \
133 ((struct symbol *)(sym-OTHER_POINTER_LOWTAG))->tls_index= \
134 make_fixnum(THREAD_SLOT_OFFSET_WORDS(field))
136 STATIC_TLS_INIT(BINDING_STACK_START,binding_stack_start);
137 STATIC_TLS_INIT(BINDING_STACK_POINTER,binding_stack_pointer);
138 STATIC_TLS_INIT(CONTROL_STACK_START,control_stack_start);
139 STATIC_TLS_INIT(CONTROL_STACK_END,control_stack_end);
140 STATIC_TLS_INIT(ALIEN_STACK,alien_stack_pointer);
141 #ifdef LISP_FEATURE_X86
142 STATIC_TLS_INIT(PSEUDO_ATOMIC_ATOMIC,pseudo_atomic_atomic);
143 STATIC_TLS_INIT(PSEUDO_ATOMIC_INTERRUPTED,pseudo_atomic_interrupted);
145 #undef STATIC_TLS_INIT
149 th=&per_thread->thread;
150 th->control_stack_start = spaces;
151 th->binding_stack_start=
152 (lispobj*)((void*)th->control_stack_start+THREAD_CONTROL_STACK_SIZE);
153 th->control_stack_end = th->binding_stack_start;
154 th->alien_stack_start=
155 (lispobj*)((void*)th->binding_stack_start+BINDING_STACK_SIZE);
156 th->binding_stack_pointer=th->binding_stack_start;
159 th->interrupt_fun=NIL;
160 th->interrupt_fun_lock=0;
161 th->state=STATE_STARTING;
162 #ifdef LISP_FEATURE_STACK_GROWS_DOWNWARD_NOT_UPWARD
163 th->alien_stack_pointer=((void *)th->alien_stack_start
164 + ALIEN_STACK_SIZE-N_WORD_BYTES);
166 th->alien_stack_pointer=((void *)th->alien_stack_start);
168 #if defined(LISP_FEATURE_X86) || defined (LISP_FEATURE_X86_64)
169 th->pseudo_atomic_interrupted=0;
170 th->pseudo_atomic_atomic=0;
172 #ifdef LISP_FEATURE_GENCGC
173 gc_set_region_empty(&th->alloc_region);
176 #ifndef LISP_FEATURE_SB_THREAD
177 /* the tls-points-into-struct-thread trick is only good for threaded
178 * sbcl, because unithread sbcl doesn't have tls. So, we copy the
179 * appropriate values from struct thread here, and make sure that
180 * we use the appropriate SymbolValue macros to access any of the
181 * variable quantities from the C runtime. It's not quite OAOOM,
182 * it just feels like it */
183 SetSymbolValue(BINDING_STACK_START,(lispobj)th->binding_stack_start,th);
184 SetSymbolValue(CONTROL_STACK_START,(lispobj)th->control_stack_start,th);
185 SetSymbolValue(CONTROL_STACK_END,(lispobj)th->control_stack_end,th);
186 #if defined(LISP_FEATURE_X86) || defined (LISP_FEATURE_X86_64)
187 SetSymbolValue(BINDING_STACK_POINTER,(lispobj)th->binding_stack_pointer,th);
188 SetSymbolValue(ALIEN_STACK,(lispobj)th->alien_stack_pointer,th);
189 SetSymbolValue(PSEUDO_ATOMIC_ATOMIC,(lispobj)th->pseudo_atomic_atomic,th);
190 SetSymbolValue(PSEUDO_ATOMIC_INTERRUPTED,th->pseudo_atomic_interrupted,th);
192 current_binding_stack_pointer=th->binding_stack_pointer;
193 current_control_stack_pointer=th->control_stack_start;
196 bind_variable(CURRENT_CATCH_BLOCK,make_fixnum(0),th);
197 bind_variable(CURRENT_UNWIND_PROTECT_BLOCK,make_fixnum(0),th);
198 bind_variable(FREE_INTERRUPT_CONTEXT_INDEX,make_fixnum(0),th);
199 bind_variable(INTERRUPT_PENDING, NIL,th);
200 bind_variable(INTERRUPTS_ENABLED,T,th);
202 th->interrupt_data = (struct interrupt_data *)
203 os_validate(0,(sizeof (struct interrupt_data)));
205 memcpy(th->interrupt_data,
206 arch_os_get_current_thread()->interrupt_data,
207 sizeof (struct interrupt_data));
209 memcpy(th->interrupt_data,global_interrupt_data,
210 sizeof (struct interrupt_data));
212 th->unbound_marker=initial_function;
216 void link_thread(struct thread *th,os_thread_t kid_tid)
218 sigset_t newset,oldset;
219 sigemptyset(&newset);
220 sigaddset_blockable(&newset);
221 thread_sigmask(SIG_BLOCK, &newset, &oldset);
223 get_spinlock(&all_threads_lock,kid_tid);
224 if (all_threads) all_threads->prev=th;
225 th->next=all_threads;
228 /* note that th->os_thread is 0 at this time. We rely on
229 * all_threads_lock to ensure that we don't have >1 thread with
230 * os_thread=0 on the list at once
232 protect_control_stack_guard_page(th->os_thread,1);
233 /* child will not start until this is set */
234 th->os_thread=kid_tid;
235 release_spinlock(&all_threads_lock);
237 thread_sigmask(SIG_SETMASK,&oldset,0);
240 void create_initial_thread(lispobj initial_function) {
241 struct thread *th=create_thread_struct(initial_function);
242 os_thread_t kid_tid=thread_self();
243 if(th && kid_tid>0) {
244 link_thread(th,kid_tid);
245 initial_thread_trampoline(all_threads); /* no return */
246 } else lose("can't create initial thread");
249 #ifdef LISP_FEATURE_SB_THREAD
250 os_thread_t create_thread(lispobj initial_function) {
252 os_thread_t kid_tid=0;
255 if(linux_no_threads_p) return 0;
256 th=create_thread_struct(initial_function);
259 SHOW("create_thread:waiting on lock");
261 get_spinlock(&thread_start_lock,arch_os_get_current_thread()->os_thread);
263 SHOW("create_thread:got lock");
265 /* The new thread inherits the restrictive signal mask set here,
266 * and enables signals again when it is set up properly. */
268 sigset_t newset,oldset;
269 sigemptyset(&newset);
270 sigaddset_blockable(&newset);
271 thread_sigmask(SIG_BLOCK, &newset, &oldset);
272 if((pthread_attr_init(&attr)) ||
273 (pthread_attr_setstack(&attr,th->control_stack_start,
274 THREAD_CONTROL_STACK_SIZE-16)) ||
276 (&kid_tid,&attr,(void *(*)(void *))new_thread_trampoline,th)))
278 thread_sigmask(SIG_SETMASK,&oldset,0);
281 link_thread(th,kid_tid);
282 /* it's started and initialized, it's safe to gc */
283 release_spinlock(&thread_start_lock);
285 SHOW("create_thread:released lock");
287 /* by now the kid might have already exited */
290 release_spinlock(&thread_start_lock);
292 SHOW("create_thread:released lock(failure)");
294 os_invalidate((os_vm_address_t) th->control_stack_start,
296 * (th->control_stack_end-th->control_stack_start)) +
297 BINDING_STACK_SIZE+ALIEN_STACK_SIZE+dynamic_values_bytes+
304 struct thread *find_thread_by_os_thread(os_thread_t tid)
308 if(th->os_thread==tid) return th;
312 #if defined LISP_FEATURE_SB_THREAD
313 /* This is not needed unless #+SB-THREAD, as there's a trivial null
314 * unithread definition. */
316 void reap_dead_threads()
318 struct thread *th,*next,*prev=0;
322 if(th->state==STATE_DEAD) {
323 #ifdef LISP_FEATURE_GENCGC
324 gc_alloc_update_page_tables(0, &th->alloc_region);
326 get_spinlock(&all_threads_lock,th->os_thread);
327 if(prev) prev->next=next;
328 else all_threads=next;
329 release_spinlock(&all_threads_lock);
330 if(th->tls_cookie>=0) arch_os_thread_cleanup(th);
331 os_invalidate((os_vm_address_t) th->control_stack_start,
333 * (th->control_stack_end-th->control_stack_start)) +
334 BINDING_STACK_SIZE+ALIEN_STACK_SIZE+dynamic_values_bytes+
342 int interrupt_thread(os_thread_t tid, lispobj function)
346 if((th->os_thread==tid) && (th->state != STATE_DEAD)) {
347 /* In clone_threads, if A and B both interrupt C at approximately
348 * the same time, it does not matter: the second signal will be
349 * masked until the handler has returned from the first one.
350 * In pthreads though, we can't put the knowledge of what function
351 * to call into the siginfo, so we have to store it in the
352 * destination thread, and do it in such a way that A won't
353 * clobber B's interrupt. Hence this stupid linked list.
355 * This does depend on SIG_INTERRUPT_THREAD being queued
356 * (as POSIX RT signals are): we need to keep
357 * interrupt_fun data for exactly as many signals as are
358 * going to be received by the destination thread.
362 /* mask the signals in case this thread is being interrupted */
363 sigset_t newset,oldset;
364 sigemptyset(&newset);
365 sigaddset_blockable(&newset);
366 thread_sigmask(SIG_BLOCK, &newset, &oldset);
368 get_spinlock(&th->interrupt_fun_lock,
369 (int)arch_os_get_current_thread());
370 kill_status=thread_kill(th->os_thread,SIG_INTERRUPT_THREAD);
372 c=alloc_cons(function,th->interrupt_fun);
375 release_spinlock(&th->interrupt_fun_lock);
376 thread_sigmask(SIG_SETMASK,&oldset,0);
377 return (kill_status ? -1 : 0);
379 errno=EPERM; return -1;
382 /* stopping the world is a two-stage process. From this thread we signal
383 * all the others with SIG_STOP_FOR_GC. The handler for this signal does
384 * the usual pseudo-atomic checks (we don't want to stop a thread while
385 * it's in the middle of allocation) then waits for another SIG_STOP_FOR_GC.
388 void gc_stop_the_world()
390 struct thread *p,*th=arch_os_get_current_thread();
392 SHOW("gc_stop_the_world:begin");
394 /* keep threads from starting while the world is stopped. */
395 get_spinlock(&thread_start_lock,th->os_thread);
397 SHOW("gc_stop_the_world:locked");
399 /* stop all other threads by sending them SIG_STOP_FOR_GC */
400 for(p=all_threads; p; p=p->next) {
401 while(p->state==STATE_STARTING) sched_yield();
402 if((p!=th) && (p->os_thread!=0) && (p->state==STATE_RUNNING)) {
403 p->state=STATE_STOPPING;
404 if(thread_kill(p->os_thread,SIG_STOP_FOR_GC)==-1) {
405 /* FIXME: we can't kill the thread; assume because it died
412 SHOW("gc_stop_the_world:signals sent");
414 /* wait for the running threads to stop */
415 for(p=all_threads;p;) {
416 if((p==th) || (p->os_thread==0) || (p->state==STATE_STARTING) ||
417 (p->state==STATE_DEAD) || (p->state==STATE_STOPPED)) {
422 SHOW("gc_stop_the_world:end");
426 void gc_start_the_world()
428 struct thread *p,*th=arch_os_get_current_thread();
429 /* if a resumed thread creates a new thread before we're done with
430 * this loop, the new thread will get consed on the front of
431 * all_threads, but it won't have been stopped so won't need
432 * restarting; there can be threads just starting from before
433 * gc_stop_the_world, though */
435 SHOW("gc_start_the_world:begin");
437 for(p=all_threads;p;p=p->next) {
438 if((p!=th) && (p->os_thread!=0) && (p->state!=STATE_STARTING) &&
439 (p->state!=STATE_DEAD)) {
440 if(p->state!=STATE_STOPPED) {
441 lose("gc_start_the_world: wrong thread state is %ld\n",
442 fixnum_value(p->state));
444 thread_kill(p->os_thread,SIG_STOP_FOR_GC);
447 /* we must wait for all threads to leave stopped state else we
448 * risk signal accumulation and lose any meaning of
450 for(p=all_threads;p;) {
451 gc_assert(p->state!=STATE_STOPPING);
452 if((p==th) || (p->os_thread==0) || (p->state!=STATE_STOPPED)) {
456 release_spinlock(&thread_start_lock);
458 SHOW("gc_start_the_world:end");