+(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 (* 4 sb!vm::thread-pid-slot))))
+ (when (= pid id) (return thread))
+ (setf thread (sb!sys:sap-ref-sap thread (* 4 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-32 thread (* 4 index))))
+ (if (eql tl-val sb!vm::unbound-marker-widetag)
+ (sb!vm::symbol-global-value symbol)
+ (sb!kernel:make-lisp-obj tl-val))))))
+