1 // Copyright 2021 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "src/core/lib/promise/activity.h"
16
17 #include <grpc/support/port_platform.h>
18 #include <stddef.h>
19
20 #include <vector>
21
22 #include "absl/log/check.h"
23 #include "absl/strings/str_cat.h"
24 #include "absl/strings/str_format.h"
25 #include "absl/strings/str_join.h"
26 #include "src/core/util/atomic_utils.h"
27
28 namespace grpc_core {
29
30 ///////////////////////////////////////////////////////////////////////////////
31 // GLOBALS
32
33 #if !defined(_WIN32) || !defined(_DLL)
34 thread_local Activity* Activity::g_current_activity_{nullptr};
35 #endif
36
37 namespace promise_detail {
38
39 ///////////////////////////////////////////////////////////////////////////////
40 // HELPER TYPES
41
ActivityDebugTag(WakeupMask) const42 std::string Unwakeable::ActivityDebugTag(WakeupMask) const {
43 return "<unknown>";
44 }
45
46 // Weak handle to an Activity.
47 // Handle can persist while Activity goes away.
48 class FreestandingActivity::Handle final : public Wakeable {
49 public:
Handle(FreestandingActivity * activity)50 explicit Handle(FreestandingActivity* activity) : activity_(activity) {}
51
52 // Ref the Handle (not the activity).
Ref()53 void Ref() { refs_.fetch_add(1, std::memory_order_relaxed); }
54
55 // Activity is going away... drop its reference and sever the connection back.
DropActivity()56 void DropActivity() ABSL_LOCKS_EXCLUDED(mu_) {
57 mu_.Lock();
58 CHECK_NE(activity_, nullptr);
59 activity_ = nullptr;
60 mu_.Unlock();
61 Unref();
62 }
63
64 // Activity needs to wake up (if it still exists!) - wake it up, and drop the
65 // ref that was kept for this handle.
Wakeup(WakeupMask)66 void Wakeup(WakeupMask) override ABSL_LOCKS_EXCLUDED(mu_) {
67 mu_.Lock();
68 // Note that activity refcount can drop to zero, but we could win the lock
69 // against DropActivity, so we need to only increase activities refcount if
70 // it is non-zero.
71 if (activity_ && activity_->RefIfNonzero()) {
72 FreestandingActivity* activity = activity_;
73 mu_.Unlock();
74 // Activity still exists and we have a reference: wake it up, which will
75 // drop the ref.
76 activity->Wakeup(0);
77 } else {
78 // Could not get the activity - it's either gone or going. No need to wake
79 // it up!
80 mu_.Unlock();
81 }
82 // Drop the ref to the handle (we have one ref = one wakeup semantics).
83 Unref();
84 }
85
WakeupAsync(WakeupMask)86 void WakeupAsync(WakeupMask) override ABSL_LOCKS_EXCLUDED(mu_) {
87 mu_.Lock();
88 // Note that activity refcount can drop to zero, but we could win the lock
89 // against DropActivity, so we need to only increase activities refcount if
90 // it is non-zero.
91 if (activity_ && activity_->RefIfNonzero()) {
92 FreestandingActivity* activity = activity_;
93 mu_.Unlock();
94 // Activity still exists and we have a reference: wake it up, which will
95 // drop the ref.
96 activity->WakeupAsync(0);
97 } else {
98 // Could not get the activity - it's either gone or going. No need to wake
99 // it up!
100 mu_.Unlock();
101 }
102 // Drop the ref to the handle (we have one ref = one wakeup semantics).
103 Unref();
104 }
105
Drop(WakeupMask)106 void Drop(WakeupMask) override { Unref(); }
107
ActivityDebugTag(WakeupMask) const108 std::string ActivityDebugTag(WakeupMask) const override {
109 MutexLock lock(&mu_);
110 return activity_ == nullptr ? "<unknown>" : activity_->DebugTag();
111 }
112
113 private:
114 // Unref the Handle (not the activity).
Unref()115 void Unref() {
116 if (1 == refs_.fetch_sub(1, std::memory_order_acq_rel)) {
117 delete this;
118 }
119 }
120
121 // Two initial refs: one for the waiter that caused instantiation, one for the
122 // activity.
123 std::atomic<size_t> refs_{2};
124 mutable Mutex mu_ ABSL_ACQUIRED_AFTER(activity_->mu_);
125 FreestandingActivity* activity_ ABSL_GUARDED_BY(mu_);
126 };
127
128 ///////////////////////////////////////////////////////////////////////////////
129 // ACTIVITY IMPLEMENTATION
130
RefIfNonzero()131 bool FreestandingActivity::RefIfNonzero() { return IncrementIfNonzero(&refs_); }
132
RefHandle()133 FreestandingActivity::Handle* FreestandingActivity::RefHandle() {
134 if (handle_ == nullptr) {
135 // No handle created yet - construct it and return it.
136 handle_ = new Handle(this);
137 return handle_;
138 } else {
139 // Already had to create a handle, ref & return it.
140 handle_->Ref();
141 return handle_;
142 }
143 }
144
DropHandle()145 void FreestandingActivity::DropHandle() {
146 handle_->DropActivity();
147 handle_ = nullptr;
148 }
149
MakeNonOwningWaker()150 Waker FreestandingActivity::MakeNonOwningWaker() {
151 mu_.AssertHeld();
152 return Waker(RefHandle(), 0);
153 }
154
155 } // namespace promise_detail
156
DebugTag() const157 std::string Activity::DebugTag() const {
158 return absl::StrFormat("ACTIVITY[%p]", this);
159 }
160
161 ///////////////////////////////////////////////////////////////////////////////
162 // INTRA ACTIVITY WAKER IMPLEMENTATION
163
DebugString() const164 std::string IntraActivityWaiter::DebugString() const {
165 std::vector<int> bits;
166 for (size_t i = 0; i < 8 * sizeof(WakeupMask); i++) {
167 if (wakeups_ & (1 << i)) bits.push_back(i);
168 }
169 return absl::StrCat("{", absl::StrJoin(bits, ","), "}");
170 }
171
172 } // namespace grpc_core
173