19
19
20
20
#include < inttypes.h>
21
21
#include " swift/ABI/HeapObject.h"
22
+ #include " swift/Runtime/Casting.h"
22
23
23
24
namespace swift {
24
25
class AsyncContext ;
25
26
class AsyncTask ;
26
27
class DefaultActor ;
27
28
class Job ;
28
29
30
+ // / FIXME: only exists for the quick-and-dirty MainActor implementation.
31
+ SWIFT_EXPORT_FROM (swift_Concurrency)
32
+ Metadata* MainActorMetadata;
33
+
29
34
// / An ExecutorRef isn't necessarily just a pointer to an executor
30
35
// / object; it may have other bits set.
31
36
class ExecutorRef {
@@ -45,6 +50,13 @@ class ExecutorRef {
45
50
return ExecutorRef (0 );
46
51
}
47
52
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
+
48
60
// / Given a pointer to a default actor, return an executor reference
49
61
// / for it.
50
62
static ExecutorRef forDefaultActor (DefaultActor *actor) {
@@ -57,6 +69,20 @@ class ExecutorRef {
57
69
return Value == 0 ;
58
70
}
59
71
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
+
60
86
// / Is this a default-actor executor reference?
61
87
bool isDefaultActor () const {
62
88
return Value & IsDefaultActor;
@@ -77,10 +103,12 @@ class ExecutorRef {
77
103
}
78
104
79
105
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 ());
81
109
}
82
110
bool operator !=(ExecutorRef other) const {
83
- return Value != other. Value ;
111
+ return !(* this == other) ;
84
112
}
85
113
};
86
114
0 commit comments