Skip to content

Commit ed8ed32

Browse files
committed
Clean up Concurrency build system so that all the logic around choice of
which executor for which type of setting, is consolidated and we have a single knob we use to determine when to use dispatch as our global executor. Radar-Id: rdar://problem/119416196
1 parent b28aa2b commit ed8ed32

File tree

7 files changed

+38
-36
lines changed

7 files changed

+38
-36
lines changed

include/swift/Runtime/Concurrency.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,10 @@
3939
#endif
4040

4141
// Does the runtime integrate with libdispatch?
42-
#ifndef SWIFT_CONCURRENCY_ENABLE_DISPATCH
43-
#if SWIFT_CONCURRENCY_COOPERATIVE_GLOBAL_EXECUTOR || SWIFT_CONCURRENCY_TASK_TO_THREAD_MODEL
44-
#define SWIFT_CONCURRENCY_ENABLE_DISPATCH 0
42+
#if defined(SWIFT_CONCURRENCY_USES_DISPATCH)
43+
#define SWIFT_CONCURRENCY_ENABLE_DISPATCH SWIFT_CONCURRENCY_USES_DISPATCH
4544
#else
46-
#define SWIFT_CONCURRENCY_ENABLE_DISPATCH 1
47-
#endif
45+
#define SWIFT_CONCURRENCY_ENABLE_DISPATCH 0
4846
#endif
4947

5048
// Does the runtime provide priority escalation support?

stdlib/cmake/modules/AddSwiftStdlib.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,10 @@ function(_add_target_variant_c_compile_flags)
406406
list(APPEND result "-DSWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY")
407407
endif()
408408

409+
if (SWIFT_CONCURRENCY_USES_DISPATCH)
410+
list(APPEND result "-DSWIFT_CONCURRENCY_USES_DISPATCH")
411+
endif()
412+
409413
string(TOUPPER "${SWIFT_SDK_${CFLAGS_SDK}_THREADING_PACKAGE}" _threading_package)
410414
list(APPEND result "-DSWIFT_THREADING_${_threading_package}")
411415

