83
83
import java .util .function .Supplier ;
84
84
import java .util .logging .Level ;
85
85
86
+ import org .graalvm .collections .Pair ;
86
87
import org .graalvm .options .OptionValues ;
87
88
import org .graalvm .polyglot .Context ;
88
89
import org .graalvm .polyglot .PolyglotException ;
@@ -928,7 +929,7 @@ void resume(Future<Void> pauseFuture) {
928
929
@ TruffleBoundary
929
930
Object [] enterThreadChanged (boolean enterReverted , boolean pollSafepoint , boolean mustSucceed , PolyglotThreadTask polyglotThreadFirstEnter ,
930
931
boolean leaveAndEnter ) {
931
- List <Map . Entry <Thread , PolyglotThreadInfo >> deadThreads = null ;
932
+ List <Pair <Thread , PolyglotThreadInfo >> deadThreads = null ;
932
933
PolyglotThreadInfo enteredThread = null ;
933
934
boolean localEnterReverted = enterReverted ;
934
935
Object [] prev = null ;
@@ -1121,21 +1122,21 @@ Object[] enterThreadChanged(boolean enterReverted, boolean pollSafepoint, boolea
1121
1122
}
1122
1123
}
1123
1124
1124
- private void finalizeAndDisposeThreads (List <Map . Entry <Thread , PolyglotThreadInfo >> deadThreads ) {
1125
+ private void finalizeAndDisposeThreads (List <Pair <Thread , PolyglotThreadInfo >> deadThreads ) {
1125
1126
assert !Thread .holdsLock (this );
1126
1127
Throwable ex = null ;
1127
- for (Map . Entry <Thread , PolyglotThreadInfo > removedThreadInfoEntryToRemove : deadThreads ) {
1128
- ex = notifyThreadFinalizing (removedThreadInfoEntryToRemove .getValue (), ex , false );
1128
+ for (Pair <Thread , PolyglotThreadInfo > removedThreadInfoEntryToRemove : deadThreads ) {
1129
+ ex = notifyThreadFinalizing (removedThreadInfoEntryToRemove .getRight (), ex , false );
1129
1130
}
1130
1131
1131
- for (Map . Entry <Thread , PolyglotThreadInfo > threadInfoEntryToRemove : deadThreads ) {
1132
- ex = notifyThreadDisposing (threadInfoEntryToRemove .getValue (), ex );
1132
+ for (Pair <Thread , PolyglotThreadInfo > threadInfoEntryToRemove : deadThreads ) {
1133
+ ex = notifyThreadDisposing (threadInfoEntryToRemove .getRight (), ex );
1133
1134
}
1134
1135
1135
1136
synchronized (this ) {
1136
- for (Map . Entry <Thread , PolyglotThreadInfo > threadInfoEntryToRemove : deadThreads ) {
1137
- threadInfoEntryToRemove .getValue ().setContextThreadLocals (DISPOSED_CONTEXT_THREAD_LOCALS );
1138
- threads .remove (threadInfoEntryToRemove .getKey ());
1137
+ for (Pair <Thread , PolyglotThreadInfo > threadInfoEntryToRemove : deadThreads ) {
1138
+ threadInfoEntryToRemove .getRight ().setContextThreadLocals (DISPOSED_CONTEXT_THREAD_LOCALS );
1139
+ threads .remove (threadInfoEntryToRemove .getLeft ());
1139
1140
}
1140
1141
}
1141
1142
@@ -1144,20 +1145,22 @@ private void finalizeAndDisposeThreads(List<Map.Entry<Thread, PolyglotThreadInfo
1144
1145
}
1145
1146
}
1146
1147
1147
- private List <Map . Entry <Thread , PolyglotThreadInfo >> collectDeadThreads () {
1148
+ private List <Pair <Thread , PolyglotThreadInfo >> collectDeadThreads () {
1148
1149
assert Thread .holdsLock (this );
1149
- List <Map . Entry <Thread , PolyglotThreadInfo >> deadThreads = null ;
1150
+ List <Pair <Thread , PolyglotThreadInfo >> deadThreads = null ;
1150
1151
/*
1151
1152
* A thread is added to the threads map only by the thread itself, so when the thread is in
1152
1153
* the map, and it is not alive, then it surely won't be used ever again.
1153
1154
*/
1154
1155
for (Map .Entry <Thread , PolyglotThreadInfo > threadInfoEntry : threads .entrySet ()) {
1155
- if (!threadInfoEntry .getKey ().isAlive () && !threadInfoEntry .getValue ().isFinalizingDeadThread ()) {
1156
+ Thread thread = threadInfoEntry .getKey ();
1157
+ PolyglotThreadInfo threadInfo = threadInfoEntry .getValue ();
1158
+ if (thread != null && threadInfo != null && !thread .isAlive () && !threadInfo .isFinalizingDeadThread ()) {
1156
1159
if (deadThreads == null ) {
1157
1160
deadThreads = new ArrayList <>();
1158
1161
}
1159
- deadThreads .add (threadInfoEntry );
1160
- threadInfoEntry . getValue () .setFinalizingDeadThread ();
1162
+ deadThreads .add (Pair . create ( thread , threadInfo ) );
1163
+ threadInfo .setFinalizingDeadThread ();
1161
1164
}
1162
1165
}
1163
1166
return deadThreads ;
0 commit comments