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 (def!type thread-name ()
17 (def!struct (thread (:constructor %make-thread))
19 "Thread type. Do not rely on threads being structs as it may change
21 (name nil :type (or thread-name null))
22 (%alive-p nil :type boolean)
23 (os-thread nil :type (or integer null))
24 (interruptions nil :type list)
25 (result nil :type list)
27 (make-mutex :name "thread interruptions lock")
30 (make-mutex :name "thread result lock")
37 (name nil :type (or null thread-name))
38 (%owner nil :type (or null thread))
39 #!+(and sb-thread sb-futex)
40 (state 0 :type fixnum))
42 (defun mutex-value (mutex)
43 "Current owner of the mutex, NIL if the mutex is free. May return a
44 stale value, use MUTEX-OWNER instead."
47 (defun holding-mutex-p (mutex)
48 "Test whether the current thread is holding MUTEX."
49 ;; This is about the only use for which a stale value of owner is
51 (eq sb!thread:*current-thread* (mutex-%owner mutex)))
53 (defsetf mutex-value set-mutex-value)
55 (declaim (inline set-mutex-value))
56 (defun set-mutex-value (mutex value)
57 (declare (ignore mutex value))
58 (error "~S is no longer supported." '(setf mutex-value)))
60 (define-compiler-macro set-mutex-value (&whole form mutex value)
61 (declare (ignore mutex value))
62 (warn "~S is no longer supported, and will signal an error at runtime."
66 ;;; SPINLOCK no longer exists as a type -- provided for backwards compatibility.
70 (deprecation-warning :early "1.0.53.11" 'spinlock 'mutex)
73 (define-deprecated-function :early "1.0.53.11" make-spinlock make-mutex (&key name)
74 (make-mutex :name name))
76 (define-deprecated-function :early "1.0.53.11" spinlock-name mutex-name (lock)
79 (define-deprecated-function :early "1.0.53.11" (setf spinlock-name) (setf mutex-name) (name lock)
80 (setf (mutex-name lock) name))
82 (define-deprecated-function :early "1.0.53.11" spinlock-value mutex-owner (lock)
85 (define-deprecated-function :early "1.0.53.11" get-spinlock grab-mutex (lock)
88 (define-deprecated-function :early "1.0.53.11" release-spinlock release-mutex (lock)
91 (sb!xc:defmacro with-recursive-spinlock ((lock) &body body)
92 (deprecation-warning :early "1.0.53.11" 'with-recursive-spinlock 'with-recursive-lock)
93 `(with-recursive-lock (,lock)
96 (sb!xc:defmacro with-spinlock ((lock) &body body)
97 (deprecation-warning :early "1.0.53.11" 'with-spinlock 'with-mutex)
101 (sb!xc:defmacro without-thread-waiting-for ((&key already-without-interrupts) &body body)
102 (with-unique-names (thread prev)
103 (let ((without (if already-without-interrupts
105 'without-interrupts))
106 (with (if already-without-interrupts
108 'with-local-interrupts)))
109 `(let* ((,thread *current-thread*)
112 (thread-waiting-for ,thread))))
113 (flet ((exec () ,@body))
118 (setf (thread-waiting-for ,thread) nil)
121 ;; If we were waiting on a waitqueue, this becomes a bogus
123 (when (mutex-p ,prev)
124 (setf (thread-waiting-for ,thread) ,prev)
125 (barrier (:write)))))
128 (sb!xc:defmacro with-mutex ((mutex &key (wait-p t) timeout value)
131 "Acquire MUTEX for the dynamic scope of BODY. If WAIT-P is true (the default),
132 and the MUTEX is not immediately available, sleep until it is available.
134 If TIMEOUT is given, it specifies a relative timeout, in seconds, on how long
135 the system should try to acquire the lock in the contested case.
137 If the mutex isn't acquired succesfully due to either WAIT-P or TIMEOUT, the
138 body is not executed, and WITH-MUTEX returns NIL.
140 Otherwise body is executed with the mutex held by current thread, and
141 WITH-MUTEX returns the values of BODY.
143 Historically WITH-MUTEX also accepted a VALUE argument, which when provided
144 was used as the new owner of the mutex instead of the current thread. This is
145 no longer supported: if VALUE is provided, it must be either NIL or the
147 `(dx-flet ((with-mutex-thunk () ,@body))
155 (sb!xc:defmacro with-system-mutex ((mutex
156 &key without-gcing allow-with-interrupts)
158 `(dx-flet ((with-system-mutex-thunk () ,@body))
159 (,(cond (without-gcing
160 'call-with-system-mutex/without-gcing)
161 (allow-with-interrupts
162 'call-with-system-mutex/allow-with-interrupts)
164 'call-with-system-mutex))
165 #'with-system-mutex-thunk
168 (sb!xc:defmacro with-recursive-lock ((mutex &key (wait-p t) timeout) &body body)
170 "Acquire MUTEX for the dynamic scope of BODY.
172 If WAIT-P is true (the default), and the MUTEX is not immediately available or
173 held by the current thread, sleep until it is available.
175 If TIMEOUT is given, it specifies a relative timeout, in seconds, on how long
176 the system should try to acquire the lock in the contested case.
178 If the mutex isn't acquired succesfully due to either WAIT-P or TIMEOUT, the
179 body is not executed, and WITH-RECURSIVE-LOCK returns NIL.
181 Otherwise body is executed with the mutex held by current thread, and
182 WITH-RECURSIVE-LOCK returns the values of BODY.
184 Unlike WITH-MUTEX, which signals an error on attempt to re-acquire an already
185 held mutex, WITH-RECURSIVE-LOCK allows recursive lock attempts to succeed."
186 `(dx-flet ((with-recursive-lock-thunk () ,@body))
187 (call-with-recursive-lock
188 #'with-recursive-lock-thunk
193 (sb!xc:defmacro with-recursive-system-lock ((lock
196 `(dx-flet ((with-recursive-system-lock-thunk () ,@body))
197 (,(cond (without-gcing
198 'call-with-recursive-system-lock/without-gcing)
200 'call-with-recursive-system-lock))
201 #'with-recursive-system-lock-thunk
204 (macrolet ((def (name &optional variant)
205 `(defun ,(if variant (symbolicate name "/" variant) name)
207 (declare (function function))
208 (flet ((%call-with-system-mutex ()
211 (when (setf got-it (grab-mutex mutex))
214 (release-mutex mutex))))))
215 (declare (inline %call-with-system-mutex))
218 `(without-gcing (%call-with-system-mutex)))
219 (:allow-with-interrupts
221 (allow-with-interrupts (%call-with-system-mutex))))
223 `(without-interrupts (%call-with-system-mutex))))))))
224 (def call-with-system-mutex)
225 (def call-with-system-mutex :without-gcing)
226 (def call-with-system-mutex :allow-with-interrupts))
230 (defun call-with-mutex (function mutex value waitp timeout)
231 (declare (ignore mutex waitp timeout)
233 (unless (or (null value) (eq *current-thread* value))
234 (error "~S called with non-nil :VALUE that isn't the current thread."
238 (defun call-with-recursive-lock (function mutex waitp timeout)
239 (declare (ignore mutex waitp timeout)
243 (defun call-with-recursive-system-lock (function lock)
244 (declare (function function) (ignore lock))
248 (defun call-with-recursive-system-lock/without-gcing (function mutex)
249 (declare (function function) (ignore mutex))
251 (funcall function))))
254 ;;; KLUDGE: These need to use DX-LET, because the cleanup form that
255 ;;; closes over GOT-IT causes a value-cell to be allocated for it --
256 ;;; and we prefer that to go on the stack since it can.
258 (defun call-with-mutex (function mutex value waitp timeout)
259 (declare (function function))
260 (unless (or (null value) (eq *current-thread* value))
261 (error "~S called with non-nil :VALUE that isn't the current thread."
263 (dx-let ((got-it nil))
266 (when (setq got-it (allow-with-interrupts
267 (grab-mutex mutex :waitp waitp
269 (with-local-interrupts (funcall function)))
271 (release-mutex mutex))))))
273 (defun call-with-recursive-lock (function mutex waitp timeout)
274 (declare (function function))
275 (dx-let ((inner-lock-p (eq (mutex-%owner mutex) *current-thread*))
279 (when (or inner-lock-p (setf got-it (allow-with-interrupts
280 (grab-mutex mutex :waitp waitp
282 (with-local-interrupts (funcall function)))
284 (release-mutex mutex))))))
286 (macrolet ((def (name &optional variant)
287 `(defun ,(if variant (symbolicate name "/" variant) name)
289 (declare (function function))
290 (flet ((%call-with-recursive-system-lock ()
291 (dx-let ((inner-lock-p
292 (eq *current-thread* (mutex-owner lock)))
295 (when (or inner-lock-p
296 (setf got-it (grab-mutex lock)))
299 (release-mutex lock))))))
300 (declare (inline %call-with-recursive-system-lock))
303 `(without-gcing (%call-with-recursive-system-lock)))
305 `(without-interrupts (%call-with-recursive-system-lock))))))))
306 (def call-with-recursive-system-lock)
307 (def call-with-recursive-system-lock :without-gcing)))