7 #ifndef CLONE_PARENT /* lameass glibc 2.2 doesn't define this */
8 #define CLONE_PARENT 0x00008000 /* even though the manpage documents it */
12 #include "validate.h" /* for CONTROL_STACK_SIZE etc */
15 #include "target-arch-os.h"
19 #include "genesis/cons.h"
20 #define ALIEN_STACK_SIZE (1*1024*1024) /* 1Mb size chosen at random */
22 int dynamic_values_bytes=4096*sizeof(lispobj); /* same for all threads */
23 struct thread *all_threads;
24 volatile lispobj all_threads_lock;
25 volatile int countdown_to_gc;
26 extern struct interrupt_data * global_interrupt_data;
28 void get_spinlock(lispobj *word,int value);
30 /* this is the first thing that clone() runs in the child (which is
31 * why the silly calling convention). Basically it calls the user's
32 * requested lisp function after doing arch_os_thread_init and
33 * whatever other bookkeeping needs to be done
36 /* set go to 0 to stop the thread before it starts. Convenient if you
37 * want to attach a debugger to it before it does anything */
41 new_thread_trampoline(struct thread *th)
45 function = th->unbound_marker;
47 fprintf(stderr, "/pausing 0x%lx(%d,%d) before new_thread_trampoline(0x%lx)\n",
48 (unsigned long)th,th->pid,getpid(),(unsigned long)function);
50 fprintf(stderr, "/continue\n");
52 th->unbound_marker = UNBOUND_MARKER_WIDETAG;
53 if(arch_os_thread_init(th)==0)
54 return 1; /* failure. no, really */
55 #ifdef LISP_FEATURE_SB_THREAD
56 /* wait here until our thread is linked into all_threads: see below */
57 while(th->pid<1) sched_yield();
60 lose("th->pid not set up right");
63 th->state=STATE_RUNNING;
64 #if !defined(LISP_FEATURE_SB_THREAD) && defined(LISP_FEATURE_X86)
65 return call_into_lisp_first_time(function,args,0);
67 return funcall0(function);
71 /* this is called from any other thread to create the new one, and
72 * initialize all parts of it that can be initialized from another
76 pid_t create_thread(lispobj initial_function) {
77 union per_thread_data *per_thread;
78 struct thread *th=0; /* subdue gcc */
82 /* may as well allocate all the spaces at once: it saves us from
83 * having to decide what to do if only some of the allocations
86 THREAD_CONTROL_STACK_SIZE+
92 if(!spaces) goto cleanup;
93 per_thread=(union per_thread_data *)
95 THREAD_CONTROL_STACK_SIZE+
99 th=&per_thread->thread;
101 memcpy(per_thread,arch_os_get_current_thread(),
102 dynamic_values_bytes);
104 #ifdef LISP_FEATURE_SB_THREAD
106 for(i=0;i<(dynamic_values_bytes/sizeof(lispobj));i++)
107 per_thread->dynamic_values[i]=UNBOUND_MARKER_WIDETAG;
108 if(SymbolValue(FREE_TLS_INDEX,0)==UNBOUND_MARKER_WIDETAG)
111 make_fixnum(MAX_INTERRUPTS+
112 sizeof(struct thread)/sizeof(lispobj)),
114 #define STATIC_TLS_INIT(sym,field) \
115 ((struct symbol *)(sym-OTHER_POINTER_LOWTAG))->tls_index= \
116 make_fixnum(THREAD_SLOT_OFFSET_WORDS(field))
118 STATIC_TLS_INIT(BINDING_STACK_START,binding_stack_start);
119 STATIC_TLS_INIT(BINDING_STACK_POINTER,binding_stack_pointer);
120 STATIC_TLS_INIT(CONTROL_STACK_START,control_stack_start);
121 STATIC_TLS_INIT(CONTROL_STACK_END,control_stack_end);
122 STATIC_TLS_INIT(ALIEN_STACK,alien_stack_pointer);
123 #ifdef LISP_FEATURE_X86
124 STATIC_TLS_INIT(PSEUDO_ATOMIC_ATOMIC,pseudo_atomic_atomic);
125 STATIC_TLS_INIT(PSEUDO_ATOMIC_INTERRUPTED,pseudo_atomic_interrupted);
127 #undef STATIC_TLS_INIT
131 th->control_stack_start = spaces;
132 th->binding_stack_start=
133 (lispobj*)((void*)th->control_stack_start+THREAD_CONTROL_STACK_SIZE);
134 th->control_stack_end = th->binding_stack_start;
135 th->alien_stack_start=
136 (lispobj*)((void*)th->binding_stack_start+BINDING_STACK_SIZE);
137 th->binding_stack_pointer=th->binding_stack_start;
140 th->state=STATE_STOPPED;
141 #ifdef LISP_FEATURE_STACK_GROWS_DOWNWARD_NOT_UPWARD
142 th->alien_stack_pointer=((void *)th->alien_stack_start
143 + ALIEN_STACK_SIZE-4); /* naked 4. FIXME */
145 th->alien_stack_pointer=((void *)th->alien_stack_start);
147 #ifdef LISP_FEATURE_X86
148 th->pseudo_atomic_interrupted=0;
149 th->pseudo_atomic_atomic=0;
151 #ifdef LISP_FEATURE_GENCGC
152 gc_set_region_empty(&th->alloc_region);
155 #ifndef LISP_FEATURE_SB_THREAD
156 /* the tls-points-into-struct-thread trick is only good for threaded
157 * sbcl, because unithread sbcl doesn't have tls. So, we copy the
158 * appropriate values from struct thread here, and make sure that
159 * we use the appropriate SymbolValue macros to access any of the
160 * variable quantities from the C runtime. It's not quite OAOOM,
161 * it just feels like it */
162 SetSymbolValue(BINDING_STACK_START,th->binding_stack_start,th);
163 SetSymbolValue(CONTROL_STACK_START,th->control_stack_start,th);
164 SetSymbolValue(CONTROL_STACK_END,th->control_stack_end,th);
165 #ifdef LISP_FEATURE_X86
166 SetSymbolValue(BINDING_STACK_POINTER,th->binding_stack_pointer,th);
167 SetSymbolValue(ALIEN_STACK,th->alien_stack_pointer,th);
168 SetSymbolValue(PSEUDO_ATOMIC_ATOMIC,th->pseudo_atomic_atomic,th);
169 SetSymbolValue(PSEUDO_ATOMIC_INTERRUPTED,th->pseudo_atomic_interrupted,th);
171 current_binding_stack_pointer=th->binding_stack_pointer;
172 current_control_stack_pointer=th->control_stack_start;
175 bind_variable(CURRENT_CATCH_BLOCK,make_fixnum(0),th);
176 bind_variable(CURRENT_UNWIND_PROTECT_BLOCK,make_fixnum(0),th);
177 bind_variable(FREE_INTERRUPT_CONTEXT_INDEX,make_fixnum(0),th);
178 bind_variable(INTERRUPT_PENDING, NIL,th);
179 bind_variable(INTERRUPTS_ENABLED,T,th);
181 th->interrupt_data=os_validate(0,(sizeof (struct interrupt_data)));
183 memcpy(th->interrupt_data,
184 arch_os_get_current_thread()->interrupt_data,
185 sizeof (struct interrupt_data));
187 memcpy(th->interrupt_data,global_interrupt_data,
188 sizeof (struct interrupt_data));
190 th->unbound_marker=initial_function;
191 #ifdef LISP_FEATURE_SB_THREAD
192 #if defined(LISP_FEATURE_X86) && defined (LISP_FEATURE_LINUX)
194 clone(new_thread_trampoline,
195 (((void*)th->control_stack_start)+THREAD_CONTROL_STACK_SIZE-4),
196 (((getpid()!=parent_pid)?(CLONE_PARENT):0)
197 |CLONE_FILES|SIGALRM|CLONE_VM),th);
201 #error this stuff presently only works on x86 Linux
206 get_spinlock(&all_threads_lock,kid_pid);
207 th->next=all_threads;
209 /* note that th->pid is 0 at this time. We rely on all_threads_lock
210 * to ensure that we don't have >1 thread with pid=0 on the list at once
212 protect_control_stack_guard_page(th->pid,1);
213 release_spinlock(&all_threads_lock);
214 th->pid=kid_pid; /* child will not start until this is set */
215 #ifndef LISP_FEATURE_SB_THREAD
216 new_thread_trampoline(all_threads); /* call_into_lisp */
217 lose("Clever child? Idiot savant, verging on the.");
222 /* if(th && th->tls_cookie>=0) os_free_tls_pointer(th); */
223 if(spaces) os_invalidate(spaces,
224 THREAD_CONTROL_STACK_SIZE+BINDING_STACK_SIZE+
225 ALIEN_STACK_SIZE+dynamic_values_bytes);
229 void destroy_thread (struct thread *th)
231 /* precondition: the unix task has already been killed and exited.
232 * This is called by the parent */
233 #ifdef LISP_FEATURE_GENCGC
234 gc_alloc_update_page_tables(0, &th->alloc_region);
236 get_spinlock(&all_threads_lock,th->pid);
237 if(countdown_to_gc>0) countdown_to_gc--;
238 th->state=STATE_STOPPED;
240 all_threads=th->next;
242 struct thread *th1=all_threads;
243 while(th1->next!=th) th1=th1->next;
244 th1->next=th->next; /* unlink */
246 release_spinlock(&all_threads_lock);
247 if(th && th->tls_cookie>=0) arch_os_thread_cleanup(th);
248 os_invalidate((os_vm_address_t) th->control_stack_start,
250 * (th->control_stack_end-th->control_stack_start)) +
251 BINDING_STACK_SIZE+ALIEN_STACK_SIZE+dynamic_values_bytes+
256 struct thread *find_thread_by_pid(pid_t pid)
260 if(th->pid==pid) return th;
264 /* These are not needed unless #+SB-THREAD, and since sigwaitinfo()
265 * doesn't seem to be easily available everywhere (OpenBSD...) it's
266 * more trouble than it's worth to compile it when not needed. */
267 #if defined LISP_FEATURE_SB_THREAD
268 void block_sigcont(void)
270 /* don't allow ourselves to receive SIGCONT while we're in the
271 * "ambiguous" state of being on the queue but not actually stopped.
274 sigemptyset(&newset);
275 sigaddset(&newset,SIG_DEQUEUE);
276 sigprocmask(SIG_BLOCK, &newset, 0);
279 void unblock_sigcont_and_sleep(void)
283 sigaddset(&set,SIG_DEQUEUE);
287 }while(errno==EINTR);
288 sigprocmask(SIG_UNBLOCK,&set,0);
291 int interrupt_thread(pid_t pid, lispobj function)
294 sigval.sival_int=function;
296 return sigqueue(pid, SIG_INTERRUPT_THREAD, sigval);
299 int signal_thread_to_dequeue (pid_t pid)
301 return kill (pid, SIG_DEQUEUE);
305 /* stopping the world is a two-stage process. From this thread we signal
306 * all the others with SIG_STOP_FOR_GC. The handler for this thread does
307 * the usual pseudo-atomic checks (we don't want to stop a thread while
308 * it's in the middle of allocation) then kills _itself_ with SIGSTOP.
309 * At any given time, countdown_to_gc should reflect the number of threads
310 * signalled but which haven't yet come to rest
313 void gc_stop_the_world()
315 /* stop all other threads by sending them SIG_STOP_FOR_GC */
316 struct thread *p,*th=arch_os_get_current_thread();
320 get_spinlock(&all_threads_lock,th->pid);
321 for(p=all_threads,old_pid=p->pid; p; p=p->next) {
323 if(p->state!=STATE_RUNNING) continue;
325 p->state=STATE_STOPPING;
326 /* Note no return value check from kill(). If the
327 * thread had been reaped already, we kill it and
328 * increment countdown_to_gc anyway. This is to avoid
329 * complicating the logic in destroy_thread, which would
330 * otherwise have to know whether the thread died before or
331 * after it was killed
333 kill(p->pid,SIG_STOP_FOR_GC);
335 release_spinlock(&all_threads_lock);
337 /* if everything has stopped, and there is no possibility that
338 * a new thread has been created, we're done. Otherwise go
339 * round again and signal anything that sprang up since last
341 if(old_pid==all_threads->pid) {
344 finished = finished &&
345 ((p==th) || (p->state==STATE_STOPPED));
350 void gc_start_the_world()
352 struct thread *p,*th=arch_os_get_current_thread();
353 get_spinlock(&all_threads_lock,th->pid);
354 for(p=all_threads;p;p=p->next) {
356 p->state=STATE_RUNNING;
357 kill(p->pid,SIGCONT);
359 release_spinlock(&all_threads_lock);