Skip to content

Commit 276a885

Browse files
author
Jaroslav Tulach
committed
Allow system controlled Managed_Resource
1 parent ae83008 commit 276a885

3 files changed

Lines changed: 55 additions & 7 deletions

File tree

distribution/lib/Standard/Base/0.0.0-dev/src/Runtime/Managed_Resource.enso

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import project.Any.Any
44
import project.Nothing.Nothing
5+
import project.Data.Boolean.Boolean
56

67
## Resource provides an API for manual management of computation resources.
78

@@ -34,17 +35,25 @@ type Managed_Resource
3435
ADVANCED
3536

3637
Registers a resource with the resource manager to be cleaned up using
37-
function once it is no longer in use.
38+
function once it is no longer in use. The optional `system_finalization_allowed`
39+
flag allow the system to explicitly call `finalize` on the resource
40+
when _"needed"_. The definition is intentionally vague, but
41+
currently the IDE performs such a call when user requests a _"reload"_ -
42+
e.g. using `Managed_Resource.register cache cleanup_fn True` is useful
43+
for creating user managed caches.
3844

3945
Arguments:
4046
- resource: The resource to register.
4147
- function: The action to be executed on resource to clean it up when
4248
it is no longer in use.
49+
- system_finalization_allowed: is the system allowed to call `finalize`
50+
on the resource when "needed"
4351

4452
Returns:
4553
A `Managed_Resource` object that can be used to access the resource.
46-
register : Any -> (Any -> Nothing) -> Managed_Resource
47-
register resource function = @Builtin_Method "Managed_Resource.register"
54+
register : Any -> (Any -> Nothing) -> Boolean -> Managed_Resource
55+
register resource function system_finalization_allowed=Boolean.False =
56+
@Tail_Call register_builtin resource function system_finalization_allowed
4857

4958
## PRIVATE
5059
ADVANCED
@@ -78,3 +87,5 @@ type Managed_Resource
7887
managed resources system.
7988
take : Any
8089
take self = @Builtin_Method "Managed_Resource.take"
90+
91+
register_builtin r fn sys:Boolean = @Builtin_Method "Managed_Resource.register_builtin"

engine/runtime/src/main/java/org/enso/interpreter/runtime/ResourceManager.java

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,22 +112,40 @@ public void take(ManagedResource resource) {
112112
removeFromItems(resource.getPhantomReference());
113113
}
114114

115+
/**
116+
* Registers a new (non-system controlled) resource to the system. Calls {@link
117+
* #register(java.lang.Object, java.lang.Object, boolean)} with {@code false} as last argument.
118+
*
119+
* @param object the underlying resource
120+
* @param function the finalizer action to call on the underlying resource
121+
* @return a wrapper object, containing the resource and serving as a reachability probe
122+
*/
123+
@CompilerDirectives.TruffleBoundary
124+
public ManagedResource register(Object object, Object function) {
125+
return register(object, function, false);
126+
}
127+
115128
/**
116129
* Registers a new resource to the system. {@code function} will be called on {@code object} when
117130
* the value returned by this method becomes unreachable.
118131
*
119132
* @param object the underlying resource
120133
* @param function the finalizer action to call on the underlying resource
134+
* @param systemResource resource is subject to finalization when {@link #scheduleFinalization} is
135+
* called
121136
* @return a wrapper object, containing the resource and serving as a reachability probe
122137
*/
123138
@CompilerDirectives.TruffleBoundary
124-
public synchronized ManagedResource register(Object object, Object function) {
139+
public synchronized ManagedResource register(
140+
Object object, Object function, boolean systemResource) {
125141
if (CLOSED == processor) {
126142
throw EnsoContext.get(null)
127143
.raiseAssertionPanic(
128144
null, "Can't register new resources after resource manager is closed.", null);
129145
}
130-
var resource = new ManagedResource(object, r -> new Item(r, object, function, referenceQueue));
146+
var resource =
147+
new ManagedResource(
148+
object, r -> new Item(r, object, function, systemResource, referenceQueue));
131149
var ref = (Item) resource.getPhantomReference();
132150
addPendingItem(ref);
133151
return resource;
@@ -186,6 +204,21 @@ private synchronized void removeFromItems(PhantomReference<ManagedResource> it)
186204
}
187205
}
188206

207+
/**
208+
* Explicitly schedules all the <em>system references</em> registered with the manager for
209+
* finalization.
210+
*
211+
* @see #register
212+
*/
213+
@CompilerDirectives.TruffleBoundary
214+
public final synchronized void scheduleFinalization() {
215+
for (var item : pendingItems) {
216+
if (item.systemResource) {
217+
item.enqueue();
218+
}
219+
}
220+
}
221+
189222
/**
190223
* Awaits next item in the queue, if any.
191224
*
@@ -360,6 +393,7 @@ Collection<Item> awaitShutdown() {
360393

361394
/** A storage representation of a finalizable object handled by this system. */
362395
private static final class Item extends PhantomReference<ManagedResource> {
396+
private final boolean systemResource;
363397
private final Object underlying;
364398
private final Object finalizer;
365399

@@ -393,10 +427,12 @@ private Item(
393427
ManagedResource referent,
394428
Object underlying,
395429
Object finalizer,
430+
boolean systemResource,
396431
ReferenceQueue<ManagedResource> queue) {
397432
super(referent, queue);
398433
this.underlying = underlying;
399434
this.finalizer = finalizer;
435+
this.systemResource = systemResource;
400436
}
401437

402438
/**

engine/runtime/src/main/java/org/enso/interpreter/runtime/data/ManagedResource.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,9 @@ public PhantomReference<ManagedResource> getPhantomReference() {
7373
"Makes an object into a managed resource, automatically finalized when the returned"
7474
+ " object is garbage collected.")
7575
@Builtin.Specialize
76-
public static ManagedResource register(EnsoContext context, Object resource, Function function) {
77-
return context.getResourceManager().register(resource, function);
76+
public static ManagedResource register_builtin(
77+
EnsoContext context, Object resource, Function function, boolean systemCanFinalize) {
78+
return context.getResourceManager().register(resource, function, systemCanFinalize);
7879
}
7980

8081
@Builtin.Method(

0 commit comments

Comments
 (0)