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

Concurrency: Fix build for p1-threads target #287

Merged
merged 2 commits into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ jobs:
toolchain:
download-url: https://download.swift.org/swift-6.0.2-release/ubuntu2204/swift-6.0.2-RELEASE/swift-6.0.2-RELEASE-ubuntu22.04.tar.gz
wasi-backend: Node
target: "wasm32-unknown-wasi"
- os: ubuntu-22.04
toolchain:
download-url: https://download.swift.org/development/ubuntu2204/swift-DEVELOPMENT-SNAPSHOT-2025-02-26-a/swift-DEVELOPMENT-SNAPSHOT-2025-02-26-a-ubuntu22.04.tar.gz
wasi-backend: Node
target: "wasm32-unknown-wasi"
- os: ubuntu-22.04
toolchain:
download-url: https://download.swift.org/development/ubuntu2204/swift-DEVELOPMENT-SNAPSHOT-2025-02-26-a/swift-DEVELOPMENT-SNAPSHOT-2025-02-26-a-ubuntu22.04.tar.gz
wasi-backend: Node
target: "wasm32-unknown-wasip1-threads"

runs-on: ${{ matrix.entry.os }}
env:
Expand All @@ -33,6 +36,8 @@ jobs:
download-url: ${{ matrix.entry.toolchain.download-url }}
- uses: swiftwasm/setup-swiftwasm@v2
id: setup-swiftwasm
with:
target: ${{ matrix.entry.target }}
- name: Configure Swift SDK
run: echo "SWIFT_SDK_ID=${{ steps.setup-swiftwasm.outputs.swift-sdk-id }}" >> $GITHUB_ENV
- run: make bootstrap
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ extern void *_Nullable swift_task_asyncMainDrainQueue_hook SWIFT_NONISOLATED_UNS

/// MARK: - thread local storage

extern _Thread_local void * _Nullable swjs_thread_local_event_loop;
extern _Thread_local void * _Nullable swjs_thread_local_event_loop SWIFT_NONISOLATED_UNSAFE;

extern _Thread_local void * _Nullable swjs_thread_local_task_executor_worker SWIFT_NONISOLATED_UNSAFE;

Expand Down
36 changes: 17 additions & 19 deletions Tests/JavaScriptEventLoopTests/WebWorkerTaskExecutorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import _CJavaScriptKit // For swjs_get_worker_thread_id
func isMainThread() -> Bool

final class WebWorkerTaskExecutorTests: XCTestCase {
override func setUp() {
WebWorkerTaskExecutor.installGlobalExecutor()
override func setUp() async {
await WebWorkerTaskExecutor.installGlobalExecutor()
}

func testTaskRunOnMainThread() async throws {
Expand Down Expand Up @@ -152,48 +152,46 @@ final class WebWorkerTaskExecutorTests: XCTestCase {

func testThreadLocalPerThreadValues() async throws {
struct Check {
@ThreadLocal(boxing: ())
static var value: Int?
static let value = ThreadLocal<Int>(boxing: ())
}
let executor = try await WebWorkerTaskExecutor(numberOfThreads: 1)
XCTAssertNil(Check.value)
Check.value = 42
XCTAssertEqual(Check.value, 42)
XCTAssertNil(Check.value.wrappedValue)
Check.value.wrappedValue = 42
XCTAssertEqual(Check.value.wrappedValue, 42)

let task = Task(executorPreference: executor) {
XCTAssertEqual(Check.value, nil)
Check.value = 100
XCTAssertEqual(Check.value, 100)
return Check.value
XCTAssertNil(Check.value.wrappedValue)
Check.value.wrappedValue = 100
XCTAssertEqual(Check.value.wrappedValue, 100)
return Check.value.wrappedValue
}
let result = await task.value
XCTAssertEqual(result, 100)
XCTAssertEqual(Check.value, 42)
XCTAssertEqual(Check.value.wrappedValue, 42)
executor.terminate()
}

func testLazyThreadLocalPerThreadInitialization() async throws {
struct Check {
static var valueToInitialize = 42
static var countOfInitialization = 0
@LazyThreadLocal(initialize: {
nonisolated(unsafe) static var valueToInitialize = 42
nonisolated(unsafe) static var countOfInitialization = 0
static let value = LazyThreadLocal<Int>(initialize: {
countOfInitialization += 1
return valueToInitialize
})
static var value: Int
}
let executor = try await WebWorkerTaskExecutor(numberOfThreads: 1)
XCTAssertEqual(Check.countOfInitialization, 0)
XCTAssertEqual(Check.value, 42)
XCTAssertEqual(Check.value.wrappedValue, 42)
XCTAssertEqual(Check.countOfInitialization, 1)

Check.valueToInitialize = 100

let task = Task(executorPreference: executor) {
XCTAssertEqual(Check.countOfInitialization, 1)
XCTAssertEqual(Check.value, 100)
XCTAssertEqual(Check.value.wrappedValue, 100)
XCTAssertEqual(Check.countOfInitialization, 2)
return Check.value
return Check.value.wrappedValue
}
let result = await task.value
XCTAssertEqual(result, 100)
Expand Down