Skip to content

Commit 775bff0

Browse files
authored
Merge pull request #37965 from rjmccall/dispatch-main-q
Use &_dispatch_main_q as the identity of the main actor.
2 parents 528764c + ca62a79 commit 775bff0

File tree

14 files changed

+129
-31
lines changed

14 files changed

+129
-31
lines changed

include/swift/ABI/Executor.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,15 @@ class ExecutorRef {
8282
return ExecutorRef(actor, 0);
8383
}
8484

85+
/// Given a pointer to a serial executor and its SerialExecutor
86+
/// conformance, return an executor reference for it.
87+
static ExecutorRef forOrdinary(HeapObject *identity,
88+
const SerialExecutorWitnessTable *witnessTable) {
89+
assert(identity);
90+
assert(witnessTable);
91+
return ExecutorRef(identity, reinterpret_cast<uintptr_t>(witnessTable));
92+
}
93+
8594
HeapObject *getIdentity() const {
8695
return Identity;
8796
}
@@ -112,6 +121,9 @@ class ExecutorRef {
112121
return Identity != newExecutor.Identity;
113122
}
114123

124+
/// Is this executor the main executor?
125+
bool isMainExecutor() const;
126+
115127
bool operator==(ExecutorRef other) const {
116128
return Identity == other.Identity;
117129
}

include/swift/Basic/Compiler.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,29 @@
137137
#define SWIFT_POINTER_IS_4_BYTES 1
138138
#endif
139139

140+
// Produce a string literal for the raw argument tokens.
141+
#define SWIFT_STRINGIZE_RAW(TOK) #TOK
142+
143+
// Produce a string literal for the macro-expanded argument tokens.
144+
#define SWIFT_STRINGIZE_EXPANDED(TOK) SWIFT_STRINGIZE_RAW(TOK)
145+
146+
#if defined(__USER_LABEL_PREFIX__)
147+
#define SWIFT_SYMBOL_PREFIX_STRING \
148+
SWIFT_STRINGIZE_EXPANDED(__USER_LABEL_PREFIX__)
149+
#else
150+
// Clang and GCC always define __USER_LABEL_PREFIX__, so this should
151+
// only come up with MSVC, and Windows doesn't use a prefix.
152+
#define SWIFT_SYMBOL_PREFIX_STRING ""
153+
#endif
154+
155+
// An attribute to override the symbol name of a declaration.
156+
// This does not compensate for platform symbol prefixes; for that,
157+
// use SWIFT_ASM_LABEL_WITH_PREFIX.
158+
//
159+
// This only actually works on Clang or GCC; MSVC does not provide
160+
// an attribute to change the asm label.
161+
#define SWIFT_ASM_LABEL_RAW(STRING) __asm__(STRING)
162+
#define SWIFT_ASM_LABEL_WITH_PREFIX(STRING) \
163+
SWIFT_ASM_LABEL_RAW(SWIFT_SYMBOL_PREFIX_STRING STRING)
164+
140165
#endif // SWIFT_BASIC_COMPILER_H

include/swift/Basic/Features.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,6 @@ LANGUAGE_FEATURE(BuiltinTaskGroup, 0, "TaskGroup builtins", true)
5151
LANGUAGE_FEATURE(InheritActorContext, 0, "@_inheritActorContext attribute", true)
5252
LANGUAGE_FEATURE(ImplicitSelfCapture, 0, "@_implicitSelfCapture attribute", true)
5353
LANGUAGE_FEATURE(BuiltinBuildExecutor, 0, "Executor-building builtins", true)
54+
LANGUAGE_FEATURE(BuiltinBuildMainExecutor, 0, "MainActor executor building builtin", true)
5455

5556
#undef LANGUAGE_FEATURE

include/swift/Runtime/Concurrency.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,10 @@ void swift_task_enqueueGlobalWithDelay(unsigned long long delay, Job *job);
563563
SWIFT_EXPORT_FROM(swift_Concurrency) SWIFT_CC(swift)
564564
void swift_task_enqueueMainExecutor(Job *job);
565565

566+
/// Enqueue the given job on the main executor.
567+
SWIFT_EXPORT_FROM(swift_Concurrency) SWIFT_CC(swift)
568+
void swift_task_enqueueOnDispatchQueue(Job *job, HeapObject *queue);
569+
566570
/// A hook to take over global enqueuing.
567571
typedef SWIFT_CC(swift) void (*swift_task_enqueueGlobal_original)(Job *job);
568572
SWIFT_EXPORT_FROM(swift_Concurrency)
@@ -680,6 +684,10 @@ AsyncTask *swift_task_getCurrent(void);
680684
SWIFT_EXPORT_FROM(swift_Concurrency) SWIFT_CC(swift)
681685
ExecutorRef swift_task_getCurrentExecutor(void);
682686

687+
/// Return the main-actor executor reference.
688+
SWIFT_EXPORT_FROM(swift_Concurrency) SWIFT_CC(swift)
689+
ExecutorRef swift_task_getMainExecutor(void);
690+
683691
SWIFT_EXPORT_FROM(swift_Concurrency) SWIFT_CC(swift)
684692
bool swift_task_isCurrentExecutor(ExecutorRef executor);
685693

