+(defun destroy-thread (thread-id)
+ "Deprecated. Soon to be removed or reimplemented using pthread_cancel."
+ (terminate-thread thread-id))
+
+;;; a moderate degree of care is expected for use of interrupt-thread,
+;;; due to its nature: if you interrupt a thread that was holding
+;;; important locks then do something that turns out to need those
+;;; locks, you probably won't like the effect. Used with thought
+;;; though, it's a good deal gentler than the last-resort functions above
+
+(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)))
+ (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 ()
+ (sb!sys:sap-int
+ (sb!vm::current-thread-offset-sap sb!vm::thread-os-thread-slot)))
+
+;;;; 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))
+ ;; FIXME: 32/64 bit
+ (let ((pid (sb!sys:sap-ref-32 thread (* sb!vm:n-word-bytes
+ sb!vm::thread-os-thread-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 :name "session lock"))
+ (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 mark_thread_dead().
+(defun handle-thread-exit (tid)
+ (when *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)
+ ;; FIXME: threads created in other threads may escape termination
+ (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 the thread but don't be surprised if it has
+ ;; exited in the meantime
+ (handler-case (terminate-thread p)
+ (interrupt-thread-error ()))))))
+
+;;; 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)))