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 0 :type sb
!vm
:word
)
28 (interruptions nil
:type list
)
29 ;; On succesful execution of the thread's lambda a list of values.
32 (make-mutex :name
"thread interruptions lock")
35 (make-mutex :name
"thread result lock")
39 (def!struct
(foreign-thread
41 (:conc-name
"THREAD-"))
43 "Type of native threads which are attached to the runtime as Lisp threads
46 #!+(and sb-safepoint-strictly
(not win32
))
47 (def!struct
(signal-handling-thread
48 (:include foreign-thread
)
49 (:conc-name
"THREAD-"))
51 "Asynchronous signal handling thread."
52 (signal-number nil
:type integer
))
54 (declaim (inline make-mutex
)) ;; for possible DX-allocating
58 (name nil
:type
(or null thread-name
))
59 (%owner nil
:type
(or null thread
))
60 #!+(and sb-thread sb-futex
)
61 (state 0 :type fixnum
))
62 (declaim (notinline make-mutex
))
64 (defun mutex-value (mutex)
66 "Current owner of the mutex, NIL if the mutex is free. May return a
67 stale value, use MUTEX-OWNER instead."
70 (defun holding-mutex-p (mutex)
72 "Test whether the current thread is holding MUTEX."
73 ;; This is about the only use for which a stale value of owner is
75 (eq sb
!thread
:*current-thread
* (mutex-%owner mutex
)))
77 (defsetf mutex-value set-mutex-value
)
79 (declaim (inline set-mutex-value
))
80 (defun set-mutex-value (mutex value
)
81 (declare (ignore mutex value
))
82 (error "~S is no longer supported." '(setf mutex-value
)))
84 (define-compiler-macro set-mutex-value
(&whole form mutex value
)
85 (declare (ignore mutex value
))
86 (warn "~S is no longer supported, and will signal an error at runtime."
90 ;;; SPINLOCK no longer exists as a type -- provided for backwards compatibility.
95 (deprecation-warning :early
"1.0.53.11" 'spinlock
'mutex
)
98 (define-deprecated-function :early
"1.0.53.11" make-spinlock make-mutex
(&key name
)
99 (make-mutex :name name
))
101 (define-deprecated-function :early
"1.0.53.11" spinlock-name mutex-name
(lock)
104 (define-deprecated-function :early
"1.0.53.11" (setf spinlock-name
) (setf mutex-name
) (name lock
)
105 (setf (mutex-name lock
) name
))
107 (define-deprecated-function :early
"1.0.53.11" spinlock-value mutex-owner
(lock)
110 (define-deprecated-function :early
"1.0.53.11" get-spinlock grab-mutex
(lock)
113 (define-deprecated-function :early
"1.0.53.11" release-spinlock release-mutex
(lock)
114 (release-mutex lock
))
116 (sb!xc
:defmacro with-recursive-spinlock
((lock) &body body
)
117 (deprecation-warning :early
"1.0.53.11" 'with-recursive-spinlock
'with-recursive-lock
)
118 `(with-recursive-lock (,lock
)
121 (sb!xc
:defmacro with-spinlock
((lock) &body body
)
122 (deprecation-warning :early
"1.0.53.11" 'with-spinlock
'with-mutex
)
126 (sb!xc
:defmacro without-thread-waiting-for
((&key already-without-interrupts
) &body body
)
127 (with-unique-names (thread prev
)
128 (let ((without (if already-without-interrupts
130 'without-interrupts
))
131 (with (if already-without-interrupts
133 'with-local-interrupts
)))
134 `(let* ((,thread
*current-thread
*)
137 (thread-waiting-for ,thread
))))
138 (flet ((exec () ,@body
))
143 (setf (thread-waiting-for ,thread
) nil
)
146 ;; If we were waiting on a waitqueue, this becomes a bogus
148 (when (mutex-p ,prev
)
149 (setf (thread-waiting-for ,thread
) ,prev
)
150 (barrier (:write
)))))
153 (sb!xc
:defmacro with-mutex
((mutex &key
(wait-p t
) timeout value
)
156 "Acquire MUTEX for the dynamic scope of BODY. If WAIT-P is true (the default),
157 and the MUTEX is not immediately available, sleep until it is available.
159 If TIMEOUT is given, it specifies a relative timeout, in seconds, on how long
160 the system should try to acquire the lock in the contested case.
162 If the mutex isn't acquired succesfully due to either WAIT-P or TIMEOUT, the
163 body is not executed, and WITH-MUTEX returns NIL.
165 Otherwise body is executed with the mutex held by current thread, and
166 WITH-MUTEX returns the values of BODY.
168 Historically WITH-MUTEX also accepted a VALUE argument, which when provided
169 was used as the new owner of the mutex instead of the current thread. This is
170 no longer supported: if VALUE is provided, it must be either NIL or the
172 `(dx-flet ((with-mutex-thunk () ,@body
))
180 (sb!xc
:defmacro with-system-mutex
((mutex
181 &key without-gcing allow-with-interrupts
)
183 `(dx-flet ((with-system-mutex-thunk () ,@body
))
184 (,(cond (without-gcing
185 'call-with-system-mutex
/without-gcing
)
186 (allow-with-interrupts
187 'call-with-system-mutex
/allow-with-interrupts
)
189 'call-with-system-mutex
))
190 #'with-system-mutex-thunk
193 (sb!xc
:defmacro with-recursive-lock
((mutex &key
(wait-p t
) timeout
) &body body
)
195 "Acquire MUTEX for the dynamic scope of BODY.
197 If WAIT-P is true (the default), and the MUTEX is not immediately available or
198 held by the current thread, sleep until it is available.
200 If TIMEOUT is given, it specifies a relative timeout, in seconds, on how long
201 the system should try to acquire the lock in the contested case.
203 If the mutex isn't acquired succesfully due to either WAIT-P or TIMEOUT, the
204 body is not executed, and WITH-RECURSIVE-LOCK returns NIL.
206 Otherwise body is executed with the mutex held by current thread, and
207 WITH-RECURSIVE-LOCK returns the values of BODY.
209 Unlike WITH-MUTEX, which signals an error on attempt to re-acquire an already
210 held mutex, WITH-RECURSIVE-LOCK allows recursive lock attempts to succeed."
211 `(dx-flet ((with-recursive-lock-thunk () ,@body
))
212 (call-with-recursive-lock
213 #'with-recursive-lock-thunk
218 (sb!xc
:defmacro with-recursive-system-lock
((lock
221 `(dx-flet ((with-recursive-system-lock-thunk () ,@body
))
222 (,(cond (without-gcing
223 'call-with-recursive-system-lock
/without-gcing
)
225 'call-with-recursive-system-lock
))
226 #'with-recursive-system-lock-thunk
229 (macrolet ((def (name &optional variant
)
230 `(defun ,(if variant
(symbolicate name
"/" variant
) name
)
232 (declare (function function
))
233 (flet ((%call-with-system-mutex
()
236 (when (setf got-it
(grab-mutex mutex
))
239 (release-mutex mutex
))))))
240 (declare (inline %call-with-system-mutex
))
243 `(without-gcing (%call-with-system-mutex
)))
244 (:allow-with-interrupts
246 (allow-with-interrupts (%call-with-system-mutex
))))
248 `(without-interrupts (%call-with-system-mutex
))))))))
249 (def call-with-system-mutex
)
250 (def call-with-system-mutex
:without-gcing
)
251 (def call-with-system-mutex
:allow-with-interrupts
))
255 (defun call-with-mutex (function mutex value waitp timeout
)
256 (declare (ignore mutex waitp timeout
)
258 (unless (or (null value
) (eq *current-thread
* value
))
259 (error "~S called with non-nil :VALUE that isn't the current thread."
263 (defun call-with-recursive-lock (function mutex waitp timeout
)
264 (declare (ignore mutex waitp timeout
)
268 (defun call-with-recursive-system-lock (function lock
)
269 (declare (function function
) (ignore lock
))
273 (defun call-with-recursive-system-lock/without-gcing
(function mutex
)
274 (declare (function function
) (ignore mutex
))
276 (funcall function
))))
279 ;;; KLUDGE: These need to use DX-LET, because the cleanup form that
280 ;;; closes over GOT-IT causes a value-cell to be allocated for it --
281 ;;; and we prefer that to go on the stack since it can.
283 (defun call-with-mutex (function mutex value waitp timeout
)
284 (declare (function function
))
285 (unless (or (null value
) (eq *current-thread
* value
))
286 (error "~S called with non-nil :VALUE that isn't the current thread."
288 (dx-let ((got-it nil
))
291 (when (setq got-it
(allow-with-interrupts
292 (grab-mutex mutex
:waitp waitp
294 (with-local-interrupts (funcall function
)))
296 (release-mutex mutex
))))))
298 (defun call-with-recursive-lock (function mutex waitp timeout
)
299 (declare (function function
))
300 (dx-let ((inner-lock-p (eq (mutex-%owner mutex
) *current-thread
*))
304 (when (or inner-lock-p
(setf got-it
(allow-with-interrupts
305 (grab-mutex mutex
:waitp waitp
307 (with-local-interrupts (funcall function
)))
309 (release-mutex mutex
))))))
311 (macrolet ((def (name &optional variant
)
312 `(defun ,(if variant
(symbolicate name
"/" variant
) name
)
314 (declare (function function
))
315 (flet ((%call-with-recursive-system-lock
()
316 (dx-let ((inner-lock-p
317 (eq *current-thread
* (mutex-owner lock
)))
320 (when (or inner-lock-p
321 (setf got-it
(grab-mutex lock
)))
324 (release-mutex lock
))))))
325 (declare (inline %call-with-recursive-system-lock
))
328 `(without-gcing (%call-with-recursive-system-lock
)))
330 `(without-interrupts (%call-with-recursive-system-lock
))))))))
331 (def call-with-recursive-system-lock
)
332 (def call-with-recursive-system-lock
:without-gcing
)))