From e4b90f498c3141ba203a6359a8a0cfe790324203 Mon Sep 17 00:00:00 2001 From: Mark H Weaver Date: Sun, 17 Nov 2013 04:00:29 -0500 Subject: [PATCH 1/6] Add mutex locking functions that also block asyncs. * libguile/async.h (scm_i_pthread_mutex_lock_with_asyncs, scm_i_pthread_mutex_unlock_with_asyncs): New macros. * libguile/threads.c (do_unlock_with_asyncs): New static helper. (scm_i_dynwind_pthread_mutex_lock_with_asyncs): New function. * libguile/threads.h (scm_i_dynwind_pthread_mutex_lock_with_asyncs): Add prototype. --- libguile/async.h | 16 ++++++++++++++++ libguile/threads.c | 16 ++++++++++++++++ libguile/threads.h | 1 + 3 files changed, 33 insertions(+), 0 deletions(-) diff --git a/libguile/async.h b/libguile/async.h index ceb2b96..a909c0d 100644 --- a/libguile/async.h +++ b/libguile/async.h @@ -78,6 +78,22 @@ SCM_API void scm_critical_section_end (void); scm_async_click (); \ } while (0) +# define scm_i_pthread_mutex_lock_with_asyncs(m) \ + do \ + { \ + SCM_I_CURRENT_THREAD->block_asyncs++; \ + scm_i_pthread_mutex_lock (m); \ + } \ + while (0) + +# define scm_i_pthread_mutex_unlock_with_asyncs(m) \ + do \ + { \ + scm_i_pthread_mutex_unlock (m); \ + SCM_I_CURRENT_THREAD->block_asyncs--; \ + } \ + while (0) + #else /* !BUILDING_LIBGUILE */ # define SCM_CRITICAL_SECTION_START scm_critical_section_start () diff --git a/libguile/threads.c b/libguile/threads.c index 8cbe1e2..6aeaeb9 100644 --- a/libguile/threads.c +++ b/libguile/threads.c @@ -2010,6 +2010,22 @@ scm_pthread_cond_timedwait (scm_i_pthread_cond_t *cond, #endif +static void +do_unlock_with_asyncs (void *data) +{ + scm_i_pthread_mutex_unlock ((scm_i_pthread_mutex_t *)data); + SCM_I_CURRENT_THREAD->block_asyncs--; +} + +void +scm_i_dynwind_pthread_mutex_lock_with_asyncs (scm_i_pthread_mutex_t *mutex) +{ + SCM_I_CURRENT_THREAD->block_asyncs++; + scm_i_scm_pthread_mutex_lock (mutex); + scm_dynwind_unwind_handler (do_unlock_with_asyncs, mutex, + SCM_F_WIND_EXPLICITLY); +} + unsigned long scm_std_usleep (unsigned long usecs) { diff --git a/libguile/threads.h b/libguile/threads.h index 901c37b..5a2afa2 100644 --- a/libguile/threads.h +++ b/libguile/threads.h @@ -143,6 +143,7 @@ SCM_INTERNAL void scm_init_threads (void); SCM_INTERNAL void scm_init_thread_procs (void); SCM_INTERNAL void scm_init_threads_default_dynamic_state (void); +SCM_INTERNAL void scm_i_dynwind_pthread_mutex_lock_with_asyncs (scm_i_pthread_mutex_t *mutex); #define SCM_THREAD_SWITCHING_CODE \ do { } while (0) -- 1.7.5.4