mirror of
https://git.savannah.gnu.org/git/guile.git
synced 2025-04-30 11:50:28 +02:00
* libguile/threads.h (held_mutex): New field. * libguile/threads.c (enqueue, remqueue, dequeue): Use critical section to protect access to the queue. (guilify_self_1): Initialize held_mutex field. (on_thread_exit): If held_mutex non-null, unlock it. (fat_mutex_unlock, fat_cond_free, scm_make_condition_variable, fat_cond_signal, fat_cond_broadcast): Delete now unnecessary uses of c->lock. (fat_mutex_unlock): Pass m->lock to block_self() instead of c->lock; move scm_i_pthread_mutex_unlock(m->lock) call from before block_self() to after. (scm_pthread_cond_wait, scm_pthread_cond_timedwait, scm_i_thread_sleep_for_gc): Set held_mutex before pthread call; reset it afterwards. I was seeing a hang in srfi-18.test, when running make check in master, in the "exception handler installation is thread-safe" test. It wasn't 100% reproducible, so looked like a race. The problem is that wait-condition-variable is not actually atomic in the way that it is supposed to be. It unlocks the mutex, then starts waiting on the cond var. So it is possible for another thread to lock the same mutex, and signal the cond var, before the wait-condition-variable thread starts waiting. In order for wait-condition-variable to be atomic - e.g. in a race where thread A holds (Scheme-level) mutex M, and calls (wait-condition-variable C M), and thread B calls (begin (lock-mutex M) (signal-condition-variable C)) - it needs to call pthread_cond_wait with the same underlying mutex as is involved in the `lock-mutex' call. In terms of the threads.c code, this means that it has to use M->lock, not C->lock. block_self() used its mutex arg for two purposes: for protecting access and changes to the wait queue, and for the pthread_cond_wait call. But it wouldn't work reliably to use M->lock to protect C's wait queue, because in theory two threads can call (wait-condition-variable C M1) and (wait-condition-variable C M2) concurrently, with M1 and M2 different. So we either have to pass both C->lock and M->lock into block_self(), or use some other mutex to protect the wait queue. For this patch, I switched to using the critical section mutex, because that is a global and so easily available. (If that turns out to be a problem for performance, we could make each queue structure have its own mutex, but there's no reason to believe yet that it is a problem, because the critical section mutex isn't used much overall.) So then we call block_self() with M->lock, and move where M->lock is unlocked to after the block_self() call, instead of before. That solves the first hang, but introduces a new one, when a SRFI-18 thread is terminated (`thread-terminate!') between being launched (`make-thread') and started (`thread-start!'). The problem now is that pthread_cond_wait is a cancellation point (see man pthread_cancel), so the pthread_cond_wait call is one of the few places where a thread-terminate! call can take effect. If the thread is cancelled at that point, M->lock ends up still being locked, and then when do_thread_exit() tries to lock M->lock again, it hangs. The fix for that is a new `held_mutex' field in scm_i_thread, which is set to point to the mutex just before a pthread_cond_(timed)wait call, and set to NULL again afterwards. If on_thread_exit() finds that held_mutex is non-NULL, it unlocks that mutex. A detail is that checking and unlocking held_mutex must be done before on_thread_exit() calls scm_i_ensure_signal_delivery_thread(), because the innards of scm_i_ensure_signal_delivery_thread() can do another pthread_cond_wait() call and so overwrite held_mutex. But that's OK, because it's fine for the mutex check and unlock to happen outside Guile mode. Lastly, C->lock is then not needed, so I've removed it.
244 lines
7.7 KiB
C
244 lines
7.7 KiB
C
/* classes: h_files */
|
||
|
||
#ifndef SCM_THREADS_H
|
||
#define SCM_THREADS_H
|
||
|
||
/* Copyright (C) 1996,1997,1998,2000,2001, 2002, 2003, 2004, 2006, 2007, 2008 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
|
||
* License as published by the Free Software Foundation; either
|
||
* version 2.1 of the License, or (at your option) any later version.
|
||
*
|
||
* This library is distributed in the hope that it will be useful,
|
||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||
* Lesser General Public License for more details.
|
||
*
|
||
* You should have received a copy of the GNU Lesser General Public
|
||
* License along with this library; if not, write to the Free Software
|
||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||
*/
|
||
|
||
|
||
|
||
#include "libguile/__scm.h"
|
||
#include "libguile/procs.h"
|
||
#include "libguile/throw.h"
|
||
#include "libguile/root.h"
|
||
#include "libguile/iselect.h"
|
||
#include "libguile/dynwind.h"
|
||
#include "libguile/continuations.h"
|
||
|
||
#if SCM_USE_PTHREAD_THREADS
|
||
#include "libguile/pthread-threads.h"
|
||
#endif
|
||
|
||
#if SCM_USE_NULL_THREADS
|
||
#include "libguile/null-threads.h"
|
||
#endif
|
||
|
||
|
||
|
||
/* smob tags for the thread datatypes */
|
||
SCM_API scm_t_bits scm_tc16_thread;
|
||
SCM_API scm_t_bits scm_tc16_mutex;
|
||
SCM_API scm_t_bits scm_tc16_condvar;
|
||
|
||
typedef struct scm_i_thread {
|
||
struct scm_i_thread *next_thread;
|
||
|
||
SCM handle;
|
||
scm_i_pthread_t pthread;
|
||
|
||
SCM cleanup_handler;
|
||
SCM join_queue;
|
||
|
||
scm_i_pthread_mutex_t admin_mutex;
|
||
SCM mutexes;
|
||
scm_i_pthread_mutex_t *held_mutex;
|
||
|
||
SCM result;
|
||
int canceled;
|
||
int exited;
|
||
|
||
SCM sleep_object;
|
||
scm_i_pthread_mutex_t *sleep_mutex;
|
||
scm_i_pthread_cond_t sleep_cond;
|
||
int sleep_fd, sleep_pipe[2];
|
||
|
||
/* This mutex represents this threads right to access the heap.
|
||
That right can temporarily be taken away by the GC.
|
||
*/
|
||
scm_i_pthread_mutex_t heap_mutex;
|
||
|
||
/* The freelists of this thread. Each thread has its own lists so
|
||
that they can all allocate concurrently.
|
||
*/
|
||
SCM freelist, freelist2;
|
||
int clear_freelists_p; /* set if GC was done while thread was asleep */
|
||
int gc_running_p; /* non-zero while this thread does GC or a
|
||
sweep. */
|
||
|
||
/* Other thread local things.
|
||
*/
|
||
SCM dynamic_state;
|
||
scm_t_debug_frame *last_debug_frame;
|
||
SCM dynwinds;
|
||
|
||
/* For system asyncs.
|
||
*/
|
||
SCM active_asyncs; /* The thunks to be run at the next
|
||
safe point */
|
||
unsigned int block_asyncs; /* Non-zero means that asyncs should
|
||
not be run. */
|
||
unsigned int pending_asyncs; /* Non-zero means that asyncs might be pending.
|
||
*/
|
||
|
||
/* The current continuation root and the stack base for it.
|
||
|
||
The continuation root is an arbitrary but unique object that
|
||
identifies a dynamic extent. Continuations created during that
|
||
extent can also only be invoked during it.
|
||
|
||
We use pairs where the car is the thread handle and the cdr links
|
||
to the previous pair. This might be used for better error
|
||
messages but is not essential for identifying continuation roots.
|
||
|
||
The continuation base is the far end of the stack upto which it
|
||
needs to be copied.
|
||
*/
|
||
SCM continuation_root;
|
||
SCM_STACKITEM *continuation_base;
|
||
|
||
/* For keeping track of the stack and registers. */
|
||
SCM vm;
|
||
SCM_STACKITEM *base;
|
||
SCM_STACKITEM *top;
|
||
jmp_buf regs;
|
||
#ifdef __ia64__
|
||
void *register_backing_store_base;
|
||
scm_t_contregs *pending_rbs_continuation;
|
||
#endif
|
||
|
||
} scm_i_thread;
|
||
|
||
#define SCM_I_IS_THREAD(x) SCM_SMOB_PREDICATE (scm_tc16_thread, x)
|
||
#define SCM_I_THREAD_DATA(x) ((scm_i_thread *) SCM_SMOB_DATA (x))
|
||
|
||
#define SCM_VALIDATE_THREAD(pos, a) \
|
||
scm_assert_smob_type (scm_tc16_thread, (a))
|
||
#define SCM_VALIDATE_MUTEX(pos, a) \
|
||
scm_assert_smob_type (scm_tc16_mutex, (a))
|
||
#define SCM_VALIDATE_CONDVAR(pos, a) \
|
||
scm_assert_smob_type (scm_tc16_condvar, (a))
|
||
|
||
SCM_API SCM scm_spawn_thread (scm_t_catch_body body, void *body_data,
|
||
scm_t_catch_handler handler, void *handler_data);
|
||
|
||
SCM_API void *scm_without_guile (void *(*func)(void *), void *data);
|
||
SCM_API void *scm_with_guile (void *(*func)(void *), void *data);
|
||
|
||
SCM_INTERNAL void *scm_i_with_guile_and_parent (void *(*func)(void *),
|
||
void *data, SCM parent);
|
||
|
||
|
||
extern int scm_i_thread_go_to_sleep;
|
||
|
||
SCM_INTERNAL void scm_i_thread_put_to_sleep (void);
|
||
SCM_INTERNAL void scm_i_thread_wake_up (void);
|
||
SCM_INTERNAL void scm_i_thread_invalidate_freelists (void);
|
||
void scm_i_thread_sleep_for_gc (void);
|
||
|
||
SCM_INTERNAL void scm_threads_prehistory (SCM_STACKITEM *);
|
||
SCM_INTERNAL void scm_threads_init_first_thread (void);
|
||
SCM_INTERNAL void scm_threads_mark_stacks (void);
|
||
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);
|
||
|
||
|
||
#define SCM_THREAD_SWITCHING_CODE \
|
||
do { \
|
||
if (scm_i_thread_go_to_sleep) \
|
||
scm_i_thread_sleep_for_gc (); \
|
||
} while (0)
|
||
|
||
SCM_API SCM scm_call_with_new_thread (SCM thunk, SCM handler);
|
||
SCM_API SCM scm_yield (void);
|
||
SCM_API SCM scm_cancel_thread (SCM t);
|
||
SCM_API SCM scm_set_thread_cleanup_x (SCM thread, SCM proc);
|
||
SCM_API SCM scm_thread_cleanup (SCM thread);
|
||
SCM_API SCM scm_join_thread (SCM t);
|
||
SCM_API SCM scm_join_thread_timed (SCM t, SCM timeout, SCM timeoutval);
|
||
SCM_API SCM scm_thread_p (SCM t);
|
||
|
||
SCM_API SCM scm_make_mutex (void);
|
||
SCM_API SCM scm_make_recursive_mutex (void);
|
||
SCM_API SCM scm_make_mutex_with_flags (SCM flags);
|
||
SCM_API SCM scm_lock_mutex (SCM m);
|
||
SCM_API SCM scm_lock_mutex_timed (SCM m, SCM timeout, SCM owner);
|
||
SCM_API void scm_dynwind_lock_mutex (SCM mutex);
|
||
SCM_API SCM scm_try_mutex (SCM m);
|
||
SCM_API SCM scm_unlock_mutex (SCM m);
|
||
SCM_API SCM scm_unlock_mutex_timed (SCM m, SCM cond, SCM timeout);
|
||
SCM_API SCM scm_mutex_p (SCM o);
|
||
SCM_API SCM scm_mutex_locked_p (SCM m);
|
||
SCM_API SCM scm_mutex_owner (SCM m);
|
||
SCM_API SCM scm_mutex_level (SCM m);
|
||
|
||
SCM_API SCM scm_make_condition_variable (void);
|
||
SCM_API SCM scm_wait_condition_variable (SCM cond, SCM mutex);
|
||
SCM_API SCM scm_timed_wait_condition_variable (SCM cond, SCM mutex,
|
||
SCM abstime);
|
||
SCM_API SCM scm_signal_condition_variable (SCM cond);
|
||
SCM_API SCM scm_broadcast_condition_variable (SCM cond);
|
||
SCM_API SCM scm_condition_variable_p (SCM o);
|
||
|
||
SCM_API SCM scm_current_thread (void);
|
||
SCM_API SCM scm_all_threads (void);
|
||
|
||
SCM_API int scm_c_thread_exited_p (SCM thread);
|
||
SCM_API SCM scm_thread_exited_p (SCM thread);
|
||
|
||
SCM_API void scm_dynwind_critical_section (SCM mutex);
|
||
|
||
#define SCM_I_CURRENT_THREAD \
|
||
((scm_i_thread *) scm_i_pthread_getspecific (scm_i_thread_key))
|
||
SCM_API scm_i_pthread_key_t scm_i_thread_key;
|
||
|
||
#define scm_i_dynwinds() (SCM_I_CURRENT_THREAD->dynwinds)
|
||
#define scm_i_set_dynwinds(w) (SCM_I_CURRENT_THREAD->dynwinds = (w))
|
||
#define scm_i_last_debug_frame() (SCM_I_CURRENT_THREAD->last_debug_frame)
|
||
#define scm_i_set_last_debug_frame(f) \
|
||
(SCM_I_CURRENT_THREAD->last_debug_frame = (f))
|
||
|
||
SCM_INTERNAL scm_i_pthread_mutex_t scm_i_misc_mutex;
|
||
|
||
/* Convenience functions for working with the pthread API in guile
|
||
mode.
|
||
*/
|
||
|
||
#if SCM_USE_PTHREAD_THREADS
|
||
SCM_API int scm_pthread_mutex_lock (pthread_mutex_t *mutex);
|
||
SCM_API void scm_dynwind_pthread_mutex_lock (pthread_mutex_t *mutex);
|
||
SCM_API int scm_pthread_cond_wait (pthread_cond_t *cond,
|
||
pthread_mutex_t *mutex);
|
||
SCM_API int scm_pthread_cond_timedwait (pthread_cond_t *cond,
|
||
pthread_mutex_t *mutex,
|
||
const struct timespec *abstime);
|
||
#endif
|
||
|
||
/* More convenience functions.
|
||
*/
|
||
|
||
SCM_API unsigned int scm_std_sleep (unsigned int);
|
||
SCM_API unsigned long scm_std_usleep (unsigned long);
|
||
|
||
#endif /* SCM_THREADS_H */
|
||
|
||
/*
|
||
Local Variables:
|
||
c-file-style: "gnu"
|
||
End:
|
||
*/
|