Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ jobs:
tests:
name: Test
uses: swiftlang/github-workflows/.github/workflows/swift_package_test.yml@main
with:
enable_wasm_sdk_build: true
wasm_sdk_build_command: swift build -Xcc -D_WASI_EMULATED_PTHREAD

soundness:
name: Soundness
uses: swiftlang/github-workflows/.github/workflows/soundness.yml@main
Expand Down
12 changes: 7 additions & 5 deletions Sources/AsyncAlgorithms/Locking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ import Musl
import WinSDK
#elseif canImport(Bionic)
import Bionic
#elseif canImport(wasi_pthread)
import wasi_pthread
#else
#error("Unsupported platform")
#endif

internal struct Lock {
#if canImport(Darwin)
typealias Primitive = os_unfair_lock
#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic)
#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic) || canImport(wasi_pthread)
typealias Primitive = pthread_mutex_t
#elseif canImport(WinSDK)
typealias Primitive = SRWLOCK
Expand All @@ -44,7 +46,7 @@ internal struct Lock {
fileprivate static func initialize(_ platformLock: PlatformLock) {
#if canImport(Darwin)
platformLock.initialize(to: os_unfair_lock())
#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic)
#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic) || canImport(wasi_pthread)
let result = pthread_mutex_init(platformLock, nil)
precondition(result == 0, "pthread_mutex_init failed")
#elseif canImport(WinSDK)
Expand All @@ -55,7 +57,7 @@ internal struct Lock {
}

fileprivate static func deinitialize(_ platformLock: PlatformLock) {
#if canImport(Glibc) || canImport(Musl) || canImport(Bionic)
#if canImport(Glibc) || canImport(Musl) || canImport(Bionic) || canImport(wasi_pthread)
let result = pthread_mutex_destroy(platformLock)
precondition(result == 0, "pthread_mutex_destroy failed")
#endif
Expand All @@ -65,7 +67,7 @@ internal struct Lock {
fileprivate static func lock(_ platformLock: PlatformLock) {
#if canImport(Darwin)
os_unfair_lock_lock(platformLock)
#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic)
#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic) || canImport(wasi_pthread)
pthread_mutex_lock(platformLock)
#elseif canImport(WinSDK)
AcquireSRWLockExclusive(platformLock)
Expand All @@ -77,7 +79,7 @@ internal struct Lock {
fileprivate static func unlock(_ platformLock: PlatformLock) {
#if canImport(Darwin)
os_unfair_lock_unlock(platformLock)
#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic)
#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic) || canImport(wasi_pthread)
let result = pthread_mutex_unlock(platformLock)
precondition(result == 0, "pthread_mutex_unlock failed")
#elseif canImport(WinSDK)
Expand Down
10 changes: 6 additions & 4 deletions Sources/AsyncSequenceValidation/TaskDriver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import Glibc
import Musl
#elseif canImport(Bionic)
import Bionic
#elseif canImport(wasi_pthread)
import wasi_pthread
#elseif canImport(WinSDK)
#error("TODO: Port TaskDriver threading to windows")
#else
Expand All @@ -31,7 +33,7 @@ func start_thread(_ raw: UnsafeMutableRawPointer) -> UnsafeMutableRawPointer? {
Unmanaged<TaskDriver>.fromOpaque(raw).takeRetainedValue().run()
return nil
}
#elseif (canImport(Glibc) && !os(Android)) || canImport(Musl)
#elseif (canImport(Glibc) && !os(Android)) || canImport(Musl) || canImport(wasi_pthread)
@available(AsyncAlgorithms 1.0, *)
func start_thread(_ raw: UnsafeMutableRawPointer?) -> UnsafeMutableRawPointer? {
Unmanaged<TaskDriver>.fromOpaque(raw!).takeRetainedValue().run()
Expand All @@ -51,7 +53,7 @@ func start_thread(_ raw: UnsafeMutableRawPointer) -> UnsafeMutableRawPointer {
final class TaskDriver {
let work: (TaskDriver) -> Void
let queue: WorkQueue
#if canImport(Darwin)
#if canImport(Darwin) || canImport(wasi_pthread)
var thread: pthread_t?
#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic)
var thread = pthread_t()
Expand All @@ -65,7 +67,7 @@ final class TaskDriver {
}

func start() {
#if canImport(Darwin) || canImport(Glibc) || canImport(Musl) || canImport(Bionic)
#if canImport(Darwin) || canImport(Glibc) || canImport(Musl) || canImport(Bionic) || canImport(wasi_pthread)
pthread_create(
&thread,
nil,
Expand All @@ -85,7 +87,7 @@ final class TaskDriver {
}

func join() {
#if canImport(Darwin)
#if canImport(Darwin) || canImport(wasi_pthread)
pthread_join(thread!, nil)
#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic)
pthread_join(thread, nil)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,14 @@ SWIFT_EXPORT_FROM(swift_Concurrency)
SWIFT_CC(swift) void (* _Nullable swift_task_enqueueGlobal_hook)(
JobRef _Nonnull job, swift_task_enqueueGlobal_original _Nonnull original);

// Workaround for `macro 'pthread_join' unavailable: function like macros not supported`
#if defined(__wasi__)

#include <pthread.h>

int swift_async_sequencey_pthread_join(pthread_t thread, void **value_ptr) {
return pthread_join(thread, value_ptr);
}

#endif

Loading