unofficial mirror of bug-guile@gnu.org 
 help / color / mirror / Atom feed
From: taylanbayirli@gmail.com (Taylan Ulrich Bayırlı/Kammer)
To: Andy Wingo <wingo@pobox.com>
Cc: 21887@debbugs.gnu.org
Subject: bug#21887: 'monitor' form broken
Date: Sat, 25 Jun 2016 16:51:26 +0200	[thread overview]
Message-ID: <87wpldgw41.fsf@T420.taylan> (raw)
In-Reply-To: <87inwyefpl.fsf@pobox.com> (Andy Wingo's message of "Fri, 24 Jun 2016 18:04:06 +0200")

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


  reply	other threads:[~2016-06-25 14:51 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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=87wpldgw41.fsf@T420.taylan \
    --to=taylanbayirli@gmail.com \
    --cc=21887@debbugs.gnu.org \
    --cc=wingo@pobox.com \
    /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).