X-Git-Url: http://repo.macrolet.net/gitweb/?a=blobdiff_plain;f=src%2Fruntime%2Fthread.c;h=8d3d36a6e6887e55b08168d245e2fb931815289b;hb=427cac784579a935a06b0d66bac63dbf9bf325a4;hp=f8812961e10163bcbf8daac3e6ab63465eedc5ca;hpb=783f83ce96494aede66580ba55aadbdc7ce3b127;p=sbcl.git diff --git a/src/runtime/thread.c b/src/runtime/thread.c index f881296..8d3d36a 100644 --- a/src/runtime/thread.c +++ b/src/runtime/thread.c @@ -1,14 +1,30 @@ +/* + * This software is part of the SBCL system. See the README file for + * more information. + * + * This software is derived from the CMU CL system, which was + * written at Carnegie Mellon University and released into the + * public domain. The software is in the public domain and is + * provided with absolutely no warranty. See the COPYING and CREDITS + * files for more information. + */ + +#include "sbcl.h" + #include #include #include +#ifndef LISP_FEATURE_WIN32 #include +#endif #include #include #include #include +#ifndef LISP_FEATURE_WIN32 #include +#endif -#include "sbcl.h" #include "runtime.h" #include "validate.h" /* for CONTROL_STACK_SIZE etc */ #include "alloc.h" @@ -23,6 +39,14 @@ #include "interr.h" /* for lose() */ #include "gc-internal.h" +#ifdef LISP_FEATURE_WIN32 +/* + * Win32 doesn't have SIGSTKSZ, and we're not switching stacks anyway, + * so define it arbitrarily + */ +#define SIGSTKSZ 1024 +#endif + #define ALIEN_STACK_SIZE (1*1024*1024) /* 1Mb size chosen at random */ struct freeable_stack { @@ -38,52 +62,13 @@ extern struct interrupt_data * global_interrupt_data; extern int linux_no_threads_p; #ifdef LISP_FEATURE_SB_THREAD - pthread_mutex_t all_threads_lock = PTHREAD_MUTEX_INITIALIZER; - -/* When trying to get all_threads_lock one should make sure that - * SIG_STOP_FOR_GC is not blocked. Else there would be a possible - * deadlock: gc locks it, other thread blocks signals, gc sends stop - * request to other thread and waits, other thread blocks on lock. */ -void check_sig_stop_for_gc_can_arrive_or_lose() -{ - /* Get the current sigmask, by blocking the empty set. */ - sigset_t empty,current; - sigemptyset(&empty); - thread_sigmask(SIG_BLOCK, &empty, ¤t); - if (sigismember(¤t,SIG_STOP_FOR_GC)) - lose("SIG_STOP_FOR_GC cannot arrive: it is blocked\n"); - if (SymbolValue(GC_INHIBIT,arch_os_get_current_thread()) != NIL) - lose("SIG_STOP_FOR_GC cannot arrive: gc is inhibited\n"); - if (arch_pseudo_atomic_atomic(NULL)) - lose("SIG_STOP_FOR_GC cannot arrive: in pseudo atomic\n"); -} - -#define GET_ALL_THREADS_LOCK(name) \ - { \ - sigset_t _newset,_oldset; \ - sigemptyset(&_newset); \ - sigaddset_deferrable(&_newset); \ - thread_sigmask(SIG_BLOCK, &_newset, &_oldset); \ - check_sig_stop_for_gc_can_arrive_or_lose(); \ - FSHOW_SIGNAL((stderr,"/%s:waiting on lock=%ld, thread=%lu\n",name, \ - all_threads_lock,arch_os_get_current_thread()->os_thread)); \ - pthread_mutex_lock(&all_threads_lock); \ - FSHOW_SIGNAL((stderr,"/%s:got lock, thread=%lu\n", \ - name,arch_os_get_current_thread()->os_thread)); - -#define RELEASE_ALL_THREADS_LOCK(name) \ - FSHOW_SIGNAL((stderr,"/%s:released lock\n",name)); \ - pthread_mutex_unlock(&all_threads_lock); \ - thread_sigmask(SIG_SETMASK,&_oldset,0); \ - } +#endif #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64) extern lispobj call_into_lisp_first_time(lispobj fun, lispobj *args, int nargs); #endif -#endif - static void link_thread(struct thread *th) { @@ -119,7 +104,6 @@ initial_thread_trampoline(struct thread *th) link_thread(th); th->os_thread=thread_self(); protect_control_stack_guard_page(1); - th->state = STATE_RUNNING; #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64) return call_into_lisp_first_time(function,args,0); @@ -180,21 +164,13 @@ new_thread_trampoline(struct thread *th) th->os_thread=thread_self(); protect_control_stack_guard_page(1); - /* This thread is in STATE_STARTING so the GC is not sending it - * SIG_STOP_FOR_GC => no danger of deadlocking even with - * SIG_STOP_FOR_GC blocked. The lock is acquired in order not to - * enter running state with the gc running. */ + /* Since GC can only know about this thread from the all_threads + * list and we're just adding this thread to it there is no danger + * of deadlocking even with SIG_STOP_FOR_GC blocked (which it is + * not). */ pthread_mutex_lock(&all_threads_lock); - th->state = STATE_RUNNING; + link_thread(th); pthread_mutex_unlock(&all_threads_lock); - /* Now that we entered STATE_RUNNING let the gc suspend this - * thread. */ - { - sigset_t sigset; - sigemptyset(&sigset); - sigaddset(&sigset, SIG_STOP_FOR_GC); - thread_sigmask(SIG_UNBLOCK, &sigset, 0); - } result = funcall0(function); th->state=STATE_DEAD; @@ -236,6 +212,9 @@ create_thread_struct(lispobj initial_function) { union per_thread_data *per_thread; struct thread *th=0; /* subdue gcc */ void *spaces=0; +#ifdef LISP_FEATURE_SB_THREAD + int i; +#endif /* may as well allocate all the spaces at once: it saves us from * having to decide what to do if only some of the allocations @@ -249,17 +228,14 @@ create_thread_struct(lispobj initial_function) { BINDING_STACK_SIZE+ ALIEN_STACK_SIZE); - if(all_threads) { - memcpy(per_thread,arch_os_get_current_thread(), - dynamic_values_bytes); - } else { #ifdef LISP_FEATURE_SB_THREAD - int i; - for(i=0;i<(dynamic_values_bytes/sizeof(lispobj));i++) - per_thread->dynamic_values[i]=NO_TLS_VALUE_MARKER_WIDETAG; + for(i = 0; i < (dynamic_values_bytes / sizeof(lispobj)); i++) + per_thread->dynamic_values[i] = NO_TLS_VALUE_MARKER_WIDETAG; + if (all_threads == 0) { if(SymbolValue(FREE_TLS_INDEX,0)==UNBOUND_MARKER_WIDETAG) { SetSymbolValue (FREE_TLS_INDEX, + /* FIXME: should be MAX_INTERRUPTS -1 ? */ make_fixnum(MAX_INTERRUPTS+ sizeof(struct thread)/sizeof(lispobj)), 0); @@ -279,8 +255,8 @@ create_thread_struct(lispobj initial_function) { STATIC_TLS_INIT(PSEUDO_ATOMIC_INTERRUPTED,pseudo_atomic_interrupted); #endif #undef STATIC_TLS_INIT -#endif } +#endif th=&per_thread->thread; th->control_stack_start = spaces; @@ -292,7 +268,7 @@ create_thread_struct(lispobj initial_function) { th->binding_stack_pointer=th->binding_stack_start; th->this=th; th->os_thread=0; - th->state=STATE_STARTING; + th->state=STATE_RUNNING; #ifdef LISP_FEATURE_STACK_GROWS_DOWNWARD_NOT_UPWARD th->alien_stack_pointer=((void *)th->alien_stack_start + ALIEN_STACK_SIZE-N_WORD_BYTES); @@ -352,7 +328,7 @@ void create_initial_thread(lispobj initial_function) { struct thread *th=create_thread_struct(initial_function); if(th) { initial_thread_trampoline(th); /* no return */ - } else lose("can't create initial thread"); + } else lose("can't create initial thread\n"); } #ifdef LISP_FEATURE_SB_THREAD @@ -370,7 +346,10 @@ boolean create_os_thread(struct thread *th,os_thread_t *kid_tid) sigset_t newset,oldset; boolean r=1; sigemptyset(&newset); - sigaddset_blockable(&newset); + /* Blocking deferrable signals is enough, no need to block + * SIG_STOP_FOR_GC because the child process is not linked onto + * all_threads until it's ready. */ + sigaddset_deferrable(&newset); thread_sigmask(SIG_BLOCK, &newset, &oldset); if((pthread_attr_init(&attr)) || @@ -389,23 +368,16 @@ os_thread_t create_thread(lispobj initial_function) { if(linux_no_threads_p) return 0; - /* The new thread must be linked immediately onto all_threads for - * gc. */ - GET_ALL_THREADS_LOCK("create_thread") - /* If it is too slow most of the allocation/initialization can - * be done without the lock. */ + /* Assuming that a fresh thread struct has no lisp objects in it, + * linking it to all_threads can be left to the thread itself + * without fear of gc lossage. initial_function violates this + * assumption and must stay pinned until the child starts up. */ th = create_thread_struct(initial_function); - if (th) - link_thread(th); - RELEASE_ALL_THREADS_LOCK("create_thread") if(th==0) return 0; if (create_os_thread(th,&kid_tid)) { return kid_tid; } else { - GET_ALL_THREADS_LOCK("create_thread") - unlink_thread(th); - RELEASE_ALL_THREADS_LOCK("create_thread") free_thread_struct(th); return 0; } @@ -440,7 +412,7 @@ int signal_interrupt_thread(os_thread_t os_thread) } else if (status == ESRCH) { return -1; } else { - lose("cannot send SIG_INTERRUPT_THREAD to thread=%lu: %d, %s", + lose("cannot send SIG_INTERRUPT_THREAD to thread=%lu: %d, %s\n", os_thread, status, strerror(status)); } } @@ -466,6 +438,7 @@ void gc_stop_the_world() th->os_thread)); /* stop all other threads by sending them SIG_STOP_FOR_GC */ for(p=all_threads; p; p=p->next) { + gc_assert(p->os_thread != 0); if((p!=th) && ((p->state==STATE_RUNNING))) { FSHOW_SIGNAL((stderr,"/gc_stop_the_world: suspending %lu\n", p->os_thread)); @@ -474,7 +447,7 @@ void gc_stop_the_world() /* This thread has exited. */ gc_assert(p->state==STATE_DEAD); } else if (status) { - lose("cannot send suspend thread=%lu: %d, %s", + lose("cannot send suspend thread=%lu: %d, %s\n", p->os_thread,status,strerror(status)); } } @@ -483,7 +456,6 @@ void gc_stop_the_world() /* wait for the running threads to stop or finish */ for(p=all_threads;p;) { if((p!=th) && (p->state==STATE_RUNNING)) { - gc_assert(p->os_thread!=0); sched_yield(); } else { p=p->next; @@ -502,8 +474,8 @@ void gc_start_the_world() * restarting */ FSHOW_SIGNAL((stderr,"/gc_start_the_world:begin\n")); for(p=all_threads;p;p=p->next) { - if((p!=th) && (p->state!=STATE_STARTING) && (p->state!=STATE_DEAD)) { - gc_assert(p->os_thread!=0); + gc_assert(p->os_thread!=0); + if((p!=th) && (p->state!=STATE_DEAD)) { if(p->state!=STATE_SUSPENDED) { lose("gc_start_the_world: wrong thread state is %d\n", fixnum_value(p->state)); @@ -513,7 +485,7 @@ void gc_start_the_world() p->state=STATE_RUNNING; status=kill_thread_safely(p->os_thread,SIG_STOP_FOR_GC); if (status) { - lose("cannot resume thread=%lu: %d, %s", + lose("cannot resume thread=%lu: %d, %s\n", p->os_thread,status,strerror(status)); } }