-
-
Notifications
You must be signed in to change notification settings - Fork 698
Add haxe.GcFinalizer with handle-based API #12766
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
83f179c
Add haxe.GcFinalizer (FinalizationRegistry pattern)
jdonaldson e7d949b
Redesign GcFinalizer: token-based → handle-based API
jdonaldson 34a9550
Update CHANGES.txt for ICloseable and GcFinalizer handle API
jdonaldson 913d379
Rename ICloseable to IHandle, stub cpp implementation
jdonaldson 54ec1d4
Implement cpp GcFinalizer with WeakRef polling, add atomics to eval/JVM
jdonaldson 16c4998
Use compareExchange for race-safe cleanup in GcFinalizer
jdonaldson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| /* | ||
| * Copyright (C)2005-2019 Haxe Foundation | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a | ||
| * copy of this software and associated documentation files (the "Software"), | ||
| * to deal in the Software without restriction, including without limitation | ||
| * the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
| * and/or sell copies of the Software, and to permit persons to whom the | ||
| * Software is furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in | ||
| * all copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
| * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
| * DEALINGS IN THE SOFTWARE. | ||
| */ | ||
|
|
||
| package haxe; | ||
|
|
||
| import haxe.atomic.AtomicBool; | ||
|
|
||
| private class Registration<T> { | ||
| public var heldValue:Null<T>; | ||
| public var callback:Null<T->Void>; | ||
| public var cancelled:AtomicBool; | ||
| public var weakTarget:cpp.vm.WeakRef<Dynamic>; | ||
|
|
||
| public function new(target:Dynamic, heldValue:T, callback:T->Void) { | ||
| this.weakTarget = new cpp.vm.WeakRef<Dynamic>(target); | ||
| this.heldValue = heldValue; | ||
| this.callback = callback; | ||
| this.cancelled = new AtomicBool(false); | ||
| } | ||
| } | ||
|
|
||
| private class Handle<T> implements IHandle { | ||
| var reg:Registration<T>; | ||
|
|
||
| public function new(reg:Registration<T>) { | ||
| this.reg = reg; | ||
| } | ||
|
|
||
| public function close():Void { | ||
| if (reg.cancelled.compareExchange(false, true) == false) { | ||
| reg.callback = null; | ||
| reg.heldValue = null; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @:coreApi | ||
| class GcFinalizer<T> { | ||
| var callback:T->Void; | ||
| var registrations:Array<Registration<T>>; | ||
|
|
||
| public function new(callback:T->Void) { | ||
| this.callback = callback; | ||
| this.registrations = []; | ||
| } | ||
|
|
||
| function pollQueue():Void { | ||
| var i = registrations.length - 1; | ||
| while (i >= 0) { | ||
| var reg = registrations[i]; | ||
| if (reg.weakTarget.get() == null) { | ||
| if (reg.cancelled.compareExchange(false, true) == false) { | ||
| reg.callback(reg.heldValue); | ||
| reg.callback = null; | ||
| reg.heldValue = null; | ||
| } | ||
| registrations.splice(i, 1); | ||
| } else if (reg.cancelled.load()) { | ||
| // close() already nulled fields | ||
| registrations.splice(i, 1); | ||
| } | ||
| i--; | ||
| } | ||
| } | ||
|
|
||
| public function register(target:{}, heldValue:T):IHandle { | ||
| pollQueue(); | ||
| var reg = new Registration(target, heldValue, callback); | ||
| registrations.push(reg); | ||
| return new Handle(reg); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| /* | ||
| * Copyright (C)2005-2019 Haxe Foundation | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a | ||
| * copy of this software and associated documentation files (the "Software"), | ||
| * to deal in the Software without restriction, including without limitation | ||
| * the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
| * and/or sell copies of the Software, and to permit persons to whom the | ||
| * Software is furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in | ||
| * all copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
| * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
| * DEALINGS IN THE SOFTWARE. | ||
| */ | ||
|
|
||
| package haxe; | ||
|
|
||
| import haxe.atomic.AtomicBool; | ||
|
|
||
| private class Registration<T> { | ||
| public var heldValue:Null<T>; | ||
| public var cancelled:AtomicBool; | ||
| public var callback:Null<T->Void>; | ||
|
|
||
| public function new(heldValue:T, callback:T->Void) { | ||
| this.heldValue = heldValue; | ||
| this.cancelled = new AtomicBool(false); | ||
| this.callback = callback; | ||
| } | ||
| } | ||
|
|
||
| private class Handle<T> implements IHandle { | ||
| var reg:Registration<T>; | ||
|
|
||
| public function new(reg:Registration<T>) { | ||
| this.reg = reg; | ||
| } | ||
|
|
||
| public function close():Void { | ||
| if (reg.cancelled.compareExchange(false, true) == false) { | ||
| reg.callback = null; | ||
| reg.heldValue = null; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @:coreApi | ||
| class GcFinalizer<T> { | ||
| var callback:T->Void; | ||
|
|
||
| public function new(callback:T->Void) { | ||
| this.callback = callback; | ||
| } | ||
|
|
||
| public function register(target:{}, heldValue:T):IHandle { | ||
| var reg = new Registration(heldValue, callback); | ||
| eval.vm.Gc.finalise(function(_) { | ||
| if (reg.cancelled.compareExchange(false, true) == false) { | ||
| reg.callback(reg.heldValue); | ||
| reg.callback = null; | ||
| reg.heldValue = null; | ||
| } | ||
| }, target); | ||
|
|
||
| return new Handle(reg); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| /* | ||
| * Copyright (C)2005-2019 Haxe Foundation | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a | ||
| * copy of this software and associated documentation files (the "Software"), | ||
| * to deal in the Software without restriction, including without limitation | ||
| * the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
| * and/or sell copies of the Software, and to permit persons to whom the | ||
| * Software is furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in | ||
| * all copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
| * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
| * DEALINGS IN THE SOFTWARE. | ||
| */ | ||
|
|
||
| package haxe; | ||
|
|
||
| /** | ||
| A GC finalizer registry that invokes a callback when a watched object | ||
| is garbage-collected. Modeled after JavaScript's `FinalizationRegistry`. | ||
|
|
||
| The callback receives a held value (not the collected object itself), | ||
| which is safe because the object may already be in an invalid state. | ||
|
|
||
| Not all targets support GC finalizers. On unsupported targets, the | ||
| constructor throws `NotImplementedException`. | ||
| **/ | ||
| class GcFinalizer<T> { | ||
| /** | ||
| Creates a new `GcFinalizer` with the given cleanup `callback`. | ||
| The callback will be invoked with the held value when a registered | ||
| target object is garbage-collected. | ||
| **/ | ||
| public function new(callback:T->Void) { | ||
|
Aidan63 marked this conversation as resolved.
|
||
| throw new haxe.exceptions.NotImplementedException("Not implemented for this platform"); | ||
| } | ||
|
|
||
| /** | ||
| Registers `target` for clean-up. When `target` is garbage-collected, | ||
| the callback will be invoked with `heldValue`. | ||
|
|
||
| Returns an `IHandle` handle. Calling `close()` on the handle | ||
| cancels the registration, preventing the callback from firing. | ||
| **/ | ||
| public function register(target:{}, heldValue:T):IHandle { | ||
| return null; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| /* | ||
| * Copyright (C)2005-2019 Haxe Foundation | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a | ||
| * copy of this software and associated documentation files (the "Software"), | ||
| * to deal in the Software without restriction, including without limitation | ||
| * the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
| * and/or sell copies of the Software, and to permit persons to whom the | ||
| * Software is furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in | ||
| * all copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
| * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
| * DEALINGS IN THE SOFTWARE. | ||
| */ | ||
|
|
||
| package haxe; | ||
|
|
||
| /** | ||
| A general-purpose interface for handles that can be released | ||
| by calling `close()`. Used by `GcFinalizer` to return cancellation | ||
| handles, and intended for thread callback handles, coroutine | ||
| scheduler handles, and similar patterns. | ||
| **/ | ||
| interface IHandle { | ||
| function close():Void; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| /* | ||
| * Copyright (C)2005-2019 Haxe Foundation | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a | ||
| * copy of this software and associated documentation files (the "Software"), | ||
| * to deal in the Software without restriction, including without limitation | ||
| * the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
| * and/or sell copies of the Software, and to permit persons to whom the | ||
| * Software is furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in | ||
| * all copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
| * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
| * DEALINGS IN THE SOFTWARE. | ||
| */ | ||
|
|
||
| package haxe; | ||
|
|
||
| @:coreApi | ||
| class GcFinalizer<T> { | ||
| var h:js.lib.FinalizationRegistry<T>; | ||
|
|
||
| public inline function new(callback:T->Void) { | ||
| h = new js.lib.FinalizationRegistry(callback); | ||
| } | ||
|
|
||
| public inline function register(target:{}, heldValue:T):IHandle { | ||
| var token = {}; | ||
| h.register(target, heldValue, token); | ||
| return cast {close: function() h.unregister(token)}; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package js.lib; | ||
| /* | ||
| * Copyright (C)2005-2019 Haxe Foundation | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a | ||
| * copy of this software and associated documentation files (the "Software"), | ||
| * to deal in the Software without restriction, including without limitation | ||
| * the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
| * and/or sell copies of the Software, and to permit persons to whom the | ||
| * Software is furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in | ||
| * all copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
| * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
| * DEALINGS IN THE SOFTWARE. | ||
| */ | ||
|
|
||
| /** | ||
| A `FinalizationRegistry` object lets you request a callback when a value is garbage-collected. | ||
|
|
||
| Documentation [FinalizationRegistry](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/FinalizationRegistry) by | ||
| [Mozilla Contributors](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/FinalizationRegistry/contributors.txt), | ||
| licensed under [CC-BY-SA 2.5](https://creativecommons.org/licenses/by-sa/2.5/). | ||
| **/ | ||
| @:native("FinalizationRegistry") | ||
| extern class FinalizationRegistry<T> { | ||
| /** | ||
| Creates a new `FinalizationRegistry` with the given cleanup callback. | ||
| **/ | ||
| function new(cleanupCallback:T->Void); | ||
|
|
||
| /** | ||
| Registers a target object with the registry, associating it with a held value | ||
| and an optional unregister token. | ||
| **/ | ||
| function register(target:{}, heldValue:T, ?unregisterToken:{}):Void; | ||
|
|
||
| /** | ||
| Unregisters any registrations associated with the given unregister token. | ||
| Returns `true` if at least one registration was removed. | ||
| **/ | ||
| function unregister(unregisterToken:{}):Bool; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
claude left these in again...
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.
Maybe adding a section above that for next release would both make release process a bit easier and allow Claude to add changelog lines properly?
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.
In that case we'll probably want to make it a requirement to put changelog entries with every PR to avoid having to filter through stuff at release time?