0.9.0.39:
[sbcl.git] / src / runtime / thread.c
index c9558a5..32dad36 100644 (file)
@@ -8,8 +8,8 @@
 #include <sys/types.h>
 #include <sys/wait.h>
 
-#include "runtime.h"
 #include "sbcl.h"
+#include "runtime.h"
 #include "validate.h"          /* for CONTROL_STACK_SIZE etc */
 #include "thread.h"
 #include "arch.h"
 #include "dynbind.h"
 #include "genesis/cons.h"
 #include "genesis/fdefn.h"
+#include "interr.h"             /* for lose() */
+#include "gc-internal.h"
+
 #define ALIEN_STACK_SIZE (1*1024*1024) /* 1Mb size chosen at random */
 
 int dynamic_values_bytes=4096*sizeof(lispobj); /* same for all threads */
 struct thread *all_threads;
 volatile lispobj all_threads_lock;
 extern struct interrupt_data * global_interrupt_data;
+extern int linux_no_threads_p;
 
 int
 initial_thread_trampoline(struct thread *th)
 {
     lispobj function;
+#if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
     lispobj *args = NULL;
+#endif
+
     function = th->unbound_marker;
     th->unbound_marker = UNBOUND_MARKER_WIDETAG;
     if(arch_os_thread_init(th)==0) return 1;
 
     if(th->pid < 1) lose("th->pid not set up right");
     th->state=STATE_RUNNING;
-#if defined(LISP_FEATURE_X86)
+#if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
     return call_into_lisp_first_time(function,args,0);
 #else
     return funcall0(function);
@@ -85,16 +92,15 @@ struct thread * create_thread_struct(lispobj initial_function) {
                       BINDING_STACK_SIZE+
                       ALIEN_STACK_SIZE+
                       dynamic_values_bytes+
-                      32*SIGSTKSZ
-                      );
-    if(!spaces) goto cleanup;
+                      32*SIGSTKSZ);
+    if(!spaces)
+        return NULL;
     per_thread=(union per_thread_data *)
        (spaces+
         THREAD_CONTROL_STACK_SIZE+
         BINDING_STACK_SIZE+
         ALIEN_STACK_SIZE);
 
