#616 Semaphores implemented using exemptions don't work

lisp error
open
5
2011-10-17
2011-10-17
Sopin Dmitry
No

I tried following code to implement semaphores:

(defstruct clisp-semaphore
state
lock
condition)

(defun make-semaphore (value)
(make-clisp-semaphore :state value
:lock (make-mutex)
:condition (make-exemption)))

(defun wait-semaphore (semaphore)
(let ((lock (clisp-semaphore-lock semaphore))
(condition (clisp-semaphore-condition semaphore)))
(mutex-lock lock)
(loop
(if (> (clisp-semaphore-state semaphore) 0)
(return)
(mt:exemption-wait condition lock)))
(decf (clisp-semaphore-state semaphore))
(mutex-unlock lock))

(defun signal-semaphore (semaphore)
(let ((lock (clisp-semaphore-lock semaphore)))
(lock-mutex lock)
(incf (clisp-semaphore-state semaphore))
(mt:exemption-signal (clisp-semaphore-condition semaphore))
(unlock-mutex lock)))

And following test code:
(let ((semaphore (make-semaphore)))
(dotimes (i 10)
(make-thread #'(lambda () (dotimes (i 100) (wait-semaphore semaphore))))
(make-thread #'(lambda () (dotimes (i 100) (signal-semaphore semaphore))))))

This test code falls into deadlock.
Equal code written in SBCL works. Is something wrong with exemptions?

Tried clisp versions 2.48 and 2.49 under Gentoo Linux.

Discussion