unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: Mark H Weaver <mhw@netris.org>
To: guile-devel@gnu.org
Subject: Concurrent MVars for Guile
Date: Mon, 02 Sep 2013 03:55:07 -0400	[thread overview]
Message-ID: <87fvtn1wv8.fsf@tines.lan> (raw)

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

Hello all,

I've attached a preliminary implementation of MVars à la Concurrent
Haskell for Guile.  I made a few small changes to the API to better
match Scheme conventions, and added atomicity guarantees for 'read-mvar'
and 'swap-mvar'.  I also added the interface 'try-read-mvar'.

Note that the fairness of this implementation relies upon the fairness
of Guile's mutexes and condition variables, which I have not checked.

Comments and suggestions welcome.

I was thinking that maybe we should add something like this to core
Guile.  What do you think?

     Mark



[-- Attachment #2: MVars for Guile --]
[-- Type: text/plain, Size: 5368 bytes --]

(define-module (ice-9 mvars)
  #:use-module (srfi srfi-8)     ; receive
  #:use-module (srfi srfi-9)     ; records
  #:use-module (srfi srfi-9 gnu)
  #:use-module (ice-9 threads)
  #:export (new-mvar mvar? mvar-empty?
                     take-mvar put-mvar read-mvar swap-mvar
                     try-take-mvar try-put-mvar try-read-mvar
                     with-mvar modify-mvar modify-mvar*))

(define-record-type <mvar>
  (make-mvar contents empty? mutex condvar)
  mvar?
  (contents mvar-contents set-mvar-contents!)
  (empty? mvar-empty? set-mvar-empty?!)
  (mutex mvar-mutex)
  (condvar mvar-condvar))

