unofficial mirror of bug-guile@gnu.org 
 help / color / mirror / Atom feed
* bug#21887: 'monitor' form broken
@ 2015-11-12 15:29 Taylan Ulrich Bayırlı/Kammer
  2016-06-24 16:04 ` Andy Wingo
  0 siblings, 1 reply; 5+ messages in thread
From: Taylan Ulrich Bayırlı/Kammer @ 2015-11-12 15:29 UTC (permalink / raw)
  To: 21887

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





^ permalink raw reply	[flat|nested] 5+ messages in thread

* bug#21887: 'monitor' form broken
  2015-11-12 15:29 bug#21887: 'monitor' form broken Taylan Ulrich Bayırlı/Kammer
@ 2016-06-24 16:04 ` Andy Wingo
  2016-06-25 14:51   ` Taylan Ulrich Bayırlı/Kammer
  0 siblings, 1 reply; 5+ messages in thread
From: Andy Wingo @ 2016-06-24 16:04 UTC (permalink / raw)
  To: Taylan Ulrich "Bayırlı/Kammer"; +Cc: 21887

Hi Taylan,

On Thu 12 Nov 2015 16:29, taylanbayirli@gmail.com (Taylan Ulrich "Bayırlı/Kammer") writes:

> 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?

You're totally right on all points.  Please do prepare a patch :)  I
wish we could do something faster for the "embedded" mutex but
correctness should come first.

Cheers,

Andy





^ permalink raw reply	[flat|nested] 5+ messages in thread

* bug#21887: 'monitor' form broken
  2016-06-24 16:04 ` 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
  0 siblings, 2 replies; 5+ messages in thread
From: Taylan Ulrich Bayırlı/Kammer @ 2016-06-25 14:51 UTC (permalink / raw)
  To: Andy Wingo; +Cc: 21887

[-- Attachment #1: Type: text/plain, Size: 387 bytes --]

Here's a patch, tested minimally by running

    (par-for-each (lambda (x)
                    (monitor
                      (sleep 1)
                      (display "foo\n")))
                  (iota 10))

on a quad-core.  Previously it would print the "foo"s in groups of four
with a second between each group; now it prints them one by one with a
second between each, as should be.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Fix-monitor-macro.patch --]
[-- Type: text/x-diff, Size: 1471 bytes --]

From 08c7f4cd98c86fbb6551c7c0b6f17262c67e7b23 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Taylan=20Ulrich=20Bay=C4=B1rl=C4=B1/Kammer?=
 <taylanbayirli@gmail.com>
Date: Sat, 25 Jun 2016 16:43:36 +0200
Subject: [PATCH] Fix 'monitor' macro.

* module/ice-9/threads.scm (monitor-mutex-table)
(monitor-mutex-table-mutex, monitor-mutex-with-id): New variables.
(monitor): Fix it.
---
 module/ice-9/threads.scm | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)

diff --git a/module/ice-9/threads.scm b/module/ice-9/threads.scm
index 9f9e1bf..14da113 100644
--- a/module/ice-9/threads.scm
+++ b/module/ice-9/threads.scm
@@ -85,9 +85,24 @@
       (lambda () (begin e0 e1 ...))
       (lambda () (unlock-mutex x)))))
 
-(define-syntax-rule (monitor first rest ...)
-  (with-mutex (make-mutex)
-    first rest ...))
+(define monitor-mutex-table (make-hash-table))
+
+(define monitor-mutex-table-mutex (make-mutex))
+
+(define (monitor-mutex-with-id id)
+  (with-mutex monitor-mutex-table-mutex
+    (or (hashq-ref monitor-mutex-table id)
+        (let ((mutex (make-mutex)))
+          (hashq-set! monitor-mutex-table id mutex)
+          mutex))))
+
+(define-syntax monitor
+  (lambda (stx)
+    (syntax-case stx ()
+      ((_ body body* ...)
+       (let ((id (datum->syntax #'body (gensym))))
+         #`(with-mutex (monitor-mutex-with-id '#,id)
+             body body* ...))))))
 
 (define (par-mapper mapper cons)
   (lambda (proc . lists)
-- 
2.8.4


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* bug#21887: 'monitor' form broken
  2016-06-25 14:51   ` Taylan Ulrich Bayırlı/Kammer
@ 2016-06-27  7:33     ` Andy Wingo
  2016-06-27  7:33     ` Andy Wingo
  1 sibling, 0 replies; 5+ messages in thread
From: Andy Wingo @ 2016-06-27  7:33 UTC (permalink / raw)
  To: Taylan Ulrich "Bayırlı/Kammer"; +Cc: 21887

On Sat 25 Jun 2016 16:51, taylanbayirli@gmail.com (Taylan Ulrich "Bayırlı/Kammer") writes:

> Here's a patch, tested minimally by running
>
>     (par-for-each (lambda (x)
>                     (monitor
>                       (sleep 1)
>                       (display "foo\n")))
>                   (iota 10))
>
> on a quad-core.  Previously it would print the "foo"s in groups of four
> with a second between each group; now it prints them one by one with a
> second between each, as should be.

Applied, thanks!

Andy





^ permalink raw reply	[flat|nested] 5+ messages in thread

* bug#21887: 'monitor' form broken
  2016-06-25 14:51   ` Taylan Ulrich Bayırlı/Kammer
  2016-06-27  7:33     ` Andy Wingo
@ 2016-06-27  7:33     ` Andy Wingo
  1 sibling, 0 replies; 5+ messages in thread
From: Andy Wingo @ 2016-06-27  7:33 UTC (permalink / raw)
  To: 21887-done

thanks





^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2016-06-27  7:33 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-11-12 15:29 bug#21887: 'monitor' form broken Taylan Ulrich Bayırlı/Kammer
2016-06-24 16:04 ` 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

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).