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