From 33970351e71bb5f12ba56fc40270089e948ae112 Mon Sep 17 00:00:00 2001 From: Stas Boukarev Date: Tue, 29 Nov 2022 19:03:42 +0300 Subject: [PATCH] cffi-sbcl: workaround Darwin issues in call-within-initial-thread. Darwin does something weird when the thread sigmask has blocked signals, run the interruption inside sb-sys:with-interrupts. Don't interrupt at all if the current thread is the initial thread. --- src/cffi-sbcl.lisp | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/src/cffi-sbcl.lisp b/src/cffi-sbcl.lisp index af097cf..5278c09 100644 --- a/src/cffi-sbcl.lisp +++ b/src/cffi-sbcl.lisp @@ -342,19 +342,23 @@ WITH-POINTER-TO-VECTOR-DATA." #+darwin (defun call-within-initial-thread (fn &rest args) - (let (result - error - (sem (sb-thread:make-semaphore))) - (sb-thread:interrupt-thread - sb-thread::*initial-thread* - (lambda () - (multiple-value-setq (result error) - (ignore-errors (apply fn args))) - (sb-thread:signal-semaphore sem))) - (sb-thread:wait-on-semaphore sem) - (if error - (signal error) - result))) + (if (eq sb-thread:*current-thread* + sb-thread::*initial-thread*) + (apply fn args) + (let (result + error + (sem (sb-thread:make-semaphore))) + (sb-thread:interrupt-thread + sb-thread::*initial-thread* + (lambda () + (sb-sys:with-interrupts + (multiple-value-setq (result error) + (ignore-errors (apply fn args)))) + (sb-thread:signal-semaphore sem))) + (sb-thread:wait-on-semaphore sem) + (if error + (signal error) + result)))) (declaim (inline %load-foreign-library)) (defun %load-foreign-library (name path) @@ -362,7 +366,7 @@ WITH-POINTER-TO-VECTOR-DATA." (declare (ignore name)) ;; As of MacOS X 10.6.6, loading things like CoreFoundation from a ;; thread other than the initial one results in a crash. - #+(and darwin sb-thread) (call-within-initial-thread 'load-shared-object path) + #+(and darwin sb-thread) (call-within-initial-thread #'load-shared-object path) #-(and darwin sb-thread) (load-shared-object path)) ;;; SBCL 1.0.21.15 renamed SB-ALIEN::SHARED-OBJECT-FILE but introduced -- 2.11.4.GIT