| #ifndef PRODUCTION_BORG_MGMT_NODE_PROXY_SAFEPOWER_SAFEPOWER_AGENT_BLOCKING_QUEUE_H_ | |
| #define PRODUCTION_BORG_MGMT_NODE_PROXY_SAFEPOWER_SAFEPOWER_AGENT_BLOCKING_QUEUE_H_ | |
| #include <memory> | |
| #include <vector> | |
| #include "absl/status/statusor.h" | |
| #include "absl/time/time.h" | |
| namespace safepower_agent { | |
| // A thread-safe or fiber-safe blocking FIFO queue (depending on the linked | |
| // platform implementation). | |
| // | |
| // Multiple producers may call `Push()` concurrently, and consumers may block | |
| // waiting for items via `PopWithTimeout()` or `PopAllWithTimeout()`. | |
| // | |
| // Because member function definitions reside in platform-specific `.cc` files | |
| // (`thread::Channel` on NodeOS, `absl::Mutex` + `std::deque` on gBMC), only | |
| // explicitly instantiated types `T` are supported (currently `Event` and | |
| // `absl::AnyInvocable<void()>`). | |
| template <typename T> | |
| class BlockingQueue { | |
| public: | |
| BlockingQueue(); | |
| ~BlockingQueue(); | |
| // Thread-safe / Fiber-safe push. | |
| // Requires that Close() has not been called. Pushing to a closed queue | |
| // is a programming error and will result in a fatal log (DFATAL). | |
| void Push(T item); | |
| // Closes the queue. Subsequent Pops will return an error once the queue is | |
| // empty. | |
| // Requires that Close() is called at most once. | |
| // The caller must externally coordinate to ensure that no calls to Push() | |
| // occur concurrently with or after Close(). | |
| void Close(); | |
| // Blocks until an item is available or timeout is reached. | |
| // Returns the popped item, DeadlineExceededError on timeout, or AbortedError | |
| // if the queue/channel is closed and empty. | |
| absl::StatusOr<T> PopWithTimeout( | |
| absl::Duration timeout = absl::InfiniteDuration()); | |
| // Blocks until items are available or timeout is reached. | |
| // Returns all popped items (or an empty vector on timeout; never returns | |
| // DeadlineExceededError), or AbortedError if the queue/channel is closed and | |
| // empty. | |
| absl::StatusOr<std::vector<T>> PopAllWithTimeout(absl::Duration timeout); | |
| private: | |
| struct Impl; | |
| std::unique_ptr<Impl> impl_; | |
| }; | |
| } // namespace safepower_agent | |
| #endif // PRODUCTION_BORG_MGMT_NODE_PROXY_SAFEPOWER_SAFEPOWER_AGENT_BLOCKING_QUEUE_H_ |