Skip to content

Commit a4f79f3

Browse files
committed
[Concurrency][Linux] Fix WASI build.
This doesn't actually add proper WASI support - it just fixes the build so that we're no worse off than we were. rdar://141348916
1 parent 8caa5c5 commit a4f79f3

File tree

6 files changed

+88
-85
lines changed

6 files changed

+88
-85
lines changed

stdlib/public/Concurrency/CMakeLists.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,12 @@ set(SWIFT_RUNTIME_CONCURRENCY_C_SOURCES
9393
linker-support/magic-symbols-for-install-name.c
9494
)
9595

96-
set(SWIFT_RUNTIME_CONCURRENCY_EXECUTOR_SOURCES
97-
DispatchGlobalExecutor.cpp
96+
set(SWIFT_RUNTIME_CONCURRENCY_EXECUTOR_SOURCES)
97+
if("${SWIFT_CONCURRENCY_GLOBAL_EXECUTOR}" STREQUAL "dispatch")
98+
set(SWIFT_RUNTIME_CONCURRENCY_EXECUTOR_SOURCES
99+
DispatchGlobalExecutor.cpp
98100
)
101+
endif()
99102

100103
set(LLVM_OPTIONAL_SOURCES
101104
CooperativeGlobalExecutor.cpp
@@ -168,9 +171,11 @@ set(SWIFT_RUNTIME_CONCURRENCY_SWIFT_SOURCES
168171
TaskSleepDuration.swift
169172
DispatchExecutor.swift
170173
CFExecutor.swift
174+
DummyExecutor.swift
171175
PlatformExecutorDarwin.swift
172176
PlatformExecutorLinux.swift
173177
PlatformExecutorWindows.swift
178+
PlatformExecutorWASI.swift
174179
)
175180

176181
set(SWIFT_RUNTIME_CONCURRENCY_NONEMBEDDED_SWIFT_SOURCES

stdlib/public/Concurrency/DispatchExecutor.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
#if !$Embedded
13+
#if !$Embedded && !os(WASI)
1414

1515
import Swift
1616

@@ -200,4 +200,4 @@ extension DispatchGlobalTaskExecutor: DispatchExecutorProtocol {
200200
extension DispatchMainExecutor: DispatchExecutorProtocol {
201201
}
202202

203-
#endif // !$Embedded
203+
#endif // !$Embedded && !os(WASI)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2020 - 2025 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import Swift
14+
15+
// .. Main Executor ............................................................
16+
17+
@available(SwiftStdlib 6.2, *)
18+
public class DummyMainExecutor: MainExecutor, @unchecked Sendable {
19+
public init() {}
20+
21+
public func run() throws {
22+
fatalError("There is no executor implementation active")
23+
}
24+
25+
public func stop() {
26+
fatalError("There is no executor implementation active")
27+
}
28+
29+
public func enqueue(_ job: consuming ExecutorJob) {
30+
fatalError("There is no executor implementation active")
31+
}
32+
33+
public var isMainExecutor: Bool { true }
34+
35+
public func checkIsolated() {
36+
// Do nothing
37+
}
38+
}
39+
40+
// .. Task Executor ............................................................
41+
42+
@available(SwiftStdlib 6.2, *)
43+
public class DummyTaskExecutor: TaskExecutor, @unchecked Sendable {
44+
public init() {}
45+
46+
public func enqueue(_ job: consuming ExecutorJob) {
47+
fatalError("There is no executor implementation active")
48+
}
49+
50+
public var isMainExecutor: Bool { false }
51+
}

stdlib/public/Concurrency/ExecutorBridge.cpp

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
#if !SWIFT_CONCURRENCY_EMBEDDED
13+
#if SWIFT_CONCURRENCY_USES_DISPATCH
1414
#include <dispatch/dispatch.h>
1515
#endif
1616

@@ -119,38 +119,7 @@ void *swift_job_getExecutorPrivateData(Job *job) {
119119
return &job->SchedulerPrivate[0];
120120
}
121121

122-
#if !SWIFT_CONCURRENCY_EMBEDDED
123-
extern "C" SWIFT_CC(swift)
124-
void *swift_createDispatchEventC(void (*handler)(void *), void *context) {
125-
dispatch_source_t source = dispatch_source_create(DISPATCH_SOURCE_TYPE_DATA_OR,
126-
0, 0,
127-
dispatch_get_main_queue());
128-
dispatch_source_set_event_handler_f(source, handler);
129-
dispatch_activate(source);
130-
131-
return source;
132-
}
133-
134-
extern "C" SWIFT_CC(swift)
135-
void swift_destroyDispatchEventC(void *event) {
136-
dispatch_source_t source = (dispatch_source_t)event;
137-
138-
dispatch_source_cancel(source);
139-
dispatch_release(source);
140-
}
141-
142-
extern "C" SWIFT_CC(swift)
143-
void *swift_getDispatchEventContext(void *event) {
144-
return dispatch_get_context((dispatch_source_t)event);
145-
}
146-
147-
extern "C" SWIFT_CC(swift)
148-
void swift_signalDispatchEvent(void *event) {
149-
dispatch_source_t source = (dispatch_source_t)event;
150-
151-
dispatch_source_merge_data(source, 1);
152-
}
153-
122+
#if SWIFT_CONCURRENCY_USES_DISPATCH
154123
extern "C" SWIFT_CC(swift) __attribute__((noreturn))
155124
void swift_dispatchMain() {
156125
dispatch_main();
@@ -160,7 +129,6 @@ extern "C" SWIFT_CC(swift)
160129
void swift_dispatchAssertMainQueue() {
161130
dispatch_assert_queue(dispatch_get_main_queue());
162131
}
163-
164-
#endif // !SWIFT_CONCURRENCY_EMBEDDED
132+
#endif // SWIFT_CONCURRENCY_ENABLE_DISPATCH
165133

166134
#pragma clang diagnostic pop

stdlib/public/Concurrency/ExecutorBridge.swift

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -112,49 +112,3 @@ internal func _dispatchEnqueueWithDeadline(_ global: CBool,
112112
@available(SwiftStdlib 6.2, *)
113113
@_silgen_name("swift_dispatchAssertMainQueue")
114114
internal func _dispatchAssertMainQueue()
115-
116-
@available(SwiftStdlib 6.2, *)
117-
@_silgen_name("swift_createDispatchEventC")
118-
internal func _createDispatchEventC(
119-
handler: @convention(c) @escaping (UnsafeMutableRawPointer) -> (),
120-
context: UnsafeMutableRawPointer
121-
) -> OpaquePointer
122-
123-
fileprivate class DispatchEventHandlerBox {
124-
var handler: @Sendable () -> ()
125-
init(handler: @escaping @Sendable () -> ()) {
126-
self.handler = handler
127-
}
128-
}
129-
130-
@available(SwiftStdlib 6.2, *)
131-
internal func _createDispatchEvent(handler: @escaping @Sendable () -> ()) -> OpaquePointer {
132-
let boxed = DispatchEventHandlerBox(handler: handler)
133-
let opaqueHandlerBox = unsafe Unmanaged.passRetained(boxed).toOpaque()
134-
return unsafe _createDispatchEventC(
135-
handler: { context in
136-
let unmanaged = unsafe Unmanaged<DispatchEventHandlerBox>.fromOpaque(context)
137-
unsafe unmanaged.takeUnretainedValue().handler()
138-
},
139-
context: opaqueHandlerBox
140-
)
141-
}
142-
143-
@available(SwiftStdlib 6.2, *)
144-
@_silgen_name("swift_destroyDispatchEventC")
145-
internal func _destroyDispatchEventC(_ event: OpaquePointer)
146-
147-
@available(SwiftStdlib 6.2, *)
148-
@_silgen_name("swift_getDispatchEventContext")
149-
internal func _getDispatchEventContext(_ event: OpaquePointer) -> UnsafeMutableRawPointer
150-
151-
@available(SwiftStdlib 6.2, *)
152-
internal func _destroyDispatchEvent(_ event: OpaquePointer) {
153-
let context = unsafe _getDispatchEventContext(event)
154-
unsafe Unmanaged<DispatchEventHandlerBox>.fromOpaque(context).release()
155-
unsafe _destroyDispatchEventC(event)
156-
}
157-
158-
@available(SwiftStdlib 6.2, *)
159-
@_silgen_name("swift_signalDispatchEvent")
160-
internal func _signalDispatchEvent(_ event: OpaquePointer)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2020 - 2025 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#if os(WASI)
14+
15+
import Swift
16+
17+
// The default executors for now are Dispatch-based
18+
@available(SwiftStdlib 6.2, *)
19+
public struct PlatformExecutorFactory: ExecutorFactory {
20+
public static let mainExecutor: any MainExecutor = DummyMainExecutor()
21+
public static let defaultExecutor: any TaskExecutor
22+
= DummyTaskExecutor()
23+
}
24+
25+
#endif // os(WASI)

0 commit comments

Comments
 (0)