51
51
import java .util .logging .Level ;
52
52
53
53
import org .graalvm .collections .EconomicMap ;
54
+ import org .graalvm .collections .MapCursor ;
54
55
import org .graalvm .nativeimage .ImageInfo ;
55
56
import org .graalvm .options .OptionKey ;
56
57
69
70
import com .oracle .graal .python .builtins .objects .dict .PDict ;
70
71
import com .oracle .graal .python .builtins .objects .frame .PFrame ;
71
72
import com .oracle .graal .python .builtins .objects .frame .PFrame .Reference ;
73
+ import com .oracle .graal .python .builtins .objects .function .PKeyword ;
72
74
import com .oracle .graal .python .builtins .objects .list .PList ;
73
75
import com .oracle .graal .python .builtins .objects .module .PythonModule ;
74
76
import com .oracle .graal .python .builtins .objects .object .PythonObjectLibrary ;
@@ -164,6 +166,18 @@ List<WeakReference<Thread>> getOwners() {
164
166
}
165
167
}
166
168
169
+ private static final class AtExitHook {
170
+ final Object [] arguments ;
171
+ final PKeyword [] keywords ;
172
+ final CallTarget ct ;
173
+
174
+ AtExitHook (Object [] arguments , PKeyword [] keywords , CallTarget ct ) {
175
+ this .arguments = arguments ;
176
+ this .keywords = keywords ;
177
+ this .ct = ct ;
178
+ }
179
+ }
180
+
167
181
static final String PREFIX = "/" ;
168
182
static final String LIB_PYTHON_3 = "/lib-python/3" ;
169
183
static final String LIB_GRAALPYTHON = "/lib-graalpython" ;
@@ -178,7 +192,7 @@ List<WeakReference<Thread>> getOwners() {
178
192
private PythonModule mainModule ;
179
193
private final PythonCore core ;
180
194
private final List <ShutdownHook > shutdownHooks = new ArrayList <>();
181
- private final EconomicMap <Object , CallTarget > atExitHooks = EconomicMap .create ();
195
+ private final EconomicMap <Object , AtExitHook > atExitHooks = EconomicMap .create (0 );
182
196
private final HashMap <PythonNativeClass , CyclicAssumption > nativeClassStableAssumptions = new HashMap <>();
183
197
private final AtomicLong globalId = new AtomicLong (Integer .MAX_VALUE * 2L + 4L );
184
198
private final ThreadGroup threadGroup = new ThreadGroup (GRAALPYTHON_THREADS );
@@ -653,8 +667,8 @@ public void registerShutdownHook(ShutdownHook shutdownHook) {
653
667
}
654
668
655
669
@ TruffleBoundary
656
- public void registerShutdownHook (Object callable , CallTarget ct ) {
657
- atExitHooks .put (callable , ct );
670
+ public void registerShutdownHook (Object callable , Object [] arguments , PKeyword [] keywords , CallTarget ct ) {
671
+ atExitHooks .put (callable , new AtExitHook ( arguments , keywords , ct ) );
658
672
}
659
673
660
674
@ TruffleBoundary
@@ -666,13 +680,16 @@ public void deregisterShutdownHook(Object callable) {
666
680
public void runShutdownHooks () {
667
681
handler .shutdown ();
668
682
// run atExitHooks in reverse order they were registered
669
- CallTarget [] hooks = new CallTarget [atExitHooks .size ()];
670
- Iterator <CallTarget > hooksIter = atExitHooks .getValues ().iterator ();
683
+ MapCursor <Object , AtExitHook > cursor = atExitHooks .getEntries ();
684
+ AtExitHook [] hooks = new AtExitHook [atExitHooks .size ()];
685
+ Object [] callables = new Object [atExitHooks .size ()];
671
686
for (int i = 0 ; i < hooks .length ; i ++) {
672
- hooks [i ] = hooksIter .next ();
687
+ cursor .advance ();
688
+ callables [i ] = cursor .getKey ();
689
+ hooks [i ] = cursor .getValue ();
673
690
}
674
691
for (int i = hooks .length - 1 ; i >= 0 ; i --) {
675
- hooks [i ].call ();
692
+ hooks [i ].ct . call (callables [ i ], hooks [ i ]. arguments , hooks [ i ]. keywords );
676
693
}
677
694
for (ShutdownHook h : shutdownHooks ) {
678
695
h .call (this );
0 commit comments