diff options
Diffstat (limited to 'libguile/threads.c')
-rw-r--r-- | libguile/threads.c | 75 |
1 files changed, 45 insertions, 30 deletions
diff --git a/libguile/threads.c b/libguile/threads.c index a3aee0f17..61fc43e7f 100644 --- a/libguile/threads.c +++ b/libguile/threads.c @@ -63,7 +63,6 @@ #include "libguile/init.h" #include "libguile/scmsigs.h" #include "libguile/strings.h" -#include "libguile/weaks.h" #include <full-read.h> @@ -398,11 +397,11 @@ thread_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED) else id = u.um; - scm_puts ("#<thread ", port); + scm_puts_unlocked ("#<thread ", port); scm_uintprint (id, 10, port); - scm_puts (" (", port); + scm_puts_unlocked (" (", port); scm_uintprint ((scm_t_bits)t, 16, port); - scm_puts (")>", port); + scm_puts_unlocked (")>", port); return 1; } @@ -544,7 +543,9 @@ guilify_self_1 (struct GC_stack_base *base) t.held_mutex = NULL; t.join_queue = SCM_EOL; t.dynamic_state = SCM_BOOL_F; - t.dynwinds = SCM_EOL; + t.dynstack.base = NULL; + t.dynstack.top = NULL; + t.dynstack.limit = NULL; t.active_asyncs = SCM_EOL; t.block_asyncs = 1; t.pending_asyncs = 1; @@ -618,6 +619,10 @@ guilify_self_2 (SCM parent) else t->dynamic_state = scm_i_make_initial_dynamic_state (); + t->dynstack.base = scm_gc_malloc (16 * sizeof (scm_t_bits), "dynstack"); + t->dynstack.limit = t->dynstack.base + 16; + t->dynstack.top = t->dynstack.base + SCM_DYNSTACK_HEADER_LEN; + t->join_queue = make_queue (); t->block_asyncs = 0; @@ -664,10 +669,6 @@ do_thread_exit (void *v) { scm_i_thread *t = (scm_i_thread *) v; - /* Ensure the signal handling thread has been launched, because we might be - shutting it down. This needs to be done in Guile mode. */ - scm_i_ensure_signal_delivery_thread (); - if (!scm_is_false (t->cleanup_handler)) { SCM ptr = t->cleanup_handler; @@ -688,9 +689,9 @@ do_thread_exit (void *v) while (!scm_is_null (t->mutexes)) { - SCM mutex = SCM_WEAK_PAIR_CAR (t->mutexes); + SCM mutex = scm_c_weak_vector_ref (scm_car (t->mutexes), 0); - if (!SCM_UNBNDP (mutex)) + if (scm_is_true (mutex)) { fat_mutex *m = SCM_MUTEX_DATA (mutex); @@ -705,7 +706,7 @@ do_thread_exit (void *v) scm_i_pthread_mutex_unlock (&m->lock); } - t->mutexes = SCM_WEAK_PAIR_CDR (t->mutexes); + t->mutexes = scm_cdr (t->mutexes); } scm_i_pthread_mutex_unlock (&t->admin_mutex); @@ -1300,21 +1301,13 @@ SCM_DEFINE (scm_thread_p, "thread?", 1, 0, 0, #undef FUNC_NAME -static size_t -fat_mutex_free (SCM mx) -{ - fat_mutex *m = SCM_MUTEX_DATA (mx); - scm_i_pthread_mutex_destroy (&m->lock); - return 0; -} - static int fat_mutex_print (SCM mx, SCM port, scm_print_state *pstate SCM_UNUSED) { fat_mutex *m = SCM_MUTEX_DATA (mx); - scm_puts ("#<mutex ", port); + scm_puts_unlocked ("#<mutex ", port); scm_uintprint ((scm_t_bits)m, 16, port); - scm_puts (">", port); + scm_puts_unlocked (">", port); return 1; } @@ -1323,9 +1316,12 @@ make_fat_mutex (int recursive, int unchecked_unlock, int external_unlock) { fat_mutex *m; SCM mx; + scm_i_pthread_mutex_t lock = SCM_I_PTHREAD_MUTEX_INITIALIZER; m = scm_gc_malloc (sizeof (fat_mutex), "mutex"); - scm_i_pthread_mutex_init (&m->lock, NULL); + /* Because PTHREAD_MUTEX_INITIALIZER is static, it's plain old data, + and so we can just copy it. */ + memcpy (&m->lock, &lock, sizeof (m->lock)); m->owner = SCM_BOOL_F; m->level = 0; @@ -1417,7 +1413,8 @@ fat_mutex_lock (SCM mutex, scm_t_timespec *timeout, SCM owner, int *ret) The weak pair itself is eventually removed when MUTEX is unlocked. Note that `t->mutexes' lists mutexes currently held by T, so it should be small. */ - t->mutexes = scm_weak_car_pair (mutex, t->mutexes); + t->mutexes = scm_cons (scm_make_weak_vector (SCM_INUM1, mutex), + t->mutexes); scm_i_pthread_mutex_unlock (&t->admin_mutex); } @@ -1562,6 +1559,25 @@ 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 void +remove_mutex_from_thread (SCM mutex, scm_i_thread *t) +{ + SCM walk, prev; + + for (prev = SCM_BOOL_F, walk = t->mutexes; scm_is_pair (walk); + walk = SCM_CDR (walk)) + { + if (scm_is_eq (mutex, scm_c_weak_vector_ref (SCM_CAR (walk), 0))) + { + if (scm_is_pair (prev)) + SCM_SETCDR (prev, SCM_CDR (walk)); + else + t->mutexes = SCM_CDR (walk); + break; + } + } +} + static int fat_mutex_unlock (SCM mutex, SCM cond, const scm_t_timespec *waittime, int relock) @@ -1606,7 +1622,7 @@ fat_mutex_unlock (SCM mutex, SCM cond, if (m->level == 0) { /* Change the owner of MUTEX. */ - t->mutexes = scm_delq_x (mutex, t->mutexes); + remove_mutex_from_thread (mutex, t); m->owner = unblock_from_queue (m->waiting); } @@ -1640,7 +1656,7 @@ fat_mutex_unlock (SCM mutex, SCM cond, } t->block_asyncs--; - scm_async_click (); + scm_async_tick (); scm_remember_upto_here_2 (cond, mutex); @@ -1654,7 +1670,7 @@ fat_mutex_unlock (SCM mutex, SCM cond, if (m->level == 0) { /* Change the owner of MUTEX. */ - t->mutexes = scm_delq_x (mutex, t->mutexes); + remove_mutex_from_thread (mutex, t); m->owner = unblock_from_queue (m->waiting); } @@ -1750,9 +1766,9 @@ static int fat_cond_print (SCM cv, SCM port, scm_print_state *pstate SCM_UNUSED) { fat_cond *c = SCM_CONDVAR_DATA (cv); - scm_puts ("#<condition-variable ", port); + scm_puts_unlocked ("#<condition-variable ", port); scm_uintprint ((scm_t_bits)c, 16, port); - scm_puts (">", port); + scm_puts_unlocked (">", port); return 1; } @@ -2172,7 +2188,6 @@ scm_init_threads () scm_tc16_mutex = scm_make_smob_type ("mutex", sizeof (fat_mutex)); 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)); |