From: Tobias R. <tri...@us...> - 2010-03-28 13:37:32
|
Update of /cvsroot/sbcl/sbcl/src/code In directory sfp-cvsdas-3.v30.ch3.sourceforge.com:/tmp/cvs-serv1164/src/code Modified Files: target-thread.lisp Log Message: 1.0.37.6: Add SB-THREAD:TRY-SEMAPHORE. Index: target-thread.lisp =================================================================== RCS file: /cvsroot/sbcl/sbcl/src/code/target-thread.lisp,v retrieving revision 1.120 retrieving revision 1.121 diff -u -d -r1.120 -r1.121 --- target-thread.lisp 11 Mar 2010 10:11:49 -0000 1.120 +++ target-thread.lisp 28 Mar 2010 13:37:22 -0000 1.121 @@ -680,8 +680,26 @@ do (condition-wait (semaphore-queue semaphore) (semaphore-mutex semaphore))) (setf (semaphore-%count semaphore) (1- count))) + ;; Even safe when CONDITION-WAIT is unwinded without + ;; having reacquired the lock: a) we know at this point + ;; that an INCF must have happened before, b) the DECF + ;; will become visible to other CPUs as the implicit + ;; RELEASE-MUTEX involves a CAS and hence a memory + ;; barrier. (decf (semaphore-waitcount semaphore))))))) +(defun try-semaphore (semaphore) + #!+sb-doc + "Try to decrement the count of SEMAPHORE if the count would not be +negative. Else return NIL." + ;; No need for disabling interrupts; the mutex prevents interleaved + ;; modifications, and we don't leave temporarily inconsistent state + ;; around. + (with-mutex ((semaphore-mutex semaphore)) + (let ((count (semaphore-%count semaphore))) + (when (plusp count) + (setf (semaphore-%count semaphore) (1- count)))))) + (defun signal-semaphore (semaphore &optional (n 1)) #!+sb-doc "Increment the count of SEMAPHORE by N. If there are threads waiting |