Skip to content
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

Resource scopes draft #354

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft

Conversation

rnett
Copy link
Contributor

@rnett rnett commented Jul 16, 2021

This is a rough draft of resource scopes. It's not fully implemented or used anywhere, but the deallocation behavior and API are there, and that's mostly what I want comments on. Synchronization can probably be done better, too, but not to much should change (suggestions on that are welcome if you notice anything that sticks out).

There's four variants from two variables: implicit/explicit and strong/weak. Implicit/explicit controls whether the scope itself is GC'd (unlike panama, you can still manually close implicit scopes). Strong/weak controls whether the resources will be GC'd. A strong scope holds on to all of it's references and dereferences them on close, while a weak scope only holds onto them weakly, and thus only dereferences the ones that are still reachable at close. This can result in some resources being deallocated after their scope closes, which is why I provide the option for strong scopes, but it's mostly fine.

I've considered getting rid of implicit/explicit and just making all scopes be closed on GC, but I assume there's a reason panama didn't do that. I don't think we would run into the same issues since we're reference counting: even if a scope is GC'd, its resources would only be deallocated if there were no other scopes referencing them, but I'm not sure enough to remove it.

I also have addDependency which works like panama's in that a scope can't be closed until any scopes with dependencies on it are.

cc @karllessard @Craigacp @saudet

Signed-off-by: Ryan Nett <JNett96@gmail.com>
Signed-off-by: Ryan Nett <JNett96@gmail.com>
Signed-off-by: Ryan Nett <JNett96@gmail.com>
@rnett
Copy link
Contributor Author

rnett commented Jul 17, 2021

The WeakIdentityHashMap is taken from https://github.com/plume-lib/plume-util and is mostly a placeholder atm.

@rnett
Copy link
Contributor Author

rnett commented Sep 16, 2021

I'm planning on doing the same thing as discussed in https://inside.java/2021/09/16/finalizing-the-foreign-apis/: registering all scopes w/ the cleaner, but also allowing them to be closed manually.

@saudet
Copy link
Contributor

saudet commented Sep 16, 2021

I'm planning on doing the same thing as discussed in https://inside.java/2021/09/16/finalizing-the-foreign-apis/: registering all scopes w/ the cleaner, but also allowing them to be closed manually.

That's fine, as long as there is a way to disable this behavior, see issue #313.

@rnett
Copy link
Contributor Author

rnett commented Sep 24, 2021

I'm also going to add some way to add "on added" and "on removed" handlers at a scope level, to support stuff like the new gradient API's memory management (see the "Some details on memory management" section above here).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants