Hi.
I use condition-wait in many acquire recursive-lock. Other thread try to acquire recusirve-lock's lock at dead locked.
Below code reproduce a problem on Clozure CL. --- (defvar *condition-variable* (bt:make-condition-variable)) (defvar *recursive-lock* (bt:make-recursive-lock))
(defun worker-job (out) (bt:with-recursive-lock-held (*recursive-lock*) (bt:with-recursive-lock-held (*recursive-lock*) (format out "before condition wait~%") (force-output out) (bt:condition-wait *condition-variable* *recursive-lock*) (format out "after condition wait~%") (force-output out))))
(defun condition-use-in-recursive-lock () (let* ((out *standard-output*) (worker (bt:make-thread (lambda () (worker-job out))))) (sleep 1) (format out "before condition notify~%") (force-output out) (bt:with-recursive-lock-held (*recursive-lock*) ; Dead lock point. (bt:condition-notify *condition-variable*) (format out "after condition notify~%") (force-output out)) (bt:join-thread worker)))
(condition-use-in-recurive-lock) ---
conditon-wait is lock release and wait semaphore. Lock object acquirable many times. but conditon-wait is release lock at once.
I make a patch at here. https://github.com/rayfill/bordeaux-threads/commit/192124d18a2b2858e6e8aa2cc...
Poor english sorry...
In article CALOceq_ccA9118qZPpSQ763yaOnFecQLkzS1eEj_cZ271K1xiw@mail.gmail.com, rayfill rayfill@gmail.com wrote:
I use condition-wait in many acquire recursive-lock. Other thread try to acquire recusirve-lock's lock at dead locked.
Don't.
"Never use a recursive mutex with condition variables because the implicit unlock performed by pthread_cond_wait() or pthread_cond_timedwait() will not actually release the mutex if it had been locked multiple times." (see http://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_xsh_chap02.html#t ag_22_02_09_08)
Partially releasing the recursive mutex on condition-wait will almost always result in a deadlock. However, fully releasing it may very well break invariants in sections of code that acquired the mutex, earlier in the call stack.
Paul Khuong
bordeaux-threads-devel@common-lisp.net