-    th=&per_thread->thread;
     if(all_threads) {
        memcpy(per_thread,arch_os_get_current_thread(),
               dynamic_values_bytes);
@@ -126,6 +132,7 @@ struct thread * create_thread_struct(lispobj initial_function) {
 #endif
     }
 
+    th=&per_thread->thread;
     th->control_stack_start = spaces;
     th->binding_stack_start=
        (lispobj*)((void*)th->control_stack_start+THREAD_CONTROL_STACK_SIZE);
@@ -138,11 +145,11 @@ struct thread * create_thread_struct(lispobj initial_function) {
     th->state=STATE_STOPPED;
 #ifdef LISP_FEATURE_STACK_GROWS_DOWNWARD_NOT_UPWARD
     th->alien_stack_pointer=((void *)th->alien_stack_start
-                            + ALIEN_STACK_SIZE-4); /* naked 4.  FIXME */
+                            + ALIEN_STACK_SIZE-N_WORD_BYTES);
 #else
     th->alien_stack_pointer=((void *)th->alien_stack_start);
 #endif
-#ifdef LISP_FEATURE_X86
+#if defined(LISP_FEATURE_X86) || defined (LISP_FEATURE_X86_64)
     th->pseudo_atomic_interrupted=0;
     th->pseudo_atomic_atomic=0;
 #endif
@@ -157,13 +164,13 @@ struct thread * create_thread_struct(lispobj initial_function) {
      * we use the appropriate SymbolValue macros to access any of the
      * variable quantities from the C runtime.  It's not quite OAOOM,
      * it just feels like it */
-    SetSymbolValue(BINDING_STACK_START,th->binding_stack_start,th);
-    SetSymbolValue(CONTROL_STACK_START,th->control_stack_start,th);
-    SetSymbolValue(CONTROL_STACK_END,th->control_stack_end,th);
-#ifdef LISP_FEATURE_X86
-    SetSymbolValue(BINDING_STACK_POINTER,th->binding_stack_pointer,th);
-    SetSymbolValue(ALIEN_STACK,th->alien_stack_pointer,th);
-    SetSymbolValue(PSEUDO_ATOMIC_ATOMIC,th->pseudo_atomic_atomic,th);
+    SetSymbolValue(BINDING_STACK_START,(lispobj)th->binding_stack_start,th);
+    SetSymbolValue(CONTROL_STACK_START,(lispobj)th->control_stack_start,th);
+    SetSymbolValue(CONTROL_STACK_END,(lispobj)th->control_stack_end,th);
+#if defined(LISP_FEATURE_X86) || defined (LISP_FEATURE_X86_64)
+    SetSymbolValue(BINDING_STACK_POINTER,(lispobj)th->binding_stack_pointer,th);
+    SetSymbolValue(ALIEN_STACK,(lispobj)th->alien_stack_pointer,th);
+    SetSymbolValue(PSEUDO_ATOMIC_ATOMIC,(lispobj)th->pseudo_atomic_atomic,th);
     SetSymbolValue(PSEUDO_ATOMIC_INTERRUPTED,th->pseudo_atomic_interrupted,th);
 #else
     current_binding_stack_pointer=th->binding_stack_pointer;
@@ -176,7 +183,8 @@ struct thread * create_thread_struct(lispobj initial_function) {
     bind_variable(INTERRUPT_PENDING, NIL,th);
     bind_variable(INTERRUPTS_ENABLED,T,th);
 
-    th->interrupt_data=os_validate(0,(sizeof (struct interrupt_data)));
+    th->interrupt_data =
+       os_validate(0,(sizeof (struct interrupt_data)));
     if(all_threads) 
        memcpy(th->interrupt_data,
               arch_os_get_current_thread()->interrupt_data,
@@ -187,12 +195,6 @@ struct thread * create_thread_struct(lispobj initial_function) {
 
     th->unbound_marker=initial_function;
     return th;
- cleanup:
-    /* if(th && th->tls_cookie>=0) os_free_tls_pointer(th); */
-    if(spaces) os_invalidate(spaces,
-                            THREAD_CONTROL_STACK_SIZE+BINDING_STACK_SIZE+
-                            ALIEN_STACK_SIZE+dynamic_values_bytes);
-    return 0;
 }
 
 void link_thread(struct thread *th,pid_t kid_pid)
@@ -226,13 +228,15 @@ void create_initial_thread(lispobj initial_function) {
 
 #ifdef LISP_FEATURE_SB_THREAD
 pid_t create_thread(lispobj initial_function) {
-    struct thread *th=create_thread_struct(initial_function);
+    struct thread *th;
     pid_t kid_pid=0;
 
+    if(linux_no_threads_p) return 0;
+    th=create_thread_struct(initial_function);
     if(th==0) return 0;
     kid_pid=clone(new_thread_trampoline,
                  (((void*)th->control_stack_start)+
-                  THREAD_CONTROL_STACK_SIZE-4),
+                  THREAD_CONTROL_STACK_SIZE-16),
                  CLONE_FILES|SIG_THREAD_EXIT|CLONE_VM,th);
     
     if(kid_pid>0) {
@@ -249,32 +253,6 @@ pid_t create_thread(lispobj initial_function) {
 }
 #endif
 
-/* unused */
-void destroy_thread (struct thread *th)
-{
-    /* precondition: the unix task has already been killed and exited.
-     * This is called by the parent or some other thread */
-#ifdef LISP_FEATURE_GENCGC
-    gc_alloc_update_page_tables(0, &th->alloc_region);
-#endif
-    get_spinlock(&all_threads_lock,th->pid);
-    th->unbound_marker=0;      /* for debugging */
-    if(th==all_threads) 
-       all_threads=th->next;
-    else {
-       struct thread *th1=all_threads;
-       while(th1 && th1->next!=th) th1=th1->next;
-       if(th1) th1->next=th->next;     /* unlink */
-    }
-    release_spinlock(&all_threads_lock);
-    if(th && th->tls_cookie>=0) arch_os_thread_cleanup(th); 
-    os_invalidate((os_vm_address_t) th->control_stack_start,
-                 ((sizeof (lispobj))
-                  * (th->control_stack_end-th->control_stack_start)) +
-                 BINDING_STACK_SIZE+ALIEN_STACK_SIZE+dynamic_values_bytes+
-                 32*SIGSTKSZ);
-}
-
 struct thread *find_thread_by_pid(pid_t pid) 
 {
     struct thread *th;
@@ -286,6 +264,21 @@ struct thread *find_thread_by_pid(pid_t pid)
 #if defined LISP_FEATURE_SB_THREAD
 /* This is not needed unless #+SB-THREAD, as there's a trivial null
  * unithread definition. */
+
+void mark_dead_threads() 
+{
+    pid_t kid;
+    int status;
+    while(1) {
+       kid=waitpid(-1,&status,__WALL|WNOHANG);
+       if(kid<=0) break;
+       if(WIFEXITED(status) || WIFSIGNALED(status)) {
+           struct thread *th=find_thread_by_pid(kid);
+           if(th) th->state=STATE_DEAD;
+       }
+    }
+}
+
 void reap_dead_threads() 
 {
     struct thread *th,*next,*prev=0;
@@ -342,9 +335,12 @@ void unblock_sigcont_and_sleep(void)
 int interrupt_thread(pid_t pid, lispobj function)
 {
     union sigval sigval;
+    struct thread *th;
     sigval.sival_int=function;
-
-    return sigqueue(pid, SIG_INTERRUPT_THREAD, sigval);
+    for_each_thread(th) 
+       if((th->pid==pid) && (th->state != STATE_DEAD))
+           return sigqueue(pid, SIG_INTERRUPT_THREAD, sigval);
+    errno=EPERM; return -1;
 }
 
 int signal_thread_to_dequeue (pid_t pid)
@@ -371,7 +367,12 @@ void gc_stop_the_world()
            if(p==th) continue;
            if(p->state==STATE_RUNNING) {
                p->state=STATE_STOPPING;
-               kill(p->pid,SIG_STOP_FOR_GC);
+               if(kill(p->pid,SIG_STOP_FOR_GC)==-1) {
+                   /* we can't kill the process; assume because it
+                    * died already (and its parent is dead so never
+                    * saw the SIGCHLD) */
+                   p->state=STATE_DEAD;
+               }
            }
            if((p->state!=STATE_STOPPED) &&
               (p->state!=STATE_DEAD)) {