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")
14 ;;; Of the WITH-PINNED-OBJECTS in this file, not every single one is
15 ;;; necessary because threads are only supported with the conservative
16 ;;; gencgc and numbers on the stack (returned by GET-LISP-OBJ-ADDRESS)
17 ;;; are treated as references.
19 ;;; set the doc here because in early-thread FDOCUMENTATION is not
22 (setf (fdocumentation '*current-thread* 'variable)
23 "Bound in each thread to the thread itself.")
25 (defstruct (thread (:constructor %make-thread))
27 "Thread type. Do not rely on threads being structs as it may change
33 (interruptions-lock (make-mutex :name "thread interruptions lock"))
35 (result-lock (make-mutex :name "thread result lock")))
38 (setf (fdocumentation 'thread-name 'function)
39 "The name of the thread. Setfable.")
41 (def!method print-object ((thread thread) stream)
42 (if (thread-name thread)
43 (print-unreadable-object (thread stream :type t :identity t)
44 (prin1 (thread-name thread) stream))
45 (print-unreadable-object (thread stream :type t :identity t)
46 ;; body is empty => there is only one space between type and
51 (defun thread-alive-p (thread)
53 "Check if THREAD is running."
54 (thread-%alive-p thread))
56 ;; A thread is eligible for gc iff it has finished and there are no
57 ;; more references to it. This list is supposed to keep a reference to
58 ;; all running threads.
59 (defvar *all-threads* ())
60 (defvar *all-threads-lock* (make-mutex :name "all threads lock"))
62 (defmacro with-all-threads-lock (&body body)
67 (with-mutex (*all-threads-lock*)
70 (defun list-all-threads ()
72 "Return a list of the live threads."
73 (with-all-threads-lock
74 (copy-list *all-threads*)))
76 (declaim (inline current-thread-sap))
77 (defun current-thread-sap ()
78 (sb!vm::current-thread-offset-sap sb!vm::thread-this-slot))
80 (declaim (inline current-thread-sap-id))
81 (defun current-thread-sap-id ()
83 (sb!vm::current-thread-offset-sap sb!vm::thread-os-thread-slot)))
85 (defun init-initial-thread ()
86 (/show0 "Entering INIT-INITIAL-THREAD")
87 (let ((initial-thread (%make-thread :name "initial thread"
89 :os-thread (current-thread-sap-id))))
90 (setq *current-thread* initial-thread)
91 ;; Either *all-threads* is empty or it contains exactly one thread
92 ;; in case we are in reinit since saving core with multiple
93 ;; threads doesn't work.
94 (setq *all-threads* (list initial-thread))))
100 ;; FIXME it would be good to define what a thread id is or isn't
101 ;; (our current assumption is that it's a fixnum). It so happens
102 ;; that on Linux it's a pid, but it might not be on posix thread
104 (define-alien-routine ("create_thread" %create-thread)
105 unsigned-long (lisp-fun-address unsigned-long))
107 (define-alien-routine "signal_interrupt_thread"
108 integer (os-thread unsigned-long))
110 (define-alien-routine "block_deferrable_signals"
115 (declaim (inline %lutex-init %lutex-wait %lutex-wake
116 %lutex-lock %lutex-unlock))
118 (sb!alien:define-alien-routine ("lutex_init" %lutex-init)
119 int (lutex unsigned-long))
121 (sb!alien:define-alien-routine ("lutex_wait" %lutex-wait)
122 int (queue-lutex unsigned-long) (mutex-lutex unsigned-long))
124 (sb!alien:define-alien-routine ("lutex_wake" %lutex-wake)
125 int (lutex unsigned-long) (n int))
127 (sb!alien:define-alien-routine ("lutex_lock" %lutex-lock)
128 int (lutex unsigned-long))
130 (sb!alien:define-alien-routine ("lutex_trylock" %lutex-trylock)
131 int (lutex unsigned-long))
133 (sb!alien:define-alien-routine ("lutex_unlock" %lutex-unlock)
134 int (lutex unsigned-long))
136 (sb!alien:define-alien-routine ("lutex_destroy" %lutex-destroy)
137 int (lutex unsigned-long))
139 ;; FIXME: Defining a whole bunch of alien-type machinery just for
140 ;; passing primitive lutex objects directly to foreign functions
141 ;; doesn't seem like fun right now. So instead we just manually
142 ;; pin the lutex, get its address, and let the callee untag it.
143 (defmacro with-lutex-address ((name lutex) &body body)
144 `(let ((,name ,lutex))
145 (with-pinned-objects (,name)
146 (let ((,name (get-lisp-obj-address ,name)))
150 (/show0 "Entering MAKE-LUTEX")
151 ;; Suppress GC until the lutex has been properly registered with
154 (let ((lutex (sb!vm::%make-lutex)))
157 (with-lutex-address (lutex lutex)
163 (declaim (inline futex-wait futex-wake))
165 (sb!alien:define-alien-routine "futex_wait"
166 int (word unsigned-long) (old-value unsigned-long)
167 (to-sec long) (to-usec unsigned-long))
169 (sb!alien:define-alien-routine "futex_wake"
170 int (word unsigned-long) (n unsigned-long))))
172 ;;; used by debug-int.lisp to access interrupt contexts
173 #!-(or sb-fluid sb-thread) (declaim (inline sb!vm::current-thread-offset-sap))
175 (defun sb!vm::current-thread-offset-sap (n)
176 (declare (type (unsigned-byte 27) n))
177 (sap-ref-sap (alien-sap (extern-alien "all_threads" (* t)))
178 (* n sb!vm:n-word-bytes)))
181 (defun sb!vm::current-thread-offset-sap (n)
182 (declare (type (unsigned-byte 27) n))
183 (sb!vm::current-thread-offset-sap n))
186 (define-structure-slot-compare-and-swap
187 compare-and-swap-spinlock-value
191 (declaim (inline get-spinlock release-spinlock))
193 (defun get-spinlock (spinlock)
194 (declare (optimize (speed 3) (safety 0)))
195 (let* ((new *current-thread*)
196 (old (compare-and-swap-spinlock-value spinlock nil new)))
199 (error "Recursive lock attempt on ~S." spinlock))
201 (loop while (compare-and-swap-spinlock-value spinlock nil new))))
204 (defun release-spinlock (spinlock)
205 (declare (optimize (speed 3) (safety 0)))
206 (setf (spinlock-value spinlock) nil)
212 (setf (fdocumentation 'make-mutex 'function)
214 (fdocumentation 'mutex-name 'function)
215 "The name of the mutex. Setfable."
216 (fdocumentation 'mutex-value 'function)
217 "The value of the mutex. NIL if the mutex is free. Setfable.")
219 #!+(and sb-thread (not sb-lutex))
221 (define-structure-slot-addressor mutex-value-address
224 (define-structure-slot-compare-and-swap
225 compare-and-swap-mutex-value
229 (defun get-mutex (mutex &optional (new-value *current-thread*) (waitp t))
231 "Acquire MUTEX, setting it to NEW-VALUE or some suitable default
232 value if NIL. If WAITP is non-NIL and the mutex is in use, sleep
233 until it is available."
234 (declare (type mutex mutex) (optimize (speed 3)))
235 (/show0 "Entering GET-MUTEX")
237 (setq new-value *current-thread*))
239 (let ((old (mutex-value mutex)))
240 (when (and old waitp)
241 (error "In unithread mode, mutex ~S was requested with WAITP ~S and ~
242 new-value ~S, but has already been acquired (with value ~S)."
243 mutex waitp new-value old))
244 (setf (mutex-value mutex) new-value)
248 (when (eql new-value (mutex-value mutex))
249 (warn "recursive lock attempt ~S~%" mutex)
250 (format *debug-io* "Thread: ~A~%" *current-thread*)
251 (sb!debug:backtrace most-positive-fixnum *debug-io*)
252 (force-output *debug-io*))
253 ;; FIXME: Lutexes do not currently support deadlines, as at least
254 ;; on Darwin pthread_foo_timedbar functions are not supported:
255 ;; this means that we probably need to use the Carbon multiprocessing
256 ;; functions on Darwin.
258 (when (zerop (with-lutex-address (lutex (mutex-lutex mutex))
261 (%lutex-trylock lutex))))
262 (setf (mutex-value mutex) new-value))
265 (when (and (setf old (compare-and-swap-mutex-value mutex nil new-value))
268 do (multiple-value-bind (to-sec to-usec) (decode-timeout nil)
269 (when (= 1 (with-pinned-objects (mutex old)
270 (futex-wait (mutex-value-address mutex)
271 (get-lisp-obj-address old)
275 (setf old (compare-and-swap-mutex-value mutex nil new-value))))
278 (defun release-mutex (mutex)
280 "Release MUTEX by setting it to NIL. Wake up threads waiting for
282 (declare (type mutex mutex))
283 (/show0 "Entering RELEASE-MUTEX")
284 (setf (mutex-value mutex) nil)
288 (with-lutex-address (lutex (mutex-lutex mutex))
289 (%lutex-unlock lutex))
291 (futex-wake (mutex-value-address mutex) 1)))
293 ;;;; waitqueues/condition variables
295 (defstruct (waitqueue (:constructor %make-waitqueue))
298 (name nil :type (or null simple-string))
299 #!+(and sb-lutex sb-thread)
304 (defun make-waitqueue (&key name)
306 "Create a waitqueue."
307 (%make-waitqueue :name name))
310 (setf (fdocumentation 'waitqueue-name 'function)
311 "The name of the waitqueue. Setfable.")
313 #!+(and sb-thread (not sb-lutex))
314 (define-structure-slot-addressor waitqueue-data-address
318 (defun condition-wait (queue mutex)
320 "Atomically release MUTEX and enqueue ourselves on QUEUE. Another
321 thread may subsequently notify us using CONDITION-NOTIFY, at which
322 time we reacquire MUTEX and return to the caller."
323 #!-sb-thread (declare (ignore queue))
325 #!-sb-thread (error "Not supported in unithread builds.")
327 (let ((value (mutex-value mutex)))
328 (/show0 "CONDITION-WAITing")
331 (setf (mutex-value mutex) nil)
332 (with-lutex-address (queue-lutex-address (waitqueue-lutex queue))
333 (with-lutex-address (mutex-lutex-address (mutex-lutex mutex))
334 (%lutex-wait queue-lutex-address mutex-lutex-address)))
335 (setf (mutex-value mutex) value))
338 (let ((me *current-thread*))
339 ;; XXX we should do something to ensure that the result of this setf
340 ;; is visible to all CPUs
341 (setf (waitqueue-data queue) me)
342 (release-mutex mutex)
343 ;; Now we go to sleep using futex-wait. If anyone else
344 ;; manages to grab MUTEX and call CONDITION-NOTIFY during
345 ;; this comment, it will change queue->data, and so
346 ;; futex-wait returns immediately instead of sleeping.
347 ;; Ergo, no lost wakeup. We may get spurious wakeups,
349 (multiple-value-bind (to-sec to-usec) (decode-timeout nil)
350 (when (= 1 (with-pinned-objects (queue me)
351 (futex-wait (waitqueue-data-address queue)
352 (get-lisp-obj-address me)
353 (or to-sec -1) ;; our way if saying "no timeout"
356 ;; If we are interrupted while waiting, we should do these things
357 ;; before returning. Ideally, in the case of an unhandled signal,
358 ;; we should do them before entering the debugger, but this is
359 ;; better than nothing.
360 (get-mutex mutex value))))
362 (defun condition-notify (queue &optional (n 1))
364 "Notify N threads waiting on QUEUE."
365 #!-sb-thread (declare (ignore queue n))
366 #!-sb-thread (error "Not supported in unithread builds.")
368 (declare (type (and fixnum (integer 1)) n))
369 (/show0 "Entering CONDITION-NOTIFY")
373 (with-lutex-address (lutex (waitqueue-lutex queue))
374 (%lutex-wake lutex n))
375 ;; no problem if >1 thread notifies during the comment in
376 ;; condition-wait: as long as the value in queue-data isn't the
377 ;; waiting thread's id, it matters not what it is
378 ;; XXX we should do something to ensure that the result of this setf
379 ;; is visible to all CPUs
381 (let ((me *current-thread*))
383 (setf (waitqueue-data queue) me)
384 (with-pinned-objects (queue)
385 (futex-wake (waitqueue-data-address queue) n))))))
387 (defun condition-broadcast (queue)
389 "Notify all threads waiting on QUEUE."
390 (condition-notify queue
391 ;; On a 64-bit platform truncating M-P-F to an int results
392 ;; in -1, which wakes up only one thread.
394 most-positive-fixnum)))
398 (defstruct (semaphore (:constructor %make-semaphore))
401 (name nil :type (or null simple-string))
402 (count 0 :type (integer 0))
404 (queue (make-waitqueue)))
406 (defun make-semaphore (&key name (count 0))
408 "Create a semaphore with the supplied COUNT."
409 (%make-semaphore :name name :count count))
411 (setf (fdocumentation 'semaphore-name 'function)
412 "The name of the semaphore. Setfable.")
414 (defun wait-on-semaphore (sem)
416 "Decrement the count of SEM if the count would not be negative. Else
417 block until the semaphore can be decremented."
418 ;; a more direct implementation based directly on futexes should be
420 (with-mutex ((semaphore-mutex sem))
421 (loop until (> (semaphore-count sem) 0)
422 do (condition-wait (semaphore-queue sem) (semaphore-mutex sem))
423 finally (decf (semaphore-count sem)))))
425 (defun signal-semaphore (sem &optional (n 1))
427 "Increment the count of SEM by N. If there are threads waiting on
428 this semaphore, then N of them is woken up."
429 (declare (type (and fixnum (integer 1)) n))
430 (with-mutex ((semaphore-mutex sem))
431 (when (= n (incf (semaphore-count sem) n))
432 (condition-notify (semaphore-queue sem) n))))
434 ;;;; job control, independent listeners
437 (lock (make-mutex :name "session lock"))
439 (interactive-threads nil)
440 (interactive-threads-queue (make-waitqueue)))
442 (defvar *session* nil)
444 ;;; the debugger itself tries to acquire the session lock, don't let
445 ;;; funny situations (like getting a sigint while holding the session
447 (defmacro with-session-lock ((session) &body body)
448 #!-sb-thread (declare (ignore session))
453 (with-mutex ((session-lock ,session))
456 (defun new-session ()
457 (make-session :threads (list *current-thread*)
458 :interactive-threads (list *current-thread*)))
460 (defun init-job-control ()
461 (/show0 "Entering INIT-JOB-CONTROL")
462 (setf *session* (new-session))
463 (/show0 "Exiting INIT-JOB-CONTROL"))
465 (defun %delete-thread-from-session (thread session)
466 (with-session-lock (session)
467 (setf (session-threads session)
468 (delete thread (session-threads session))
469 (session-interactive-threads session)
470 (delete thread (session-interactive-threads session)))))
472 (defun call-with-new-session (fn)
473 (%delete-thread-from-session *current-thread* *session*)
474 (let ((*session* (new-session)))
477 (defmacro with-new-session (args &body forms)
478 (declare (ignore args)) ;for extensibility
479 (sb!int:with-unique-names (fb-name)
480 `(labels ((,fb-name () ,@forms))
481 (call-with-new-session (function ,fb-name)))))
483 ;;; Remove thread from its session, if it has one.
485 (defun handle-thread-exit (thread)
486 (/show0 "HANDLING THREAD EXIT")
487 ;; We're going down, can't handle interrupts sanely anymore.
488 ;; GC remains enabled.
489 (block-deferrable-signals)
491 (with-all-threads-lock
492 (setf (thread-%alive-p thread) nil)
493 (setf (thread-os-thread thread) nil)
494 (setq *all-threads* (delete thread *all-threads*))
496 (%delete-thread-from-session thread *session*)))
499 (/show0 "FREEING MUTEX LUTEX")
500 (with-lutex-address (lutex (mutex-lutex (thread-interruptions-lock thread)))
501 (%lutex-destroy lutex))))
503 (defun terminate-session ()
505 "Kill all threads in session except for this one. Does nothing if current
506 thread is not the foreground thread."
507 ;; FIXME: threads created in other threads may escape termination
509 (with-session-lock (*session*)
510 (and (eq *current-thread*
511 (car (session-interactive-threads *session*)))
512 (session-threads *session*)))))
513 ;; do the kill after dropping the mutex; unwind forms in dying
514 ;; threads may want to do session things
515 (dolist (thread to-kill)
516 (unless (eq thread *current-thread*)
517 ;; terminate the thread but don't be surprised if it has
518 ;; exited in the meantime
519 (handler-case (terminate-thread thread)
520 (interrupt-thread-error ()))))))
522 ;;; called from top of invoke-debugger
523 (defun debugger-wait-until-foreground-thread (stream)
524 "Returns T if thread had been running in background, NIL if it was
526 (declare (ignore stream))
530 (with-session-lock (*session*)
531 (not (member *current-thread*
532 (session-interactive-threads *session*))))
535 (defun get-foreground ()
538 (let ((was-foreground t))
540 (/show0 "Looping in GET-FOREGROUND")
541 (with-session-lock (*session*)
542 (let ((int-t (session-interactive-threads *session*)))
543 (when (eq (car int-t) *current-thread*)
544 (unless was-foreground
545 (format *query-io* "Resuming thread ~A~%" *current-thread*))
546 (return-from get-foreground t))
547 (setf was-foreground nil)
548 (unless (member *current-thread* int-t)
549 (setf (cdr (last int-t))
550 (list *current-thread*)))
552 (session-interactive-threads-queue *session*)
553 (session-lock *session*)))))))
555 (defun release-foreground (&optional next)
557 "Background this thread. If NEXT is supplied, arrange for it to
558 have the foreground next."
559 #!-sb-thread (declare (ignore next))
562 (with-session-lock (*session*)
563 (when (rest (session-interactive-threads *session*))
564 (setf (session-interactive-threads *session*)
565 (delete *current-thread* (session-interactive-threads *session*))))
567 (setf (session-interactive-threads *session*)
569 (delete next (session-interactive-threads *session*)))))
570 (condition-broadcast (session-interactive-threads-queue *session*))))
572 (defun foreground-thread ()
573 (car (session-interactive-threads *session*)))
575 (defun make-listener-thread (tty-name)
576 (assert (probe-file tty-name))
577 (let* ((in (sb!unix:unix-open (namestring tty-name) sb!unix:o_rdwr #o666))
578 (out (sb!unix:unix-dup in))
579 (err (sb!unix:unix-dup in)))
580 (labels ((thread-repl ()
581 (sb!unix::unix-setsid)
582 (let* ((sb!impl::*stdin*
583 (make-fd-stream in :input t :buffering :line
586 (make-fd-stream out :output t :buffering :line
589 (make-fd-stream err :output t :buffering :line
592 (make-fd-stream err :input t :output t
595 (sb!impl::*descriptor-handlers* nil))
598 (sb!impl::toplevel-repl nil)
599 (sb!int:flush-standard-output-streams))))))
600 (make-thread #'thread-repl))))
604 (defun make-thread (function &key name)
606 "Create a new thread of NAME that runs FUNCTION. When the function
607 returns the thread exits. The return values of FUNCTION are kept
608 around and can be retrieved by JOIN-THREAD."
609 #!-sb-thread (declare (ignore function name))
610 #!-sb-thread (error "Not supported in unithread builds.")
612 (let* ((thread (%make-thread :name name))
613 (setup-sem (make-semaphore :name "Thread setup semaphore"))
614 (real-function (coerce function 'function))
617 ;; In time we'll move some of the binding presently done in C
620 ;; KLUDGE: Here we have a magic list of variables that are
621 ;; not thread-safe for one reason or another. As people
622 ;; report problems with the thread safety of certain
623 ;; variables, (e.g. "*print-case* in multiple threads
624 ;; broken", sbcl-devel 2006-07-14), we add a few more
625 ;; bindings here. The Right Thing is probably some variant
626 ;; of Allegro's *cl-default-special-bindings*, as that is at
627 ;; least accessible to users to secure their own libraries.
629 (let ((*current-thread* thread)
630 (*restart-clusters* nil)
631 (*handler-clusters* nil)
632 (*condition-restarts* nil)
633 (sb!impl::*step-out* nil)
634 ;; internal printer variables
635 (sb!impl::*previous-case* nil)
636 (sb!impl::*previous-readtable-case* nil)
637 (sb!impl::*merge-sort-temp-vector* (vector)) ; keep these small!
638 (sb!impl::*zap-array-data-temp* (vector)) ;
639 (sb!impl::*internal-symbol-output-fun* nil)
640 (sb!impl::*descriptor-handlers* nil)) ; serve-event
641 (setf (thread-os-thread thread) (current-thread-sap-id))
642 (with-mutex ((thread-result-lock thread))
643 (with-all-threads-lock
644 (push thread *all-threads*))
645 (with-session-lock (*session*)
646 (push thread (session-threads *session*)))
647 (setf (thread-%alive-p thread) t)
648 (signal-semaphore setup-sem)
649 ;; can't use handling-end-of-the-world, because that flushes
650 ;; output streams, and we don't necessarily have any (or we
651 ;; could be sharing them)
652 (catch 'sb!impl::toplevel-catcher
653 (catch 'sb!impl::%end-of-the-world
657 "~~@<Terminate this thread (~A)~~@:>"
661 ;; now that most things have a chance to
662 ;; work properly without messing up other
663 ;; threads, it's time to enable signals
664 (sb!unix::reset-signal-mask)
665 (setf (thread-result thread)
668 (funcall real-function)))))
669 (handle-thread-exit thread)))))))
671 ;; Keep INITIAL-FUNCTION pinned until the child thread is
672 ;; initialized properly.
673 (with-pinned-objects (initial-function)
676 (get-lisp-obj-address initial-function))))
677 (when (zerop os-thread)
678 (error "Can't create a new thread"))
679 (wait-on-semaphore setup-sem)
682 (define-condition join-thread-error (error)
683 ((thread :reader join-thread-error-thread :initarg :thread))
685 (:documentation "Joining thread failed.")
686 (:report (lambda (c s)
687 (format s "Joining thread failed: thread ~A ~
688 has not returned normally."
689 (join-thread-error-thread c)))))
692 (setf (fdocumentation 'join-thread-error-thread 'function)
693 "The thread that we failed to join.")
695 (defun join-thread (thread &key (default nil defaultp))
697 "Suspend current thread until THREAD exits. Returns the result
698 values of the thread function. If the thread does not exit normally,
699 return DEFAULT if given or else signal JOIN-THREAD-ERROR."
700 (with-mutex ((thread-result-lock thread))
701 (cond ((car (thread-result thread))
702 (values-list (cdr (thread-result thread))))
706 (error 'join-thread-error :thread thread)))))
708 (defun destroy-thread (thread)
710 "Deprecated. Same as TERMINATE-THREAD."
711 (terminate-thread thread))
713 (define-condition interrupt-thread-error (error)
714 ((thread :reader interrupt-thread-error-thread :initarg :thread))
716 (:documentation "Interrupting thread failed.")
717 (:report (lambda (c s)
718 (format s "Interrupt thread failed: thread ~A has exited."
719 (interrupt-thread-error-thread c)))))
722 (setf (fdocumentation 'interrupt-thread-error-thread 'function)
723 "The thread that was not interrupted.")
725 (defmacro with-interruptions-lock ((thread) &body body)
727 (with-mutex ((thread-interruptions-lock ,thread))
730 ;; Called from the signal handler in C.
731 (defun run-interruption ()
734 (let ((interruption (with-interruptions-lock (*current-thread*)
735 (pop (thread-interruptions *current-thread*)))))
737 ;; This is safe because it's the IN-INTERRUPTION that
738 ;; has disabled interrupts.
740 (funcall interruption))
743 ;; The order of interrupt execution is peculiar. If thread A
744 ;; interrupts thread B with I1, I2 and B for some reason receives I1
745 ;; when FUN2 is already on the list, then it is FUN2 that gets to run
746 ;; first. But when FUN2 is run SIG_INTERRUPT_THREAD is enabled again
747 ;; and I2 hits pretty soon in FUN2 and run FUN1. This is of course
748 ;; just one scenario, and the order of thread interrupt execution is
750 (defun interrupt-thread (thread function)
752 "Interrupt the live THREAD and make it run FUNCTION. A moderate
753 degree of care is expected for use of INTERRUPT-THREAD, due to its
754 nature: if you interrupt a thread that was holding important locks
755 then do something that turns out to need those locks, you probably
756 won't like the effect."
757 #!-sb-thread (declare (ignore thread))
758 (flet ((interrupt-self ()
759 ;; *IN-INTERRUPTION* is true IFF we're being called as an
760 ;; interruption without an intervening WITHOUT-INTERRUPTS,
761 ;; in which case it is safe to enable interrupts. Otherwise
762 ;; interrupts are either already enabled, or there is an outer
763 ;; WITHOUT-INTERRUPTS we know nothing about, which makes it
764 ;; unsafe to enable interrupts.
765 (if *in-interruption*
766 (with-interrupts (funcall function))
767 (funcall function))))
771 (if (eq thread *current-thread*)
773 (let ((os-thread (thread-os-thread thread)))
774 (cond ((not os-thread)
775 (error 'interrupt-thread-error :thread thread))
777 (with-interruptions-lock (thread)
778 (push function (thread-interruptions thread)))
779 (when (minusp (signal-interrupt-thread os-thread))
780 (error 'interrupt-thread-error :thread thread))))))))
782 (defun terminate-thread (thread)
784 "Terminate the thread identified by THREAD, by causing it to run
785 SB-EXT:QUIT - the usual cleanup forms will be evaluated"
786 (interrupt-thread thread 'sb!ext:quit))
788 ;;; internal use only. If you think you need to use this, either you
789 ;;; are an SBCL developer, are doing something that you should discuss
790 ;;; with an SBCL developer first, or are doing something that you
791 ;;; should probably discuss with a professional psychiatrist first
793 (defun thread-sap-for-id (id)
794 (let ((thread-sap (alien-sap (extern-alien "all_threads" (* t)))))
796 (when (sap= thread-sap (int-sap 0)) (return nil))
797 (let ((os-thread (sap-ref-word thread-sap
798 (* sb!vm:n-word-bytes
799 sb!vm::thread-os-thread-slot))))
800 (when (= os-thread id) (return thread-sap))
802 (sap-ref-sap thread-sap (* sb!vm:n-word-bytes
803 sb!vm::thread-next-slot)))))))
806 (defun symbol-value-in-thread (symbol thread-sap)
807 (let* ((index (sb!vm::symbol-tls-index symbol))
808 (tl-val (sap-ref-word thread-sap
809 (* sb!vm:n-word-bytes index))))
810 (if (eql tl-val sb!vm::no-tls-value-marker-widetag)
811 (sb!vm::symbol-global-value symbol)
812 (make-lisp-obj tl-val))))
814 (defun sb!vm::locked-symbol-global-value-add (symbol-name delta)
815 (sb!vm::locked-symbol-global-value-add symbol-name delta))
819 (defun thread-stepping ()
821 (sap-ref-word (current-thread-sap)
822 (* sb!vm::thread-stepping-slot sb!vm:n-word-bytes))))
824 (defun (setf thread-stepping) (value)
825 (setf (sap-ref-word (current-thread-sap)
826 (* sb!vm::thread-stepping-slot sb!vm:n-word-bytes))
827 (get-lisp-obj-address value)))