-
Notifications
You must be signed in to change notification settings - Fork 67
[JExtract/JNI] Add support for async Swift methods
#413
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
26 commits
Select commit
Hold shift + click to select a range
0869e55
introduce async mode
madsodgaard 7a75d45
import async functions using semaphores and completablefuture
madsodgaard d8db270
add support for async throws
madsodgaard 850225a
rename SwiftKitSwift
madsodgaard 3fe6636
move helper files to runtime support
madsodgaard 1117faf
make confined arena threadsafe
madsodgaard 4b04da2
update to Swift 6.2
madsodgaard f187e2a
use immediate when available
madsodgaard 7ce00dd
add async tests
madsodgaard adfdea8
add docs
madsodgaard d693691
add "future" mode
madsodgaard f6db47e
remove space
madsodgaard 1251307
add async benchmark
madsodgaard 2d0e79b
use deamon threads
madsodgaard 26bb992
use forkjoin pool
madsodgaard a774558
work on upcall
madsodgaard e0b3930
get upcall working
madsodgaard e7c8619
remove force unwrap of JNI environment
madsodgaard c94b250
small cleanups
madsodgaard 736a48d
update docs
madsodgaard 23b837e
fix codegen tests
madsodgaard cd5011f
remove legacy mode for now
madsodgaard 136bd14
fix dependencies
madsodgaard d64ceca
output correct lib name for FFM
madsodgaard 997f14b
remove dependencies from SwiftRuntimeFunctions
madsodgaard 49f7788
update swift-syntax
madsodgaard 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
Some comments aren't visible on the classic Files Changed page.
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
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
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
38 changes: 38 additions & 0 deletions
38
Samples/SwiftJavaExtractJNISampleApp/Sources/MySwiftLibrary/Async.swift
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 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the Swift.org open source project | ||
| // | ||
| // Copyright (c) 2025 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 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| import SwiftJava | ||
|
|
||
| public func asyncSum(i1: Int64, i2: Int64) async -> Int64 { | ||
| return i1 + i2 | ||
| } | ||
|
|
||
| public func asyncSleep() async throws { | ||
| try await Task.sleep(for: .milliseconds(500)) | ||
| } | ||
|
|
||
| public func asyncCopy(myClass: MySwiftClass) async throws -> MySwiftClass { | ||
| let new = MySwiftClass(x: myClass.x, y: myClass.y) | ||
| try await Task.sleep(for: .milliseconds(500)) | ||
| return new | ||
| } | ||
|
|
||
| public func asyncOptional(i: Int64) async throws -> Int64? { | ||
| try await Task.sleep(for: .milliseconds(100)) | ||
| return i | ||
| } | ||
|
|
||
| public func asyncThrows() async throws { | ||
| throw MySwiftError.swiftError | ||
| } |
17 changes: 17 additions & 0 deletions
17
Samples/SwiftJavaExtractJNISampleApp/Sources/MySwiftLibrary/MySwiftError.swift
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,17 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the Swift.org open source project | ||
| // | ||
| // Copyright (c) 2025 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 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| enum MySwiftError: Error { | ||
| case swiftError | ||
| } |
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
60 changes: 60 additions & 0 deletions
60
Samples/SwiftJavaExtractJNISampleApp/src/jmh/java/com/example/swift/AsyncBenchmark.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,60 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the Swift.org open source project | ||
| // | ||
| // Copyright (c) 2025 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 com.example.swift; | ||
|
|
||
| import org.openjdk.jmh.annotations.*; | ||
| import org.openjdk.jmh.infra.Blackhole; | ||
| import org.swift.swiftkit.core.ClosableSwiftArena; | ||
| import org.swift.swiftkit.core.ConfinedSwiftMemorySession; | ||
| import org.swift.swiftkit.core.SwiftArena; | ||
|
|
||
| import java.util.*; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import java.util.concurrent.ExecutorService; | ||
| import java.util.concurrent.Executors; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| @BenchmarkMode(Mode.AverageTime) | ||
| @Warmup(iterations = 5, time = 200, timeUnit = TimeUnit.MILLISECONDS) | ||
| @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS) | ||
| @OutputTimeUnit(TimeUnit.MICROSECONDS) | ||
| @State(Scope.Benchmark) | ||
| @Fork(value = 1, jvmArgsAppend = { "--enable-native-access=ALL-UNNAMED" }) | ||
| public class AsyncBenchmark { | ||
| /** | ||
| * Parameter for the number of parallel tasks to launch. | ||
| */ | ||
| @Param({"100", "500", "1000"}) | ||
| public int taskCount; | ||
|
|
||
| @Setup(Level.Trial) | ||
| public void beforeAll() {} | ||
|
|
||
| @TearDown(Level.Trial) | ||
| public void afterAll() {} | ||
|
|
||
| @Benchmark | ||
| public void asyncSum(Blackhole bh) { | ||
| CompletableFuture<Void>[] futures = new CompletableFuture[taskCount]; | ||
|
|
||
| // Launch all tasks in parallel using supplyAsync on our custom executor | ||
| for (int i = 0; i < taskCount; i++) { | ||
| futures[i] = MySwiftLibrary.asyncSum(10, 5).thenAccept(bh::consume); | ||
| } | ||
|
|
||
| // Wait for all futures to complete. | ||
| CompletableFuture.allOf(futures).join(); | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.