(set-record-type-printer!
 <mvar>
 (lambda (mvar port)
   (display "#<mvar " port)
   (display (number->string (object-address mvar) 16) port)
   (display " " port)
   (write (receive (x full?) (try-read-mvar mvar)
            (if full? (list x) '()))
          port)
   (display ">" port)))

(define new-mvar
  (case-lambda
    "Return a freshly allocated mvar.  The optional argument, if provided,\n\
specifies the initial contents of the mvar, otherwise it will be empty."
    (()  (make-mvar #f #t (make-mutex) (make-condition-variable)))
    ((x) (make-mvar x  #f (make-mutex) (make-condition-variable)))))

(define (take-mvar mvar)
  "Block until MVAR is full, then atomically remove and return its contents."
  (with-mutex (mvar-mutex mvar)
    (when (mvar-empty? mvar)
      (wait-condition-variable (mvar-condvar mvar) (mvar-mutex mvar))
      (when (mvar-empty? mvar)
        (error "take-mvar: expected full mvar after waiting")))
    (let ((x (mvar-contents mvar)))
      (set-mvar-contents! mvar #f)
      (set-mvar-empty?! mvar #t)
      (signal-condition-variable (mvar-condvar mvar))
      x)))

(define (put-mvar mvar x)
  "Block until MVAR is empty, then put X into it."
  (with-mutex (mvar-mutex mvar)
    (unless (mvar-empty? mvar)
      (wait-condition-variable (mvar-condvar mvar) (mvar-mutex mvar))
      (unless (mvar-empty? mvar)
        (error "put-mvar: expected empty mvar after waiting")))
    (set-mvar-contents! mvar x)
    (set-mvar-empty?! mvar #f)
    (signal-condition-variable (mvar-condvar mvar))
    *unspecified*))

(define (read-mvar mvar)
  "Block until MVAR is full, then return its contents, leaving MVAR unchanged."
  (with-mutex (mvar-mutex mvar)
    (when (mvar-empty? mvar)
      (wait-condition-variable (mvar-condvar mvar) (mvar-mutex mvar))
      (when (mvar-empty? mvar)
        (error "read-mvar: expected full mvar after waiting")))
    (mvar-contents mvar)))

(define (swap-mvar mvar new)
  "Block until MVAR is full, and then atomically swap its contents\n\
with NEW and return the previous contents."
  (with-mutex (mvar-mutex mvar)
    (when (mvar-empty? mvar)
      (wait-condition-variable (mvar-condvar mvar) (mvar-mutex mvar))
      (when (mvar-empty? mvar)
        (error "swap-mvar: expected full mvar after waiting")))
    (let ((old (mvar-contents mvar)))
      (set-mvar-contents! mvar new)
      old)))

(define (try-take-mvar mvar)
  "If MVAR is full, return its contents and #t, else return #f and #f."
  (with-mutex (mvar-mutex mvar)
    (if (mvar-empty? mvar)
        (values #f #f)
        (let ((x (mvar-contents mvar)))
          (set-mvar-contents! mvar #f)
          (set-mvar-empty?! mvar #t)
          (signal-condition-variable (mvar-condvar mvar))
          (values x #t)))))

(define (try-put-mvar mvar x)
  "If MVAR is empty, put X into it and return #t, else return #f."
  (with-mutex (mvar-mutex mvar)
    (and (mvar-empty? mvar)
         (begin
           (set-mvar-contents! mvar x)
           (set-mvar-empty?! mvar #f)
           (signal-condition-variable (mvar-condvar mvar))
           #t))))

(define (try-read-mvar mvar)
  "If MVAR is full, return its contents and #t, else return #f and #f."
  (with-mutex (mvar-mutex mvar)
    (if (mvar-empty? mvar)
        (values #f #f)
        (values (mvar-contents mvar) #t))))

(define (with-mvar mvar proc)
  "Take a value from MVAR and apply PROC to it.  If an exception is raised,\n\
the original value is put back into MVAR.  This procedure is atomic only if\n\
there are no other producers for MVAR."
  (let ((x (take-mvar mvar)))
    (catch #t
      (lambda () (proc x))
      (lambda (key . args)
        (put-mvar mvar x)
        (apply throw key args)))))

(define (modify-mvar mvar f)
  "Take a value x from MVAR, and then put back (F x).  If an exception is\n\
raised, the original value is put back into MVAR.  This procedure is\n\
atomic only if there are no other producers for MVAR."
  (let ((old (take-mvar mvar)))
    (catch #t
      (lambda () (put-mvar mvar (f old)))
      (lambda (key . args)
        (put-mvar mvar old)
        (apply throw key args)))))

(define (modify-mvar* mvar f)
  "Take a value x from MVAR, and apply F to it.  (F x) should return one\n\
or more values: the new value to be put back into MVAR, and zero or more\n\
additional values to be returned from MODIFY-MVAR*.  If an exception is\n\
raised, the original value is put back into MVAR.  This procedure is\n\
atomic only if there are no other producers for MVAR."
  (let ((old (take-mvar mvar)))
    (catch #t
      (lambda ()
        (receive (new . results) (f old)
          (put-mvar mvar new)
          (apply values results)))
      (lambda (key . args)
        (put-mvar mvar old)
        (apply throw key args)))))

             reply	other threads:[~2013-09-02  7:55 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-09-02  7:55 Mark H Weaver [this message]
2013-09-03 11:55 ` Concurrent MVars for Guile Mark H Weaver
     [not found]   ` <CAGua6m3o_NexANbGuDYd5_rbPQqsbXkK504ONaFE7PGdQd98og@mail.gmail.com>
2013-09-04  6:27     ` Fwd: " Stefan Israelsson Tampe
2013-09-05  2:10 ` David Thompson
2013-09-05  2:53   ` Mark H Weaver
2013-09-14 13:59 ` Ludovic Courtès
2013-09-17 20:18   ` Andy Wingo
2014-01-16  0:47     ` Mark H Weaver
2014-01-16 13:01       ` Ludovic Courtès
2014-01-17  6:33 ` Mateusz Kowalczyk
2014-01-17 19:31   ` Mark H Weaver
2014-01-18  0:46     ` Mark H Weaver
2014-01-18  1:05     ` Mateusz Kowalczyk
2014-01-21  5:38       ` Mark H Weaver

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=87fvtn1wv8.fsf@tines.lan \
    --to=mhw@netris.org \
    --cc=guile-devel@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).