Skip to content

Commit

Permalink
Make usages of isActive and isActiveNotCancelled more clear. Use isAc…
Browse files Browse the repository at this point in the history
…tive for when performing cleaning for close.
  • Loading branch information
chumer committed Aug 27, 2020
1 parent 00135a0 commit 622add3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1230,7 +1230,7 @@ public void closeContext(Object impl, boolean force, Node closeLocation, boolean
throw context.createCancelException(closeLocation);
}
} else {
if (context.isActive()) {
if (context.isActiveNotCancelled()) {
throw new IllegalStateException("The context is currently active and cannot be closed. Make sure no thread is running or call closeCancelled on the context to resolve this.");
}
context.closeImpl(false, false, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ void setCachedThreadInfo(PolyglotThreadInfo info) {
}

synchronized void checkMultiThreadedAccess(PolyglotThread newThread) {
boolean singleThread = singleThreaded.isValid() ? !isActive() : false;
boolean singleThread = singleThreaded.isValid() ? !isActiveNotCancelled() : false;
checkAllThreadAccesses(newThread, singleThread);
}

Expand Down Expand Up @@ -1050,6 +1050,15 @@ Map<Thread, PolyglotThreadInfo> getSeenThreads() {
return threads;
}

synchronized boolean isActiveNotCancelled() {
for (PolyglotThreadInfo seenTinfo : threads.values()) {
if (seenTinfo.isActiveNotCancelled()) {
return true;
}
}
return false;
}

synchronized boolean isActive() {
for (PolyglotThreadInfo seenTinfo : threads.values()) {
if (seenTinfo.isActive()) {
Expand All @@ -1059,6 +1068,14 @@ synchronized boolean isActive() {
return false;
}

synchronized boolean isActiveNotCancelled(Thread thread) {
PolyglotThreadInfo info = threads.get(thread);
if (info == null || info == PolyglotThreadInfo.NULL) {
return false;
}
return info.isActiveNotCancelled();
}

synchronized boolean isActive(Thread thread) {
PolyglotThreadInfo info = threads.get(thread);
if (info == null || info == PolyglotThreadInfo.NULL) {
Expand All @@ -1074,7 +1091,7 @@ PolyglotThreadInfo getFirstActiveOtherThread(boolean includePolyglotThread) {
if (!includePolyglotThread && otherInfo.isPolyglotThread(this)) {
continue;
}
if (!otherInfo.isCurrent() && otherInfo.isActive()) {
if (!otherInfo.isCurrent() && otherInfo.isActiveNotCancelled()) {
return otherInfo;
}
}
Expand Down Expand Up @@ -1250,7 +1267,6 @@ boolean closeImpl(boolean cancelIfExecuting, boolean waitForPolyglotThreads, boo
langContext.close();
}
}
setCachedThreadInfo(PolyglotThreadInfo.NULL);
Object[] impls = this.contextImpls;
if (impls != null) {
Arrays.fill(impls, null);
Expand Down Expand Up @@ -1330,7 +1346,7 @@ synchronized void sendInterrupt() {
return;
}
for (PolyglotThreadInfo threadInfo : threads.values()) {
if (!threadInfo.isCurrent() && threadInfo.isActive()) {
if (!threadInfo.isCurrent() && threadInfo.isActiveNotCancelled()) {
/*
* We send an interrupt to the thread to wake up and to run some guest language code
* in case they are waiting in some async primitive. The interrupt is then cleared
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,14 @@ boolean isLastActive() {
return getThread() != null && enteredCount == 1 && !cancelled;
}

boolean isActive() {
boolean isActiveNotCancelled() {
return getThread() != null && enteredCount > 0 && !cancelled;
}

boolean isActive() {
return getThread() != null && enteredCount > 0;
}

@Override
public String toString() {
return super.toString() + "[thread=" + getThread() + ", enteredCount=" + enteredCount + ", cancelled=" + cancelled + "]";
Expand Down

0 comments on commit 622add3

Please sign in to comment.