• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2023 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_SRC_CORE_LIB_EVENT_ENGINE_WORK_QUEUE_BASIC_WORK_QUEUE_H
15 #define GRPC_SRC_CORE_LIB_EVENT_ENGINE_WORK_QUEUE_BASIC_WORK_QUEUE_H
16 #include <grpc/event_engine/event_engine.h>
17 #include <grpc/support/port_platform.h>
18 #include <stddef.h>
19 
20 #include <deque>
21 
22 #include "absl/base/thread_annotations.h"
23 #include "absl/functional/any_invocable.h"
24 #include "src/core/lib/event_engine/work_queue/work_queue.h"
25 #include "src/core/util/sync.h"
26 
27 namespace grpc_event_engine {
28 namespace experimental {
29 
30 // A basic WorkQueue implementation that guards an std::deque with a Mutex
31 //
32 // Implementation note: q_.back is the most recent. q_.front is the oldest. New
33 // closures are added to the back.
34 class BasicWorkQueue : public WorkQueue {
35  public:
BasicWorkQueue()36   BasicWorkQueue() : owner_(nullptr) {}
37   explicit BasicWorkQueue(void* owner);
38   // Returns whether the queue is empty
39   bool Empty() const override ABSL_LOCKS_EXCLUDED(mu_);
40   // Returns the size of the queue.
41   size_t Size() const override ABSL_LOCKS_EXCLUDED(mu_);
42   // Returns the most recent element from the queue, or nullptr if either empty
43   // or the queue is under contention. This is the fastest way to retrieve
44   // elements from the queue.
45   //
46   // This method may return nullptr even if the queue is not empty.
47   EventEngine::Closure* PopMostRecent() override ABSL_LOCKS_EXCLUDED(mu_);
48   // Returns the most recent element from the queue, or nullptr if either empty
49   // or the queue is under contention.
50   // This is expected to be the slower of the two ways to retrieve closures from
51   // the queue.
52   //
53   // This method may return nullptr even if the queue is not empty.
54   EventEngine::Closure* PopOldest() override ABSL_LOCKS_EXCLUDED(mu_);
55   // Adds a closure to the queue.
56   void Add(EventEngine::Closure* closure) override ABSL_LOCKS_EXCLUDED(mu_);
57   // Wraps an AnyInvocable and adds it to the the queue.
58   void Add(absl::AnyInvocable<void()> invocable) override
59       ABSL_LOCKS_EXCLUDED(mu_);
owner()60   const void* owner() override { return owner_; }
61 
62  private:
63   mutable grpc_core::Mutex mu_;
64   std::deque<EventEngine::Closure*> q_ ABSL_GUARDED_BY(mu_);
65   const void* const owner_ = nullptr;
66 };
67 
68 }  // namespace experimental
69 }  // namespace grpc_event_engine
70 
71 #endif  // GRPC_SRC_CORE_LIB_EVENT_ENGINE_WORK_QUEUE_BASIC_WORK_QUEUE_H
72