1 ;;;; support for threads needed at cross-compile time
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
12 (in-package "SB!THREAD")
14 (eval-when (:compile-toplevel :load-toplevel :execute)
15 (sb!xc:proclaim '(sb!ext:always-bound *current-thread*)))
17 (def!type thread-name ()
20 (def!struct (thread (:constructor %make-thread))
22 "Thread type. Do not rely on threads being structs as it may change
24 (name nil :type (or thread-name null))
25 (%alive-p nil :type boolean)
26 (%ephemeral-p nil :type boolean)
27 (os-thread nil :type (or integer null))
28 (interruptions nil :type list)
29 (result nil :type list)
31 (make-mutex :name "thread interruptions lock")
34 (make-mutex :name "thread result lock")
41 (name nil :type (or null thread-name))
42 (%owner nil :type (or null thread))
43 #!+(and sb-thread sb-futex)
44 (state 0 :type fixnum))
46 (defun mutex-value (mutex)
47 "Current owner of the mutex, NIL if the mutex is free. May return a
48 stale value, use MUTEX-OWNER instead."
51 (defun holding-mutex-p (mutex)
52 "Test whether the current thread is holding MUTEX."
53 ;; This is about the only use for which a stale value of owner is
55 (eq sb!thread:*current-thread* (mutex-%owner mutex)))
57 (defsetf mutex-value set-mutex-value)
59 (declaim (inline set-mutex-value))
60 (defun set-mutex-value (mutex value)
61 (declare (ignore mutex value))
62 (error "~S is no longer supported." '(setf mutex-value)))
64 (define-compiler-macro set-mutex-value (&whole form mutex value)
65 (declare (ignore mutex value))
66 (warn "~S is no longer supported, and will signal an error at runtime."
70 ;;; SPINLOCK no longer exists as a type -- provided for backwards compatibility.
74 (deprecation-warning :early "1.0.53.11" 'spinlock 'mutex)
77 (define-deprecated-function :early "1.0.53.11" make-spinlock make-mutex (&key name)
78 (make-mutex :name name))
80 (define-deprecated-function :early "1.0.53.11" spinlock-name mutex-name (lock)
83 (define-deprecated-function :early "1.0.53.11" (setf spinlock-name) (setf mutex-name) (name lock)
84 (setf (mutex-name lock) name))
86 (define-deprecated-function :early "1.0.53.11" spinlock-value mutex-owner (lock)
89 (define-deprecated-function :early "1.0.53.11" get-spinlock grab-mutex (lock)
92 (define-deprecated-function :early "1.0.53.11" release-spinlock release-mutex (lock)
95 (sb!xc:defmacro with-recursive-spinlock ((lock) &body body)
96 (deprecation-warning :early "1.0.53.11" 'with-recursive-spinlock 'with-recursive-lock)
97 `(with-recursive-lock (,lock)
100 (sb!xc:defmacro with-spinlock ((lock) &body body)
101 (deprecation-warning :early "1.0.53.11" 'with-spinlock 'with-mutex)
105 (sb!xc:defmacro without-thread-waiting-for ((&key already-without-interrupts) &body body)
106 (with-unique-names (thread prev)
107 (let ((without (if already-without-interrupts
109 'without-interrupts))
110 (with (if already-without-interrupts
112 'with-local-interrupts)))
113 `(let* ((,thread *current-thread*)
116 (thread-waiting-for ,thread))))
117 (flet ((exec () ,@body))
122 (setf (thread-waiting-for ,thread) nil)
125 ;; If we were waiting on a waitqueue, this becomes a bogus
127 (when (mutex-p ,prev)
128 (setf (thread-waiting-for ,thread) ,prev)
129 (barrier (:write)))))
132 (sb!xc:defmacro with-mutex ((mutex &key (wait-p t) timeout value)
135 "Acquire MUTEX for the dynamic scope of BODY. If WAIT-P is true (the default),
136 and the MUTEX is not immediately available, sleep until it is available.
138 If TIMEOUT is given, it specifies a relative timeout, in seconds, on how long
139 the system should try to acquire the lock in the contested case.
141 If the mutex isn't acquired succesfully due to either WAIT-P or TIMEOUT, the
142 body is not executed, and WITH-MUTEX returns NIL.
144 Otherwise body is executed with the mutex held by current thread, and
145 WITH-MUTEX returns the values of BODY.
147 Historically WITH-MUTEX also accepted a VALUE argument, which when provided
148 was used as the new owner of the mutex instead of the current thread. This is
149 no longer supported: if VALUE is provided, it must be either NIL or the
151 `(dx-flet ((with-mutex-thunk () ,@body))
159 (sb!xc:defmacro with-system-mutex ((mutex
160 &key without-gcing allow-with-interrupts)
162 `(dx-flet ((with-system-mutex-thunk () ,@body))
163 (,(cond (without-gcing
164 'call-with-system-mutex/without-gcing)
165 (allow-with-interrupts
166 'call-with-system-mutex/allow-with-interrupts)
168 'call-with-system-mutex))
169 #'with-system-mutex-thunk
172 (sb!xc:defmacro with-recursive-lock ((mutex &key (wait-p t) timeout) &body body)
174 "Acquire MUTEX for the dynamic scope of BODY.
176 If WAIT-P is true (the default), and the MUTEX is not immediately available or
177 held by the current thread, sleep until it is available.
179 If TIMEOUT is given, it specifies a relative timeout, in seconds, on how long
180 the system should try to acquire the lock in the contested case.
182 If the mutex isn't acquired succesfully due to either WAIT-P or TIMEOUT, the
183 body is not executed, and WITH-RECURSIVE-LOCK returns NIL.
185 Otherwise body is executed with the mutex held by current thread, and
186 WITH-RECURSIVE-LOCK returns the values of BODY.
188 Unlike WITH-MUTEX, which signals an error on attempt to re-acquire an already
189 held mutex, WITH-RECURSIVE-LOCK allows recursive lock attempts to succeed."
190 `(dx-flet ((with-recursive-lock-thunk () ,@body))
191 (call-with-recursive-lock
192 #'with-recursive-lock-thunk
197 (sb!xc:defmacro with-recursive-system-lock ((lock
200 `(dx-flet ((with-recursive-system-lock-thunk () ,@body))
201 (,(cond (without-gcing
202 'call-with-recursive-system-lock/without-gcing)
204 'call-with-recursive-system-lock))
205 #'with-recursive-system-lock-thunk
208 (macrolet ((def (name &optional variant)
209 `(defun ,(if variant (symbolicate name "/" variant) name)
211 (declare (function function))
212 (flet ((%call-with-system-mutex ()
215 (when (setf got-it (grab-mutex mutex))
218 (release-mutex mutex))))))
219 (declare (inline %call-with-system-mutex))
222 `(without-gcing (%call-with-system-mutex)))
223 (:allow-with-interrupts
225 (allow-with-interrupts (%call-with-system-mutex))))
227 `(without-interrupts (%call-with-system-mutex))))))))
228 (def call-with-system-mutex)
229 (def call-with-system-mutex :without-gcing)
230 (def call-with-system-mutex :allow-with-interrupts))
234 (defun call-with-mutex (function mutex value waitp timeout)
235 (declare (ignore mutex waitp timeout)
237 (unless (or (null value) (eq *current-thread* value))
238 (error "~S called with non-nil :VALUE that isn't the current thread."
242 (defun call-with-recursive-lock (function mutex waitp timeout)
243 (declare (ignore mutex waitp timeout)
247 (defun call-with-recursive-system-lock (function lock)
248 (declare (function function) (ignore lock))
252 (defun call-with-recursive-system-lock/without-gcing (function mutex)
253 (declare (function function) (ignore mutex))
255 (funcall function))))
258 ;;; KLUDGE: These need to use DX-LET, because the cleanup form that
259 ;;; closes over GOT-IT causes a value-cell to be allocated for it --
260 ;;; and we prefer that to go on the stack since it can.
262 (defun call-with-mutex (function mutex value waitp timeout)
263 (declare (function function))
264 (unless (or (null value) (eq *current-thread* value))
265 (error "~S called with non-nil :VALUE that isn't the current thread."
267 (dx-let ((got-it nil))
270 (when (setq got-it (allow-with-interrupts
271 (grab-mutex mutex :waitp waitp
273 (with-local-interrupts (funcall function)))
275 (release-mutex mutex))))))
277 (defun call-with-recursive-lock (function mutex waitp timeout)
278 (declare (function function))
279 (dx-let ((inner-lock-p (eq (mutex-%owner mutex) *current-thread*))
283 (when (or inner-lock-p (setf got-it (allow-with-interrupts
284 (grab-mutex mutex :waitp waitp
286 (with-local-interrupts (funcall function)))
288 (release-mutex mutex))))))
290 (macrolet ((def (name &optional variant)
291 `(defun ,(if variant (symbolicate name "/" variant) name)
293 (declare (function function))
294 (flet ((%call-with-recursive-system-lock ()
295 (dx-let ((inner-lock-p
296 (eq *current-thread* (mutex-owner lock)))
299 (when (or inner-lock-p
300 (setf got-it (grab-mutex lock)))
303 (release-mutex lock))))))
304 (declare (inline %call-with-recursive-system-lock))
307 `(without-gcing (%call-with-recursive-system-lock)))
309 `(without-interrupts (%call-with-recursive-system-lock))))))))
310 (def call-with-recursive-system-lock)
311 (def call-with-recursive-system-lock :without-gcing)))