-
Notifications
You must be signed in to change notification settings - Fork 318
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Removing need for asynchronous thread to execute ResourceManager finalizers #6335
Changes from all commits
2475e7a
102869d
e8344b7
61cda38
f637e19
88d37cf
4128784
545b89c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
package org.enso.interpreter.runtime; | ||
|
||
import com.oracle.truffle.api.CompilerDirectives; | ||
import com.oracle.truffle.api.ThreadLocalAction; | ||
import com.oracle.truffle.api.TruffleSafepoint; | ||
import com.oracle.truffle.api.interop.InteropLibrary; | ||
import org.enso.interpreter.runtime.data.ManagedResource; | ||
|
||
|
@@ -9,8 +11,10 @@ | |
import java.lang.ref.ReferenceQueue; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ConcurrentMap; | ||
import java.util.concurrent.Future; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
/** Allows the context to attach garbage collection hooks on the removal of certain objects. */ | ||
public class ResourceManager { | ||
|
@@ -59,7 +63,7 @@ public void unpark(ManagedResource resource) { | |
return; | ||
} | ||
it.getParkedCount().decrementAndGet(); | ||
tryFinalize(it); | ||
scheduleFinalizationAtSafepoint(it); | ||
} | ||
|
||
/** | ||
|
@@ -75,7 +79,7 @@ public void close(ManagedResource resource) { | |
return; | ||
} | ||
// Unconditional finalization – user controls the resource manually. | ||
it.doFinalize(context); | ||
it.finalizeNow(context); | ||
} | ||
|
||
/** | ||
|
@@ -89,7 +93,7 @@ public void take(ManagedResource resource) { | |
items.remove(resource.getPhantomReference()); | ||
} | ||
|
||
private void tryFinalize(Item it) { | ||
private void scheduleFinalizationAtSafepoint(Item it) { | ||
if (it.isFlaggedForFinalization().get()) { | ||
if (it.getParkedCount().get() == 0) { | ||
// We already know that isFlaggedForFinalization was true at some | ||
|
@@ -101,8 +105,21 @@ private void tryFinalize(Item it) { | |
// no further attempts are made. | ||
boolean continueFinalizing = it.isFlaggedForFinalization().compareAndSet(true, false); | ||
if (continueFinalizing) { | ||
it.doFinalize(context); | ||
items.remove(it.reference); | ||
var futureToCancel = new AtomicReference<Future<Void>>(null); | ||
var performFinalizeNow = | ||
new ThreadLocalAction(false, false, true) { | ||
@Override | ||
protected void perform(ThreadLocalAction.Access access) { | ||
var tmp = futureToCancel.getAndSet(null); | ||
if (tmp == null) { | ||
return; | ||
} | ||
tmp.cancel(false); | ||
it.finalizeNow(context); | ||
items.remove(it.reference); | ||
} | ||
}; | ||
futureToCancel.set(context.getEnvironment().submitThreadLocal(null, performFinalizeNow)); | ||
} | ||
} | ||
} | ||
|
@@ -124,7 +141,7 @@ public ManagedResource register(Object object, Object function) { | |
} | ||
if (workerThread == null || !workerThread.isAlive()) { | ||
worker.setKilled(false); | ||
workerThread = context.getEnvironment().createThread(worker); | ||
workerThread = context.getEnvironment().createSystemThread(worker); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need thread to read "finalizable" references from |
||
workerThread.start(); | ||
} | ||
ManagedResource resource = new ManagedResource(object); | ||
|
@@ -158,7 +175,7 @@ public void shutdown() { | |
Item it = items.remove(key); | ||
if (it != null) { | ||
// Finalize unconditionally – all other threads are dead by now. | ||
it.doFinalize(context); | ||
it.finalizeNow(context); | ||
} | ||
} | ||
} | ||
|
@@ -181,7 +198,7 @@ public void run() { | |
continue; | ||
} | ||
it.isFlaggedForFinalization().set(true); | ||
tryFinalize(it); | ||
scheduleFinalizationAtSafepoint(it); | ||
} | ||
if (killed) { | ||
return; | ||
|
@@ -229,18 +246,16 @@ public Item(Object underlying, Object finalizer, PhantomReference<ManagedResourc | |
} | ||
|
||
/** | ||
* Unconditionally performs the finalization action of this resource. | ||
* Performs the finalization action of this resource right now. The thread must be inside of a | ||
* context. | ||
* | ||
* @param context current execution context | ||
*/ | ||
public void doFinalize(EnsoContext context) { | ||
Object p = context.getThreadManager().enter(); | ||
public void finalizeNow(EnsoContext context) { | ||
try { | ||
InteropLibrary.getUncached(finalizer).execute(finalizer, underlying); | ||
} catch (Exception e) { | ||
context.getErr().println("Exception in finalizer: " + e.getMessage()); | ||
} finally { | ||
context.getThreadManager().leave(p); | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -113,6 +113,7 @@ class InterpreterContext( | |
.newBuilder(LanguageInfo.ID) | ||
.allowExperimentalOptions(true) | ||
.allowAllAccess(true) | ||
.allowCreateThread(false) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The changes in this PR remove the need for multiple thread. All |
||
.out(output) | ||
.err(err) | ||
.option(RuntimeOptions.LOG_LEVEL, "WARNING") | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unreleated. Addresses #6304 (comment)