10 #include "validate.h" /* for CONTROL_STACK_SIZE etc */
13 #include "target-arch-os.h"
17 #include "genesis/cons.h"
18 #define ALIEN_STACK_SIZE (1*1024*1024) /* 1Mb size chosen at random */
20 int dynamic_values_bytes=4096*sizeof(lispobj); /* same for all threads */
21 struct thread *all_threads;
22 volatile lispobj all_threads_lock;
23 extern struct interrupt_data * global_interrupt_data;
25 void get_spinlock(lispobj *word,int value);
28 initial_thread_trampoline(struct thread *th)
32 function = th->unbound_marker;
33 th->unbound_marker = UNBOUND_MARKER_WIDETAG;
34 if(arch_os_thread_init(th)==0) return 1;
36 if(th->pid < 1) lose("th->pid not set up right");
37 th->state=STATE_RUNNING;
38 #if defined(LISP_FEATURE_X86)
39 return call_into_lisp_first_time(function,args,0);
41 return funcall0(function);
45 /* this is the first thing that clone() runs in the child (which is
46 * why the silly calling convention). Basically it calls the user's
47 * requested lisp function after doing arch_os_thread_init and
48 * whatever other bookkeeping needs to be done
52 new_thread_trampoline(struct thread *th)
55 function = th->unbound_marker;
56 th->unbound_marker = UNBOUND_MARKER_WIDETAG;
57 if(arch_os_thread_init(th)==0) return 1;
59 /* wait here until our thread is linked into all_threads: see below */
60 while(th->pid<1) sched_yield();
62 th->state=STATE_RUNNING;
63 return funcall0(function);
66 /* this is called from any other thread to create the new one, and
67 * initialize all parts of it that can be initialized from another
71 struct thread * create_thread_struct(lispobj initial_function) {
72 union per_thread_data *per_thread;
73 struct thread *th=0; /* subdue gcc */
76 /* may as well allocate all the spaces at once: it saves us from
77 * having to decide what to do if only some of the allocations
80 THREAD_CONTROL_STACK_SIZE+
86 if(!spaces) goto cleanup;
87 per_thread=(union per_thread_data *)
89 THREAD_CONTROL_STACK_SIZE+
93 th=&per_thread->thread;
95 memcpy(per_thread,arch_os_get_current_thread(),
96 dynamic_values_bytes);
98 #ifdef LISP_FEATURE_SB_THREAD
100 for(i=0;i<(dynamic_values_bytes/sizeof(lispobj));i++)
101 per_thread->dynamic_values[i]=UNBOUND_MARKER_WIDETAG;
102 if(SymbolValue(FREE_TLS_INDEX,0)==UNBOUND_MARKER_WIDETAG)
105 make_fixnum(MAX_INTERRUPTS+
106 sizeof(struct thread)/sizeof(lispobj)),
108 #define STATIC_TLS_INIT(sym,field) \
109 ((struct symbol *)(sym-OTHER_POINTER_LOWTAG))->tls_index= \
110 make_fixnum(THREAD_SLOT_OFFSET_WORDS(field))
112 STATIC_TLS_INIT(BINDING_STACK_START,binding_stack_start);
113 STATIC_TLS_INIT(BINDING_STACK_POINTER,binding_stack_pointer);
114 STATIC_TLS_INIT(CONTROL_STACK_START,control_stack_start);
115 STATIC_TLS_INIT(CONTROL_STACK_END,control_stack_end);
116 STATIC_TLS_INIT(ALIEN_STACK,alien_stack_pointer);
117 #ifdef LISP_FEATURE_X86
118 STATIC_TLS_INIT(PSEUDO_ATOMIC_ATOMIC,pseudo_atomic_atomic);
119 STATIC_TLS_INIT(PSEUDO_ATOMIC_INTERRUPTED,pseudo_atomic_interrupted);
121 #undef STATIC_TLS_INIT
125 th->control_stack_start = spaces;
126 th->binding_stack_start=
127 (lispobj*)((void*)th->control_stack_start+THREAD_CONTROL_STACK_SIZE);
128 th->control_stack_end = th->binding_stack_start;
129 th->alien_stack_start=
130 (lispobj*)((void*)th->binding_stack_start+BINDING_STACK_SIZE);
131 th->binding_stack_pointer=th->binding_stack_start;
134 th->state=STATE_STOPPED;
135 #ifdef LISP_FEATURE_STACK_GROWS_DOWNWARD_NOT_UPWARD
136 th->alien_stack_pointer=((void *)th->alien_stack_start
137 + ALIEN_STACK_SIZE-4); /* naked 4. FIXME */
139 th->alien_stack_pointer=((void *)th->alien_stack_start);
141 #ifdef LISP_FEATURE_X86
142 th->pseudo_atomic_interrupted=0;
143 th->pseudo_atomic_atomic=0;
145 #ifdef LISP_FEATURE_GENCGC
146 gc_set_region_empty(&th->alloc_region);
149 #ifndef LISP_FEATURE_SB_THREAD
150 /* the tls-points-into-struct-thread trick is only good for threaded
151 * sbcl, because unithread sbcl doesn't have tls. So, we copy the
152 * appropriate values from struct thread here, and make sure that
153 * we use the appropriate SymbolValue macros to access any of the
154 * variable quantities from the C runtime. It's not quite OAOOM,
155 * it just feels like it */
156 SetSymbolValue(BINDING_STACK_START,th->binding_stack_start,th);
157 SetSymbolValue(CONTROL_STACK_START,th->control_stack_start,th);
158 SetSymbolValue(CONTROL_STACK_END,th->control_stack_end,th);
159 #ifdef LISP_FEATURE_X86
160 SetSymbolValue(BINDING_STACK_POINTER,th->binding_stack_pointer,th);
161 SetSymbolValue(ALIEN_STACK,th->alien_stack_pointer,th);
162 SetSymbolValue(PSEUDO_ATOMIC_ATOMIC,th->pseudo_atomic_atomic,th);
163 SetSymbolValue(PSEUDO_ATOMIC_INTERRUPTED,th->pseudo_atomic_interrupted,th);
165 current_binding_stack_pointer=th->binding_stack_pointer;
166 current_control_stack_pointer=th->control_stack_start;
169 bind_variable(CURRENT_CATCH_BLOCK,make_fixnum(0),th);
170 bind_variable(CURRENT_UNWIND_PROTECT_BLOCK,make_fixnum(0),th);
171 bind_variable(FREE_INTERRUPT_CONTEXT_INDEX,make_fixnum(0),th);
172 bind_variable(INTERRUPT_PENDING, NIL,th);
173 bind_variable(INTERRUPTS_ENABLED,T,th);
175 th->interrupt_data=os_validate(0,(sizeof (struct interrupt_data)));
177 memcpy(th->interrupt_data,
178 arch_os_get_current_thread()->interrupt_data,
179 sizeof (struct interrupt_data));
181 memcpy(th->interrupt_data,global_interrupt_data,
182 sizeof (struct interrupt_data));
184 th->unbound_marker=initial_function;
187 /* if(th && th->tls_cookie>=0) os_free_tls_pointer(th); */
188 if(spaces) os_invalidate(spaces,
189 THREAD_CONTROL_STACK_SIZE+BINDING_STACK_SIZE+
190 ALIEN_STACK_SIZE+dynamic_values_bytes);
194 void link_thread(struct thread *th,pid_t kid_pid)
196 get_spinlock(&all_threads_lock,kid_pid);
197 th->next=all_threads;
199 /* note that th->pid is 0 at this time. We rely on all_threads_lock
200 * to ensure that we don't have >1 thread with pid=0 on the list at once
202 protect_control_stack_guard_page(th->pid,1);
203 release_spinlock(&all_threads_lock);
204 th->pid=kid_pid; /* child will not start until this is set */
207 void create_initial_thread(lispobj initial_function) {
208 struct thread *th=create_thread_struct(initial_function);
209 pid_t kid_pid=getpid();
210 if(th && kid_pid>0) {
211 link_thread(th,kid_pid);
212 initial_thread_trampoline(all_threads); /* no return */
213 } else lose("can't create initial thread");
216 #ifdef LISP_FEATURE_LINUX
217 pid_t create_thread(lispobj initial_function) {
218 struct thread *th=create_thread_struct(initial_function);
219 pid_t kid_pid=clone(new_thread_trampoline,
220 (((void*)th->control_stack_start)+
221 THREAD_CONTROL_STACK_SIZE-4),
222 CLONE_FILES|SIG_THREAD_EXIT|CLONE_VM,th);
224 if(th && kid_pid>0) {
225 link_thread(th,kid_pid);
234 void destroy_thread (struct thread *th)
236 /* precondition: the unix task has already been killed and exited.
237 * This is called by the parent */
238 #ifdef LISP_FEATURE_GENCGC
239 gc_alloc_update_page_tables(0, &th->alloc_region);
241 get_spinlock(&all_threads_lock,th->pid);
242 th->state=STATE_STOPPED;
244 all_threads=th->next;
246 struct thread *th1=all_threads;
247 while(th1 && th1->next!=th) th1=th1->next;
248 if(th1) th1->next=th->next; /* unlink */
250 release_spinlock(&all_threads_lock);
251 if(th && th->tls_cookie>=0) arch_os_thread_cleanup(th);
252 os_invalidate((os_vm_address_t) th->control_stack_start,
254 * (th->control_stack_end-th->control_stack_start)) +
255 BINDING_STACK_SIZE+ALIEN_STACK_SIZE+dynamic_values_bytes+
260 struct thread *find_thread_by_pid(pid_t pid)
264 if(th->pid==pid) return th;
268 /* These are not needed unless #+SB-THREAD, and since sigwaitinfo()
269 * doesn't seem to be easily available everywhere (OpenBSD...) it's
270 * more trouble than it's worth to compile it when not needed. */
271 #if defined LISP_FEATURE_SB_THREAD
272 void block_sigcont(void)
274 /* don't allow ourselves to receive SIGCONT while we're in the
275 * "ambiguous" state of being on the queue but not actually stopped.
278 sigemptyset(&newset);
279 sigaddset(&newset,SIG_DEQUEUE);
280 sigprocmask(SIG_BLOCK, &newset, 0);
283 void unblock_sigcont_and_sleep(void)
287 sigaddset(&set,SIG_DEQUEUE);
291 }while(errno==EINTR);
292 sigprocmask(SIG_UNBLOCK,&set,0);
295 int interrupt_thread(pid_t pid, lispobj function)
298 sigval.sival_int=function;
300 return sigqueue(pid, SIG_INTERRUPT_THREAD, sigval);
303 int signal_thread_to_dequeue (pid_t pid)
305 return kill (pid, SIG_DEQUEUE);
309 /* stopping the world is a two-stage process. From this thread we signal
310 * all the others with SIG_STOP_FOR_GC. The handler for this thread does
311 * the usual pseudo-atomic checks (we don't want to stop a thread while
312 * it's in the middle of allocation) then kills _itself_ with SIGSTOP.
315 void gc_stop_the_world()
317 /* stop all other threads by sending them SIG_STOP_FOR_GC */
318 struct thread *p,*th=arch_os_get_current_thread();
322 get_spinlock(&all_threads_lock,th->pid);
323 for(p=all_threads,old_pid=p->pid; p; p=p->next) {
325 if(p->state!=STATE_RUNNING) continue;
326 p->state=STATE_STOPPING;
327 kill(p->pid,SIG_STOP_FOR_GC);
329 release_spinlock(&all_threads_lock);
331 /* if everything has stopped, and there is no possibility that
332 * a new thread has been created, we're done. Otherwise go
333 * round again and signal anything that sprang up since last
335 if(old_pid==all_threads->pid) {
338 finished = finished &&
339 ((p==th) || (p->state==STATE_STOPPED));
344 void gc_start_the_world()
346 struct thread *p,*th=arch_os_get_current_thread();
347 get_spinlock(&all_threads_lock,th->pid);
348 for(p=all_threads;p;p=p->next) {
350 p->state=STATE_RUNNING;
351 kill(p->pid,SIG_STOP_FOR_GC);
353 release_spinlock(&all_threads_lock);