Skip to content

[Synchronization] Fix wasm atomic intrinsic declarations #74171

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
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
14 changes: 9 additions & 5 deletions stdlib/public/Synchronization/Mutex/WasmImpl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,34 @@
// Note: All atomic accesses on WASM are sequentially consistent regardless of
// what ordering we tell LLVM to use.

@_extern(c, "llvm.wasm32.memory.atomic.wait32")
@_extern(c, "llvm.wasm.memory.atomic.wait32")
internal func _swift_stdlib_wait(
on: UnsafePointer<UInt32>,
expected: UInt32,
timeout: Int64
) -> UInt32

@_extern(c, "llvm.wasm32.memory.atomic.notify")
internal func _swift_stdlib_wake(on: UnsafePointer<UInt32>, count: UInt32)
@_extern(c, "llvm.wasm.memory.atomic.notify")
internal func _swift_stdlib_wake(on: UnsafePointer<UInt32>, count: UInt32) -> UInt32

extension Atomic where Value == _MutexHandle.State {
internal borrowing func _wait(expected: _MutexHandle.State) {
_swift_stdlib_wait(
#if _runtime(_multithreaded)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should generally make Mutex unavailable when the runtime isn't multithreaded

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

although I guess it doesn't really matter because Mutex can always acquire in the single threaded context and needing to call wait or notify should ever occur so this is probably fine.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, it might be better to have a separate noop implementations for single threaded targets instead of just skipping wait/notify operations.

_ = _swift_stdlib_wait(
on: .init(_rawAddress),
expected: expected.rawValue,

// A timeout of < 0 means indefinitely.
timeout: -1
)
#endif
}

internal borrowing func _wake() {
#if _runtime(_multithreaded)
// Only wake up 1 thread
_swift_stdlib_wake(on: .init(_rawAddress), count: 1)
_ = _swift_stdlib_wake(on: .init(_rawAddress), count: 1)
#endif
}
}

Expand Down