include/swift/Runtime/RuntimeFunctions.def

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1666,6 +1666,14 @@ FUNCTION(TaskGetCurrentExecutor,
16661666
ARGS(),
16671667
ATTRS(NoUnwind, ArgMemOnly))
16681668

1669+
// ExecutorRef swift_task_getMainExecutor();
1670+
FUNCTION(TaskGetMainExecutor,
1671+
swift_task_getMainExecutor, SwiftCC,
1672+
ConcurrencyAvailability,
1673+
RETURNS(SwiftExecutorTy),
1674+
ARGS(),
1675+
ATTRS(NoUnwind, ArgMemOnly))
1676+
16691677
// void swift_defaultActor_initialize(DefaultActor *actor);
16701678
FUNCTION(DefaultActorInitialize,
16711679
swift_defaultActor_initialize, SwiftCC,

lib/AST/ASTPrinter.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2768,6 +2768,10 @@ static bool usesFeatureBuiltinBuildExecutor(Decl *decl) {
27682768
return false;
27692769
}
27702770

2771+
static bool usesFeatureBuiltinBuildMainExecutor(Decl *decl) {
2772+
return false;
2773+
}
2774+
27712775
static bool usesFeatureBuiltinContinuation(Decl *decl) {
27722776
return false;
27732777
}

lib/IRGen/GenConcurrency.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,12 @@ const LoadableTypeInfo &TypeConverter::getExecutorTypeInfo() {
127127

128128
void irgen::emitBuildMainActorExecutorRef(IRGenFunction &IGF,
129129
Explosion &out) {
130-
// FIXME
130+
auto call = IGF.Builder.CreateCall(IGF.IGM.getTaskGetMainExecutorFn(),
131+
{});
132+
call->setDoesNotThrow();
133+
call->setCallingConv(IGF.IGM.SwiftCC);
134+
135+
IGF.emitAllExtractValues(call, IGF.IGM.SwiftExecutorTy, out);
131136
}
132137

133138
void irgen::emitBuildDefaultActorExecutorRef(IRGenFunction &IGF,

stdlib/public/Concurrency/Actor.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,7 @@ static bool swift_task_isCurrentExecutorImpl(ExecutorRef executor) {
321321
return currentTracking->getActiveExecutor() == executor;
322322
}
323323

324-
return executor == _swift_task_getMainExecutor()
325-
&& isExecutingOnMainThread();
324+
return executor.isMainExecutor() && isExecutingOnMainThread();
326325
}
327326

328327
/// Logging level for unexpected executors:
@@ -365,7 +364,7 @@ void swift::swift_task_reportUnexpectedExecutor(
365364

366365
const char *functionIsolation;
367366
const char *whereExpected;
368-
if (executor == _swift_task_getMainExecutor()) {
367+
if (executor.isMainExecutor()) {
369368
functionIsolation = "@MainActor function";
370369
whereExpected = "the main thread";
371370
} else {

stdlib/public/Concurrency/Actor.swift

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,3 @@ public func _defaultActorDestroy(_ actor: AnyObject)
5151
@_silgen_name("swift_task_enqueueMainExecutor")
5252
@usableFromInline
5353
internal func _enqueueOnMain(_ job: UnownedJob)
54-
55-
// Used by the concurrency runtime
56-
@available(SwiftStdlib 5.5, *)
57-
extension SerialExecutor {
58-
@_silgen_name("_swift_task_getMainExecutor")
59-
internal func _getMainExecutor() -> UnownedSerialExecutor {
60-
return MainActor.shared.unownedExecutor
61-
}
62-
}

stdlib/public/Concurrency/Executor.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,27 @@ func _checkExpectedExecutor(_filenameStart: Builtin.RawPointer,
9292
_reportUnexpectedExecutor(
9393
_filenameStart, _filenameLength, _filenameIsASCII, _line, _executor)
9494
}
95+
96+
@available(SwiftStdlib 5.5, *)
97+
@_silgen_name("swift_task_enqueueOnDispatchQueue")
98+
internal func _enqueueOnDispatchQueue(_ job: UnownedJob, queue: AnyObject)
99+
100+
/// Used by the runtime solely for the witness table it produces.
101+
/// FIXME: figure out some way to achieve that which doesn't generate
102+
/// all the other metadata
103+
///
104+
/// Expected to work for any primitive dispatch queue; note that this
105+
/// means a dispatch_queue_t, which is not the same as DispatchQueue
106+
/// on platforms where that is an instance of a wrapper class.
107+
@available(SwiftStdlib 5.5, *)
108+
internal class DispatchQueueShim: UnsafeSendable, SerialExecutor {
109+
@inlinable
110+
func enqueue(_ job: UnownedJob) {
111+
_enqueueOnDispatchQueue(job, queue: self)
112+
}
113+
114+
@inlinable
115+
func asUnownedSerialExecutor() -> UnownedSerialExecutor {
116+
return UnownedSerialExecutor(ordinary: self)
117+
}
118+
}

0 commit comments

Comments
 (0)