+(def!type thread-name ()
+ 'simple-string)
+
+(def!struct (thread (:constructor %make-thread))
+ #!+sb-doc
+ "Thread type. Do not rely on threads being structs as it may change
+in future versions."
+ (name nil :type (or thread-name null))
+ (%alive-p nil :type boolean)
+ (os-thread nil :type (or integer null))
+ (interruptions nil :type list)
+ (result nil :type list)
+ (interruptions-lock
+ (make-mutex :name "thread interruptions lock")
+ :type mutex)
+ (result-lock
+ (make-mutex :name "thread result lock")
+ :type mutex))
+
+(def!struct mutex
+ #!+sb-doc
+ "Mutex type."
+ (name nil :type (or null thread-name))
+ (%owner nil :type (or null thread))
+ #!+(and (not sb-lutex) sb-thread)
+ (state 0 :type fixnum)
+ #!+(and sb-lutex sb-thread)
+ (lutex (make-lutex)))
+
+(defun mutex-value (mutex)
+ "Current owner of the mutex, NIL if the mutex is free. May return a
+stale value, use MUTEX-OWNER instead."
+ (mutex-%owner mutex))
+
+(defun holding-mutex-p (mutex)
+ "Test whether the current thread is holding MUTEX."
+ ;; This is about the only use for which a stale value of owner is
+ ;; sufficient.
+ (eq sb!thread:*current-thread* (mutex-%owner mutex)))
+
+(defsetf mutex-value set-mutex-value)
+
+(declaim (inline set-mutex-value))
+(defun set-mutex-value (mutex value)
+ (declare (ignore mutex value))
+ (error "~S is no longer supported." '(setf mutex-value)))
+
+(define-compiler-macro set-mutex-value (&whole form mutex value)
+ (declare (ignore mutex value))
+ (warn "~S is no longer supported, and will signal an error at runtime."
+ '(setf mutex-value))
+ form)
+
+(def!struct spinlock
+ #!+sb-doc
+ "Spinlock type."
+ (name nil :type (or null thread-name))
+ (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 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"
+ `(dx-flet ((with-mutex-thunk () ,@body))
+ (call-with-mutex
+ #'with-mutex-thunk
+ ,mutex
+ ,value
+ ,wait-p)))
+
+(sb!xc:defmacro with-system-mutex ((mutex
+ &key without-gcing allow-with-interrupts)
+ &body body)
+ `(dx-flet ((with-system-mutex-thunk () ,@body))
+ (,(cond (without-gcing
+ 'call-with-system-mutex/without-gcing)
+ (allow-with-interrupts
+ 'call-with-system-mutex/allow-with-interrupts)
+ (t
+ 'call-with-system-mutex))
+ #'with-system-mutex-thunk
+ ,mutex)))
+
+(sb!xc:defmacro with-system-spinlock ((spinlock &key) &body body)
+ `(dx-flet ((with-system-spinlock-thunk () ,@body))
+ (call-with-system-spinlock
+ #'with-system-spinlock-thunk
+ ,spinlock)))