Ludovic Courtès <l...@gnu.org> writes:

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

I like it, but in that case MAKE-CHANNEL is called every time
CALL-WITH-CRITICAL-SECTION is called.  Do you think it is a significant
overhead?

Clément

Reply via email to