1919
2020#include < inttypes.h>
2121#include " swift/ABI/HeapObject.h"
22+ #include " swift/Runtime/Casting.h"
2223
2324namespace swift {
2425class AsyncContext ;
2526class AsyncTask ;
2627class DefaultActor ;
2728class Job ;
2829
30+ // / FIXME: only exists for the quick-and-dirty MainActor implementation.
31+ SWIFT_EXPORT_FROM (swift_Concurrency)
32+ Metadata* MainActorMetadata;
33+
2934// / An ExecutorRef isn't necessarily just a pointer to an executor
3035// / object; it may have other bits set.
3136class ExecutorRef {
@@ -45,6 +50,13 @@ class ExecutorRef {
4550 return ExecutorRef (0 );
4651 }
4752
53+ // / FIXME: only exists for the quick-and-dirty MainActor implementation.
54+ // / NOTE: I didn't go with Executor::forMainActor(DefaultActor*) because
55+ // / __swift_run_job_main_executor can't take more than one argument.
56+ constexpr static ExecutorRef mainExecutor () {
57+ return ExecutorRef (2 );
58+ }
59+
4860 // / Given a pointer to a default actor, return an executor reference
4961 // / for it.
5062 static ExecutorRef forDefaultActor (DefaultActor *actor) {
@@ -57,6 +69,20 @@ class ExecutorRef {
5769 return Value == 0 ;
5870 }
5971
72+ // / FIXME: only exists for the quick-and-dirty MainActor implementation.
73+ bool isMainExecutor () const {
74+ if (Value == ExecutorRef::mainExecutor ().Value )
75+ return true ;
76+
77+ HeapObject *heapObj = reinterpret_cast <HeapObject*>(Value & ~PointerMask);
78+
79+ if (heapObj == nullptr || MainActorMetadata == nullptr )
80+ return false ;
81+
82+ Metadata const * metadata = swift_getObjectType (heapObj);
83+ return metadata == MainActorMetadata;
84+ }
85+
6086 // / Is this a default-actor executor reference?
6187 bool isDefaultActor () const {
6288 return Value & IsDefaultActor;
@@ -77,10 +103,12 @@ class ExecutorRef {
77103 }
78104
79105 bool operator ==(ExecutorRef other) const {
80- return Value == other.Value ;
106+ return Value == other.Value
107+ // / FIXME: only exists for the quick-and-dirty MainActor implementation.
108+ || (isMainExecutor () && other.isMainExecutor ());
81109 }
82110 bool operator !=(ExecutorRef other) const {
83- return Value != other. Value ;
111+ return !(* this == other) ;
84112 }
85113};
86114
0 commit comments