stdlib/cmake/modules/StdlibOptions.cmake

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ endif()
117117
option(SWIFT_STDLIB_ENABLE_PRESPECIALIZATION
118118
"Should stdlib be built with generic metadata prespecialization enabled. Defaults to On on Darwin and on Linux."
119119
"${SWIFT_STDLIB_ENABLE_PRESPECIALIZATION_default}")
120-
120+
121121
option(SWIFT_STDLIB_ENABLE_UNICODE_DATA
122122
"Should stdlib be built with full unicode support"
123123
TRUE)
@@ -198,12 +198,6 @@ option(SWIFT_ENABLE_REFLECTION
198198
set(SWIFT_STDLIB_REFLECTION_METADATA "enabled" CACHE STRING
199199
"Build stdlib with runtime metadata (valid options are 'enabled', 'disabled' and 'debugger-only').")
200200

201-
if(SWIFT_FREESTANDING_FLAVOR STREQUAL "apple" AND NOT SWIFT_FREESTANDING_IS_DARWIN)
202-
set(SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY_default TRUE)
203-
else()
204-
set(SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY_default FALSE)
205-
endif()
206-
207201
option(SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
208202
"Should concurrency use the task-to-thread model."
209203
"${SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY_default}")
@@ -220,13 +214,28 @@ option(SWIFT_STDLIB_SINGLE_THREADED_CONCURRENCY
220214
"Build the standard libraries assuming that they will be used in an environment with only a single thread."
221215
FALSE)
222216

223-
if(SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY)
224-
set(SWIFT_CONCURRENCY_GLOBAL_EXECUTOR_default "none")
225-
elseif(SWIFT_STDLIB_SINGLE_THREADED_CONCURRENCY)
226-
set(SWIFT_CONCURRENCY_GLOBAL_EXECUTOR_default "singlethreaded")
227-
else()
228-
set(SWIFT_CONCURRENCY_GLOBAL_EXECUTOR_default "dispatch")
229-
endif()
217+
# Use dispatch as the system scheduler by default.
218+
# For convenience, we set this to false when concurrency is disabled.
219+
set(SWIFT_CONCURRENCY_USES_DISPATCH FALSE)
220+
if (SWIFT_ENABLE_EXPERIMENTAL_CONCURRENCY)
221+
222+
if (SWIFT_ENABLE_DISPATCH)
223+
set(SWIFT_CONCURRENCY_USES_DISPATCH TRUE)
224+
if (NOT CMAKE_SYSTEM_NAME STREQUAL "Darwin" AND NOT EXISTS "${SWIFT_PATH_TO_LIBDISPATCH_SOURCE}")
225+
message(SEND_ERROR "Concurrency requires libdispatch on non-Darwin hosts. Please specify SWIFT_PATH_TO_LIBDISPATCH_SOURCE")
226+
endif()
227+
endif()
228+
229+
if(SWIFT_CONCURRENCY_USES_DISPATCH)
230+
set(SWIFT_CONCURRENCY_GLOBAL_EXECUTOR_default "dispatch")
231+
elseif(SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY)
232+
set(SWIFT_CONCURRENCY_GLOBAL_EXECUTOR_default "none")
233+
elseif(SWIFT_STDLIB_SINGLE_THREADED_CONCURRENCY)
234+
set(SWIFT_CONCURRENCY_GLOBAL_EXECUTOR_default "singlethreaded")
235+
else()
236+
set(SWIFT_CONCURRENCY_GLOBAL_EXECUTOR_default "hooked")
237+
endif()
238+
endif() # SWIFT_ENABLE_EXPERIMENTAL_CONCURRENCY
230239

231240
set(SWIFT_CONCURRENCY_GLOBAL_EXECUTOR
232241
"${SWIFT_CONCURRENCY_GLOBAL_EXECUTOR_default}" CACHE STRING
@@ -283,16 +292,3 @@ set(SWIFT_RUNTIME_FIXED_BACKTRACER_PATH "" CACHE STRING
283292
"If set, provides a fixed path to the swift-backtrace binary. This
284293
will disable dynamic determination of the path and will also disable
285294
the setting in SWIFT_BACKTRACE.")
286-
287-
# Use dispatch as the system scheduler by default.
288-
# For convenience, we set this to false when concurrency is disabled.
289-
set(SWIFT_CONCURRENCY_USES_DISPATCH FALSE)
290-
if(SWIFT_ENABLE_EXPERIMENTAL_CONCURRENCY AND "${SWIFT_CONCURRENCY_GLOBAL_EXECUTOR}" STREQUAL "dispatch")
291-
set(SWIFT_CONCURRENCY_USES_DISPATCH TRUE)
292-
endif()
293-
294-
if(SWIFT_CONCURRENCY_USES_DISPATCH AND NOT CMAKE_SYSTEM_NAME STREQUAL "Darwin")
295-
if(NOT EXISTS "${SWIFT_PATH_TO_LIBDISPATCH_SOURCE}")
296-
message(SEND_ERROR "Concurrency requires libdispatch on non-Darwin hosts. Please specify SWIFT_PATH_TO_LIBDISPATCH_SOURCE")
297-
endif()
298-
endif()

stdlib/cmake/modules/SwiftSource.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,10 @@ function(_add_target_variant_swift_compile_flags
358358
list(APPEND result "-D" "SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY")
359359
endif()
360360

361+
if (SWIFT_CONCURRENCY_USES_DISPATCH)
362+
list(APPEND result "-D" "SWIFT_CONCURRENCY_USES_DISPATCH")
363+
endif()
364+
361365
if(SWIFT_STDLIB_OVERRIDABLE_RETAIN_RELEASE)
362366
list(APPEND result "-D" "SWIFT_STDLIB_OVERRIDABLE_RETAIN_RELEASE")
363367
endif()

stdlib/public/Concurrency/Actor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ JobPriority swift::swift_task_getCurrentThreadPriority() {
294294
return JobPriority::UserInitiated;
295295
#elif SWIFT_CONCURRENCY_TASK_TO_THREAD_MODEL
296296
return JobPriority::Unspecified;
297-
#elif defined(__APPLE__)
297+
#elif defined(__APPLE__) && SWIFT_CONCURRENCY_ENABLE_DISPATCH
298298
return static_cast<JobPriority>(qos_class_self());
299299
#else
300300
if (isExecutingOnMainThread())

stdlib/public/Concurrency/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ if(SWIFT_SHOULD_BUILD_EMBEDDED_STDLIB AND SWIFT_SHOULD_BUILD_EMBEDDED_CONCURRENC
191191
set(SWIFT_STDLIB_SUPPORT_BACK_DEPLOYMENT OFF)
192192
set(SWIFT_STDLIB_STABLE_ABI OFF)
193193
set(SWIFT_STDLIB_ENABLE_OBJC_INTEROP OFF)
194+
set(SWIFT_CONCURRENCY_USES_DISPATCH FALSE)
194195
set(SWIFT_STDLIB_SINGLE_THREADED_CONCURRENCY TRUE)
195196
set(SWIFT_STDLIB_CONCURRENCY_TRACING FALSE)
196197

stdlib/public/Concurrency/Executor.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,6 @@ where E: SerialExecutor {
396396
#endif // !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
397397
}
398398

399-
// Used by the concurrency runtime
400399
@_unavailableInEmbedded
401400
@available(SwiftStdlib 9999, *)
402401
@_silgen_name("_swift_task_enqueueOnTaskExecutor")
@@ -412,7 +411,7 @@ internal func _enqueueOnTaskExecutor<E>(job unownedJob: UnownedJob, executor: E)
412411
#endif // !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
413412
}
414413

415-
#if !SWIFT_STDLIB_SINGLE_THREADED_CONCURRENCY && !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
414+
#if SWIFT_CONCURRENCY_USES_DISPATCH
416415
// This must take a DispatchQueueShim, not something like AnyObject,
417416
// or else SILGen will emit a retain/release in unoptimized builds,
418417
// which won't work because DispatchQueues aren't actually
@@ -439,4 +438,4 @@ internal final class DispatchQueueShim: @unchecked Sendable, SerialExecutor {
439438
return UnownedSerialExecutor(ordinary: self)
440439
}
441440
}
442-
#endif // SWIFT_STDLIB_SINGLE_THREADED_CONCURRENCY
441+
#endif // SWIFT_CONCURRENCY_USES_DISPATCH

0 commit comments

Comments
 (0)