+(def!struct mutex
+ #!+sb-doc
+ "Mutex type."
+ (name nil :type (or null simple-string))
+ (value nil)
+ #!+(and sb-lutex sb-thread)
+ (lutex (make-lutex)))
+
+(def!struct spinlock
+ #!+sb-doc
+ "Spinlock type."
+ (name nil :type (or null simple-string))
+ (value nil))
+
+(sb!xc:defmacro with-mutex ((mutex &key (value '*current-thread*) (wait-p t))
+ &body body)
+ #!+sb-doc
+ "Acquire MUTEX for the dynamic scope of BODY, setting it to
+NEW-VALUE or some suitable default value if NIL. If WAIT-P is non-NIL
+and the mutex is in use, sleep until it is available"
+ #!-sb-thread (declare (ignore mutex value wait-p))
+ #!+sb-thread
+ (with-unique-names (got mutex1)
+ `(let ((,mutex1 ,mutex)
+ ,got)
+ (/show0 "WITH-MUTEX")
+ (unwind-protect
+ ;; FIXME: async unwind in SETQ form
+ (when (setq ,got (get-mutex ,mutex1 ,value ,wait-p))
+ (locally
+ ,@body))
+ (when ,got
+ (release-mutex ,mutex1)))))
+ ;; KLUDGE: this separate expansion for (NOT SB-THREAD) is not
+ ;; strictly necessary; GET-MUTEX and RELEASE-MUTEX are implemented.
+ ;; However, there would be a (possibly slight) performance hit in
+ ;; using them.
+ #!-sb-thread
+ `(locally ,@body))