| #ifndef PRODUCTION_BORG_MGMT_NODE_PROXY_SAFEPOWER_SAFEPOWER_AGENT_WORKER_POOL_H_ | |
| #define PRODUCTION_BORG_MGMT_NODE_PROXY_SAFEPOWER_SAFEPOWER_AGENT_WORKER_POOL_H_ | |
| #include <cstddef> | |
| #include <memory> | |
| #include "blocking_queue.h" | |
| #include "absl/functional/any_invocable.h" | |
| namespace safepower_agent { | |
| // A fixed-size RAII worker pool backed by BlockingQueue<WorkItem>. | |
| // | |
| // Workers (thread::Fiber on NodeOS, std::thread on gBMC) are started on | |
| // construction. Destruction closes the task queue and blocks until all queued | |
| // and in-flight tasks have completed and all workers have joined. | |
| // | |
| // Because child fibers on NodeOS must be joined by the same fiber that created | |
| // them, WorkerPool is non-copyable and non-movable and must be destroyed on the | |
| // same thread/fiber that constructed it. | |
| class WorkerPool { | |
| public: | |
| using WorkItem = absl::AnyInvocable<void()>; | |
| explicit WorkerPool(size_t num_workers = 1); | |
| ~WorkerPool(); | |
| WorkerPool(const WorkerPool&) = delete; | |
| WorkerPool& operator=(const WorkerPool&) = delete; | |
| WorkerPool(WorkerPool&&) = delete; | |
| WorkerPool& operator=(WorkerPool&&) = delete; | |
| // Enqueues a task to be executed by an available worker. | |
| // Must not be called concurrently with ~WorkerPool(). | |
| void Schedule(WorkItem task); | |
| private: | |
| void WorkerLoop(); | |
| BlockingQueue<WorkItem> queue_; | |
| struct Impl; | |
| std::unique_ptr<Impl> impl_; | |
| }; | |
| } // namespace safepower_agent | |
| #endif // PRODUCTION_BORG_MGMT_NODE_PROXY_SAFEPOWER_SAFEPOWER_AGENT_WORKER_POOL_H_ |