[GBBopen-developer] [PATCH] Minor fixes for condition variables on Clozure.

David Brown lisp at davidb.org
Fri Oct 24 12:11:58 EDT 2008


There are a couple of monitor problems with condition variables on
Clozure:

   - condition-variable-wait never removes the task from the wait
     queue.  Without the fix, this list grows unbounded as the
     application runs.

   - condition-variable-signal shouldn't signal the semaphore if there
     is no waiter.  Proper use of condition variables requires them to
     be used in a loop, so the behavior is generally correct, but
     applications that frequently signal when there are no waiters will
     cause the waits that do happen to thrash in a loop decrementing
     the semaphore individually.

condition-variable-broadcast is still wrong for Closure.  It signals
the semaphore the proper number of times, but there is nothing to keep
it from just waking one of the threads up that many times.  The only
real fix I can think of is putting a fresh semaphore for each thread
into the wait queue.  Creating semaphores is fairly cheap on Clozure,
but it would still slow down the non broadcast waits a bit.

---
My application still has a deadlock in it somewhere, but I haven't
tracked down to whether that is in portable-threads or just in my
code.

Thanks,
David

diff --git a/portable-threads.lisp b/portable-threads.lisp
index 278ebef..3cb1eb1 100644
--- a/portable-threads.lisp
+++ b/portable-threads.lisp
@@ -1740,7 +1740,10 @@ (defun condition-variable-wait (condition-variable)
              (ccl:release-lock ccl-lock)
              (ccl:wait-on-semaphore 
               (condition-variable-semaphore condition-variable)))
-        (ccl:grab-lock ccl-lock)))
+        (ccl:grab-lock ccl-lock)
+        (setf (condition-variable-queue condition-variable)
+              (remove ccl:*current-process*
+                      (condition-variable-queue condition-variable)))))
      #+(and cmu mp)
      (progn
        (push mp:*current-process*
@@ -1888,7 +1891,8 @@ (defun condition-variable-signal (condition-variable)
      (when (and thread (thread-alive-p thread))
        (awaken-throwable-sleeper thread)))
    #+clozure
-  (ccl:signal-semaphore (condition-variable-semaphore condition-variable))
+  (when (condition-variable-queue condition-variable)
+    (ccl:signal-semaphore (condition-variable-semaphore condition-variable)))
    #+(and ecl threads)
    (mp:condition-variable-signal (condition-variable-cv condition-variable))
    #+(and sbcl sb-thread)

-- 
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.



More information about the GBBopen-developer mailing list