(define (make-semaphore n)
(let ((mutex (make-mutex)))
(define (the-semaphore m)
(cond ((eq? 'acquire)
((mutex 'acquire)
(if (> n 0)
(begin (set! n (- n 1))
(mutex 'release))
(begin (mutex 'release)
(the-semaphore 'acquire)))))
((eq? 'release)
(begin (mutex 'acquire)
(set! n (+ n 1))
(mutex 'release)))))
the-semaphore))
(define (make-semaphore n)
(let ((count n))
(define (the-semaphore m)
(cond ((eq? 'acquire)
(if (test-and-set! count)
(the-semaphore 'acquire)))
((eq? m 'release) (release! count))))
the-semaphore))
(define (release! count)
(set! count (+ count 1)))
(define (test-and-set! count)
(if (> 0 count)
(begin (set! count (- count 1))
false)
true))