+(define-condition interrupt-thread-error (error)
+ ((thread :reader interrupt-thread-error-thread :initarg :thread)
+ (errno :reader interrupt-thread-error-errno :initarg :errno))
+ (:report (lambda (c s)
+ (format s "interrupt thread ~A failed (~A: ~A)"
+ (interrupt-thread-error-thread c)
+ (interrupt-thread-error-errno c)
+ (strerror (interrupt-thread-error-errno c))))))
+
+(defun interrupt-thread (thread function)
+ "Interrupt THREAD and make it run FUNCTION."
+ (let ((function (coerce function 'function)))
+ (sb!sys:with-pinned-objects
+ (function)
+ (multiple-value-bind (res err)
+ (sb!unix::syscall ("interrupt_thread"
+ sb!alien:unsigned-long sb!alien:unsigned-long)
+ thread
+ thread
+ (sb!kernel:get-lisp-obj-address function))
+ (unless res
+ (error 'interrupt-thread-error :thread thread :errno err))))))
+
+
+(defun terminate-thread (thread-id)
+ "Terminate the thread identified by THREAD-ID, by causing it to run
+SB-EXT:QUIT - the usual cleanup forms will be evaluated"
+ (interrupt-thread thread-id 'sb!ext:quit))
+
+(declaim (inline current-thread-id))
+(defun current-thread-id ()
+ (logand
+ (sb!sys:sap-int
+ (sb!vm::current-thread-offset-sap sb!vm::thread-pid-slot))
+ ;; KLUDGE pids are 16 bit really. Avoid boxing the return value
+ (1- (ash 1 16))))
+
+;;;; iterate over the in-memory threads
+
+(defun mapcar-threads (function)
+ "Call FUNCTION once for each known thread, giving it the thread structure as argument"
+ (let ((function (coerce function 'function)))
+ (loop for thread = (alien-sap (extern-alien "all_threads" (* t)))
+ then (sb!sys:sap-ref-sap thread (* sb!vm:n-word-bytes
+ sb!vm::thread-next-slot))
+ until (sb!sys:sap= thread (sb!sys:int-sap 0))
+ collect (funcall function thread))))
+
+(defun thread-sap-from-id (id)
+ (let ((thread (alien-sap (extern-alien "all_threads" (* t)))))
+ (loop
+ (when (sb!sys:sap= thread (sb!sys:int-sap 0)) (return nil))
+ (let ((pid (sb!sys:sap-ref-32 thread (* sb!vm:n-word-bytes
+ sb!vm::thread-pid-slot))))
+ (when (= pid id) (return thread))
+ (setf thread (sb!sys:sap-ref-sap thread (* sb!vm:n-word-bytes
+ sb!vm::thread-next-slot)))))))
+
+;;; internal use only. If you think you need to use this, either you
+;;; are an SBCL developer, are doing something that you should discuss
+;;; with an SBCL developer first, or are doing something that you
+;;; should probably discuss with a professional psychiatrist first
+(defun symbol-value-in-thread (symbol thread-id)
+ (let ((thread (thread-sap-from-id thread-id)))
+ (when thread
+ (let* ((index (sb!vm::symbol-tls-index symbol))
+ (tl-val (sb!sys:sap-ref-word thread
+ (* sb!vm:n-word-bytes index))))
+ (if (eql tl-val sb!vm::unbound-marker-widetag)
+ (sb!vm::symbol-global-value symbol)
+ (sb!kernel:make-lisp-obj tl-val))))))
+
+;;;; job control, independent listeners
+
+(defstruct session
+ (lock (make-mutex))
+ (threads nil)
+ (interactive-threads nil)
+ (interactive-threads-queue (make-waitqueue)))
+
+(defun new-session ()
+ (let ((tid (current-thread-id)))
+ (make-session :threads (list tid)
+ :interactive-threads (list tid))))
+
+(defun init-job-control ()
+ (setf *session* (new-session)))
+
+(defun %delete-thread-from-session (tid session)
+ (with-mutex ((session-lock session))
+ (setf (session-threads session)
+ (delete tid (session-threads session))
+ (session-interactive-threads session)
+ (delete tid (session-interactive-threads session)))))
+
+(defun call-with-new-session (fn)
+ (%delete-thread-from-session (current-thread-id) *session*)
+ (let ((*session* (new-session))) (funcall fn)))
+
+(defmacro with-new-session (args &body forms)
+ (declare (ignore args)) ;for extensibility
+ (sb!int:with-unique-names (fb-name)
+ `(labels ((,fb-name () ,@forms))
+ (call-with-new-session (function ,fb-name)))))
+
+;;; Remove thread id TID from its session, if it has one. This is
+;;; called from C reap_dead_threads() so is run in the context of
+;;; whichever thread called that (usually after a GC), which may not have
+;;; any meaningful parent/child/sibling relationship with the dead thread
+(defun handle-thread-exit (tid)
+ (let ((session (symbol-value-in-thread '*session* tid)))
+ (and session (%delete-thread-from-session tid session))))
+
+(defun terminate-session ()
+ "Kill all threads in session except for this one. Does nothing if current
+thread is not the foreground thread"
+ (reap-dead-threads)
+ (let* ((tid (current-thread-id))
+ (to-kill
+ (with-mutex ((session-lock *session*))
+ (and (eql tid (car (session-interactive-threads *session*)))
+ (session-threads *session*)))))
+ ;; do the kill after dropping the mutex; unwind forms in dying
+ ;; threads may want to do session things
+ (dolist (p to-kill)
+ (unless (eql p tid) (terminate-thread p)))))
+
+;;; called from top of invoke-debugger
+(defun debugger-wait-until-foreground-thread (stream)
+ "Returns T if thread had been running in background, NIL if it was
+interactive."
+ (declare (ignore stream))
+ (prog1
+ (with-mutex ((session-lock *session*))
+ (not (member (current-thread-id)
+ (session-interactive-threads *session*))))
+ (get-foreground)))
+
+
+(defun get-foreground ()
+ (let ((was-foreground t))
+ (loop
+ (with-mutex ((session-lock *session*))
+ (let ((tid (current-thread-id))
+ (int-t (session-interactive-threads *session*)))
+ (when (eql (car int-t) tid)
+ (unless was-foreground
+ (format *query-io* "Resuming thread ~A~%" tid))
+ (sb!sys:enable-interrupt sb!unix:sigint #'sb!unix::sigint-handler)
+ (return-from get-foreground t))
+ (setf was-foreground nil)
+ (unless (member tid int-t)
+ (setf (cdr (last int-t))
+ (list tid)))
+ (condition-wait
+ (session-interactive-threads-queue *session*)
+ (session-lock *session*)))))))
+
+(defun release-foreground (&optional next)
+ "Background this thread. If NEXT is supplied, arrange for it to have the foreground next"
+ (with-mutex ((session-lock *session*))
+ (let ((tid (current-thread-id)))
+ (setf (session-interactive-threads *session*)
+ (delete tid (session-interactive-threads *session*)))
+ (sb!sys:enable-interrupt sb!unix:sigint :ignore)
+ (when next
+ (setf (session-interactive-threads *session*)
+ (list* next
+ (delete next (session-interactive-threads *session*)))))
+ (condition-broadcast (session-interactive-threads-queue *session*)))))