[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: srfi-18 requirements
From: |
Julian Graham |
Subject: |
Re: srfi-18 requirements |
Date: |
Sat, 19 Jan 2008 15:10:48 -0500 |
Hi Neil,
> OK. While looking through the docs, though, and playing with possible
> solutions, I noted a couple of other pitfalls (which the current code
> also appears to suffer from).
>
> 1. pthread_cond_wait() returning does not necessarily mean that the
> cond var was signalled. Apparently pthread_cond_wait() can return
> early because of an interrupt.
Yes, the pthreads docs refer to this as a "spurious wakeup."
> 2. If two threads are using pthread_cond_wait and pthread_cond_signal
> to communicate, and using the cond_var itself as a state
> indication, they have to be certain that the pthread_cond_wait
> starts before the pthread_cond_signal, otherwise it won't work.
Right -- holding the right mutexes when you signal / broadcast is
pretty important.
> The practical impact of these is that one shouldn't use the cond_var
> itself as an indication of "reached so-and-so state". Instead, one
> can represent the state using an explicit variable, which is protected
> by the associated mutex, and then interpret the cond_var as indicating
> simply that the variable _might_ have changed.
>
> In our case, I think the state variable could be
> scm_i_thread_go_to_sleep, protected by thread_admin_mutex. Here's a
> possible solution based on this, but it isn't yet complete, because it
> doesn't explain how num_guile_threads_awake is calculated. (And I
> have to go to bed!)
I've come up with something similar that seems to work decently and
seems a bit simple. See what you think (apologies for the
formatting):
static scm_i_pthread_cond_t wake_up_cond;
static scm_i_pthread_mutex_t wake_up_mutex;
static int wake_up_flag = 0;
int scm_i_thread_go_to_sleep;
void
scm_i_thread_put_to_sleep ()
{
if (threads_initialized_p)
{
scm_i_thread *t;
scm_leave_guile ();
scm_i_pthread_mutex_lock (&thread_admin_mutex);
wake_up_flag = 0;
scm_i_thread_go_to_sleep = 1;
for (t = all_threads; t; t = t->next_thread)
{
scm_i_pthread_mutex_lock (&t->heap_mutex);
}
scm_i_thread_go_to_sleep = 0;
}
}
void
scm_i_thread_wake_up ()
{
if (threads_initialized_p)
{
scm_i_thread *t;
scm_i_pthread_mutex_lock (&wake_up_mutex);
wake_up_flag = 1;
scm_i_pthread_cond_broadcast (&wake_up_cond);
scm_i_pthread_mutex_unlock (&wake_up_mutex);
for (t = all_threads; t; t = t->next_thread)
{
scm_i_pthread_mutex_unlock (&t->heap_mutex);
}
scm_i_pthread_mutex_unlock (&thread_admin_mutex);
scm_enter_guile ((scm_t_guile_ticket) SCM_I_CURRENT_THREAD);
}
}
void
scm_i_thread_sleep_for_gc ()
{
scm_i_thread *t = suspend ();
scm_i_pthread_cleanup_push ((void (*)(void *)) scm_i_pthread_mutex_unlock,
&wake_up_mutex);
scm_i_pthread_mutex_lock (&wake_up_mutex);
scm_i_pthread_mutex_unlock (&t->heap_mutex);
do
{
scm_i_pthread_cond_wait (&wake_up_cond, &wake_up_mutex);
}
while (!wake_up_flag);
scm_i_pthread_mutex_lock (&t->heap_mutex);
scm_i_pthread_mutex_unlock (&wake_up_mutex);
scm_i_pthread_cleanup_pop (0);
resume (t);
}
> > So why hasn't this been reported before? I'm not really sure, except
> > that based on my logs, a GC involving more than two threads (one
> > thread stays awake, of course, to manage the collection) is kind of
> > rare. It doesn't even necessarily happen during an entire run of my
> > SRFI-18 test suite, which lasts for several seconds and is fairly
> > multi-threaded.
>
> Not sure what you mean here. Surely if there are >2 threads, they all
> have to go to sleep before GC can proceed?
Of course -- all I meant by this was that in the existing thread tests
(and in much of the SRFI-18 test code I wrote) the lifespans of
threads besides the main thread (and the signal delivery thread) are
usually short enough that they don't end up participating in this
whole co-op GC process. Maybe we need some test code for
longer-running, guile-mode threads. (Perhaps developers with
multi-threaded Guile application development under their belts would
care to chime in here?)
> > It *is* possible, because a thread can enter and leave guile mode and
> > do a fair number of things without SCM_TICK getting called. I don't
> > know if that's significant or not.
>
> That may mean that we need some more SCM_TICK calls. What kind of
> processing was the thread doing?
I'm not totally sure -- I'll have to add some more logs and get back
to you. I think are definitely some places where an extra SCM_TICK
might do some good (in fat_cond_timedwait, e.g.).
Regards,
Julian
- Re: srfi-18 requirements, Neil Jerram, 2008/01/01
- Re: srfi-18 requirements, Julian Graham, 2008/01/04
- Re: srfi-18 requirements, Neil Jerram, 2008/01/04
- Re: srfi-18 requirements, Julian Graham, 2008/01/06
- Re: srfi-18 requirements, Neil Jerram, 2008/01/08
- Re: srfi-18 requirements, Julian Graham, 2008/01/10
- Re: srfi-18 requirements, Neil Jerram, 2008/01/16
- Re: srfi-18 requirements,
Julian Graham <=
- Re: srfi-18 requirements, Neil Jerram, 2008/01/23
- Re: srfi-18 requirements, Julian Graham, 2008/01/23
- Re: srfi-18 requirements, Neil Jerram, 2008/01/24
- Re: srfi-18 requirements, Julian Graham, 2008/01/24
- Re: srfi-18 requirements, Julian Graham, 2008/01/27
- Re: srfi-18 requirements, Neil Jerram, 2008/01/08