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-recursive-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 (value '*current-thread*) (wait-p t))
131 "Acquire MUTEX for the dynamic scope of BODY, setting it to VALUE or
132 some suitable default value if NIL. If WAIT-P is non-NIL and the mutex
133 is in use, sleep until it is available"
134 `(dx-flet ((with-mutex-thunk () ,@body))
141 (sb!xc:defmacro with-system-mutex ((mutex
142 &key without-gcing allow-with-interrupts)
144 `(dx-flet ((with-system-mutex-thunk () ,@body))
145 (,(cond (without-gcing
146 'call-with-system-mutex/without-gcing)
147 (allow-with-interrupts
148 'call-with-system-mutex/allow-with-interrupts)
150 'call-with-system-mutex))
151 #'with-system-mutex-thunk
154 (sb!xc:defmacro with-recursive-lock ((mutex) &body body)
156 "Acquires MUTEX for the dynamic scope of BODY. Within that scope
157 further recursive lock attempts for the same mutex succeed. It is
158 allowed to mix WITH-MUTEX and WITH-RECURSIVE-LOCK for the same mutex
159 provided the default value is used for the mutex."
160 `(dx-flet ((with-recursive-lock-thunk () ,@body))
161 (call-with-recursive-lock
162 #'with-recursive-lock-thunk
165 (sb!xc:defmacro with-recursive-system-lock ((lock
168 `(dx-flet ((with-recursive-system-lock-thunk () ,@body))
169 (,(cond (without-gcing
170 'call-with-recursive-system-lock/without-gcing)
172 'call-with-recursive-system-lock))
173 #'with-recursive-system-lock-thunk
176 (macrolet ((def (name &optional variant)
177 `(defun ,(if variant (symbolicate name "/" variant) name)
179 (declare (function function))
180 (flet ((%call-with-system-mutex ()
183 (when (setf got-it (get-mutex mutex))
186 (release-mutex mutex))))))
187 (declare (inline %call-with-system-mutex))
190 `(without-gcing (%call-with-system-mutex)))
191 (:allow-with-interrupts
193 (allow-with-interrupts (%call-with-system-mutex))))
195 `(without-interrupts (%call-with-system-mutex))))))))
196 (def call-with-system-mutex)
197 (def call-with-system-mutex :without-gcing)
198 (def call-with-system-mutex :allow-with-interrupts))
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-recursive-system-lock (function lock)
212 (declare (function function) (ignore lock))
216 (defun call-with-recursive-system-lock/without-gcing (function mutex)
217 (declare (function function) (ignore mutex))
219 (funcall function))))
222 ;;; KLUDGE: These need to use DX-LET, because the cleanup form that
223 ;;; closes over GOT-IT causes a value-cell to be allocated for it --
224 ;;; and we prefer that to go on the stack since it can.
226 (defun call-with-mutex (function mutex value waitp)
227 (declare (function function))
228 (dx-let ((got-it nil))
231 (when (setq got-it (allow-with-interrupts
232 (get-mutex mutex value waitp)))
233 (with-local-interrupts (funcall function)))
235 (release-mutex mutex))))))
237 (defun call-with-recursive-lock (function mutex)
238 (declare (function function))
239 (dx-let ((inner-lock-p (eq (mutex-%owner mutex) *current-thread*))
243 (when (or inner-lock-p (setf got-it (allow-with-interrupts
245 (with-local-interrupts (funcall function)))
247 (release-mutex mutex))))))
249 (macrolet ((def (name &optional variant)
250 `(defun ,(if variant (symbolicate name "/" variant) name)
252 (declare (function function))
253 (flet ((%call-with-recursive-system-lock ()
254 (dx-let ((inner-lock-p
255 (eq *current-thread* (mutex-owner lock)))
258 (when (or inner-lock-p
259 (setf got-it (grab-mutex lock)))
262 (release-mutex lock))))))
263 (declare (inline %call-with-recursive-system-lock))
266 `(without-gcing (%call-with-recursive-system-lock)))
268 `(without-interrupts (%call-with-recursive-system-lock))))))))
269 (def call-with-recursive-system-lock)
270 (def call-with-recursive-system-lock :without-gcing)))