| 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 |
#ifndef GRPC_CORE_LIB_EVENT_ENGINE_WORK_QUEUE_H |
| 15 |
#define GRPC_CORE_LIB_EVENT_ENGINE_WORK_QUEUE_H |
| 16 |
|
| 17 |
#include <grpc/support/port_platform.h> |
| 18 |
|
| 19 |
#include <stdint.h> |
| 20 |
|
| 21 |
#include <atomic> |
| 22 |
#include <deque> |
| 23 |
|
| 24 |
#include "absl/base/thread_annotations.h" |
| 25 |
#include "absl/functional/any_invocable.h" |
| 26 |
#include "absl/types/optional.h" |
| 27 |
|
| 28 |
#include <grpc/event_engine/event_engine.h> |
| 29 |
|
| 30 |
#include "src/core/lib/gprpp/sync.h" |
| 31 |
#include "src/core/lib/gprpp/time.h" |
| 32 |
|
| 33 |
namespace grpc_event_engine { |
| 34 |
namespace experimental { |
| 35 |
|
| 36 |
// A fast work queue based lightly on an internal Google implementation. |
| 37 |
// |
| 38 |
// This uses atomics to access the most recent element in the queue, making it |
| 39 |
// fast for LIFO operations. Accessing the oldest (next) element requires taking |
| 40 |
// a mutex lock. |
| 41 |
class WorkQueue { |
| 42 |
public: |
| 43 |
// comparable to Timestamp::milliseconds_after_process_epoch() |
| 44 |
static const int64_t kInvalidTimestamp = -1; |
| 45 |
|
| 46 |
WorkQueue() = default; |
| 47 |
// Returns whether the queue is empty |
| 48 |
bool Empty() const; |
| 49 |
// Returns the Timestamp of when the most recently-added element was |
| 50 |
// enqueued. |
| 51 |
grpc_core::Timestamp OldestEnqueuedTimestamp() const; |
| 52 |
// Returns the next (oldest) element from the queue, or nullopt if empty |
| 53 |
EventEngine::Closure* PopFront() ABSL_LOCKS_EXCLUDED(mu_); |
| 54 |
// Returns the most recent element from the queue, or nullopt if empty |
| 55 |
EventEngine::Closure* PopBack(); |
| 56 |
// Adds a closure to the back of the queue |
| 57 |
void Add(EventEngine::Closure* closure); |
| 58 |
// Wraps an AnyInvocable and adds it to the back of the queue |
| 59 |
void Add(absl::AnyInvocable<void()> invocable); |
| 60 |
|
| 61 |
private: |
| 62 |
class Storage { |
| 63 |
public: |
| 64 |
Storage() = default; |
| 65 |
// Take a non-owned Closure* |
| 66 |
// Requires an exec_ctx on the stack |
| 67 |
// TODO(ctiller): replace with an alternative time source |
| 68 |
explicit Storage(EventEngine::Closure* closure) noexcept; |
| 69 |
// Wrap an AnyInvocable into a Closure. |
| 70 |
// The closure must be executed or explicitly deleted to prevent memory |
| 71 |
// leaks. Requires an exec_ctx on the stack |
| 72 |
// TODO(ctiller): replace with an alternative time source |
| 73 |
explicit Storage(absl::AnyInvocable<void()> callback) noexcept; |
| 74 |
~Storage() = default; |
| 75 |
// not copyable |
| 76 |
Storage(const Storage&) = delete; |
| 77 |
Storage& operator=(const Storage&) = delete; |
| 78 |
// moveable |
| 79 |
Storage(Storage&& other) noexcept; |
| 80 |
Storage& operator=(Storage&& other) noexcept; |
| 81 |
// Is this enqueued? |
| 82 |
int64_t enqueued() const { return enqueued_; } |
| 83 |
// Get the stored closure, or wrapped AnyInvocable |
| 84 |
EventEngine::Closure* closure(); |
| 85 |
|
| 86 |
private: |
| 87 |
EventEngine::Closure* closure_ = nullptr; |
| 88 |
int64_t enqueued_ = kInvalidTimestamp; |
| 89 |
}; |
| 90 |
|
| 91 |
// Attempts to pop from the front of the queue (oldest). |
| 92 |
// This will return nullopt if the queue is empty, or if other workers |
| 93 |
// are already attempting to pop from this queue. |
| 94 |
EventEngine::Closure* TryLockAndPop(bool front) ABSL_LOCKS_EXCLUDED(mu_); |
| 95 |
// Internal implementation, helps with thread safety analysis in TryLockAndPop |
| 96 |
EventEngine::Closure* PopLocked(bool front) |
| 97 |
ABSL_EXCLUSIVE_LOCKS_REQUIRED(mu_); |
| 98 |
// Attempts to pop from the back of the queue (most recent). |
| 99 |
// This will return nullopt if the queue is empty, or if other workers |
| 100 |
// are already attempting to pop from this queue. |
| 101 |
EventEngine::Closure* TryPopMostRecentElement(); |
| 102 |
// Common code for the Add methods |
| 103 |
void AddInternal(Storage&& storage); |
| 104 |
|
| 105 |
// The managed items in the queue |
| 106 |
std::deque<Storage> elements_ ABSL_GUARDED_BY(mu_); |
| 107 |
// The most recently enqueued element. This is reserved from work stealing |
| 108 |
absl::optional<Storage> most_recent_element_ |
| 109 |
ABSL_GUARDED_BY(most_recent_element_lock_); |
| 110 |
grpc_core::Mutex ABSL_ACQUIRED_AFTER(mu_) most_recent_element_lock_; |
| 111 |
// TODO(hork): consider ABSL_CACHELINE_ALIGNED |
| 112 |
std::atomic<int64_t> most_recent_element_enqueue_timestamp_{ |
| 113 |
kInvalidTimestamp}; |
| 114 |
std::atomic<int64_t> oldest_enqueued_timestamp_{kInvalidTimestamp}; |
| 115 |
grpc_core::Mutex mu_; |
| 116 |
}; |
| 117 |
|
| 118 |
} // namespace experimental |
| 119 |
} // namespace grpc_event_engine |
| 120 |
|
| 121 |
#endif // GRPC_CORE_LIB_EVENT_ENGINE_WORK_QUEUE_H |