| 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 <grpc/support/port_platform.h> |
| 16 |
|
| 17 |
#include "src/core/lib/promise/activity.h" |
| 18 |
|
| 19 |
#include <stddef.h> |
| 20 |
|
| 21 |
#include "absl/strings/str_format.h" |
| 22 |
|
| 23 |
#include "src/core/lib/gprpp/atomic_utils.h" |
| 24 |
|
| 25 |
namespace grpc_core { |
| 26 |
|
| 27 |
/////////////////////////////////////////////////////////////////////////////// |
| 28 |
// GLOBALS |
| 29 |
|
| 30 |
thread_local Activity* Activity::g_current_activity_{nullptr}; |
| 31 |
|
| 32 |
namespace promise_detail { |
| 33 |
|
| 34 |
/////////////////////////////////////////////////////////////////////////////// |
| 35 |
// HELPER TYPES |
| 36 |
|
| 37 |
std::string Unwakeable::ActivityDebugTag() const { return "<unknown>"; } |
| 38 |
|
| 39 |
// Weak handle to an Activity. |
| 40 |
// Handle can persist while Activity goes away. |
| 41 |
class FreestandingActivity::Handle final : public Wakeable { |
| 42 |
public: |
| 43 |
explicit Handle(FreestandingActivity* activity) : activity_(activity) {} |
| 44 |
|
| 45 |
// Ref the Handle (not the activity). |
| 46 |
void Ref() { refs_.fetch_add(1, std::memory_order_relaxed); } |
| 47 |
|
| 48 |
// Activity is going away... drop its reference and sever the connection back. |
| 49 |
void DropActivity() ABSL_LOCKS_EXCLUDED(mu_) { |
| 50 |
mu_.Lock(); |
| 51 |
GPR_ASSERT(activity_ != nullptr); |
| 52 |
activity_ = nullptr; |
| 53 |
mu_.Unlock(); |
| 54 |
Unref(); |
| 55 |
} |
| 56 |
|
| 57 |
// Activity needs to wake up (if it still exists!) - wake it up, and drop the |
| 58 |
// ref that was kept for this handle. |
| 59 |
void Wakeup() override ABSL_LOCKS_EXCLUDED(mu_) { |
| 60 |
mu_.Lock(); |
| 61 |
// Note that activity refcount can drop to zero, but we could win the lock |
| 62 |
// against DropActivity, so we need to only increase activities refcount if |
| 63 |
// it is non-zero. |
| 64 |
if (activity_ && activity_->RefIfNonzero()) { |
| 65 |
FreestandingActivity* activity = activity_; |
| 66 |
mu_.Unlock(); |
| 67 |
// Activity still exists and we have a reference: wake it up, which will |
| 68 |
// drop the ref. |
| 69 |
activity->Wakeup(); |
| 70 |
} else { |
| 71 |
// Could not get the activity - it's either gone or going. No need to wake |
| 72 |
// it up! |
| 73 |
mu_.Unlock(); |
| 74 |
} |
| 75 |
// Drop the ref to the handle (we have one ref = one wakeup semantics). |
| 76 |
Unref(); |
| 77 |
} |
| 78 |
|
| 79 |
void Drop() override { Unref(); } |
| 80 |
|
| 81 |
std::string ActivityDebugTag() const override { |
| 82 |
MutexLock lock(&mu_); |
| 83 |
return activity_ == nullptr ? "<unknown>" : activity_->DebugTag(); |
| 84 |
} |
| 85 |
|
| 86 |
private: |
| 87 |
// Unref the Handle (not the activity). |
| 88 |
void Unref() { |
| 89 |
if (1 == refs_.fetch_sub(1, std::memory_order_acq_rel)) { |
| 90 |
delete this; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
// Two initial refs: one for the waiter that caused instantiation, one for the |
| 95 |
// activity. |
| 96 |
std::atomic<size_t> refs_{2}; |
| 97 |
mutable Mutex mu_ ABSL_ACQUIRED_AFTER(activity_->mu_); |
| 98 |
FreestandingActivity* activity_ ABSL_GUARDED_BY(mu_); |
| 99 |
}; |
| 100 |
|
| 101 |
/////////////////////////////////////////////////////////////////////////////// |
| 102 |
// ACTIVITY IMPLEMENTATION |
| 103 |
|
| 104 |
bool FreestandingActivity::RefIfNonzero() { return IncrementIfNonzero(&refs_); } |
| 105 |
|
| 106 |
FreestandingActivity::Handle* FreestandingActivity::RefHandle() { |
| 107 |
if (handle_ == nullptr) { |
| 108 |
// No handle created yet - construct it and return it. |
| 109 |
handle_ = new Handle(this); |
| 110 |
return handle_; |
| 111 |
} else { |
| 112 |
// Already had to create a handle, ref & return it. |
| 113 |
handle_->Ref(); |
| 114 |
return handle_; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
void FreestandingActivity::DropHandle() { |
| 119 |
handle_->DropActivity(); |
| 120 |
handle_ = nullptr; |
| 121 |
} |
| 122 |
|
| 123 |
Waker FreestandingActivity::MakeNonOwningWaker() { |
| 124 |
mu_.AssertHeld(); |
| 125 |
return Waker(RefHandle()); |
| 126 |
} |
| 127 |
|
| 128 |
} // namespace promise_detail |
| 129 |
|
| 130 |
std::string Activity::DebugTag() const { |
| 131 |
return absl::StrFormat("ACTIVITY[%p]", this); |
| 132 |
} |
| 133 |
|
| 134 |
} // namespace grpc_core |