;; FIXME: this does not work on SMP Pentium Pro and OOSTORE systems,
;; neither on most non-x86 architectures (but we don't have threads
;; on those).
- (setf (spinlock-value spinlock) nil))
+ (setf (spinlock-value spinlock) nil)
+
+ ;; FIXME: Is a :memory barrier too strong here? Can we use a :write
+ ;; barrier instead?
+ #!+(not (or x86 x86-64))
+ (barrier (:memory)))
\f
;;;; Mutexes
#!-sb-thread (ignore waitp timeout))
(unless new-owner
(setq new-owner *current-thread*))
+ (barrier (:read))
(let ((old (mutex-%owner mutex)))
(when (eq new-owner old)
(error "Recursive lock attempt ~S." mutex))
(with-interrupts (%lutex-lock lutex))
(%lutex-trylock lutex))))
(setf (mutex-%owner mutex) new-owner)
+ (barrier (:write))
t))
#!-sb-lutex
;; This is a direct translation of the Mutex 2 algorithm from
(defun condition-wait (queue mutex)
#!+sb-doc
- "Atomically release MUTEX and enqueue ourselves on QUEUE. Another
-thread may subsequently notify us using CONDITION-NOTIFY, at which
-time we reacquire MUTEX and return to the caller.
-
-Note that if CONDITION-WAIT unwinds (due to eg. a timeout) instead of
+ "Atomically release MUTEX and enqueue ourselves on QUEUE. Another thread may
+subsequently notify us using CONDITION-NOTIFY, at which time we reacquire
+MUTEX and return to the caller.
+
+Important: CONDITION-WAIT may return without CONDITION-NOTIFY having occurred.
+The correct way to write code that uses CONDITION-WAIT is to loop around the
+call, checking the the associated data:
+
+ (defvar *data* nil)
+ (defvar *queue* (make-waitqueue))
+ (defvar *lock* (make-mutex))
+
+ ;; Consumer
+ (defun pop-data ()
+ (with-mutex (*lock*)
+ (loop until *data*
+ do (condition-wait *queue* *lock*))
+ (pop *data*)))
+
+ ;; Producer
+ (defun push-data (data)
+ (with-mutex (*lock*)
+ (push data *data*)
+ (condition-notify *queue*)))
+
+Also note that if CONDITION-WAIT unwinds (due to eg. a timeout) instead of
returning normally, it may do so without holding the mutex."
#!-sb-thread (declare (ignore queue))
(assert mutex)
#!-sb-thread (error "Not supported in unithread builds.")
#!+sb-thread
(let ((me *current-thread*))
+ (barrier (:read))
(assert (eq me (mutex-%owner mutex)))
(/show0 "CONDITION-WAITing")
#!+sb-lutex
(with-lutex-address (mutex-lutex-address (mutex-lutex mutex))
(with-local-interrupts
(%lutex-wait queue-lutex-address mutex-lutex-address)))))
- (setf (mutex-%owner mutex) me)))
+ (barrier (:write)
+ (setf (mutex-%owner mutex) me))))
#!-sb-lutex
;; Need to disable interrupts so that we don't miss grabbing the
;; mutex on our way out.