all messages for Guix-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: ludo@gnu.org (Ludovic Courtès)
To: "Clément Lassieur" <clement@lassieur.org>
Cc: Guix-devel <guix-devel@gnu.org>,
	Tatiana Sholokhova <tanja201396@gmail.com>
Subject: Re: packaging Bootstrap for Cuirass' web interface
Date: Fri, 27 Jul 2018 10:50:47 +0200	[thread overview]
Message-ID: <87k1phkqoo.fsf@gnu.org> (raw)
In-Reply-To: <877elhqwqf.fsf@lassieur.org> ("Clément Lassieur"'s message of "Fri, 27 Jul 2018 03:43:36 +0200")

Hi Clément!

Clément Lassieur <clement@lassieur.org> skribis:

> The bug is here:
>
> (define (make-critical-section . args)
>   "Return a channel used to implement a critical section.  That channel can
> then be passed to 'join-critical-section', which will ensure sequential
> ordering.  ARGS are the arguments of the critical section.
>
> Critical sections are implemented by passing the procedure to execute to a
> dedicated fiber."
>   (let ((channel (make-channel)))
>     (spawn-fiber
>      (lambda ()
>        (let loop ()
>          (match (get-message channel)
>            ((? procedure? proc)
>             (put-message channel (apply proc args))))
>          (loop))))
>     channel))
>
> (define (call-with-critical-section channel proc)
>   "Call PROC in the critical section corresponding to CHANNEL.  Return the
> result of PROC."
>   (put-message channel proc)
>   (get-message channel))
>
>
> Say I have 2 concurrent fibers (F1 and F2) wanting to serialize
> messages through the critical section.
>
> - F1-PUT-MESSAGE puts F1-MESSAGE to CRITICAL-SECTION
> - CRITICAL-SECTION gets F1-MESSAGE, which unblocks F1-PUT-MESSAGE
> - F1-GET-MESSAGE is called and blocks
>
> - F2-PUT-MESSAGE puts F2-MESSAGE, and the only receiver available
>   is... F1-GET-MESSAGE, because CRITICAL-SECTION is busy
> - F2-GET-MESSAGE is called and block
>
> - CRITICAL-SECTION is done handling F1-MESSAGE, and put
>   F1-MESSAGE-MODIFIED to...  F2-GET-MESSAGE.
>
> F2-MESSAGE is a procedure, whereas F1-MESSAGE-MODIFIED is a list, which
> causes the (map f F2-MESSAGE) error.

D’oh!  Good catch!

> A solution could be to use two channels, in and out:
>
> (define (make-critical-section . args)
>   "Return a pair of channels used to implement a critical section.  It can
> then be passed to 'with-critical-section', which will ensure sequential
> ordering.  ARGS are the arguments of the critical section.
>
> Critical sections are implemented by passing the procedure to execute to a
> dedicated fiber."
>   (let ((channel-in (make-channel))
>         (channel-out (make-channel)))
>     (spawn-fiber
>      (lambda ()
>        (let loop ()
>          (match (get-message channel-in)
>            ((? procedure? proc)
>             (put-message channel-out (apply proc args))))
>          (loop))))
>     (cons channel-in channel-out)))
>
> (define (call-with-critical-section channel proc)
>   "Call PROC in the critical section corresponding to CHANNEL.  Return the
> result of PROC."
>   (match channel
>     ((channel-out . channel-in)
>      (begin
>        (put-message channel-out proc)
>        (get-message channel-in)))))
>
> WDYT?

Or maybe like this:

(define (make-critical-section . args)
  "Return a channel used to implement a critical section.  That channel can
then be passed to 'join-critical-section', which will ensure sequential
ordering.  ARGS are the arguments of the critical section.

Critical sections are implemented by passing the procedure to execute to a
dedicated fiber."
  (let ((channel (make-channel)))
    (spawn-fiber
     (lambda ()
       (let loop ()
         (match (get-message channel)
           (((? channel? reply) . (? procedure? proc))
            (put-message reply (apply proc args))))
         (loop))))
    channel))

(define (call-with-critical-section channel proc)
  "Call PROC in the critical section corresponding to CHANNEL.  Return the
result of PROC."
  (let ((reply (make-channel)))
    (put-message channel (cons reply proc))
    (get-message reply)))

That makes it clear that the reply channel is used only by the calling
fiber.

WDYT?

Thanks for debugging this!

Ludo’.

  reply	other threads:[~2018-07-27  8:50 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-07-23  7:48 packaging Bootstrap for Cuirass' web interface Clément Lassieur
2018-07-23  8:03 ` Gábor Boskovits
2018-07-23  9:04   ` Ludovic Courtès
2018-07-23 11:29     ` Clément Lassieur
2018-07-24 20:29       ` Tatiana Sholokhova
2018-07-24 23:39         ` Clément Lassieur
2018-07-25  8:10           ` Clément Lassieur
2018-07-25  8:46             ` Clément Lassieur
2018-07-25  9:18               ` Clément Lassieur
2018-07-25 10:05               ` Gábor Boskovits
2018-07-25 10:16                 ` Clément Lassieur
2018-07-26  9:07                 ` Clément Lassieur
2018-07-26 13:41                   ` Ludovic Courtès
2018-07-27  1:43                     ` Clément Lassieur
2018-07-27  8:50                       ` Ludovic Courtès [this message]
2018-07-27 10:18                         ` Clément Lassieur
2018-07-27 17:43                           ` Clément Lassieur
2018-07-29 16:54                             ` Ludovic Courtès
2018-07-26 13:39               ` Ludovic Courtès
2018-07-29 23:45                 ` Clément Lassieur
2018-07-26 13:42           ` Ludovic Courtès
2018-07-23  9:31 ` Björn Höfling
2018-07-23 11:18   ` Clément Lassieur
2018-07-27  8:41   ` Chris Marusich

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

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

  git send-email \
    --in-reply-to=87k1phkqoo.fsf@gnu.org \
    --to=ludo@gnu.org \
    --cc=clement@lassieur.org \
    --cc=guix-devel@gnu.org \
    --cc=tanja201396@gmail.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.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/guix.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.