unofficial mirror of bug-guile@gnu.org 
 help / color / mirror / Atom feed
From: taylanbayirli@gmail.com (Taylan Ulrich Bayırlı/Kammer)
To: 21887@debbugs.gnu.org
Subject: bug#21887: 'monitor' form broken
Date: Thu, 12 Nov 2015 16:29:30 +0100	[thread overview]
Message-ID: <87vb97s0bp.fsf@T420.taylan> (raw)

It seems that the 'monitor' form is currently a no-op.  The form

    (par-for-each (lambda (x)
                    (monitor
                      (foo)))
                  xs)

should be functionally equivalent to

    (let ((mutex (make-mutex)))
      (par-for-each (lambda (x)
                      (with-mutex mutex
                        (foo)))
                    xs))

but currently becomes

    (par-for-each (lambda (x)
                    (let ((mutex (make-mutex)))
                      (with-mutex mutex
                        (foo))))
                  xs)

which is ineffective.

I don't know what's the best way to fix this.  The simplest thing that
comes to my mind is something along the lines of:

    (define-syntax monitor
      (lambda (stx)
        (syntax-case stx ()
          ((_ body body* ...)
           (let ((uuid (generate-uuid)))
             #`(with-mutex (mutex-with-uuid #,uuid)
                 body body* ...))))))

where mutex-with-uuid looks it up from a hash table at run-time and
instantiates it when it doesn't exist, this operation also being
synchronized across threads, like:

    (define mutex-table (make-hash-table))

    (define mutex-table-mutex (make-mutex))

    (define (mutex-with-uuid uuid)
      (with-mutex mutex-table-mutex
        (or (hash-ref mutex-table uuid)
            (let ((mutex (make-mutex)))
              (hash-set! mutex-table uuid mutex)
              mutex))))

If that looks OK, I can try to make a proper patch from it.  I'm not
sure what I'd use in place of `generate-uuid' though.  Would `gensym' be
good enough?


Shameless advertisement: with SRFI-126, the (or (hash-ref ...) ...) bit
would have been just:

    (hashtable-intern! mutex-table uuid make-mutex)

It's borrowed from MIT/GNU Scheme.  Seems pretty useful.

Taylan





             reply	other threads:[~2015-11-12 15:29 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-12 15:29 Taylan Ulrich Bayırlı/Kammer [this message]
2016-06-24 16:04 ` bug#21887: 'monitor' form broken Andy Wingo
2016-06-25 14:51   ` Taylan Ulrich Bayırlı/Kammer
2016-06-27  7:33     ` Andy Wingo
2016-06-27  7:33     ` Andy Wingo

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=87vb97s0bp.fsf@T420.taylan \
    --to=taylanbayirli@gmail.com \
    --cc=21887@debbugs.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).