1 ;;;; support for threads in the target machine
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")
16 (define-condition thread-error (error)
17 ((thread :reader thread-error-thread :initarg :thread))
20 "Conditions of type THREAD-ERROR are signalled when thread operations fail.
21 The offending thread is initialized by the :THREAD initialization argument and
22 read by the function THREAD-ERROR-THREAD."))
24 (define-condition thread-deadlock (thread-error)
25 ((cycle :initarg :cycle :reader thread-deadlock-cycle))
27 (lambda (condition stream)
28 (let ((*print-circle* t))
29 (format stream "Deadlock cycle detected:~%~@< ~@;~
31 (mapcar #'car (thread-deadlock-cycle condition)))))))
35 (fdocumentation 'thread-error-thread 'function)
36 "Return the offending thread that the THREAD-ERROR pertains to.")
38 (define-condition symbol-value-in-thread-error (cell-error thread-error)
39 ((info :reader symbol-value-in-thread-error-info :initarg :info))
41 (lambda (condition stream)
42 (destructuring-bind (op problem)
43 (symbol-value-in-thread-error-info condition)
44 (format stream "Cannot ~(~A~) value of ~S in ~S: ~S"
46 (cell-error-name condition)
47 (thread-error-thread condition)
49 (:unbound-in-thread "the symbol is unbound in thread.")
50 (:no-tls-value "the symbol has no thread-local value.")
51 (:thread-dead "the thread has exited.")
52 (:invalid-tls-value "the thread-local value is not valid."))))))
55 "Signalled when SYMBOL-VALUE-IN-THREAD or its SETF version fails due to eg.
56 the symbol not having a thread-local value, or the target thread having
57 exited. The offending symbol can be accessed using CELL-ERROR-NAME, and the
58 offending thread using THREAD-ERROR-THREAD."))
60 (define-condition join-thread-error (thread-error) ()
61 (:report (lambda (c s)
62 (format s "Joining thread failed: thread ~A ~
63 did not return normally."
64 (thread-error-thread c))))
67 "Signalled when joining a thread fails due to abnormal exit of the thread
68 to be joined. The offending thread can be accessed using
69 THREAD-ERROR-THREAD."))
71 (define-deprecated-function :late "1.0.29.17" join-thread-error-thread thread-error-thread
73 (thread-error-thread condition))
75 (define-condition interrupt-thread-error (thread-error) ()
76 (:report (lambda (c s)
77 (format s "Interrupt thread failed: thread ~A has exited."
78 (thread-error-thread c))))
81 "Signalled when interrupting a thread fails because the thread has already
82 exited. The offending thread can be accessed using THREAD-ERROR-THREAD."))
84 (define-deprecated-function :late "1.0.29.17" interrupt-thread-error-thread thread-error-thread
86 (thread-error-thread condition))
88 ;;; Of the WITH-PINNED-OBJECTS in this file, not every single one is
89 ;;; necessary because threads are only supported with the conservative
90 ;;; gencgc and numbers on the stack (returned by GET-LISP-OBJ-ADDRESS)
91 ;;; are treated as references.
93 ;;; set the doc here because in early-thread FDOCUMENTATION is not
96 (setf (fdocumentation '*current-thread* 'variable)
97 "Bound in each thread to the thread itself.")
101 (fdocumentation 'thread-name 'function)
102 "Name of the thread. Can be assigned to using SETF. Thread names can be
103 arbitrary printable objects, and need not be unique.")
105 (def!method print-object ((thread thread) stream)
106 (print-unreadable-object (thread stream :type t :identity t)
107 (let* ((cookie (list thread))
108 (info (if (thread-alive-p thread)
111 (join-thread thread :default cookie))))
112 (state (if (eq :running info)
113 (let* ((lock (thread-waiting-for thread)))
116 (list "waiting for:" (cdr lock)
117 "timeout: " (car lock)))
121 (list "waiting for:" lock))))
122 (if (eq cookie (car info))
125 (values (when (eq :finished state)
129 "~@[~S ~]~:[~{~I~A~^~2I~_ ~}~_~;~A~:[ no values~; values: ~:*~{~S~^, ~}~]~]"
135 (defun print-lock (lock name owner stream)
136 (let ((*print-circle* t))
137 (print-unreadable-object (lock stream :type t :identity (not name))
139 (format stream "~@[~S ~]~2I~_owner: ~S" name owner)
140 (format stream "~@[~S ~](free)" name)))))
142 (def!method print-object ((mutex mutex) stream)
143 (print-lock mutex (mutex-name mutex) (mutex-owner mutex) stream))
145 (def!method print-object ((spinlock spinlock) stream)
146 (print-lock spinlock (spinlock-name spinlock) (spinlock-value spinlock) stream))
148 (defun thread-alive-p (thread)
150 "Return T if THREAD is still alive. Note that the return value is
151 potentially stale even before the function returns, as the thread may exit at
153 (thread-%alive-p thread))
155 ;; A thread is eligible for gc iff it has finished and there are no
156 ;; more references to it. This list is supposed to keep a reference to
157 ;; all running threads.
158 (defvar *all-threads* ())
159 (defvar *all-threads-lock* (make-mutex :name "all threads lock"))
161 (defvar *default-alloc-signal* nil)
163 (defmacro with-all-threads-lock (&body body)
164 `(with-system-mutex (*all-threads-lock*)
167 (defun list-all-threads ()
169 "Return a list of the live threads. Note that the return value is
170 potentially stale even before the function returns, as new threads may be
171 created and old ones may exit at any time."
172 (with-all-threads-lock
173 (copy-list *all-threads*)))
175 (declaim (inline current-thread-sap))
176 (defun current-thread-sap ()
177 (sb!vm::current-thread-offset-sap sb!vm::thread-this-slot))
179 (declaim (inline current-thread-os-thread))
180 (defun current-thread-os-thread ()
182 (sap-int (sb!vm::current-thread-offset-sap sb!vm::thread-os-thread-slot))
186 (defun init-initial-thread ()
187 (/show0 "Entering INIT-INITIAL-THREAD")
188 (let ((initial-thread (%make-thread :name "initial thread"
190 :os-thread (current-thread-os-thread))))
191 (setq *current-thread* initial-thread)
192 ;; Either *all-threads* is empty or it contains exactly one thread
193 ;; in case we are in reinit since saving core with multiple
194 ;; threads doesn't work.
195 (setq *all-threads* (list initial-thread))))
198 ;;;; Aliens, low level stuff
200 (define-alien-routine "kill_safely"
202 (os-thread #!-alpha unsigned-long #!+alpha unsigned-int)
207 ;; FIXME it would be good to define what a thread id is or isn't
208 ;; (our current assumption is that it's a fixnum). It so happens
209 ;; that on Linux it's a pid, but it might not be on posix thread
211 (define-alien-routine ("create_thread" %create-thread)
212 unsigned-long (lisp-fun-address unsigned-long))
214 (declaim (inline %block-deferrable-signals))
215 (define-alien-routine ("block_deferrable_signals" %block-deferrable-signals)
217 (where sb!alien:unsigned-long)
218 (old sb!alien:unsigned-long))
220 (defun block-deferrable-signals ()
221 (%block-deferrable-signals 0 0))
225 (declaim (inline %lutex-init %lutex-wait %lutex-wake
226 %lutex-lock %lutex-unlock))
228 (define-alien-routine ("lutex_init" %lutex-init)
229 int (lutex unsigned-long))
231 (define-alien-routine ("lutex_wait" %lutex-wait)
232 int (queue-lutex unsigned-long) (mutex-lutex unsigned-long))
234 (define-alien-routine ("lutex_wake" %lutex-wake)
235 int (lutex unsigned-long) (n int))
237 (define-alien-routine ("lutex_lock" %lutex-lock)
238 int (lutex unsigned-long))
240 (define-alien-routine ("lutex_trylock" %lutex-trylock)
241 int (lutex unsigned-long))
243 (define-alien-routine ("lutex_unlock" %lutex-unlock)
244 int (lutex unsigned-long))
246 (define-alien-routine ("lutex_destroy" %lutex-destroy)
247 int (lutex unsigned-long))
249 ;; FIXME: Defining a whole bunch of alien-type machinery just for
250 ;; passing primitive lutex objects directly to foreign functions
251 ;; doesn't seem like fun right now. So instead we just manually
252 ;; pin the lutex, get its address, and let the callee untag it.
253 (defmacro with-lutex-address ((name lutex) &body body)
254 `(let ((,name ,lutex))
255 (with-pinned-objects (,name)
256 (let ((,name (get-lisp-obj-address ,name)))
260 (/show0 "Entering MAKE-LUTEX")
261 ;; Suppress GC until the lutex has been properly registered with
264 (let ((lutex (sb!vm::%make-lutex)))
267 (with-lutex-address (lutex lutex)
273 (declaim (inline futex-wait %futex-wait futex-wake))
275 (define-alien-routine ("futex_wait" %futex-wait)
276 int (word unsigned-long) (old-value unsigned-long)
277 (to-sec long) (to-usec unsigned-long))
279 (defun futex-wait (word old to-sec to-usec)
281 (%futex-wait word old to-sec to-usec)))
283 (define-alien-routine "futex_wake"
284 int (word unsigned-long) (n unsigned-long))))
286 ;;; used by debug-int.lisp to access interrupt contexts
287 #!-(or sb-fluid sb-thread) (declaim (inline sb!vm::current-thread-offset-sap))
289 (defun sb!vm::current-thread-offset-sap (n)
290 (declare (type (unsigned-byte 27) n))
291 (sap-ref-sap (alien-sap (extern-alien "all_threads" (* t)))
292 (* n sb!vm:n-word-bytes)))
295 (defun sb!vm::current-thread-offset-sap (n)
296 (declare (type (unsigned-byte 27) n))
297 (sb!vm::current-thread-offset-sap n))
302 (defmacro with-deadlocks ((thread lock &optional timeout) &body forms)
303 (declare (ignorable timeout))
304 (with-unique-names (prev n-thread n-lock n-timeout new)
305 `(let* ((,n-thread ,thread)
307 (,n-timeout #!-sb-lutex
310 (when sb!impl::*deadline*
311 sb!impl::*deadline-seconds*))))
312 ;; If we get interrupted while waiting for a lock, etc.
313 (,prev (thread-waiting-for ,n-thread))
315 (cons ,n-timeout ,n-lock)
317 (declare (dynamic-extent ,new))
318 ;; No WITHOUT-INTERRUPTS, since WITH-DEADLOCKS is used
319 ;; in places where interrupts should already be disabled.
322 (setf (thread-waiting-for ,n-thread) ,new)
324 (setf (thread-waiting-for ,n-thread) ,prev)))))
326 (declaim (inline get-spinlock release-spinlock))
328 ;;; Should always be called with interrupts disabled.
329 (defun get-spinlock (spinlock)
330 (declare (optimize (speed 3) (safety 0)))
331 (let* ((new *current-thread*)
332 (old (sb!ext:compare-and-swap (spinlock-value spinlock) nil new)))
335 (error "Recursive lock attempt on ~S." spinlock))
337 (with-deadlocks (new spinlock)
339 (if (sb!ext:compare-and-swap (spinlock-value spinlock) nil new)
341 (return-from get-spinlock t))))
345 (with-interrupts (check-deadlock))
346 (if (and (not *interrupts-enabled*) *allow-with-interrupts*)
347 ;; If interrupts are disabled, but we are allowed to
348 ;; enabled them, check for pending interrupts every once
349 ;; in a while. %CHECK-INTERRUPTS is taking shortcuts, make
350 ;; sure that deferrables are unblocked by doing an empty
351 ;; WITH-INTERRUPTS once.
355 (loop repeat 128 do (cas)) ; 128 is arbitrary here
356 (sb!unix::%check-interrupts)))
360 (defun release-spinlock (spinlock)
361 (declare (optimize (speed 3) (safety 0)))
362 ;; On x86 and x86-64 we can get away with no memory barriers, (see
363 ;; Linux kernel mailing list "spin_unlock optimization(i386)"
364 ;; thread, summary at
365 ;; http://kt.iserv.nl/kernel-traffic/kt19991220_47.html#1.
367 ;; If the compiler may reorder this with other instructions, insert
368 ;; compiler barrier here.
370 ;; FIXME: this does not work on SMP Pentium Pro and OOSTORE systems,
371 ;; neither on most non-x86 architectures (but we don't have threads
373 (setf (spinlock-value spinlock) nil)
375 ;; FIXME: Is a :memory barrier too strong here? Can we use a :write
377 #!+(not (or x86 x86-64))
384 (setf (fdocumentation 'make-mutex 'function)
386 (fdocumentation 'mutex-name 'function)
387 "The name of the mutex. Setfable.")
389 #!+(and sb-thread (not sb-lutex))
391 (define-structure-slot-addressor mutex-state-address
394 ;; Important: current code assumes these are fixnums or other
395 ;; lisp objects that don't need pinning.
396 (defconstant +lock-free+ 0)
397 (defconstant +lock-taken+ 1)
398 (defconstant +lock-contested+ 2))
400 (defun mutex-owner (mutex)
401 "Current owner of the mutex, NIL if the mutex is free. Naturally,
402 this is racy by design (another thread may acquire the mutex after
403 this function returns), it is intended for informative purposes. For
404 testing whether the current thread is holding a mutex see
406 ;; Make sure to get the current value.
407 (sb!ext:compare-and-swap (mutex-%owner mutex) nil nil))
409 ;;; Signals an error if owner of LOCK is waiting on a lock whose release
410 ;;; depends on the current thread. Does not detect deadlocks from sempahores.
411 (defun check-deadlock ()
412 (let* ((self *current-thread*)
413 (origin (thread-waiting-for self)))
414 (labels ((lock-owner (lock)
416 (mutex (mutex-%owner lock))
417 (spinlock (spinlock-value lock))))
418 (detect-deadlock (lock)
419 (let ((other-thread (lock-owner lock)))
420 (cond ((not other-thread))
421 ((eq self other-thread)
422 (let* ((chain (deadlock-chain self origin))
425 "~%WARNING: DEADLOCK CYCLE DETECTED:~%~@< ~@;~
428 (mapcar #'car chain))))
429 ;; Barf to stderr in case the system is too tied up
430 ;; to report the error properly -- to avoid cross-talk
431 ;; build the whole string up first.
432 (write-string barf sb!sys:*stderr*)
433 (finish-output sb!sys:*stderr*)
434 (error 'thread-deadlock
435 :thread *current-thread*
438 (let ((other-lock (thread-waiting-for other-thread)))
439 ;; If the thread is waiting with a timeout OTHER-LOCK
440 ;; is a cons, and we don't consider it a deadlock -- since
441 ;; it will time out on its own sooner or later.
442 (when (and other-lock (not (consp other-lock)))
443 (detect-deadlock other-lock)))))))
444 (deadlock-chain (thread lock)
445 (let* ((other-thread (lock-owner lock))
446 (other-lock (when other-thread
447 (thread-waiting-for other-thread))))
448 (cond ((not other-thread)
449 ;; The deadlock is gone -- maybe someone unwound
450 ;; from the same deadlock already?
451 (return-from check-deadlock nil))
453 ;; There's a timeout -- no deadlock.
454 (return-from check-deadlock nil))
455 ((eq self other-thread)
457 (list (list thread lock)))
460 (cons (list thread lock)
461 (deadlock-chain other-thread other-lock))
462 ;; Again, the deadlock is gone?
463 (return-from check-deadlock nil)))))))
464 ;; Timeout means there is no deadlock
465 (unless (consp origin)
466 (detect-deadlock origin)
469 (defun get-mutex (mutex &optional new-owner
470 (waitp t) (timeout nil))
472 "Deprecated in favor of GRAB-MUTEX."
473 (declare (type mutex mutex) (optimize (speed 3))
474 #!-sb-thread (ignore waitp timeout))
476 (setq new-owner *current-thread*))
478 (let ((old (mutex-%owner mutex)))
479 (when (eq new-owner old)
480 (error "Recursive lock attempt ~S." mutex))
483 (error "Strange deadlock on ~S in an unithreaded build?" mutex)))
485 (setf (mutex-%owner mutex) new-owner)
487 (with-deadlocks (new-owner mutex timeout)
488 ;; FIXME: Lutexes do not currently support deadlines, as at least
489 ;; on Darwin pthread_foo_timedbar functions are not supported:
490 ;; this means that we probably need to use the Carbon multiprocessing
491 ;; functions on Darwin.
493 ;; FIXME: This is definitely not interrupt safe: what happens if
494 ;; we get hit (1) during the lutex calls (ok, they may be safe,
495 ;; but has that been checked?) (2) after the lutex call, but
496 ;; before setting the mutex owner.
500 (error "Mutex timeouts not supported on this platform."))
501 (when (zerop (with-lutex-address (lutex (mutex-lutex mutex))
503 (let ((once (%lutex-trylock lutex)))
509 ;; Check for deadlocks before waiting
511 (%lutex-lock lutex)))))
512 (%lutex-trylock lutex))))
513 ;; FIXME: If %LUTEX-LOCK unwinds due to a signal, we may actually
514 ;; be holding the lock already -- and but neglect to mark ourselves
515 ;; as the owner here. This is bad.
516 (setf (mutex-%owner mutex) new-owner)
520 ;; This is a direct translation of the Mutex 2 algorithm from
521 ;; "Futexes are Tricky" by Ulrich Drepper.
522 (let ((old (sb!ext:compare-and-swap (mutex-state mutex)
525 (unless (or (eql +lock-free+ old) (not waitp))
528 (when (or (eql +lock-contested+ old)
529 (not (eql +lock-free+
530 (sb!ext:compare-and-swap (mutex-state mutex)
533 ;; Wait on the contested lock.
537 (multiple-value-bind (to-sec to-usec stop-sec stop-usec deadlinep)
538 (decode-timeout timeout)
539 (declare (ignore stop-sec stop-usec))
540 (case (with-pinned-objects (mutex)
541 (futex-wait (mutex-state-address mutex)
542 (get-lisp-obj-address +lock-contested+)
547 (return-from get-mutex nil)))
549 (otherwise (return)))))))
550 (setf old (sb!ext:compare-and-swap (mutex-state mutex)
554 (unless (eql +lock-free+ old)
556 (cond ((eql +lock-free+ old)
557 (let ((prev (sb!ext:compare-and-swap (mutex-%owner mutex)
560 (bug "Old owner in free mutex: ~S" prev))
563 (bug "Failed to acquire lock with WAITP."))))))
565 (defun grab-mutex (mutex &key (waitp t) (timeout nil))
567 "Acquire MUTEX for the current thread. If WAITP is true (the default) and
568 the mutex is not immediately available, sleep until it is available.
570 If TIMEOUT is given, it specifies a relative timeout, in seconds, on
571 how long GRAB-MUTEX should try to acquire the lock in the contested
572 case. Unsupported on :SB-LUTEX platforms (eg. Darwin), where a non-NIL
573 TIMEOUT signals an error.
575 If GRAB-MUTEX returns T, the lock acquisition was successful. In case
576 of WAITP being NIL, or an expired TIMEOUT, GRAB-MUTEX may also return
577 NIL which denotes that GRAB-MUTEX did -not- acquire the lock.
581 - GRAB-MUTEX is not interrupt safe. The correct way to call it is:
585 (ALLOW-WITH-INTERRUPTS (GRAB-MUTEX ...))
588 WITHOUT-INTERRUPTS is necessary to avoid an interrupt unwinding
589 the call while the mutex is in an inconsistent state while
590 ALLOW-WITH-INTERRUPTS allows the call to be interrupted from
593 - (GRAB-MUTEX <mutex> :timeout 0.0) differs from
594 (GRAB-MUTEX <mutex> :waitp nil) in that the former may signal a
595 DEADLINE-TIMEOUT if the global deadline was due already on
598 The exact interplay of GRAB-MUTEX and deadlines are reserved to
599 change in future versions.
601 - It is recommended that you use WITH-MUTEX instead of calling
604 (get-mutex mutex nil waitp timeout))
606 (defun release-mutex (mutex &key (if-not-owner :punt))
608 "Release MUTEX by setting it to NIL. Wake up threads waiting for
611 RELEASE-MUTEX is not interrupt safe: interrupts should be disabled
614 If the current thread is not the owner of the mutex then it silently
615 returns without doing anything (if IF-NOT-OWNER is :PUNT), signals a
616 WARNING (if IF-NOT-OWNER is :WARN), or releases the mutex anyway (if
617 IF-NOT-OWNER is :FORCE)."
618 (declare (type mutex mutex))
619 ;; Order matters: set owner to NIL before releasing state.
620 (let* ((self *current-thread*)
621 (old-owner (sb!ext:compare-and-swap (mutex-%owner mutex) self nil)))
622 (unless (eql self old-owner)
624 ((:punt) (return-from release-mutex nil))
626 (warn "Releasing ~S, owned by another thread: ~S" mutex old-owner))
630 (setf (mutex-%owner mutex) nil)
632 (with-lutex-address (lutex (mutex-lutex mutex))
633 (%lutex-unlock lutex))
635 ;; FIXME: once ATOMIC-INCF supports struct slots with word sized
636 ;; unsigned-byte type this can be used:
638 ;; (let ((old (sb!ext:atomic-incf (mutex-state mutex) -1)))
639 ;; (unless (eql old +lock-free+)
640 ;; (setf (mutex-state mutex) +lock-free+)
641 ;; (with-pinned-objects (mutex)
642 ;; (futex-wake (mutex-state-address mutex) 1))))
643 (let ((old (sb!ext:compare-and-swap (mutex-state mutex)
644 +lock-taken+ +lock-free+)))
645 (when (eql old +lock-contested+)
646 (sb!ext:compare-and-swap (mutex-state mutex)
647 +lock-contested+ +lock-free+)
648 (with-pinned-objects (mutex)
649 (futex-wake (mutex-state-address mutex) 1))))
653 ;;;; Waitqueues/condition variables
655 (defstruct (waitqueue (:constructor %make-waitqueue))
658 (name nil :type (or null thread-name))
659 #!+(and sb-lutex sb-thread)
664 (def!method print-object ((waitqueue waitqueue) stream)
665 (print-unreadable-object (waitqueue stream :type t :identity t)
666 (format stream "~@[~A~]" (waitqueue-name waitqueue))))
668 (defun make-waitqueue (&key name)
670 "Create a waitqueue."
671 (%make-waitqueue :name name))
674 (setf (fdocumentation 'waitqueue-name 'function)
675 "The name of the waitqueue. Setfable.")
677 #!+(and sb-thread (not sb-lutex))
678 (define-structure-slot-addressor waitqueue-token-address
682 (defun condition-wait (queue mutex)
684 "Atomically release MUTEX and enqueue ourselves on QUEUE. Another thread may
685 subsequently notify us using CONDITION-NOTIFY, at which time we reacquire
686 MUTEX and return to the caller.
688 Important: CONDITION-WAIT may return without CONDITION-NOTIFY having occurred.
689 The correct way to write code that uses CONDITION-WAIT is to loop around the
690 call, checking the the associated data:
693 (defvar *queue* (make-waitqueue))
694 (defvar *lock* (make-mutex))
700 do (condition-wait *queue* *lock*))
704 (defun push-data (data)
707 (condition-notify *queue*)))
709 Also note that if CONDITION-WAIT unwinds (due to eg. a timeout) instead of
710 returning normally, it may do so without holding the mutex."
711 #!-sb-thread (declare (ignore queue))
713 #!-sb-thread (error "Not supported in unithread builds.")
715 (let ((me *current-thread*))
717 (assert (eq me (mutex-%owner mutex)))
718 (/show0 "CONDITION-WAITing")
720 ;; Need to disable interrupts so that we don't miss setting the
721 ;; owner on our way out. (pthread_cond_wait handles the actual
726 (setf (mutex-%owner mutex) nil)
727 (with-lutex-address (queue-lutex-address (waitqueue-lutex queue))
728 (with-lutex-address (mutex-lutex-address (mutex-lutex mutex))
729 (with-local-interrupts
730 (%lutex-wait queue-lutex-address mutex-lutex-address)))))
732 (setf (mutex-%owner mutex) me))))
734 ;; Need to disable interrupts so that we don't miss grabbing the
735 ;; mutex on our way out.
737 ;; This setf becomes visible to other CPUS due to the usual
738 ;; memory barrier semantics of lock acquire/release. This must
739 ;; not be moved into the loop else wakeups may be lost upon
740 ;; continuing after a deadline or EINTR.
741 (setf (waitqueue-token queue) me)
743 (multiple-value-bind (to-sec to-usec)
744 (allow-with-interrupts (decode-timeout nil))
745 (case (unwind-protect
746 (with-pinned-objects (queue me)
747 ;; RELEASE-MUTEX is purposefully as close to
748 ;; FUTEX-WAIT as possible to reduce the size of
749 ;; the window where the token may be set by a
751 (release-mutex mutex)
752 ;; Now we go to sleep using futex-wait. If
753 ;; anyone else manages to grab MUTEX and call
754 ;; CONDITION-NOTIFY during this comment, it
755 ;; will change the token, and so futex-wait
756 ;; returns immediately instead of sleeping.
757 ;; Ergo, no lost wakeup. We may get spurious
758 ;; wakeups, but that's ok.
759 (allow-with-interrupts
760 (futex-wait (waitqueue-token-address queue)
761 (get-lisp-obj-address me)
762 ;; our way of saying "no
766 ;; If we are interrupted while waiting, we should
767 ;; do these things before returning. Ideally, in
768 ;; the case of an unhandled signal, we should do
769 ;; them before entering the debugger, but this is
770 ;; better than nothing.
771 (allow-with-interrupts (get-mutex mutex)))
772 ;; ETIMEDOUT; we know it was a timeout, yet we cannot
773 ;; signal a deadline unconditionally here because the
774 ;; call to GET-MUTEX may already have signaled it.
776 ;; EINTR; we do not need to return to the caller because
777 ;; an interleaved wakeup would change the token causing an
778 ;; EWOULDBLOCK in the next iteration.
780 ;; EWOULDBLOCK, -1 here, is the possible spurious wakeup
781 ;; case. 0 is the normal wakeup.
782 (otherwise (return))))))))
784 (defun condition-notify (queue &optional (n 1))
786 "Notify N threads waiting on QUEUE. The same mutex that is used in
787 the corresponding CONDITION-WAIT must be held by this thread during
789 #!-sb-thread (declare (ignore queue n))
790 #!-sb-thread (error "Not supported in unithread builds.")
792 (declare (type (and fixnum (integer 1)) n))
793 (/show0 "Entering CONDITION-NOTIFY")
797 (with-lutex-address (lutex (waitqueue-lutex queue))
798 (%lutex-wake lutex n))
799 ;; No problem if >1 thread notifies during the comment in condition-wait:
800 ;; as long as the value in queue-data isn't the waiting thread's id, it
801 ;; matters not what it is -- using the queue object itself is handy.
803 ;; XXX we should do something to ensure that the result of this setf
804 ;; is visible to all CPUs.
806 ;; ^-- surely futex_wake() involves a memory barrier?
809 (setf (waitqueue-token queue) queue)
810 (with-pinned-objects (queue)
811 (futex-wake (waitqueue-token-address queue) n)))))
813 (defun condition-broadcast (queue)
815 "Notify all threads waiting on QUEUE."
816 (condition-notify queue
817 ;; On a 64-bit platform truncating M-P-F to an int
818 ;; results in -1, which wakes up only one thread.
820 most-positive-fixnum)))
825 (defstruct (semaphore (:constructor %make-semaphore (name %count)))
827 "Semaphore type. The fact that a SEMAPHORE is a STRUCTURE-OBJECT
828 should be considered an implementation detail, and may change in the
830 (name nil :type (or null thread-name))
831 (%count 0 :type (integer 0))
832 (waitcount 0 :type sb!vm:word)
834 (queue (make-waitqueue)))
836 (setf (fdocumentation 'semaphore-name 'function)
837 "The name of the semaphore INSTANCE. Setfable.")
839 (declaim (inline semaphore-count))
840 (defun semaphore-count (instance)
841 "Returns the current count of the semaphore INSTANCE."
842 (semaphore-%count instance))
844 (defun make-semaphore (&key name (count 0))
846 "Create a semaphore with the supplied COUNT and NAME."
847 (%make-semaphore name count))
849 (defun wait-on-semaphore (semaphore)
851 "Decrement the count of SEMAPHORE if the count would not be
852 negative. Else blocks until the semaphore can be decremented."
853 ;; A more direct implementation based directly on futexes should be
856 ;; We need to disable interrupts so that we don't forget to
857 ;; decrement the waitcount (which would happen if an asynch
858 ;; interrupt should catch us on our way out from the loop.)
859 (with-system-mutex ((semaphore-mutex semaphore) :allow-with-interrupts t)
860 ;; Quick check: is it positive? If not, enter the wait loop.
861 (let ((count (semaphore-%count semaphore)))
863 (setf (semaphore-%count semaphore) (1- count))
866 ;; Need to use ATOMIC-INCF despite the lock, because on our
867 ;; way out from here we might not be locked anymore -- so
868 ;; another thread might be tweaking this in parallel using
869 ;; ATOMIC-DECF. No danger over overflow, since there it
870 ;; at most one increment per thread waiting on the semaphore.
871 (sb!ext:atomic-incf (semaphore-waitcount semaphore))
872 (loop until (plusp (setf count (semaphore-%count semaphore)))
873 do (condition-wait (semaphore-queue semaphore)
874 (semaphore-mutex semaphore)))
875 (setf (semaphore-%count semaphore) (1- count)))
876 ;; Need to use ATOMIC-DECF instead of DECF, as CONDITION-WAIT
877 ;; may unwind without the lock being held due to timeouts.
878 (sb!ext:atomic-decf (semaphore-waitcount semaphore)))))))
880 (defun try-semaphore (semaphore &optional (n 1))
882 "Try to decrement the count of SEMAPHORE by N. If the count were to
883 become negative, punt and return NIL, otherwise return true."
884 (declare (type (integer 1) n))
885 (with-mutex ((semaphore-mutex semaphore))
886 (let ((new-count (- (semaphore-%count semaphore) n)))
887 (when (not (minusp new-count))
888 (setf (semaphore-%count semaphore) new-count)))))
890 (defun signal-semaphore (semaphore &optional (n 1))
892 "Increment the count of SEMAPHORE by N. If there are threads waiting
893 on this semaphore, then N of them is woken up."
894 (declare (type (integer 1) n))
895 ;; Need to disable interrupts so that we don't lose a wakeup after
896 ;; we have incremented the count.
897 (with-system-mutex ((semaphore-mutex semaphore) :allow-with-interrupts t)
898 (let ((waitcount (semaphore-waitcount semaphore))
899 (count (incf (semaphore-%count semaphore) n)))
900 (when (plusp waitcount)
901 (condition-notify (semaphore-queue semaphore) (min waitcount count))))))
904 ;;;; Job control, independent listeners
907 (lock (make-mutex :name "session lock"))
909 (interactive-threads nil)
910 (interactive-threads-queue (make-waitqueue)))
912 (defvar *session* nil)
914 ;;; The debugger itself tries to acquire the session lock, don't let
915 ;;; funny situations (like getting a sigint while holding the session
916 ;;; lock) occur. At the same time we need to allow interrupts while
917 ;;; *waiting* for the session lock for things like GET-FOREGROUND to
918 ;;; be interruptible.
920 ;;; Take care: we sometimes need to obtain the session lock while
921 ;;; holding on to *ALL-THREADS-LOCK*, so we must _never_ obtain it
922 ;;; _after_ getting a session lock! (Deadlock risk.)
924 ;;; FIXME: It would be good to have ordered locks to ensure invariants
926 (defmacro with-session-lock ((session) &body body)
927 `(with-system-mutex ((session-lock ,session) :allow-with-interrupts t)
930 (defun new-session ()
931 (make-session :threads (list *current-thread*)
932 :interactive-threads (list *current-thread*)))
934 (defun init-job-control ()
935 (/show0 "Entering INIT-JOB-CONTROL")
936 (setf *session* (new-session))
937 (/show0 "Exiting INIT-JOB-CONTROL"))
939 (defun %delete-thread-from-session (thread session)
940 (with-session-lock (session)
941 (setf (session-threads session)
942 (delete thread (session-threads session))
943 (session-interactive-threads session)
944 (delete thread (session-interactive-threads session)))))
946 (defun call-with-new-session (fn)
947 (%delete-thread-from-session *current-thread* *session*)
948 (let ((*session* (new-session)))
951 (defmacro with-new-session (args &body forms)
952 (declare (ignore args)) ;for extensibility
953 (sb!int:with-unique-names (fb-name)
954 `(labels ((,fb-name () ,@forms))
955 (call-with-new-session (function ,fb-name)))))
957 ;;; Remove thread from its session, if it has one.
959 (defun handle-thread-exit (thread)
960 (/show0 "HANDLING THREAD EXIT")
962 (with-all-threads-lock
963 (setf (thread-%alive-p thread) nil)
964 (setf (thread-os-thread thread) nil)
965 (setq *all-threads* (delete thread *all-threads*))
967 (%delete-thread-from-session thread *session*)))
970 (/show0 "FREEING MUTEX LUTEX")
971 (with-lutex-address (lutex (mutex-lutex (thread-interruptions-lock thread)))
972 (%lutex-destroy lutex))))
974 (defun terminate-session ()
976 "Kill all threads in session except for this one. Does nothing if current
977 thread is not the foreground thread."
978 ;; FIXME: threads created in other threads may escape termination
980 (with-session-lock (*session*)
981 (and (eq *current-thread*
982 (car (session-interactive-threads *session*)))
983 (session-threads *session*)))))
984 ;; do the kill after dropping the mutex; unwind forms in dying
985 ;; threads may want to do session things
986 (dolist (thread to-kill)
987 (unless (eq thread *current-thread*)
988 ;; terminate the thread but don't be surprised if it has
989 ;; exited in the meantime
990 (handler-case (terminate-thread thread)
991 (interrupt-thread-error ()))))))
993 ;;; called from top of invoke-debugger
994 (defun debugger-wait-until-foreground-thread (stream)
995 "Returns T if thread had been running in background, NIL if it was
997 (declare (ignore stream))
1001 (with-session-lock (*session*)
1002 (not (member *current-thread*
1003 (session-interactive-threads *session*))))
1006 (defun get-foreground ()
1009 (let ((was-foreground t))
1011 (/show0 "Looping in GET-FOREGROUND")
1012 (with-session-lock (*session*)
1013 (let ((int-t (session-interactive-threads *session*)))
1014 (when (eq (car int-t) *current-thread*)
1015 (unless was-foreground
1016 (format *query-io* "Resuming thread ~A~%" *current-thread*))
1017 (return-from get-foreground t))
1018 (setf was-foreground nil)
1019 (unless (member *current-thread* int-t)
1020 (setf (cdr (last int-t))
1021 (list *current-thread*)))
1023 (session-interactive-threads-queue *session*)
1024 (session-lock *session*)))))))
1026 (defun release-foreground (&optional next)
1028 "Background this thread. If NEXT is supplied, arrange for it to
1029 have the foreground next."
1030 #!-sb-thread (declare (ignore next))
1033 (with-session-lock (*session*)
1034 (when (rest (session-interactive-threads *session*))
1035 (setf (session-interactive-threads *session*)
1036 (delete *current-thread* (session-interactive-threads *session*))))
1038 (setf (session-interactive-threads *session*)
1040 (delete next (session-interactive-threads *session*)))))
1041 (condition-broadcast (session-interactive-threads-queue *session*))))
1043 (defun foreground-thread ()
1044 (car (session-interactive-threads *session*)))
1046 (defun make-listener-thread (tty-name)
1047 (assert (probe-file tty-name))
1048 (let* ((in (sb!unix:unix-open (namestring tty-name) sb!unix:o_rdwr #o666))
1049 (out (sb!unix:unix-dup in))
1050 (err (sb!unix:unix-dup in)))
1051 (labels ((thread-repl ()
1052 (sb!unix::unix-setsid)
1053 (let* ((sb!impl::*stdin*
1054 (make-fd-stream in :input t :buffering :line
1057 (make-fd-stream out :output t :buffering :line
1060 (make-fd-stream err :output t :buffering :line
1063 (make-fd-stream err :input t :output t
1066 (sb!impl::*descriptor-handlers* nil))
1067 (with-new-session ()
1069 (sb!impl::toplevel-repl nil)
1070 (sb!int:flush-standard-output-streams))))))
1071 (make-thread #'thread-repl))))
1076 (defun make-thread (function &key name arguments)
1078 "Create a new thread of NAME that runs FUNCTION with the argument
1079 list designator provided (defaults to no argument). When the function
1080 returns the thread exits. The return values of FUNCTION are kept
1081 around and can be retrieved by JOIN-THREAD."
1082 #!-sb-thread (declare (ignore function name arguments))
1083 #!-sb-thread (error "Not supported in unithread builds.")
1084 #!+sb-thread (assert (or (atom arguments)
1085 (null (cdr (last arguments))))
1087 "Argument passed to ~S, ~S, is an improper list."
1088 'make-thread arguments)
1090 (let* ((thread (%make-thread :name name))
1091 (setup-sem (make-semaphore :name "Thread setup semaphore"))
1092 (real-function (coerce function 'function))
1093 (arguments (if (listp arguments)
1097 (named-lambda initial-thread-function ()
1098 ;; In time we'll move some of the binding presently done in C
1101 ;; KLUDGE: Here we have a magic list of variables that are
1102 ;; not thread-safe for one reason or another. As people
1103 ;; report problems with the thread safety of certain
1104 ;; variables, (e.g. "*print-case* in multiple threads
1105 ;; broken", sbcl-devel 2006-07-14), we add a few more
1106 ;; bindings here. The Right Thing is probably some variant
1107 ;; of Allegro's *cl-default-special-bindings*, as that is at
1108 ;; least accessible to users to secure their own libraries.
1109 ;; --njf, 2006-07-15
1111 ;; As it is, this lambda must not cons until we are ready
1112 ;; to run GC. Be very careful.
1113 (let* ((*current-thread* thread)
1114 (*restart-clusters* nil)
1115 (*handler-clusters* (sb!kernel::initial-handler-clusters))
1116 (*condition-restarts* nil)
1117 (sb!impl::*deadline* nil)
1118 (sb!impl::*deadline-seconds* nil)
1119 (sb!impl::*step-out* nil)
1120 ;; internal printer variables
1121 (sb!impl::*previous-case* nil)
1122 (sb!impl::*previous-readtable-case* nil)
1123 (sb!impl::*internal-symbol-output-fun* nil)
1124 (sb!impl::*descriptor-handlers* nil)) ; serve-event
1126 (setf sb!vm:*alloc-signal* *default-alloc-signal*)
1127 (setf (thread-os-thread thread) (current-thread-os-thread))
1128 (with-mutex ((thread-result-lock thread))
1129 (with-all-threads-lock
1130 (push thread *all-threads*))
1131 (with-session-lock (*session*)
1132 (push thread (session-threads *session*)))
1133 (setf (thread-%alive-p thread) t)
1134 (signal-semaphore setup-sem)
1135 ;; can't use handling-end-of-the-world, because that flushes
1136 ;; output streams, and we don't necessarily have any (or we
1137 ;; could be sharing them)
1138 (catch 'sb!impl::toplevel-catcher
1139 (catch 'sb!impl::%end-of-the-world
1140 (with-simple-restart
1143 "~~@<Terminate this thread (~A)~~@:>"
1147 (with-local-interrupts
1148 ;; Now that most things have a chance
1149 ;; to work properly without messing up
1150 ;; other threads, it's time to enable
1152 (sb!unix::unblock-deferrable-signals)
1153 (setf (thread-result thread)
1155 (multiple-value-list
1156 (apply real-function arguments))))
1157 ;; Try to block deferrables. An
1158 ;; interrupt may unwind it, but for a
1159 ;; normal exit it prevents interrupt
1161 (block-deferrable-signals))
1162 ;; We're going down, can't handle interrupts
1163 ;; sanely anymore. GC remains enabled.
1164 (block-deferrable-signals)
1165 ;; We don't want to run interrupts in a dead
1166 ;; thread when we leave WITHOUT-INTERRUPTS.
1167 ;; This potentially causes important
1168 ;; interupts to be lost: SIGINT comes to
1170 (setq *interrupt-pending* nil)
1171 (handle-thread-exit thread))))))))
1173 ;; If the starting thread is stopped for gc before it signals the
1174 ;; semaphore then we'd be stuck.
1175 (assert (not *gc-inhibit*))
1176 ;; Keep INITIAL-FUNCTION pinned until the child thread is
1177 ;; initialized properly. Wrap the whole thing in
1178 ;; WITHOUT-INTERRUPTS because we pass INITIAL-FUNCTION to another
1181 (with-pinned-objects (initial-function)
1184 (get-lisp-obj-address initial-function))))
1185 (when (zerop os-thread)
1186 (error "Can't create a new thread"))
1187 (wait-on-semaphore setup-sem)
1190 (defun join-thread (thread &key (default nil defaultp))
1192 "Suspend current thread until THREAD exits. Returns the result
1193 values of the thread function. If the thread does not exit normally,
1194 return DEFAULT if given or else signal JOIN-THREAD-ERROR."
1195 (with-system-mutex ((thread-result-lock thread) :allow-with-interrupts t)
1196 (cond ((car (thread-result thread))
1197 (return-from join-thread
1198 (values-list (cdr (thread-result thread)))))
1200 (return-from join-thread default))))
1201 (error 'join-thread-error :thread thread))
1203 (defun destroy-thread (thread)
1205 "Deprecated. Same as TERMINATE-THREAD."
1206 (terminate-thread thread))
1208 (defmacro with-interruptions-lock ((thread) &body body)
1209 `(with-system-mutex ((thread-interruptions-lock ,thread))
1212 ;;; Called from the signal handler.
1214 (defun run-interruption ()
1215 (let ((interruption (with-interruptions-lock (*current-thread*)
1216 (pop (thread-interruptions *current-thread*)))))
1217 ;; If there is more to do, then resignal and let the normal
1218 ;; interrupt deferral mechanism take care of the rest. From the
1219 ;; OS's point of view the signal we are in the handler for is no
1220 ;; longer pending, so the signal will not be lost.
1221 (when (thread-interruptions *current-thread*)
1222 (kill-safely (thread-os-thread *current-thread*) sb!unix:sigpipe))
1224 (funcall interruption))))
1226 (defun interrupt-thread (thread function)
1228 "Interrupt the live THREAD and make it run FUNCTION. A moderate
1229 degree of care is expected for use of INTERRUPT-THREAD, due to its
1230 nature: if you interrupt a thread that was holding important locks
1231 then do something that turns out to need those locks, you probably
1232 won't like the effect. FUNCTION runs with interrupts disabled, but
1233 WITH-INTERRUPTS is allowed in it. Keep in mind that many things may
1234 enable interrupts (GET-MUTEX when contended, for instance) so the
1235 first thing to do is usually a WITH-INTERRUPTS or a
1236 WITHOUT-INTERRUPTS. Within a thread interrupts are queued, they are
1237 run in same the order they were sent."
1239 (declare (ignore thread))
1241 (with-interrupt-bindings
1242 (with-interrupts (funcall function)))
1244 (let ((os-thread (thread-os-thread thread)))
1245 (cond ((not os-thread)
1246 (error 'interrupt-thread-error :thread thread))
1248 (with-interruptions-lock (thread)
1249 ;; Append to the end of the interruptions queue. It's
1250 ;; O(N), but it does not hurt to slow interruptors down a
1251 ;; bit when the queue gets long.
1252 (setf (thread-interruptions thread)
1253 (append (thread-interruptions thread)
1256 (allow-with-interrupts
1257 (funcall function))))))))
1258 (when (minusp (kill-safely os-thread sb!unix:sigpipe))
1259 (error 'interrupt-thread-error :thread thread))))))
1261 (defun terminate-thread (thread)
1263 "Terminate the thread identified by THREAD, by causing it to run
1264 SB-EXT:QUIT - the usual cleanup forms will be evaluated"
1265 (interrupt-thread thread 'sb!ext:quit))
1267 (define-alien-routine "thread_yield" int)
1270 (setf (fdocumentation 'thread-yield 'function)
1271 "Yield the processor to other threads.")
1273 ;;; internal use only. If you think you need to use these, either you
1274 ;;; are an SBCL developer, are doing something that you should discuss
1275 ;;; with an SBCL developer first, or are doing something that you
1276 ;;; should probably discuss with a professional psychiatrist first
1279 (defun %thread-sap (thread)
1280 (let ((thread-sap (alien-sap (extern-alien "all_threads" (* t))))
1281 (target (thread-os-thread thread)))
1283 (when (sap= thread-sap (int-sap 0)) (return nil))
1284 (let ((os-thread (sap-ref-word thread-sap
1285 (* sb!vm:n-word-bytes
1286 sb!vm::thread-os-thread-slot))))
1287 (when (= os-thread target) (return thread-sap))
1289 (sap-ref-sap thread-sap (* sb!vm:n-word-bytes
1290 sb!vm::thread-next-slot)))))))
1292 (defun %symbol-value-in-thread (symbol thread)
1293 ;; Prevent the thread from dying completely while we look for the TLS
1295 (with-all-threads-lock
1297 (if (thread-alive-p thread)
1298 (let* ((epoch sb!kernel::*gc-epoch*)
1299 (offset (* sb!vm:n-word-bytes
1300 (sb!vm::symbol-tls-index symbol)))
1301 (tl-val (sap-ref-word (%thread-sap thread) offset)))
1302 (cond ((zerop offset)
1303 (return (values nil :no-tls-value)))
1304 ((or (eql tl-val sb!vm:no-tls-value-marker-widetag)
1305 (eql tl-val sb!vm:unbound-marker-widetag))
1306 (return (values nil :unbound-in-thread)))
1308 (multiple-value-bind (obj ok) (make-lisp-obj tl-val nil)
1309 ;; The value we constructed may be invalid if a GC has
1310 ;; occurred. That is harmless, though, since OBJ is
1311 ;; either in a register or on stack, and we are
1312 ;; conservative on both on GENCGC -- so a bogus object
1313 ;; is safe here as long as we don't return it. If we
1314 ;; ever port threads to a non-conservative GC we must
1315 ;; pin the TL-VAL address before constructing OBJ, or
1316 ;; make WITH-ALL-THREADS-LOCK imply WITHOUT-GCING.
1318 ;; The reason we don't just rely on TL-VAL pinning the
1319 ;; object is that the call to MAKE-LISP-OBJ may cause
1320 ;; bignum allocation, at which point TL-VAL might not
1321 ;; be alive anymore -- hence the epoch check.
1322 (when (eq epoch sb!kernel::*gc-epoch*)
1324 (return (values obj :ok))
1325 (return (values obj :invalid-tls-value))))))))
1326 (return (values nil :thread-dead))))))
1328 (defun %set-symbol-value-in-thread (symbol thread value)
1329 (with-pinned-objects (value)
1330 ;; Prevent the thread from dying completely while we look for the TLS
1332 (with-all-threads-lock
1333 (if (thread-alive-p thread)
1334 (let ((offset (* sb!vm:n-word-bytes
1335 (sb!vm::symbol-tls-index symbol))))
1336 (cond ((zerop offset)
1337 (values nil :no-tls-value))
1339 (setf (sap-ref-word (%thread-sap thread) offset)
1340 (get-lisp-obj-address value))
1341 (values value :ok))))
1342 (values nil :thread-dead)))))
1344 (define-alien-variable tls-index-start unsigned-int)
1346 ;; Get values from the TLS area of the current thread.
1347 (defun %thread-local-references ()
1349 (let ((sap (%thread-sap *current-thread*)))
1350 (loop for index from tls-index-start
1351 below (symbol-value 'sb!vm::*free-tls-index*)
1352 for value = (sap-ref-word sap (* sb!vm:n-word-bytes index))
1353 for (obj ok) = (multiple-value-list (sb!kernel:make-lisp-obj value nil))
1355 (typep obj '(or fixnum character))
1357 '(#.sb!vm:no-tls-value-marker-widetag
1358 #.sb!vm:unbound-marker-widetag))
1359 (member obj seen :test #'eq))
1360 collect obj into seen
1361 finally (return seen))))))
1363 (defun symbol-value-in-thread (symbol thread &optional (errorp t))
1364 "Return the local value of SYMBOL in THREAD, and a secondary value of T
1367 If the value cannot be retrieved (because the thread has exited or because it
1368 has no local binding for NAME) and ERRORP is true signals an error of type
1369 SYMBOL-VALUE-IN-THREAD-ERROR; if ERRORP is false returns a primary value of
1370 NIL, and a secondary value of NIL.
1372 Can also be used with SETF to change the thread-local value of SYMBOL.
1374 SYMBOL-VALUE-IN-THREAD is primarily intended as a debugging tool, and not as a
1375 mechanism for inter-thread communication."
1376 (declare (symbol symbol) (thread thread))
1378 (multiple-value-bind (res status) (%symbol-value-in-thread symbol thread)
1382 (error 'symbol-value-in-thread-error
1385 :info (list :read status))
1389 (values (symbol-value symbol) t)
1391 (error 'symbol-value-in-thread-error
1394 :info (list :read :unbound-in-thread))
1397 (defun (setf symbol-value-in-thread) (value symbol thread &optional (errorp t))
1398 (declare (symbol symbol) (thread thread))
1400 (multiple-value-bind (res status) (%set-symbol-value-in-thread symbol thread value)
1404 (error 'symbol-value-in-thread-error
1407 :info (list :write status))
1411 (values (setf (symbol-value symbol) value) t)
1413 (error 'symbol-value-in-thread-error
1416 :info (list :write :unbound-in-thread))
1419 (defun sb!vm::locked-symbol-global-value-add (symbol-name delta)
1420 (sb!vm::locked-symbol-global-value-add symbol-name delta))
1425 (defun thread-stepping ()
1427 (sap-ref-word (current-thread-sap)
1428 (* sb!vm::thread-stepping-slot sb!vm:n-word-bytes))))
1430 (defun (setf thread-stepping) (value)
1431 (setf (sap-ref-word (current-thread-sap)
1432 (* sb!vm::thread-stepping-slot sb!vm:n-word-bytes))
1433 (get-lisp-obj-address value)))