50
50
import java .util .concurrent .locks .ReentrantLock ;
51
51
import java .util .logging .Level ;
52
52
53
- import com .oracle .graal .python .builtins .PythonBuiltinClassType ;
54
- import com .oracle .graal .python .nodes .object .IsBuiltinClassProfile ;
55
- import org .graalvm .collections .EconomicMap ;
56
- import org .graalvm .collections .MapCursor ;
57
53
import org .graalvm .nativeimage .ImageInfo ;
58
54
import org .graalvm .options .OptionKey ;
59
55
60
56
import com .oracle .graal .python .PythonLanguage ;
57
+ import com .oracle .graal .python .builtins .PythonBuiltinClassType ;
61
58
import com .oracle .graal .python .builtins .objects .PNone ;
62
59
import com .oracle .graal .python .builtins .objects .PythonAbstractObject ;
63
60
import com .oracle .graal .python .builtins .objects .cext .PythonNativeClass ;
82
79
import com .oracle .graal .python .nodes .SpecialMethodNames ;
83
80
import com .oracle .graal .python .nodes .attributes .ReadAttributeFromObjectNode ;
84
81
import com .oracle .graal .python .nodes .call .CallNode ;
82
+ import com .oracle .graal .python .nodes .object .IsBuiltinClassProfile ;
85
83
import com .oracle .graal .python .runtime .AsyncHandler .AsyncAction ;
86
84
import com .oracle .graal .python .runtime .exception .ExceptionUtils ;
87
85
import com .oracle .graal .python .runtime .exception .PException ;
@@ -187,11 +185,13 @@ void reprLeave(Object item) {
187
185
}
188
186
189
187
private static final class AtExitHook {
188
+ final Object callable ;
190
189
final Object [] arguments ;
191
190
final PKeyword [] keywords ;
192
191
final CallTarget ct ;
193
192
194
- AtExitHook (Object [] arguments , PKeyword [] keywords , CallTarget ct ) {
193
+ AtExitHook (Object callable , Object [] arguments , PKeyword [] keywords , CallTarget ct ) {
194
+ this .callable = callable ;
195
195
this .arguments = arguments ;
196
196
this .keywords = keywords ;
197
197
this .ct = ct ;
@@ -211,7 +211,7 @@ private static final class AtExitHook {
211
211
private PythonModule mainModule ;
212
212
private final PythonCore core ;
213
213
private final List <ShutdownHook > shutdownHooks = new ArrayList <>();
214
- private final EconomicMap < Object , AtExitHook > atExitHooks = EconomicMap . create ( 0 );
214
+ private final List < AtExitHook > atExitHooks = new ArrayList <>( );
215
215
private final HashMap <PythonNativeClass , CyclicAssumption > nativeClassStableAssumptions = new HashMap <>();
216
216
private final AtomicLong globalId = new AtomicLong (Integer .MAX_VALUE * 2L + 4L );
217
217
private final ThreadGroup threadGroup = new ThreadGroup (GRAALPYTHON_THREADS );
@@ -717,12 +717,12 @@ public void registerAtexitHook(ShutdownHook shutdownHook) {
717
717
718
718
@ TruffleBoundary
719
719
public void registerAtexitHook (Object callable , Object [] arguments , PKeyword [] keywords , CallTarget ct ) {
720
- atExitHooks .put ( callable , new AtExitHook (arguments , keywords , ct ));
720
+ atExitHooks .add ( new AtExitHook (callable , arguments , keywords , ct ));
721
721
}
722
722
723
723
@ TruffleBoundary
724
- public void deregisterAtexitHook (Object callable ) {
725
- atExitHooks .removeKey ( callable );
724
+ public void unregisterAtexitHook (Object callable ) {
725
+ atExitHooks .removeIf ( hook -> hook . callable == callable );
726
726
}
727
727
728
728
@ TruffleBoundary
@@ -745,18 +745,11 @@ public int getAtexitHookCount() {
745
745
@ TruffleBoundary
746
746
public void runAtexitHooks () {
747
747
// run atExitHooks in reverse order they were registered
748
- MapCursor <Object , AtExitHook > cursor = atExitHooks .getEntries ();
749
- AtExitHook [] hooks = new AtExitHook [atExitHooks .size ()];
750
- Object [] callables = new Object [atExitHooks .size ()];
751
- for (int i = 0 ; i < hooks .length ; i ++) {
752
- cursor .advance ();
753
- callables [i ] = cursor .getKey ();
754
- hooks [i ] = cursor .getValue ();
755
- }
756
748
PException lastException = null ;
757
- for (int i = hooks .length - 1 ; i >= 0 ; i --) {
749
+ for (int i = atExitHooks .size () - 1 ; i >= 0 ; i --) {
750
+ AtExitHook hook = atExitHooks .get (i );
758
751
try {
759
- hooks [ i ] .ct .call (callables [ i ], hooks [ i ] .arguments , hooks [ i ] .keywords );
752
+ hook .ct .call (hook . callable , hook .arguments , hook .keywords );
760
753
} catch (PException e ) {
761
754
lastException = e ;
762
755
if (!IsBuiltinClassProfile .profileClassSlowPath (e .getEscapedException (), PythonBuiltinClassType .SystemExit )) {
@@ -765,6 +758,7 @@ public void runAtexitHooks() {
765
758
}
766
759
}
767
760
}
761
+ atExitHooks .clear ();
768
762
if (lastException != null ) {
769
763
throw lastException ;
770
764
}
0 commit comments