6262import java .util .List ;
6363import java .util .Objects ;
6464import java .util .Set ;
65- import java .util .concurrent .CancellationException ;
6665import java .util .concurrent .ConcurrentHashMap ;
67- import java .util .concurrent .ExecutionException ;
68- import java .util .concurrent .Future ;
69- import java .util .concurrent .TimeUnit ;
70- import java .util .concurrent .TimeoutException ;
7166import java .util .concurrent .atomic .AtomicInteger ;
7267import java .util .concurrent .atomic .AtomicLong ;
7368import java .util .concurrent .locks .ReentrantLock ;
@@ -830,7 +825,7 @@ public static CApiContext ensureCapiWasLoaded(Node node, PythonContext context,
830825 try {
831826 CApiContext cApiContext = loadCApi (node , context , name , path , reason );
832827 assert context .getCApiState () == PythonContext .CApiState .INITIALIZING ;
833- initializeThreadStateCurrentForAttachedThreads (node , context );
828+ initializeThreadStateCurrentForAttachedThreads (context );
834829 CApiTransitions .initializeReferenceQueuePolling (context .nativeContext );
835830 context .runCApiHooks ();
836831 context .setCApiState (PythonContext .CApiState .INITIALIZED ); // volatile write
@@ -853,8 +848,7 @@ public static CApiContext ensureCapiWasLoaded(Node node, PythonContext context,
853848 return context .getCApiContext ();
854849 }
855850
856- @ SuppressWarnings ("try" )
857- private static void initializeThreadStateCurrentForAttachedThreads (Node node , PythonContext context ) throws ApiInitException {
851+ private static void initializeThreadStateCurrentForAttachedThreads (PythonContext context ) {
858852 Thread [] threads = getOtherAliveAttachedThreads (context );
859853 if (threads .length == 0 ) {
860854 return ;
@@ -865,7 +859,7 @@ protected void perform(ThreadLocalAction.Access access) {
865859 context .initializeNativeThreadState ();
866860 }
867861 };
868- waitForThreadLocalActions ( node , context , submitThreadLocalActions (context , threads , action ) );
862+ submitThreadLocalActions (context , threads , action );
869863 }
870864
871865 private static Thread [] getOtherAliveAttachedThreads (PythonContext context ) {
@@ -879,38 +873,9 @@ private static Thread[] getOtherAliveAttachedThreads(PythonContext context) {
879873 return threads .toArray (Thread []::new );
880874 }
881875
882- private static ArrayList <Future <Void >> submitThreadLocalActions (PythonContext context , Thread [] threads , ThreadLocalAction action ) {
883- ArrayList <Future <Void >> futures = new ArrayList <>(threads .length );
876+ private static void submitThreadLocalActions (PythonContext context , Thread [] threads , ThreadLocalAction action ) {
884877 for (Thread thread : threads ) {
885- futures .add (context .getEnv ().submitThreadLocal (new Thread []{thread }, action ));
886- }
887- return futures ;
888- }
889-
890- @ SuppressWarnings ("try" )
891- private static void waitForThreadLocalActions (Node node , PythonContext context , ArrayList <Future <Void >> futures ) throws ApiInitException {
892- Node waitLocation = node != null ? node : context .getLanguage ().unavailableSafepointLocation ;
893- try (GilNode .UncachedRelease ignored = GilNode .uncachedRelease ()) {
894- for (Future <Void > future : futures ) {
895- TruffleSafepoint .setBlockedThreadInterruptible (waitLocation , voidFuture -> {
896- try {
897- voidFuture .get (10 , TimeUnit .SECONDS );
898- } catch (InterruptedException e ) {
899- Thread .currentThread ().interrupt ();
900- throw new RuntimeException (e );
901- } catch (TimeoutException | ExecutionException e ) {
902- throw new RuntimeException (e );
903- } catch (CancellationException e ) {
904- // Ignore threads that went away while initialization was in progress.
905- }
906- }, future );
907- }
908- } catch (RuntimeException e ) {
909- Throwable cause = e .getCause ();
910- if (cause instanceof TimeoutException ) {
911- throw new ApiInitException (toTruffleStringUncached ("Timed out while initializing native thread state on an attached thread." ));
912- }
913- throw e ;
878+ context .getEnv ().submitThreadLocal (new Thread []{thread }, action );
914879 }
915880 }
916881
0 commit comments