diff --git a/stdlib/public/Concurrency/CMakeLists.txt b/stdlib/public/Concurrency/CMakeLists.txt index ce0d6488fbd..d9699ce50ba 100644 --- a/stdlib/public/Concurrency/CMakeLists.txt +++ b/stdlib/public/Concurrency/CMakeLists.txt @@ -93,9 +93,12 @@ set(SWIFT_RUNTIME_CONCURRENCY_C_SOURCES linker-support/magic-symbols-for-install-name.c ) -set(SWIFT_RUNTIME_CONCURRENCY_EXECUTOR_SOURCES - DispatchGlobalExecutor.cpp +set(SWIFT_RUNTIME_CONCURRENCY_EXECUTOR_SOURCES) +if("${SWIFT_CONCURRENCY_GLOBAL_EXECUTOR}" STREQUAL "dispatch") + set(SWIFT_RUNTIME_CONCURRENCY_EXECUTOR_SOURCES + DispatchGlobalExecutor.cpp ) +endif() set(LLVM_OPTIONAL_SOURCES CooperativeGlobalExecutor.cpp @@ -168,9 +171,11 @@ set(SWIFT_RUNTIME_CONCURRENCY_SWIFT_SOURCES TaskSleepDuration.swift DispatchExecutor.swift CFExecutor.swift + DummyExecutor.swift PlatformExecutorDarwin.swift PlatformExecutorLinux.swift PlatformExecutorWindows.swift + PlatformExecutorWASI.swift ) set(SWIFT_RUNTIME_CONCURRENCY_NONEMBEDDED_SWIFT_SOURCES diff --git a/stdlib/public/Concurrency/DispatchExecutor.swift b/stdlib/public/Concurrency/DispatchExecutor.swift index 405c0afbcd3..3a5abbdf73c 100644 --- a/stdlib/public/Concurrency/DispatchExecutor.swift +++ b/stdlib/public/Concurrency/DispatchExecutor.swift @@ -10,7 +10,7 @@ // //===----------------------------------------------------------------------===// -#if !$Embedded +#if !$Embedded && !os(WASI) import Swift @@ -200,4 +200,4 @@ extension DispatchGlobalTaskExecutor: DispatchExecutorProtocol { extension DispatchMainExecutor: DispatchExecutorProtocol { } -#endif // !$Embedded +#endif // !$Embedded && !os(WASI) diff --git a/stdlib/public/Concurrency/DummyExecutor.swift b/stdlib/public/Concurrency/DummyExecutor.swift new file mode 100644 index 00000000000..628b035ab22 --- /dev/null +++ b/stdlib/public/Concurrency/DummyExecutor.swift @@ -0,0 +1,51 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2020 - 2025 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors +// +//===----------------------------------------------------------------------===// + +import Swift + +// .. Main Executor ............................................................ + +@available(SwiftStdlib 6.2, *) +public class DummyMainExecutor: MainExecutor, @unchecked Sendable { + public init() {} + + public func run() throws { + fatalError("There is no executor implementation active") + } + + public func stop() { + fatalError("There is no executor implementation active") + } + + public func enqueue(_ job: consuming ExecutorJob) { + fatalError("There is no executor implementation active") + } + + public var isMainExecutor: Bool { true } + + public func checkIsolated() { + // Do nothing + } +} + +// .. Task Executor ............................................................ + +@available(SwiftStdlib 6.2, *) +public class DummyTaskExecutor: TaskExecutor, @unchecked Sendable { + public init() {} + + public func enqueue(_ job: consuming ExecutorJob) { + fatalError("There is no executor implementation active") + } + + public var isMainExecutor: Bool { false } +} diff --git a/stdlib/public/Concurrency/ExecutorBridge.cpp b/stdlib/public/Concurrency/ExecutorBridge.cpp index 289ef126591..9faf70b136a 100644 --- a/stdlib/public/Concurrency/ExecutorBridge.cpp +++ b/stdlib/public/Concurrency/ExecutorBridge.cpp @@ -10,7 +10,7 @@ // //===----------------------------------------------------------------------===// -#if !SWIFT_CONCURRENCY_EMBEDDED +#if SWIFT_CONCURRENCY_USES_DISPATCH #include #endif @@ -119,38 +119,7 @@ void *swift_job_getExecutorPrivateData(Job *job) { return &job->SchedulerPrivate[0]; } -#if !SWIFT_CONCURRENCY_EMBEDDED -extern "C" SWIFT_CC(swift) -void *swift_createDispatchEventC(void (*handler)(void *), void *context) { - dispatch_source_t source = dispatch_source_create(DISPATCH_SOURCE_TYPE_DATA_OR, - 0, 0, - dispatch_get_main_queue()); - dispatch_source_set_event_handler_f(source, handler); - dispatch_activate(source); - - return source; -} - -extern "C" SWIFT_CC(swift) -void swift_destroyDispatchEventC(void *event) { - dispatch_source_t source = (dispatch_source_t)event; - - dispatch_source_cancel(source); - dispatch_release(source); -} - -extern "C" SWIFT_CC(swift) -void *swift_getDispatchEventContext(void *event) { - return dispatch_get_context((dispatch_source_t)event); -} - -extern "C" SWIFT_CC(swift) -void swift_signalDispatchEvent(void *event) { - dispatch_source_t source = (dispatch_source_t)event; - - dispatch_source_merge_data(source, 1); -} - +#if SWIFT_CONCURRENCY_USES_DISPATCH extern "C" SWIFT_CC(swift) __attribute__((noreturn)) void swift_dispatchMain() { dispatch_main(); @@ -160,7 +129,6 @@ extern "C" SWIFT_CC(swift) void swift_dispatchAssertMainQueue() { dispatch_assert_queue(dispatch_get_main_queue()); } - -#endif // !SWIFT_CONCURRENCY_EMBEDDED +#endif // SWIFT_CONCURRENCY_ENABLE_DISPATCH #pragma clang diagnostic pop diff --git a/stdlib/public/Concurrency/ExecutorBridge.swift b/stdlib/public/Concurrency/ExecutorBridge.swift index 95a0cdbcecb..b806ff307d3 100644 --- a/stdlib/public/Concurrency/ExecutorBridge.swift +++ b/stdlib/public/Concurrency/ExecutorBridge.swift @@ -112,49 +112,3 @@ internal func _dispatchEnqueueWithDeadline(_ global: CBool, @available(SwiftStdlib 6.2, *) @_silgen_name("swift_dispatchAssertMainQueue") internal func _dispatchAssertMainQueue() - -@available(SwiftStdlib 6.2, *) -@_silgen_name("swift_createDispatchEventC") -internal func _createDispatchEventC( - handler: @convention(c) @escaping (UnsafeMutableRawPointer) -> (), - context: UnsafeMutableRawPointer -) -> OpaquePointer - -fileprivate class DispatchEventHandlerBox { - var handler: @Sendable () -> () - init(handler: @escaping @Sendable () -> ()) { - self.handler = handler - } -} - -@available(SwiftStdlib 6.2, *) -internal func _createDispatchEvent(handler: @escaping @Sendable () -> ()) -> OpaquePointer { - let boxed = DispatchEventHandlerBox(handler: handler) - let opaqueHandlerBox = unsafe Unmanaged.passRetained(boxed).toOpaque() - return unsafe _createDispatchEventC( - handler: { context in - let unmanaged = unsafe Unmanaged.fromOpaque(context) - unsafe unmanaged.takeUnretainedValue().handler() - }, - context: opaqueHandlerBox - ) -} - -@available(SwiftStdlib 6.2, *) -@_silgen_name("swift_destroyDispatchEventC") -internal func _destroyDispatchEventC(_ event: OpaquePointer) - -@available(SwiftStdlib 6.2, *) -@_silgen_name("swift_getDispatchEventContext") -internal func _getDispatchEventContext(_ event: OpaquePointer) -> UnsafeMutableRawPointer - -@available(SwiftStdlib 6.2, *) -internal func _destroyDispatchEvent(_ event: OpaquePointer) { - let context = unsafe _getDispatchEventContext(event) - unsafe Unmanaged.fromOpaque(context).release() - unsafe _destroyDispatchEventC(event) -} - -@available(SwiftStdlib 6.2, *) -@_silgen_name("swift_signalDispatchEvent") -internal func _signalDispatchEvent(_ event: OpaquePointer) diff --git a/stdlib/public/Concurrency/PlatformExecutorWASI.swift b/stdlib/public/Concurrency/PlatformExecutorWASI.swift new file mode 100644 index 00000000000..93f40262a4c --- /dev/null +++ b/stdlib/public/Concurrency/PlatformExecutorWASI.swift @@ -0,0 +1,25 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2020 - 2025 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors +// +//===----------------------------------------------------------------------===// + +#if os(WASI) + +import Swift + +// The default executors for now are Dispatch-based +@available(SwiftStdlib 6.2, *) +public struct PlatformExecutorFactory: ExecutorFactory { + public static let mainExecutor: any MainExecutor = DummyMainExecutor() + public static let defaultExecutor: any TaskExecutor + = DummyTaskExecutor() +} + +#endif // os(WASI)