0.8.6.5
[sbcl.git] / src / runtime / thread.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <sched.h>
4 #include <signal.h>
5 #include <stddef.h>
6 #include <errno.h>
7 #ifndef CLONE_PARENT            /* lameass glibc 2.2  doesn't define this */
8 #define CLONE_PARENT 0x00008000 /* even though the manpage documents it */
9 #endif
10 #include "runtime.h"
11 #include "sbcl.h"
12 #include "validate.h"           /* for CONTROL_STACK_SIZE etc */
13 #include "thread.h"
14 #include "arch.h"
15 #include "target-arch-os.h"
16 #include "os.h"
17 #include "globals.h"
18 #include "dynbind.h"
19 #include "genesis/cons.h"
20 #define ALIEN_STACK_SIZE (1*1024*1024) /* 1Mb size chosen at random */
21
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;
27
28 void get_spinlock(lispobj *word,int value);
29
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
34  */
35
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 */
38 volatile int go=1;              
39
40 int
41 new_thread_trampoline(struct thread *th)
42 {
43     lispobj function;
44     lispobj *args = NULL;
45     function = th->unbound_marker;
46     if(go==0) {
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);
49         while(go==0) ;
50         fprintf(stderr, "/continue\n");
51     }
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();
58 #else
59     if(th->pid < 1)
60         lose("th->pid not set up right");
61 #endif
62
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);
66 #else
67     return funcall0(function);
68 #endif
69 }
70
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 
73  * thread 
74  */
75
76 pid_t create_thread(lispobj initial_function) {
77     union per_thread_data *per_thread;
78     struct thread *th=0;        /*  subdue gcc */
79     void *spaces=0;
80     pid_t kid_pid;
81
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
84      * succeed */
85     spaces=os_validate(0,
86                        THREAD_CONTROL_STACK_SIZE+
87                        BINDING_STACK_SIZE+
88                        ALIEN_STACK_SIZE+
89                        dynamic_values_bytes+
90                        32*SIGSTKSZ
91                        );
92     if(!spaces) goto cleanup;
93     per_thread=(union per_thread_data *)
94         (spaces+
95          THREAD_CONTROL_STACK_SIZE+
96          BINDING_STACK_SIZE+
97          ALIEN_STACK_SIZE);
98
99     th=&per_thread->thread;
100     if(all_threads) {
101         memcpy(per_thread,arch_os_get_current_thread(),
102                dynamic_values_bytes);
103     } else {
104 #ifdef LISP_FEATURE_SB_THREAD
105         int i;
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) 
109             SetSymbolValue
110                 (FREE_TLS_INDEX,
111                  make_fixnum(MAX_INTERRUPTS+
112                              sizeof(struct thread)/sizeof(lispobj)),
113                  0);
114 #define STATIC_TLS_INIT(sym,field) \
115   ((struct symbol *)(sym-OTHER_POINTER_LOWTAG))->tls_index= \
116   make_fixnum(THREAD_SLOT_OFFSET_WORDS(field))
117                                   
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);
126 #endif
127 #undef STATIC_TLS_INIT
128 #endif
129     }
130
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;
138     th->this=th;
139     th->pid=0;
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 */
144 #else
145     th->alien_stack_pointer=((void *)th->alien_stack_start);
146 #endif
147 #ifdef LISP_FEATURE_X86
148     th->pseudo_atomic_interrupted=0;
149     th->pseudo_atomic_atomic=0;
150 #endif
151 #ifdef LISP_FEATURE_GENCGC
152     gc_set_region_empty(&th->alloc_region);
153 #endif
154
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);
170 #else
171     current_binding_stack_pointer=th->binding_stack_pointer;
172     current_control_stack_pointer=th->control_stack_start;
173 #endif
174 #endif    
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);
180
181     th->interrupt_data=os_validate(0,(sizeof (struct interrupt_data)));
182     if(all_threads) 
183         memcpy(th->interrupt_data,
184                arch_os_get_current_thread()->interrupt_data,
185                sizeof (struct interrupt_data));
186     else 
187         memcpy(th->interrupt_data,global_interrupt_data,
188                sizeof (struct interrupt_data));
189
190     th->unbound_marker=initial_function;
191 #ifdef LISP_FEATURE_SB_THREAD
192 #if defined(LISP_FEATURE_X86) && defined (LISP_FEATURE_LINUX)
193     kid_pid=
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);
198     if(kid_pid<=0) 
199         goto cleanup;
200 #else
201 #error this stuff presently only works on x86 Linux
202 #endif
203 #else
204     kid_pid=getpid();
205 #endif
206     get_spinlock(&all_threads_lock,kid_pid);
207     th->next=all_threads;
208     all_threads=th;
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
211      */
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.");
218 #endif
219
220     return th->pid;
221  cleanup:
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);
226     return 0;
227 }
228
229 void destroy_thread (struct thread *th)
230 {
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);
235 #endif
236     get_spinlock(&all_threads_lock,th->pid);
237     if(countdown_to_gc>0) countdown_to_gc--;
238     th->state=STATE_STOPPED;
239     if(th==all_threads) 
240         all_threads=th->next;
241     else {
242         struct thread *th1=all_threads;
243         while(th1->next!=th) th1=th1->next;
244         th1->next=th->next;     /* unlink */
245     }
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,
249                   ((sizeof (lispobj))
250                    * (th->control_stack_end-th->control_stack_start)) +
251                   BINDING_STACK_SIZE+ALIEN_STACK_SIZE+dynamic_values_bytes+
252                   32*SIGSTKSZ);
253 }
254
255
256 struct thread *find_thread_by_pid(pid_t pid) 
257 {
258     struct thread *th;
259     for_each_thread(th)
260         if(th->pid==pid) return th;
261     return 0;
262 }
263
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)
269 {
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.
272      */
273     sigset_t newset;
274     sigemptyset(&newset);
275     sigaddset(&newset,SIG_DEQUEUE);
276     sigprocmask(SIG_BLOCK, &newset, 0); 
277 }
278
279 void unblock_sigcont_and_sleep(void)
280 {
281     sigset_t set;
282     sigemptyset(&set);
283     sigaddset(&set,SIG_DEQUEUE);
284     do {
285         errno=0;
286         sigwaitinfo(&set,0);
287     }while(errno==EINTR);
288     sigprocmask(SIG_UNBLOCK,&set,0);
289 }
290
291 int interrupt_thread(pid_t pid, lispobj function)
292 {
293     union sigval sigval;
294     sigval.sival_int=function;
295
296     return sigqueue(pid, SIG_INTERRUPT_THREAD, sigval);
297 }
298
299 int signal_thread_to_dequeue (pid_t pid)
300 {
301     return kill (pid, SIG_DEQUEUE);
302 }
303
304
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
311  */
312
313 void gc_stop_the_world()
314 {
315     /* stop all other threads by sending them SIG_STOP_FOR_GC */
316     struct thread *p,*th=arch_os_get_current_thread();
317     pid_t old_pid;
318     int finished=0;
319     do {
320         get_spinlock(&all_threads_lock,th->pid);
321         for(p=all_threads,old_pid=p->pid; p; p=p->next) {
322             if(p==th) continue;
323             if(p->state!=STATE_RUNNING) continue;
324             countdown_to_gc++;
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
332              */
333             kill(p->pid,SIG_STOP_FOR_GC);
334         }
335         release_spinlock(&all_threads_lock);
336         sched_yield();
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
340          * time  */
341         if(old_pid==all_threads->pid) {
342             finished=1;
343             for_each_thread(p) 
344                 finished = finished &&
345                 ((p==th) || (p->state==STATE_STOPPED));
346         }
347     } while(!finished);
348 }
349
350 void gc_start_the_world()
351 {
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) {
355         if(p==th) continue;
356         p->state=STATE_RUNNING;
357         kill(p->pid,SIGCONT);
358     }
359     release_spinlock(&all_threads_lock);
360 }
361 #endif