master: Avoid certain TLS load traps

snuglas via Sbcl-commits <[email protected]>
Newsgroups gmane.lisp.steel-bank.cvs
Message-ID <[email protected]>
The branch "master" has been updated in SBCL:
       via  5d6cba27663dba7374d7bcc6d8cce2d0db370a8b (commit)
      from  526c5fca64c6346c89b574b5cb9d714ca6f77f56 (commit)

- Log -----------------------------------------------------------------
commit 5d6cba27663dba7374d7bcc6d8cce2d0db370a8b
Author: Douglas Katzman <[email protected]>
Date:   Thu Apr 9 14:31:09 2026 -0400

    Avoid certain TLS load traps
    
    Prefill indirection words for main and system thread. Other threads (especially
    foreign call-in) have high overhead already, which isn't justification to add
    even more overhead.  The primary goal was to see whether it's possible to build
    without the sigsegv handler. It's still not, but after the build you can
    start up that binary and core without installing the sigsegv handler.
---
 src/code/late-globaldb.lisp | 5 ++++-
 src/code/target-thread.lisp | 2 +-
 2 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/src/code/late-globaldb.lisp b/src/code/late-globaldb.lisp
index eee9c8575..b88bf3d44 100644
--- a/src/code/late-globaldb.lisp
+++ b/src/code/late-globaldb.lisp
@@ -103,7 +103,10 @@
   ;; may exhibit sporadic crashes under the LLVM interceptors which change the sa_flags in
   ;; struct sigaction before passing along the syscall argument. (How is that reasonable?)
   #+tls-load-indirect
-  (when (= (extern-alien "enable_tls_indirection_preinit" char) 1)
+  (when (or (= (extern-alien "enable_tls_indirection_preinit" char) 1)
+            ;; Try to avoid TLS indirection traps. This is no guarantee, just best effort.
+            (thread-ephemeral-p thread)
+            (main-thread-p))
     (do ((symbolmap (int-sap (ash sb-vm::*tls-symbol-map* sb-vm:n-fixnum-tag-bits)))
          (i (- (symbol-tls-index '*package*) 8) (+ i 16)) ; step by 2 words
          (end (- (ash sb-vm::*free-tls-index* sb-vm:n-fixnum-tag-bits) 8)))
diff --git a/src/code/target-thread.lisp b/src/code/target-thread.lisp
index 79eee874f..a85478f3f 100644
--- a/src/code/target-thread.lisp
+++ b/src/code/target-thread.lisp
@@ -381,6 +381,7 @@ created and old ones may exit at any time."
           #+sb-thread *current-thread*)) ; an ordinary read of the TLS
     ;; Run the macro-generated function which writes some values into the TLS,
     ;; most especially *CURRENT-THREAD*.
+    (setf *initial-thread* thread)
     (init-thread-local-storage thread)
     (setf (%instance-layout thread) #.(find-layout 'thread)
           (thread-%name thread) name
@@ -392,7 +393,6 @@ created and old ones may exit at any time."
     (setf sb-impl::*exit-lock* (make-mutex :name "Exit Lock")
           sb-vm::*allocator-mutex* (make-mutex :name "Allocator")
           *make-thread-lock* (make-mutex :name "Make-Thread Lock"))
-    (setf *initial-thread* thread)
     (setf *joinable-threads* nil)
     (setq *session* (new-session thread))
     (setq *all-threads*

-----------------------------------------------------------------------


hooks/post-receive
-- 
SBCL
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.