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 #include <grpc/support/port_platform.h>
15 
16 #include "src/core/lib/event_engine/work_queue/basic_work_queue.h"
17 
18 #include <utility>
19 
20 #include "src/core/lib/event_engine/common_closures.h"
21 #include "src/core/lib/gprpp/sync.h"
22 
23 namespace grpc_event_engine {
24 namespace experimental {
25 
Empty() const26 bool BasicWorkQueue::Empty() const {
27   grpc_core::MutexLock lock(&mu_);
28   return q_.empty();
29 }
30 
Size() const31 size_t BasicWorkQueue::Size() const {
32   grpc_core::MutexLock lock(&mu_);
33   return q_.size();
34 }
35 
PopMostRecent()36 EventEngine::Closure* BasicWorkQueue::PopMostRecent() {
37   grpc_core::MutexLock lock(&mu_);
38   if (q_.empty()) return nullptr;
39   auto tmp = q_.back();
40   q_.pop_back();
41   return tmp;
42 }
43 
PopOldest()44 EventEngine::Closure* BasicWorkQueue::PopOldest() {
45   grpc_core::MutexLock lock(&mu_);
46   if (q_.empty()) return nullptr;
47   auto tmp = q_.front();
48   q_.pop_front();
49   return tmp;
50 }
51 
Add(EventEngine::Closure * closure)52 void BasicWorkQueue::Add(EventEngine::Closure* closure) {
53   grpc_core::MutexLock lock(&mu_);
54   q_.push_back(closure);
55 }
56 
Add(absl::AnyInvocable<void ()> invocable)57 void BasicWorkQueue::Add(absl::AnyInvocable<void()> invocable) {
58   grpc_core::MutexLock lock(&mu_);
59   q_.push_back(SelfDeletingClosure::Create(std::move(invocable)));
60 }
61 
62 }  // namespace experimental
63 }  // namespace grpc_event_engine
64