unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: Neil Jerram <neil@ossau.uklinux.net>
To: "Julian Graham" <joolean@gmail.com>
Cc: "Ludovic Courtès" <ludo@gnu.org>, guile-devel@gnu.org
Subject: Re: srfi-18 requirements
Date: Fri, 22 Feb 2008 00:33:30 +0000	[thread overview]
Message-ID: <87r6f5zv6t.fsf@ossau.uklinux.net> (raw)
In-Reply-To: <2bc5f8210802191810v729d8fa5jec070d3ee4358493@mail.gmail.com> (Julian Graham's message of "Tue, 19 Feb 2008 21:10:10 -0500")

"Julian Graham" <joolean@gmail.com> writes:

>> >  @c begin (texi-doc-string "guile" "join-thread")
>> > -@deffn {Scheme Procedure} join-thread thread
>> > +@deffn {Scheme Procedure} join-thread thread [timeout]
>> >  @deffnx {C Function} scm_join_thread (thread)
>> > +@deffnx {C Function} scm_join_thread_timed (thread, timeout)
>>
>> Didn't we agree to add a timeout-val parameter here?
>
> No, we didn't, although I agree such a parameter would be pretty
> useful.

Well we discussed it a bit here:
http://lists.gnu.org/archive/html/guile-devel/2008-02/msg00004.html
http://lists.gnu.org/archive/html/guile-devel/2008-02/msg00005.html

>  I'll add that in the next revision I send you.

Cool, thanks.

>> > +       else if (!first_iteration)
>> > +         {
>> > +           if (timeout != NULL)
>> > +             {
>> > +               gettimeofday (&current_time, NULL);
>> > +               if (current_time.tv_sec > timeout->tv_sec ||
>> > +                   (current_time.tv_sec == timeout->tv_sec &&
>> > +                    current_time.tv_usec * 1000 > timeout->tv_nsec))
>> > +                 {
>> > +                   *ret = 0;
>> > +                   break;
>> > +                 }
>>
>> Is timeout an absolute time, or relative to when join-thread was
>> called?  Before getting to this code, I thought it was relative - but
>> then I don't see how the code above can be correct, because it is
>> comparing against the absolute gettimeofday() ...?
>
> It's absolute -- like the arguments for the existing timed
> synchronization primitives

OK, yes, I see now.  The code is fine as it stands, then.

> (and like the timed parts of the SRFI-18 API).  (Unless I'm
> mistaken...)

But that's not completely right.  SRFI-18 says that timeout-val can be
one of the following:

    * a time object represents an absolute point in time
    * an exact or inexact real number represents a relative time in seconds from the moment the primitive was called
    * #f means that there is no timeout 

So for the SRFI-18 API, timeout-val is sometimes absolute and
sometimes relative!  I guess that just means that the SRFI-18 Scheme
code will have to add (current-time), when an integer or float is
given to it.

>
>> > -static char *
>> > -fat_mutex_unlock (fat_mutex *m)
>> > +static void
>> > +fat_mutex_unlock (SCM mx)
>> >  {
>> > -  char *msg = NULL;
>> > -
>> > +  fat_mutex *m = SCM_MUTEX_DATA (mx);
>> >    scm_i_scm_pthread_mutex_lock (&m->lock);
>> > -  if (!scm_is_eq (m->owner, scm_current_thread ()))
>> > +  if (m->level > 0)
>> > +    m->level--;
>> > +  else
>>
>> It looks like there is a significant change to the semantics here: any
>> thread can unlock a mutex, not just the thread that locked it.  Is
>> that the intention, or am I misunderstanding?
>
> No, that's the intention (it's explicitly permitted by SRFI-18).  I
> thought you were okay with that, since it was not on your list of
> stuff that didn't belong in C.  If that's too big of a change, might I
> suggest we add a function that forcibly unlocks a mutex, regardless of
> the owner?

Sorry for missing this before.  The SRFI-18 semantics are really
interesting, but I think we need to preserve the existing semantics
too for back-compatibility.  i.e. we need to preserve the two
conditions described by this existing code:

  if (!scm_is_eq (m->owner, scm_current_thread ()))
    {
      if (scm_is_false (m->owner))
	msg = "mutex not locked";
      else
	msg = "mutex not locked by current thread";
    }

I guess that means that scm_unlock_mutex_timed will need to take
another optional parameter (or two) indicating whether

- it is an error to unlock an unlocked mutex (default yes, but SRFI-18
  will pass "no")

- it is an error to unlock a mutex owned by another thread (default
  yes, SRFI-18 will pass "no").

Can you propose a representation for this?

>> Actually, that strongly says to me that we don't need the `cond' part
>> of this API to be implemented in C.  Can we move that to the SRFI-18
>> Scheme code, and leave the C API as a plain unlock-mutex operation?
>
> Fine by me (again. left this one in because you didn't squawk about it
> earlier), except that it might be harder to guarantee the safety of
> mixing the mutex and cond passed to the SRFI-18 Scheme implementation
> with non-SRFI-18 calls -- C generally provides a convenient protection
> against deadlock for things like that.

I'm not sure about that argument, but I think it's moot anyway -
because I think the current implementation, which equates to

  (begin
    (wait-condition-variable cond-var mutex)
    (unlock-mutex mutex))

does not always behave as SRFI-18 says.  Specifically, if there is
another thread trying to lock `mutex', `(wait-condition-variable
cond-var mutex)' may block, after the cond-var has been signalled,
because it is not able to reacquire the mutex.  Whereas SRFI-18 says
that the thread that calls mutex-unlock! "can unblock at any time, but
no later than when an appropriate call to condition-variable-signal!
or condition-variable-broadcast! is performed (see below), and no
later than the timeout (if timeout is supplied)".

Given the definitions of `wait-condition-variable' and SRFI-18's
`mutex-unlock!', and that we want Guile to provide both of these, it
seems to me now that `mutex-unlock!' is actually the more primitive
operation, and that `wait-condition-variable' could be written as

    scm_unlock_mutex_timed (mx, cv, 0);
    scm_lock_mutex (mx;)

Is it possible to reorganize the relevant code a bit, so that
scm_unlock_mutex_timed (mx, cv, 0) does not lock and immediately
unlock the mutex after the cond var has been signalled?

Regards,
     Neil





  reply	other threads:[~2008-02-22  0:33 UTC|newest]

Thread overview: 75+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-10-11  1:54 srfi-18 requirements Julian Graham
2007-10-12  8:42 ` Ludovic Courtès
2007-10-12 15:31   ` Julian Graham
2007-10-15 22:26     ` Julian Graham
2007-10-15 22:35       ` Stephen Compall
2007-10-15 22:47         ` Julian Graham
2007-10-29 14:37           ` Julian Graham
2007-11-26 18:11             ` Julian Graham
2007-11-27  9:14               ` Ludovic Courtès
2007-11-28 18:23             ` Ludovic Courtès
2007-11-28 18:55               ` Julian Graham
2007-12-01  5:08               ` Julian Graham
2007-12-01 10:21                 ` Ludovic Courtès
2007-12-02  3:59                   ` Julian Graham
2007-12-04 22:20                     ` Neil Jerram
2007-12-04 22:29                 ` Neil Jerram
2007-12-11  4:20                   ` Julian Graham
2007-12-18  4:30               ` Julian Graham
2007-12-28 18:46                 ` Ludovic Courtès
2007-12-28 19:08                   ` Julian Graham
2007-12-28 22:35                     ` Neil Jerram
2007-12-30 11:04                 ` Neil Jerram
2007-12-30 20:38                   ` Julian Graham
2008-01-01 19:09                     ` Neil Jerram
2008-01-04  5:01                       ` Julian Graham
2008-01-05  0:30                         ` Neil Jerram
2008-01-06 21:41                           ` Julian Graham
2008-01-08 23:11                             ` Neil Jerram
2008-01-11  2:39                               ` Julian Graham
2008-01-17  1:48                                 ` Neil Jerram
2008-01-19 20:10                                   ` Julian Graham
2008-01-23 22:46                                     ` Neil Jerram
2008-01-23 23:23                                       ` Julian Graham
2008-01-25  1:07                                         ` Neil Jerram
2008-01-25  1:38                                           ` Julian Graham
2008-01-28  2:06                                             ` Julian Graham
2008-02-03  0:30                                               ` Neil Jerram
2008-02-05  6:27                                                 ` Julian Graham
2008-02-07  1:23                                                   ` Neil Jerram
2008-02-07  3:06                                                     ` Julian Graham
2008-02-07 23:26                                                       ` Neil Jerram
2008-02-07 23:33                                                         ` Julian Graham
2008-02-07 23:38                                                     ` Neil Jerram
2008-02-08  0:04                                                       ` Julian Graham
2008-02-11  5:14                                                         ` Julian Graham
2008-02-19 22:48                                                           ` Neil Jerram
2008-02-20  2:10                                                             ` Julian Graham
2008-02-22  0:33                                                               ` Neil Jerram [this message]
2008-02-22  4:14                                                                 ` Julian Graham
2008-02-24  9:41                                                                   ` Neil Jerram
2008-02-24 18:17                                                                     ` Julian Graham
2008-02-24 23:29                                                                       ` Neil Jerram
2008-03-01 19:56                                                                         ` Julian Graham
2008-03-08 16:34                                                                           ` Neil Jerram
2008-03-11  4:02                                                                             ` Julian Graham
2008-03-22 18:55                                                                               ` Julian Graham
2008-03-23 23:57                                                                                 ` Neil Jerram
2008-03-24 22:03                                                                               ` Neil Jerram
2008-03-26 15:55                                                                                 ` Julian Graham
2008-04-03  0:18                                                                                   ` Neil Jerram
2008-04-03 19:07                                                                                     ` Julian Graham
2008-04-09 21:29                                                                                       ` Neil Jerram
2008-04-14  0:43                                                                                         ` Julian Graham
2008-05-14  1:23                                                                                           ` Julian Graham
2008-05-14 21:13                                                                                             ` Neil Jerram
2008-05-14 23:11                                                                                           ` Neil Jerram
2008-05-15  5:05                                                                                             ` Julian Graham
2008-05-24 11:42                                                                                               ` Neil Jerram
2008-05-24 13:55                                                                                                 ` Neil Jerram
2008-05-25  2:07                                                                                                 ` Julian Graham
2008-05-31 21:41                                                                                                 ` Ludovic Courtès
2008-06-02  4:48                                                                                                   ` Julian Graham
2008-06-21  5:03                                                                                                     ` Julian Graham
2008-06-30 17:51                                                                                                       ` Ludovic Courtès
2008-01-08 23:41                             ` Neil Jerram

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87r6f5zv6t.fsf@ossau.uklinux.net \
    --to=neil@ossau.uklinux.net \
    --cc=guile-devel@gnu.org \
    --cc=joolean@gmail.com \
    --cc=ludo@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).