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 ;;; FIXME: most of this file looks like it's supposed to be :not-host.
15 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
16 (sb!xc
:proclaim
'(sb!ext
:always-bound
*current-thread
*)))
18 (defstruct (foreign-thread
20 (:conc-name
"THREAD-"))
22 "Type of native threads which are attached to the runtime as Lisp threads
25 #!+(and sb-safepoint-strictly
(not win32
))
26 (defstruct (signal-handling-thread
27 (:include foreign-thread
)
28 (:conc-name
"THREAD-"))
30 "Asynchronous signal handling thread."
31 (signal-number nil
:type integer
))
33 (defun mutex-value (mutex)
35 "Current owner of the mutex, NIL if the mutex is free. May return a
36 stale value, use MUTEX-OWNER instead."
39 (defun holding-mutex-p (mutex)
41 "Test whether the current thread is holding MUTEX."
42 ;; This is about the only use for which a stale value of owner is
44 (eq sb
!thread
:*current-thread
* (mutex-%owner mutex
)))
46 (defsetf mutex-value set-mutex-value
)
49 (declaim (sb!ext
:deprecated
:final
("SBCL" "1.2.15") #'set-mutex-value
))
51 ;;; SPINLOCK no longer exists as a type -- provided for backwards compatibility.
59 (declaim (sb!ext
:deprecated
60 :late
("SBCL" "1.0.53.11") (type spinlock
:replacement mutex
)))
62 (define-deprecated-function :early
"1.0.53.11" make-spinlock make-mutex
(&key name
)
63 (make-mutex :name name
))
65 (define-deprecated-function :early
"1.0.53.11" spinlock-name mutex-name
(lock)
68 (define-deprecated-function :early
"1.0.53.11" (setf spinlock-name
) (setf mutex-name
) (name lock
)
69 (setf (mutex-name lock
) name
))
71 #-sb-xc-host
; Mutex is not a type on the host.
72 (define-deprecated-function :early
"1.0.53.11" spinlock-value mutex-owner
(lock)
75 #-sb-xc-host
; Mutex is not a type on the host.
76 (define-deprecated-function :early
"1.0.53.11" get-spinlock grab-mutex
(lock)
79 #-sb-xc-host
; Mutex is not a type on the host.
80 (define-deprecated-function :early
"1.0.53.11" release-spinlock release-mutex
(lock)
83 (sb!xc
:defmacro with-recursive-spinlock
((lock) &body body
)
84 `(with-recursive-lock (,lock
)
87 (sb!xc
:defmacro with-spinlock
((lock) &body body
)
92 (declaim (sb!ext
:deprecated
93 :early
("SBCL" "1.0.53.11")
94 (function with-recursive-spinlock
:replacement with-recursive-lock
)
95 (function with-spinlock
:replacement with-mutex
)))
97 (sb!xc
:defmacro without-thread-waiting-for
((&key already-without-interrupts
) &body body
)
98 (with-unique-names (thread prev
)
99 (let ((without (if already-without-interrupts
101 'without-interrupts
))
102 (with (if already-without-interrupts
104 'with-local-interrupts
)))
105 `(let* ((,thread
*current-thread
*)
108 (thread-waiting-for ,thread
))))
109 (flet ((exec () ,@body
))
114 (setf (thread-waiting-for ,thread
) nil
)
117 ;; If we were waiting on a waitqueue, this becomes a bogus
119 (when (mutex-p ,prev
)
120 (setf (thread-waiting-for ,thread
) ,prev
)
121 (barrier (:write
)))))
124 (sb!xc
:defmacro with-mutex
((mutex &key
(wait-p t
) timeout value
)
127 "Acquire MUTEX for the dynamic scope of BODY. If WAIT-P is true (the default),
128 and the MUTEX is not immediately available, sleep until it is available.
130 If TIMEOUT is given, it specifies a relative timeout, in seconds, on how long
131 the system should try to acquire the lock in the contested case.
133 If the mutex isn't acquired successfully due to either WAIT-P or TIMEOUT, the
134 body is not executed, and WITH-MUTEX returns NIL.
136 Otherwise body is executed with the mutex held by current thread, and
137 WITH-MUTEX returns the values of BODY.
139 Historically WITH-MUTEX also accepted a VALUE argument, which when provided
140 was used as the new owner of the mutex instead of the current thread. This is
141 no longer supported: if VALUE is provided, it must be either NIL or the
143 `(dx-flet ((with-mutex-thunk () ,@body
))
151 (sb!xc
:defmacro with-system-mutex
((mutex
152 &key without-gcing allow-with-interrupts
)
154 `(dx-flet ((with-system-mutex-thunk () ,@body
))
155 (,(cond (without-gcing
156 'call-with-system-mutex
/without-gcing
)
157 (allow-with-interrupts
158 'call-with-system-mutex
/allow-with-interrupts
)
160 'call-with-system-mutex
))
161 #'with-system-mutex-thunk
164 (sb!xc
:defmacro with-recursive-lock
((mutex &key
(wait-p t
) timeout
) &body body
)
166 "Acquire MUTEX for the dynamic scope of BODY.
168 If WAIT-P is true (the default), and the MUTEX is not immediately available or
169 held by the current thread, sleep until it is available.
171 If TIMEOUT is given, it specifies a relative timeout, in seconds, on how long
172 the system should try to acquire the lock in the contested case.
174 If the mutex isn't acquired successfully due to either WAIT-P or TIMEOUT, the
175 body is not executed, and WITH-RECURSIVE-LOCK returns NIL.
177 Otherwise body is executed with the mutex held by current thread, and
178 WITH-RECURSIVE-LOCK returns the values of BODY.
180 Unlike WITH-MUTEX, which signals an error on attempt to re-acquire an already
181 held mutex, WITH-RECURSIVE-LOCK allows recursive lock attempts to succeed."
182 `(dx-flet ((with-recursive-lock-thunk () ,@body
))
183 (call-with-recursive-lock
184 #'with-recursive-lock-thunk
189 (sb!xc
:defmacro with-recursive-system-lock
((lock
192 `(dx-flet ((with-recursive-system-lock-thunk () ,@body
))
193 (,(cond (without-gcing
194 'call-with-recursive-system-lock
/without-gcing
)
196 'call-with-recursive-system-lock
))
197 #'with-recursive-system-lock-thunk
200 #-sb-xc-host
; Mutex is not a type on the host.
201 (macrolet ((def (name &optional variant
)
202 `(defun ,(if variant
(symbolicate name
"/" variant
) name
)
204 (declare (function function
))
205 (flet ((%call-with-system-mutex
()
208 (when (setf got-it
(grab-mutex mutex
))
211 (release-mutex mutex
))))))
212 (declare (inline %call-with-system-mutex
))
215 `(without-gcing (%call-with-system-mutex
)))
216 (:allow-with-interrupts
218 (allow-with-interrupts (%call-with-system-mutex
))))
220 `(without-interrupts (%call-with-system-mutex
))))))))
221 (def call-with-system-mutex
)
222 (def call-with-system-mutex
:without-gcing
)
223 (def call-with-system-mutex
:allow-with-interrupts
))
225 #!+(and (host-feature sb-xc
) (not sb-thread
))
227 (defun call-with-mutex (function mutex value waitp timeout
)
228 (declare (ignore mutex waitp timeout
)
230 (unless (or (null value
) (eq *current-thread
* value
))
231 (error "~S called with non-nil :VALUE that isn't the current thread."
235 (defun call-with-recursive-lock (function mutex waitp timeout
)
236 (declare (ignore mutex waitp timeout
)
240 (defun call-with-recursive-system-lock (function lock
)
241 (declare (function function
) (ignore lock
))
245 (defun call-with-recursive-system-lock/without-gcing
(function mutex
)
246 (declare (function function
) (ignore mutex
))
248 (funcall function
))))
250 #!+(and (host-feature sb-xc
) sb-thread
)
251 ;;; KLUDGE: These need to use DX-LET, because the cleanup form that
252 ;;; closes over GOT-IT causes a value-cell to be allocated for it --
253 ;;; and we prefer that to go on the stack since it can.
255 (defun call-with-mutex (function mutex value waitp timeout
)
256 (declare (function function
))
257 (unless (or (null value
) (eq *current-thread
* value
))
258 (error "~S called with non-nil :VALUE that isn't the current thread."
260 (dx-let ((got-it nil
))
263 (when (setq got-it
(allow-with-interrupts
264 (grab-mutex mutex
:waitp waitp
266 (with-local-interrupts (funcall function
)))
268 (release-mutex mutex
))))))
270 (defun call-with-recursive-lock (function mutex waitp timeout
)
271 (declare (function function
))
272 (dx-let ((inner-lock-p (eq (mutex-%owner mutex
) *current-thread
*))
276 (when (or inner-lock-p
(setf got-it
(allow-with-interrupts
277 (grab-mutex mutex
:waitp waitp
279 (with-local-interrupts (funcall function
)))
281 (release-mutex mutex
))))))
283 (macrolet ((def (name &optional variant
)
284 `(defun ,(if variant
(symbolicate name
"/" variant
) name
)
286 (declare (function function
))
287 (flet ((%call-with-recursive-system-lock
()
288 (dx-let ((inner-lock-p
289 (eq *current-thread
* (mutex-owner lock
)))
292 (when (or inner-lock-p
293 (setf got-it
(grab-mutex lock
)))
296 (release-mutex lock
))))))
297 (declare (inline %call-with-recursive-system-lock
))
300 `(without-gcing (%call-with-recursive-system-lock
)))
302 `(without-interrupts (%call-with-recursive-system-lock
))))))))
303 (def call-with-recursive-system-lock
)
304 (def call-with-recursive-system-lock
:without-gcing
)))