| #include <deque> | |
| #include <iterator> | |
| #include <memory> | |
| #include <utility> | |
| #include <vector> | |
| #include "blocking_queue.h" | |
| #include "event.h" | |
| #include "absl/base/thread_annotations.h" | |
| #include "absl/functional/any_invocable.h" | |
| #include "absl/log/log.h" | |
| #include "absl/status/status.h" | |
| #include "absl/status/statusor.h" | |
| #include "absl/synchronization/mutex.h" | |
| #include "absl/time/time.h" | |
| #include "bmc/status_macros.h" | |
| namespace safepower_agent { | |
| template <typename T> | |
| struct BlockingQueue<T>::Impl { | |
| absl::Mutex mutex; | |
| std::deque<T> queue ABSL_GUARDED_BY(mutex); | |
| bool closed ABSL_GUARDED_BY(mutex) = false; | |
| bool NotEmptyOrClosed() const ABSL_SHARED_LOCKS_REQUIRED(mutex) { | |
| return !queue.empty() || closed; | |
| } | |
| absl::Status WaitUntilReady(absl::Duration timeout) | |
| ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex) { | |
| if (queue.empty() && !closed && timeout > absl::ZeroDuration()) { | |
| mutex.AwaitWithTimeout(absl::Condition(this, &Impl::NotEmptyOrClosed), | |
| timeout); | |
| } | |
| if (!queue.empty()) { | |
| return absl::OkStatus(); | |
| } | |
| if (closed) { | |
| return absl::AbortedError("Blocking queue closed"); | |
| } | |
| return absl::DeadlineExceededError("Timed out waiting for event"); | |
| } | |
| }; | |
| template <typename T> | |
| BlockingQueue<T>::BlockingQueue() : impl_(std::make_unique<Impl>()) {} | |
| template <typename T> | |
| BlockingQueue<T>::~BlockingQueue() = default; | |
| template <typename T> | |
| void BlockingQueue<T>::Push(T item) { | |
| absl::MutexLock lock(impl_->mutex); | |
| if (impl_->closed) { | |
| LOG(DFATAL) << "Attempted to push to a closed BlockingQueue."; | |
| return; | |
| } | |
| impl_->queue.push_back(std::move(item)); | |
| } | |
| template <typename T> | |
| void BlockingQueue<T>::Close() { | |
| absl::MutexLock lock(impl_->mutex); | |
| impl_->closed = true; | |
| } | |
| template <typename T> | |
| absl::StatusOr<T> BlockingQueue<T>::PopWithTimeout(absl::Duration timeout) { | |
| absl::MutexLock lock(impl_->mutex); | |
| RETURN_IF_ERROR(impl_->WaitUntilReady(timeout)); | |
| T item = std::move(impl_->queue.front()); | |
| impl_->queue.pop_front(); | |
| return item; | |
| } | |
| template <typename T> | |
| absl::StatusOr<std::vector<T>> BlockingQueue<T>::PopAllWithTimeout( | |
| absl::Duration timeout) { | |
| absl::MutexLock lock(impl_->mutex); | |
| absl::Status status = impl_->WaitUntilReady(timeout); | |
| if (absl::IsDeadlineExceeded(status)) { | |
| return std::vector<T>{}; | |
| } | |
| if (!status.ok()) { | |
| return status; | |
| } | |
| std::vector<T> events(std::make_move_iterator(impl_->queue.begin()), | |
| std::make_move_iterator(impl_->queue.end())); | |
| impl_->queue.clear(); | |
| return events; | |
| } | |
| template class BlockingQueue<Event>; | |
| template class BlockingQueue<absl::AnyInvocable<void()>>; | |
| } // namespace safepower_agent |