Next: , Previous: , Up: The Language   [Contents][Index]


4.5 Process Synchronization

An exchanger is a procedure of one argument regulating mutually exclusive access to a resource. When a exchanger is called, its current content is returned, while being replaced by its argument in an atomic operation.

Function: make-exchanger obj

Returns a new exchanger with the argument obj as its initial content.

(define queue (make-exchanger (list a)))

A queue implemented as an exchanger holding a list can be protected from reentrant execution thus:

(define (pop queue)
  (let ((lst #f))
    (dynamic-wind
        (lambda () (set! lst (queue #f)))
        (lambda () (and lst (not (null? lst))
                        (let ((ret (car lst)))
                          (set! lst (cdr lst))
                          ret)))
        (lambda () (and lst (queue lst))))))

(pop queue)         ⇒ a

(pop queue)         ⇒ #f
Function: make-arbiter name

Returns an object of type arbiter and name name. Its state is initially unlocked.

Function: try-arbiter arbiter

Returns #t and locks arbiter if arbiter was unlocked. Otherwise, returns #f.

Function: release-arbiter arbiter

Returns #t and unlocks arbiter if arbiter was locked. Otherwise, returns #f.