| 1 |
// Copyright 2022 The 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 |
#include <grpc/support/port_platform.h> |
| 15 |
|
| 16 |
#include "src/core/lib/event_engine/work_queue.h" |
| 17 |
|
| 18 |
#include <utility> |
| 19 |
|
| 20 |
#include "absl/functional/any_invocable.h" |
| 21 |
|
| 22 |
#include "src/core/lib/event_engine/common_closures.h" |
| 23 |
|
| 24 |
namespace grpc_event_engine { |
| 25 |
namespace experimental { |
| 26 |
|
| 27 |
// ------ WorkQueue::Storage -------------------------------------------------- |
| 28 |
|
| 29 |
WorkQueue::Storage::Storage(EventEngine::Closure* closure) noexcept |
| 30 |
: closure_(closure), |
| 31 |
enqueued_( |
| 32 |
grpc_core::Timestamp::Now().milliseconds_after_process_epoch()) {} |
| 33 |
|
| 34 |
WorkQueue::Storage::Storage(absl::AnyInvocable<void()> callback) noexcept |
| 35 |
: closure_(SelfDeletingClosure::Create(std::move(callback))), |
| 36 |
enqueued_( |
| 37 |
grpc_core::Timestamp::Now().milliseconds_after_process_epoch()) {} |
| 38 |
|
| 39 |
WorkQueue::Storage::Storage(Storage&& other) noexcept |
| 40 |
: closure_(other.closure_), enqueued_(other.enqueued_) {} |
| 41 |
|
| 42 |
WorkQueue::Storage& WorkQueue::Storage::operator=(Storage&& other) noexcept { |
| 43 |
std::swap(closure_, other.closure_); |
| 44 |
std::swap(enqueued_, other.enqueued_); |
| 45 |
return *this; |
| 46 |
} |
| 47 |
|
| 48 |
EventEngine::Closure* WorkQueue::Storage::closure() { return closure_; } |
| 49 |
|
| 50 |
// ------ WorkQueue ----------------------------------------------------------- |
| 51 |
|
| 52 |
// Returns whether the queue is empty |
| 53 |
bool WorkQueue::Empty() const { |
| 54 |
return (most_recent_element_enqueue_timestamp_.load( |
| 55 |
std::memory_order_relaxed) == kInvalidTimestamp && |
| 56 |
oldest_enqueued_timestamp_.load(std::memory_order_relaxed) == |
| 57 |
kInvalidTimestamp); |
| 58 |
} |
| 59 |
|
| 60 |
grpc_core::Timestamp WorkQueue::OldestEnqueuedTimestamp() const { |
| 61 |
int64_t front_of_queue_timestamp = |
| 62 |
oldest_enqueued_timestamp_.load(std::memory_order_relaxed); |
| 63 |
if (front_of_queue_timestamp != kInvalidTimestamp) { |
| 64 |
return grpc_core::Timestamp::FromMillisecondsAfterProcessEpoch( |
| 65 |
front_of_queue_timestamp); |
| 66 |
} |
| 67 |
int64_t most_recent_millis = |
| 68 |
most_recent_element_enqueue_timestamp_.load(std::memory_order_relaxed); |
| 69 |
if (most_recent_millis == kInvalidTimestamp) { |
| 70 |
return grpc_core::Timestamp::InfPast(); |
| 71 |
} |
| 72 |
return grpc_core::Timestamp::FromMillisecondsAfterProcessEpoch( |
| 73 |
most_recent_millis); |
| 74 |
} |
| 75 |
|
| 76 |
EventEngine::Closure* WorkQueue::PopFront() ABSL_LOCKS_EXCLUDED(mu_) { |
| 77 |
if (oldest_enqueued_timestamp_.load(std::memory_order_relaxed) != |
| 78 |
kInvalidTimestamp) { |
| 79 |
EventEngine::Closure* t = TryLockAndPop(/*front=*/true); |
| 80 |
if (t != nullptr) return t; |
| 81 |
} |
| 82 |
if (most_recent_element_enqueue_timestamp_.load(std::memory_order_relaxed) != |
| 83 |
kInvalidTimestamp) { |
| 84 |
return TryPopMostRecentElement(); |
| 85 |
} |
| 86 |
return nullptr; |
| 87 |
} |
| 88 |
|
| 89 |
EventEngine::Closure* WorkQueue::PopBack() { |
| 90 |
if (most_recent_element_enqueue_timestamp_.load(std::memory_order_relaxed) != |
| 91 |
kInvalidTimestamp) { |
| 92 |
return TryPopMostRecentElement(); |
| 93 |
} |
| 94 |
if (oldest_enqueued_timestamp_.load(std::memory_order_relaxed) != |
| 95 |
kInvalidTimestamp) { |
| 96 |
EventEngine::Closure* t = TryLockAndPop(/*front=*/false); |
| 97 |
if (t != nullptr) return t; |
| 98 |
} |
| 99 |
return nullptr; |
| 100 |
} |
| 101 |
|
| 102 |
void WorkQueue::Add(EventEngine::Closure* closure) { |
| 103 |
AddInternal(Storage(closure)); |
| 104 |
} |
| 105 |
|
| 106 |
void WorkQueue::Add(absl::AnyInvocable<void()> invocable) { |
| 107 |
AddInternal(Storage(std::move(invocable))); |
| 108 |
} |
| 109 |
|
| 110 |
void WorkQueue::AddInternal(Storage&& storage) { |
| 111 |
Storage previous_most_recent; |
| 112 |
int64_t previous_ts; |
| 113 |
{ |
| 114 |
absl::optional<Storage> tmp_element; |
| 115 |
{ |
| 116 |
grpc_core::MutexLock lock(&most_recent_element_lock_); |
| 117 |
previous_ts = most_recent_element_enqueue_timestamp_.exchange( |
| 118 |
storage.enqueued(), std::memory_order_relaxed); |
| 119 |
tmp_element = std::exchange(most_recent_element_, std::move(storage)); |
| 120 |
} |
| 121 |
if (!tmp_element.has_value() || previous_ts == kInvalidTimestamp) return; |
| 122 |
previous_most_recent = std::move(*tmp_element); |
| 123 |
} |
| 124 |
grpc_core::MutexLock lock(&mu_); |
| 125 |
if (elements_.empty()) { |
| 126 |
oldest_enqueued_timestamp_.store(previous_ts, std::memory_order_relaxed); |
| 127 |
} |
| 128 |
elements_.push_back(std::move(previous_most_recent)); |
| 129 |
} |
| 130 |
|
| 131 |
EventEngine::Closure* WorkQueue::TryLockAndPop(bool front) |
| 132 |
ABSL_LOCKS_EXCLUDED(mu_) { |
| 133 |
// Do not block the worker if there are other workers trying to pop |
| 134 |
// tasks from this queue. |
| 135 |
if (!mu_.TryLock()) return nullptr; |
| 136 |
auto ret = PopLocked(front); |
| 137 |
mu_.Unlock(); |
| 138 |
return ret; |
| 139 |
} |
| 140 |
|
| 141 |
EventEngine::Closure* WorkQueue::PopLocked(bool front) |
| 142 |
ABSL_EXCLUSIVE_LOCKS_REQUIRED(mu_) { |
| 143 |
if (GPR_UNLIKELY(elements_.empty())) { |
| 144 |
if (most_recent_element_enqueue_timestamp_.load( |
| 145 |
std::memory_order_relaxed) == kInvalidTimestamp) { |
| 146 |
return nullptr; |
| 147 |
} |
| 148 |
return TryPopMostRecentElement(); |
| 149 |
} |
| 150 |
// the queue has elements, let's pop one and update timestamps |
| 151 |
Storage ret_s; |
| 152 |
if (front) { |
| 153 |
ret_s = std::move(elements_.front()); |
| 154 |
elements_.pop_front(); |
| 155 |
} else { |
| 156 |
ret_s = std::move(elements_.back()); |
| 157 |
elements_.pop_back(); |
| 158 |
} |
| 159 |
if (elements_.empty()) { |
| 160 |
oldest_enqueued_timestamp_.store(kInvalidTimestamp, |
| 161 |
std::memory_order_relaxed); |
| 162 |
} else if (front) { |
| 163 |
oldest_enqueued_timestamp_.store(elements_.front().enqueued(), |
| 164 |
std::memory_order_relaxed); |
| 165 |
} |
| 166 |
return ret_s.closure(); |
| 167 |
} |
| 168 |
|
| 169 |
EventEngine::Closure* WorkQueue::TryPopMostRecentElement() { |
| 170 |
if (!most_recent_element_lock_.TryLock()) return nullptr; |
| 171 |
if (GPR_UNLIKELY(!most_recent_element_.has_value())) { |
| 172 |
most_recent_element_lock_.Unlock(); |
| 173 |
return nullptr; |
| 174 |
} |
| 175 |
most_recent_element_enqueue_timestamp_.store(kInvalidTimestamp, |
| 176 |
std::memory_order_relaxed); |
| 177 |
absl::optional<Storage> tmp = |
| 178 |
std::exchange(most_recent_element_, absl::nullopt); |
| 179 |
most_recent_element_lock_.Unlock(); |
| 180 |
return tmp->closure(); |
| 181 |
} |
| 182 |
|
| 183 |
} // namespace experimental |
| 184 |
} // namespace grpc_event_engine |