Skip to content

Commit b03904d

Browse files
committed
[Freestanding] Use task-to-thread concurrency model.
Defined SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY to describe whether the standard library will use the task-to-thread model for concurrency. It is true only for freestanding non-Darwin stdlibs. When it is true, SWIFT_CONCURRENCY_TASK_TO_THREAD_MODEL is defined during stdlib compilation of both Swift and C++ sources. Added an option to LangOptions to specify which concurrency model is used, either standard or task-to-thread. When SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY is true, the model is specified to be task-to-thread.
1 parent abc2157 commit b03904d

File tree

7 files changed

+56
-0
lines changed

7 files changed

+56
-0
lines changed

include/swift/Basic/LangOptions.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ namespace swift {
9595
ErrorOnFailureRemarkOnSuccess
9696
};
9797

98+
enum class ConcurrencyModel : uint8_t {
99+
Standard,
100+
TaskToThread,
101+
};
102+
98103
/// A collection of options that affect the language dialect and
99104
/// provide compiler debugging facilities.
100105
class LangOptions final {
@@ -510,6 +515,13 @@ namespace swift {
510515
/// Enables dumping type witness systems from associated type inference.
511516
bool DumpTypeWitnessSystems = false;
512517

518+
/// The model of concurrency to be used.
519+
ConcurrencyModel ActiveConcurrencyModel = ConcurrencyModel::Standard;
520+
521+
bool isConcurrencyModelTaskToThread() const {
522+
return ActiveConcurrencyModel == ConcurrencyModel::TaskToThread;
523+
}
524+
513525
/// Sets the target we are building for and updates platform conditions
514526
/// to match.
515527
///

include/swift/Option/FrontendOptions.td

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,4 +1065,11 @@ def enable_ad_hoc_availability :
10651065
Flag<["-"], "enable-ad-hoc-availability">,
10661066
HelpText<"Enable experimental support for ad hoc availability">;
10671067

1068+
def concurrency_model :
1069+
Separate<["-"], "concurrency-model">,
1070+
HelpText<"Which concurrency model is used. Defaults to standard.">,
1071+
MetaVarName<"standard|task-to-thread">;
1072+
def concurrency_model_EQ :
1073+
Joined<["-"], "concurrency-model=">,
1074+
Alias<concurrency_model>;
10681075
} // end let Flags = [FrontendOption, NoDriverOption, HelpHidden]

include/swift/Runtime/Concurrency.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@
3232
#define SWIFT_CONCURRENCY_COOPERATIVE_GLOBAL_EXECUTOR 0
3333
#endif
3434

35+
// Does the runtime use a task-thread model?
36+
#if defined(SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY)
37+
#define SWIFT_CONCURRENCY_TASK_TO_THREAD_MODEL 1
38+
#else
39+
#define SWIFT_CONCURRENCY_TASK_TO_THREAD_MODEL 0
40+
#endif
41+
3542
// Does the runtime integrate with libdispatch?
3643
#ifndef SWIFT_CONCURRENCY_ENABLE_DISPATCH
3744
#if SWIFT_CONCURRENCY_COOPERATIVE_GLOBAL_EXECUTOR

lib/Frontend/CompilerInvocation.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,14 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
10181018

10191019
Opts.DumpTypeWitnessSystems = Args.hasArg(OPT_dump_type_witness_systems);
10201020

1021+
if (const Arg *A = Args.getLastArg(options::OPT_concurrency_model)) {
1022+
Opts.ActiveConcurrencyModel =
1023+
llvm::StringSwitch<ConcurrencyModel>(A->getValue())
1024+
.Case("standard", ConcurrencyModel::Standard)
1025+
.Case("task-to-thread", ConcurrencyModel::TaskToThread)
1026+
.Default(ConcurrencyModel::Standard);
1027+
}
1028+
10211029
return HadError || UnsupportedOS || UnsupportedArch;
10221030
}
10231031

stdlib/cmake/modules/AddSwiftStdlib.cmake

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,10 @@ function(_add_target_variant_c_compile_flags)
354354
list(APPEND result "-DSWIFT_STDLIB_SINGLE_THREADED_CONCURRENCY")
355355
endif()
356356

357+
if(SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY)
358+
list(APPEND result "-D" "SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY")
359+
endif()
360+
357361
string(TOUPPER "${SWIFT_SDK_${CFLAGS_SDK}_THREADING_PACKAGE}" _threading_package)
358362
list(APPEND result "-DSWIFT_THREADING_${_threading_package}")
359363

@@ -1773,6 +1777,10 @@ function(add_swift_target_library name)
17731777
list(APPEND SWIFTLIB_SWIFT_COMPILE_FLAGS "-Xfrontend;-prespecialize-generic-metadata")
17741778
endif()
17751779

1780+
if(SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY)
1781+
list(APPEND SWIFTLIB_SWIFT_COMPILE_FLAGS "-Xfrontend;-concurrency-model=task-to-thread")
1782+
endif()
1783+
17761784
# If we are building this library for targets, loop through the various
17771785
# SDKs building the variants of this library.
17781786
list_intersect(

stdlib/cmake/modules/StdlibOptions.cmake

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,16 @@ option(SWIFT_ENABLE_REFLECTION
172172
"Build stdlib with support for runtime reflection and mirrors."
173173
TRUE)
174174

175+
if(SWIFT_FREESTANDING_FLAVOR STREQUAL "apple" AND NOT SWIFT_FREESTANDING_IS_DARWIN)
176+
set(SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY_default TRUE)
177+
else()
178+
set(SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY_default FALSE)
179+
endif()
180+
181+
option(SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
182+
"Should concurrency use the task-to-thread model."
183+
"${SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY_default}")
184+
175185
option(SWIFT_STDLIB_HAS_STDIN
176186
"Build stdlib assuming the platform supports stdin and getline API."
177187
TRUE)

stdlib/cmake/modules/SwiftSource.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,10 @@ function(_add_target_variant_swift_compile_flags
326326
list(APPEND result "-D" "SWIFT_STDLIB_SINGLE_THREADED_CONCURRENCY")
327327
endif()
328328

329+
if(SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY)
330+
list(APPEND result "-D" "SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY")
331+
endif()
332+
329333
string(TOUPPER "${SWIFT_SDK_${sdk}_THREADING_PACKAGE}" _threading_package)
330334
list(APPEND result "-D" "SWIFT_THREADING_${_threading_package}")
331335

0 commit comments

Comments
 (0)