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 (not sb-lutex) sb-thread)
40 (state 0 :type fixnum)
41 #!+(and sb-lutex sb-thread)
44 (defun mutex-value (mutex)
45 "Current owner of the mutex, NIL if the mutex is free. May return a
46 stale value, use MUTEX-OWNER instead."
49 (defun holding-mutex-p (mutex)
50 "Test whether the current thread is holding MUTEX."
51 ;; This is about the only use for which a stale value of owner is
53 (eq sb!thread:*current-thread* (mutex-%owner mutex)))
55 (defsetf mutex-value set-mutex-value)
57 (declaim (inline set-mutex-value))
58 (defun set-mutex-value (mutex value)
59 (declare (ignore mutex value))
60 (error "~S is no longer supported." '(setf mutex-value)))
62 (define-compiler-macro set-mutex-value (&whole form mutex value)
63 (declare (ignore mutex value))
64 (warn "~S is no longer supported, and will signal an error at runtime."
71 (name nil :type (or null thread-name))
74 (sb!xc:defmacro without-thread-waiting-for ((&key already-without-interrupts) &body body)
75 (with-unique-names (thread prev)
76 (let ((without (if already-without-interrupts
79 (with (if already-without-interrupts
81 'with-local-interrupts)))
82 `(let* ((,thread *current-thread*)
83 (,prev (thread-waiting-for ,thread)))
84 (flet ((exec () ,@body))
89 (setf (thread-waiting-for ,thread) nil)
91 (setf (thread-waiting-for ,thread) ,prev)))
94 (sb!xc:defmacro with-mutex ((mutex &key (value '*current-thread*) (wait-p t))
97 "Acquire MUTEX for the dynamic scope of BODY, setting it to VALUE or
98 some suitable default value if NIL. If WAIT-P is non-NIL and the mutex
99 is in use, sleep until it is available"
100 `(dx-flet ((with-mutex-thunk () ,@body))
107 (sb!xc:defmacro with-system-mutex ((mutex
108 &key without-gcing allow-with-interrupts)
110 `(dx-flet ((with-system-mutex-thunk () ,@body))
111 (,(cond (without-gcing
112 'call-with-system-mutex/without-gcing)
113 (allow-with-interrupts
114 'call-with-system-mutex/allow-with-interrupts)
116 'call-with-system-mutex))
117 #'with-system-mutex-thunk
120 (sb!xc:defmacro with-system-spinlock ((spinlock &key) &body body)
121 `(dx-flet ((with-system-spinlock-thunk () ,@body))
122 (call-with-system-spinlock
123 #'with-system-spinlock-thunk
126 (sb!xc:defmacro with-recursive-lock ((mutex) &body body)
128 "Acquires MUTEX for the dynamic scope of BODY. Within that scope
129 further recursive lock attempts for the same mutex succeed. It is
130 allowed to mix WITH-MUTEX and WITH-RECURSIVE-LOCK for the same mutex
131 provided the default value is used for the mutex."
132 `(dx-flet ((with-recursive-lock-thunk () ,@body))
133 (call-with-recursive-lock
134 #'with-recursive-lock-thunk
137 (sb!xc:defmacro with-recursive-spinlock ((spinlock) &body body)
138 `(dx-flet ((with-recursive-spinlock-thunk () ,@body))
139 (call-with-recursive-spinlock
140 #'with-recursive-spinlock-thunk
143 (sb!xc:defmacro with-recursive-system-spinlock ((spinlock
146 `(dx-flet ((with-recursive-system-spinlock-thunk () ,@body))
147 (,(cond (without-gcing
148 'call-with-recursive-system-spinlock/without-gcing)
150 'call-with-recursive-system-spinlock))
151 #'with-recursive-system-spinlock-thunk
154 (sb!xc:defmacro with-spinlock ((spinlock) &body body)
155 `(dx-flet ((with-spinlock-thunk () ,@body))
157 #'with-spinlock-thunk
160 (macrolet ((def (name &optional variant)
161 `(defun ,(if variant (symbolicate name "/" variant) name)
163 (declare (function function))
164 (flet ((%call-with-system-mutex ()
167 (when (setf got-it (get-mutex mutex))
170 (release-mutex mutex))))))
171 (declare (inline %call-with-system-mutex))
174 `(without-gcing (%call-with-system-mutex)))
175 (:allow-with-interrupts
177 (allow-with-interrupts (%call-with-system-mutex))))
179 `(without-interrupts (%call-with-system-mutex))))))))
180 (def call-with-system-mutex)
181 (def call-with-system-mutex :without-gcing)
182 (def call-with-system-mutex :allow-with-interrupts))
186 (macrolet ((def (name &optional variant)
187 `(defun ,(if variant (symbolicate name "/" variant) name)
189 (declare (ignore lock) (function function))
192 `(without-gcing (funcall function)))
193 (:allow-with-interrupts
195 (allow-with-interrupts (funcall function))))
197 `(without-interrupts (funcall function)))))))
198 (def call-with-system-spinlock)
199 (def call-with-recursive-system-spinlock)
200 (def call-with-recursive-system-spinlock :without-gcing))
202 (defun call-with-mutex (function mutex value waitp)
203 (declare (ignore mutex value waitp)
207 (defun call-with-recursive-lock (function mutex)
208 (declare (ignore mutex) (function function))
211 (defun call-with-spinlock (function spinlock)
212 (declare (ignore spinlock) (function function))
215 (defun call-with-recursive-spinlock (function spinlock)
216 (declare (ignore spinlock) (function function))
220 ;;; KLUDGE: These need to use DX-LET, because the cleanup form that
221 ;;; closes over GOT-IT causes a value-cell to be allocated for it --
222 ;;; and we prefer that to go on the stack since it can.
224 (defun call-with-system-spinlock (function spinlock)
225 (declare (function function))
229 (when (setf got-it (get-spinlock spinlock))
232 (release-spinlock spinlock))))))
234 (macrolet ((def (name &optional variant)
235 `(defun ,(if variant (symbolicate name "/" variant) name)
237 (declare (function function))
238 (flet ((%call-with-system-spinlock ()
239 (dx-let ((inner-lock-p
241 (spinlock-value spinlock)))
244 (when (or inner-lock-p
246 (get-spinlock spinlock)))
249 (release-spinlock spinlock))))))
250 (declare (inline %call-with-system-spinlock))
253 `(without-gcing (%call-with-system-spinlock)))
255 `(without-interrupts (%call-with-system-spinlock))))))))
256 (def call-with-recursive-system-spinlock)
257 (def call-with-recursive-system-spinlock :without-gcing))
259 (defun call-with-spinlock (function spinlock)
260 (declare (function function))
261 (dx-let ((got-it nil))
264 (when (setf got-it (allow-with-interrupts
265 (get-spinlock spinlock)))
266 (with-local-interrupts (funcall function)))
268 (release-spinlock spinlock))))))
270 (defun call-with-mutex (function mutex value waitp)
271 (declare (function function))
272 (dx-let ((got-it nil))
275 (when (setq got-it (allow-with-interrupts
276 (get-mutex mutex value waitp)))
277 (with-local-interrupts (funcall function)))
279 (release-mutex mutex))))))
281 (defun call-with-recursive-lock (function mutex)
282 (declare (function function))
283 (dx-let ((inner-lock-p (eq (mutex-%owner mutex) *current-thread*))
287 (when (or inner-lock-p (setf got-it (allow-with-interrupts
289 (with-local-interrupts (funcall function)))
291 (release-mutex mutex))))))
293 (defun call-with-recursive-spinlock (function spinlock)
294 (declare (function function))
295 (dx-let ((inner-lock-p (eq (spinlock-value spinlock) *current-thread*))
299 (when (or inner-lock-p (setf got-it (allow-with-interrupts
300 (get-spinlock spinlock))))
301 (with-local-interrupts (funcall function)))
303 (release-spinlock spinlock)))))))