-
Notifications
You must be signed in to change notification settings - Fork 48
SwiftArena.ofAuto() which uses GC to manage swift instances (and destroy them) #133
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
2 commits
Select commit
Hold shift + click to select a range
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
70 changes: 70 additions & 0 deletions
70
SwiftKit/src/main/java/org/swift/swiftkit/AutoSwiftMemorySession.java
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,70 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of Swift.org project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
package org.swift.swiftkit; | ||
|
||
import java.lang.foreign.MemorySegment; | ||
import java.lang.ref.Cleaner; | ||
import java.util.Objects; | ||
import java.util.concurrent.ThreadFactory; | ||
|
||
/** | ||
* A memory session which manages registered objects via the Garbage Collector. | ||
* | ||
* <p> When registered Java wrapper classes around native Swift instances {@link SwiftInstance}, | ||
* are eligible for collection, this will trigger the cleanup of the native resources as well. | ||
* | ||
* <p> This memory session is LESS reliable than using a {@link ConfinedSwiftMemorySession} because | ||
* the timing of when the native resources are cleaned up is somewhat undefined, and rely on the | ||
* system GC. Meaning, that if an object nas been promoted to an old generation, there may be a | ||
* long time between the resource no longer being referenced "in Java" and its native memory being released, | ||
* and also the deinit of the Swift type being run. | ||
* | ||
* <p> This can be problematic for Swift applications which rely on quick release of resources, and may expect | ||
* the deinits to run in expected and "quick" succession. | ||
* | ||
* <p> Whenever possible, prefer using an explicitly managed {@link SwiftArena}, such as {@link SwiftArena#ofConfined()}. | ||
*/ | ||
final class AutoSwiftMemorySession implements SwiftArena { | ||
|
||
private final Cleaner cleaner; | ||
|
||
public AutoSwiftMemorySession(ThreadFactory cleanerThreadFactory) { | ||
this.cleaner = Cleaner.create(cleanerThreadFactory); | ||
} | ||
|
||
@Override | ||
public void register(SwiftHeapObject object) { | ||
SwiftHeapObjectCleanup cleanupAction = new SwiftHeapObjectCleanup(object.$memorySegment(), object.$swiftType()); | ||
register(object, cleanupAction); | ||
} | ||
|
||
// visible for testing | ||
void register(SwiftHeapObject object, SwiftHeapObjectCleanup cleanupAction) { | ||
Objects.requireNonNull(object, "obj"); | ||
Objects.requireNonNull(cleanupAction, "cleanupAction"); | ||
|
||
|
||
cleaner.register(object, cleanupAction); | ||
} | ||
|
||
@Override | ||
public void register(SwiftValue value) { | ||
Objects.requireNonNull(value, "value"); | ||
MemorySegment resource = value.$memorySegment(); | ||
var cleanupAction = new SwiftValueCleanup(resource); | ||
cleaner.register(value, cleanupAction); | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
SwiftKit/src/main/java/org/swift/swiftkit/ClosableSwiftArena.java
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,28 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of Swift.org project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
package org.swift.swiftkit; | ||
|
||
/** | ||
* Auto-closable version of {@link SwiftArena}. | ||
*/ | ||
public interface ClosableSwiftArena extends SwiftArena, AutoCloseable { | ||
|
||
/** | ||
* Close the arena and make sure all objects it managed are released. | ||
* Throws if unable to verify all resources have been release (e.g. over retained Swift classes) | ||
*/ | ||
void close(); | ||
|
||
} |
86 changes: 86 additions & 0 deletions
86
SwiftKit/src/main/java/org/swift/swiftkit/ConfinedSwiftMemorySession.java
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,86 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of Swift.org project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
package org.swift.swiftkit; | ||
ktoso marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
final class ConfinedSwiftMemorySession implements ClosableSwiftArena { | ||
|
||
final static int CLOSED = 0; | ||
final static int ACTIVE = 1; | ||
|
||
final Thread owner; | ||
final AtomicInteger state; | ||
|
||
final ConfinedResourceList resources; | ||
|
||
public ConfinedSwiftMemorySession(Thread owner) { | ||
this.owner = owner; | ||
this.state = new AtomicInteger(ACTIVE); | ||
this.resources = new ConfinedResourceList(); | ||
} | ||
|
||
public void checkValid() throws RuntimeException { | ||
if (this.owner != null && this.owner != Thread.currentThread()) { | ||
throw new WrongThreadException("ConfinedSwift arena is confined to %s but was closed from %s!".formatted(this.owner, Thread.currentThread())); | ||
} else if (this.state.get() < ACTIVE) { | ||
throw new RuntimeException("SwiftArena is already closed!"); | ||
} | ||
} | ||
|
||
@Override | ||
public void close() { | ||
checkValid(); | ||
|
||
// Cleanup all resources | ||
if (this.state.compareAndExchange(ACTIVE, CLOSED) == ACTIVE) { | ||
this.resources.runCleanup(); | ||
} // else, was already closed; do nothing | ||
} | ||
|
||
@Override | ||
public void register(SwiftHeapObject object) { | ||
checkValid(); | ||
|
||
var cleanup = new SwiftHeapObjectCleanup(object.$memorySegment(), object.$swiftType()); | ||
this.resources.add(cleanup); | ||
} | ||
|
||
@Override | ||
public void register(SwiftValue value) { | ||
checkValid(); | ||
|
||
var cleanup = new SwiftValueCleanup(value.$memorySegment()); | ||
this.resources.add(cleanup); | ||
} | ||
|
||
static final class ConfinedResourceList implements SwiftResourceList { | ||
// TODO: Could use intrusive linked list to avoid one indirection here | ||
final List<SwiftInstanceCleanup> resourceCleanups = new LinkedList<>(); | ||
|
||
void add(SwiftInstanceCleanup cleanup) { | ||
resourceCleanups.add(cleanup); | ||
} | ||
|
||
@Override | ||
public void runCleanup() { | ||
for (SwiftInstanceCleanup cleanup : resourceCleanups) { | ||
cleanup.run(); | ||
} | ||
} | ||
} | ||
} |
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
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.
Uh oh!
There was an error while loading. Please reload this page.