X-Git-Url: https://git.hcoop.net/bpt/guile.git/blobdiff_plain/9864812182858fefcd1f012e57654f698f730c88..35a9197ccc91a3663313e1bf7d369101754a1075:/libguile/threads.c diff --git a/libguile/threads.c b/libguile/threads.c index 16c6962e9..d15046bd2 100644 --- a/libguile/threads.c +++ b/libguile/threads.c @@ -1,4 +1,4 @@ -/* Copyright (C) 1995,1996,1997,1998,2000,2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc. +/* Copyright (C) 1995,1996,1997,1998,2000,2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -20,6 +20,7 @@ #define _GNU_SOURCE +#include "libguile/boehm-gc.h" #include "libguile/_scm.h" #if HAVE_UNISTD_H @@ -27,6 +28,11 @@ #endif #include #include + +#ifdef HAVE_STRING_H +#include /* for memset used by FD_ZERO on Solaris 10 */ +#endif + #if HAVE_SYS_TIME_H #include #endif @@ -41,6 +47,7 @@ #include "libguile/iselect.h" #include "libguile/fluids.h" #include "libguile/continuations.h" +#include "libguile/gc.h" #include "libguile/init.h" #ifdef __MINGW32__ @@ -120,17 +127,6 @@ dequeue (SCM q) /*** Thread smob routines */ -static SCM -thread_mark (SCM obj) -{ - scm_i_thread *t = SCM_I_THREAD_DATA (obj); - scm_gc_mark (t->result); - scm_gc_mark (t->join_queue); - scm_gc_mark (t->dynwinds); - scm_gc_mark (t->active_asyncs); - scm_gc_mark (t->continuation_root); - return t->dynamic_state; -} static int thread_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED) @@ -381,7 +377,7 @@ static SCM scm_i_default_dynamic_state; static void guilify_self_1 (SCM_STACKITEM *base) { - scm_i_thread *t = malloc (sizeof (scm_i_thread)); + scm_i_thread *t = scm_gc_malloc (sizeof (scm_i_thread), "thread"); t->pthread = scm_i_pthread_self (); t->handle = SCM_BOOL_F; @@ -405,6 +401,8 @@ guilify_self_1 (SCM_STACKITEM *base) scm_i_pthread_mutex_init (&t->heap_mutex, NULL); t->clear_freelists_p = 0; t->gc_running_p = 0; + t->current_mark_stack_ptr = NULL; + t->current_mark_stack_limit = NULL; t->exited = 0; t->freelist = SCM_EOL; @@ -431,7 +429,7 @@ guilify_self_2 (SCM parent) scm_i_thread *t = SCM_I_CURRENT_THREAD; SCM_NEWSMOB (t->handle, scm_tc16_thread, t); - scm_gc_register_collectable_memory (t, sizeof (scm_i_thread), "thread"); + t->continuation_root = scm_cons (t->handle, SCM_EOL); t->continuation_base = t->base; @@ -564,12 +562,9 @@ scm_i_init_thread_for_guile (SCM_STACKITEM *base, SCM parent) } } -#ifdef HAVE_LIBC_STACK_END - -extern void *__libc_stack_end; - #if SCM_USE_PTHREAD_THREADS -#ifdef HAVE_PTHREAD_ATTR_GETSTACK +/* pthread_getattr_np not available on MacOS X and Solaris 10. */ +#if HAVE_PTHREAD_ATTR_GETSTACK && HAVE_PTHREAD_GETATTR_NP #define HAVE_GET_THREAD_STACK_BASE @@ -580,18 +575,20 @@ get_thread_stack_base () void *start, *end; size_t size; - /* XXX - pthread_getattr_np from LinuxThreads does not seem to work - for the main thread, but we can use __libc_stack_end in that - case. - */ - pthread_getattr_np (pthread_self (), &attr); pthread_attr_getstack (&attr, &start, &size); end = (char *)start + size; + /* XXX - pthread_getattr_np from LinuxThreads does not seem to work + for the main thread, but we can use scm_get_stack_base in that + case. + */ + +#ifndef PTHREAD_ATTR_GETSTACK_WORKS if ((void *)&attr < start || (void *)&attr >= end) - return __libc_stack_end; + return scm_get_stack_base (); else +#endif { #if SCM_STACK_GROWS_UP return start; @@ -601,7 +598,7 @@ get_thread_stack_base () } } -#endif /* HAVE_PTHREAD_ATTR_GETSTACK */ +#endif /* HAVE_PTHREAD_ATTR_GETSTACK && HAVE_PTHREAD_GETATTR_NP */ #else /* !SCM_USE_PTHREAD_THREADS */ @@ -610,11 +607,10 @@ get_thread_stack_base () static SCM_STACKITEM * get_thread_stack_base () { - return __libc_stack_end; + return scm_get_stack_base (); } #endif /* !SCM_USE_PTHREAD_THREADS */ -#endif /* HAVE_LIBC_STACK_END */ #ifdef HAVE_GET_THREAD_STACK_BASE @@ -884,13 +880,6 @@ typedef struct { #define SCM_MUTEXP(x) SCM_SMOB_PREDICATE (scm_tc16_mutex, x) #define SCM_MUTEX_DATA(x) ((fat_mutex *) SCM_SMOB_DATA (x)) -static SCM -fat_mutex_mark (SCM mx) -{ - fat_mutex *m = SCM_MUTEX_DATA (mx); - scm_gc_mark (m->owner); - return m->waiting; -} static size_t fat_mutex_free (SCM mx) @@ -998,12 +987,12 @@ SCM_DEFINE (scm_lock_mutex, "lock-mutex", 1, 0, 0, #undef FUNC_NAME void -scm_frame_lock_mutex (SCM mutex) +scm_dynwind_lock_mutex (SCM mutex) { - scm_frame_unwind_handler_with_scm ((void(*)(SCM))scm_unlock_mutex, mutex, - SCM_F_WIND_EXPLICITLY); - scm_frame_rewind_handler_with_scm ((void(*)(SCM))scm_lock_mutex, mutex, - SCM_F_WIND_EXPLICITLY); + scm_dynwind_unwind_handler_with_scm ((void(*)(SCM))scm_unlock_mutex, mutex, + SCM_F_WIND_EXPLICITLY); + scm_dynwind_rewind_handler_with_scm ((void(*)(SCM))scm_lock_mutex, mutex, + SCM_F_WIND_EXPLICITLY); } static char * @@ -1125,13 +1114,6 @@ typedef struct { #define SCM_CONDVARP(x) SCM_SMOB_PREDICATE (scm_tc16_condvar, x) #define SCM_CONDVAR_DATA(x) ((fat_cond *) SCM_SMOB_DATA (x)) -static SCM -fat_cond_mark (SCM cv) -{ - fat_cond *c = SCM_CONDVAR_DATA (cv); - return c->waiting; -} - static size_t fat_cond_free (SCM mx) { @@ -1300,37 +1282,14 @@ SCM_DEFINE (scm_broadcast_condition_variable, "broadcast-condition-variable", 1, scm_mark_locations ((SCM_STACKITEM *) &ctx.uc_mcontext, \ ((size_t) (sizeof (SCM_STACKITEM) - 1 + sizeof ctx.uc_mcontext) \ / sizeof (SCM_STACKITEM))); \ - bot = (SCM_STACKITEM *) __libc_ia64_register_backing_store_base; \ - top = (SCM_STACKITEM *) ctx.uc_mcontext.sc_ar_bsp; \ + bot = (SCM_STACKITEM *) scm_ia64_register_backing_store_base (); \ + top = (SCM_STACKITEM *) scm_ia64_ar_bsp (&ctx); \ scm_mark_locations (bot, top - bot); } while (0) #else # define SCM_MARK_BACKING_STORE() #endif -void -scm_threads_mark_stacks (void) -{ - scm_i_thread *t; - for (t = all_threads; t; t = t->next_thread) - { - /* Check that thread has indeed been suspended. - */ - assert (t->top); - - scm_gc_mark (t->handle); - -#if SCM_STACK_GROWS_UP - scm_mark_locations (t->base, t->top - t->base); -#else - scm_mark_locations (t->top, t->base - t->top); -#endif - scm_mark_locations ((SCM_STACKITEM *) t->regs, - ((size_t) sizeof(t->regs) - / sizeof (SCM_STACKITEM))); - } - SCM_MARK_BACKING_STORE (); -} /*** Select */ @@ -1397,16 +1356,16 @@ scm_pthread_mutex_lock (scm_i_pthread_mutex_t *mutex) } static void -unlock (void *data) +do_unlock (void *data) { scm_i_pthread_mutex_unlock ((scm_i_pthread_mutex_t *)data); } void -scm_frame_pthread_mutex_lock (scm_i_pthread_mutex_t *mutex) +scm_dynwind_pthread_mutex_lock (scm_i_pthread_mutex_t *mutex) { scm_i_scm_pthread_mutex_lock (mutex); - scm_frame_unwind_handler (unlock, mutex, SCM_F_WIND_EXPLICITLY); + scm_dynwind_unwind_handler (do_unlock, mutex, SCM_F_WIND_EXPLICITLY); } int @@ -1579,15 +1538,15 @@ scm_i_thread_sleep_for_gc () scm_i_pthread_mutex_t scm_i_critical_section_mutex; int scm_i_critical_section_level = 0; -static SCM framed_critical_section_mutex; +static SCM dynwind_critical_section_mutex; void -scm_frame_critical_section (SCM mutex) +scm_dynwind_critical_section (SCM mutex) { if (scm_is_false (mutex)) - mutex = framed_critical_section_mutex; - scm_frame_lock_mutex (mutex); - scm_frame_block_asyncs (); + mutex = dynwind_critical_section_mutex; + scm_dynwind_lock_mutex (mutex); + scm_dynwind_block_asyncs (); } /*** Initialization */ @@ -1626,18 +1585,15 @@ void scm_init_threads () { scm_tc16_thread = scm_make_smob_type ("thread", sizeof (scm_i_thread)); - scm_set_smob_mark (scm_tc16_thread, thread_mark); scm_set_smob_print (scm_tc16_thread, thread_print); - scm_set_smob_free (scm_tc16_thread, thread_free); + scm_set_smob_free (scm_tc16_thread, thread_free); /* XXX: Could be removed */ scm_tc16_mutex = scm_make_smob_type ("mutex", sizeof (fat_mutex)); - scm_set_smob_mark (scm_tc16_mutex, fat_mutex_mark); scm_set_smob_print (scm_tc16_mutex, fat_mutex_print); scm_set_smob_free (scm_tc16_mutex, fat_mutex_free); scm_tc16_condvar = scm_make_smob_type ("condition-variable", sizeof (fat_cond)); - scm_set_smob_mark (scm_tc16_condvar, fat_cond_mark); scm_set_smob_print (scm_tc16_condvar, fat_cond_print); scm_set_smob_free (scm_tc16_condvar, fat_cond_free); @@ -1645,7 +1601,7 @@ scm_init_threads () guilify_self_2 (SCM_BOOL_F); threads_initialized_p = 1; - framed_critical_section_mutex = + dynwind_critical_section_mutex = scm_permanent_object (scm_make_recursive_mutex ()); }