Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

#615 Semaphores implemented using exemptions don't work

lisp error
closed
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

  • thank you for your bug report.
    the bug has been fixed in the source tree (mercurial/hg).
    you can either wait for the next release (recommended)
    or check out the current mercurial tree (see http://clisp.org\)
    and build CLISP from the sources (be advised that between
    releases the source tree is very unstable and may not even build
    on your platform).

     
    • status: open --> closed