feat: implement MoloApp plans 01-15 with integration
Full C++23 framework tree: runtime, observability, security, data, app, realtime, supervisor, clients, CLI, example plus enterprise tenancy, audit, operability, governance, supplychain. Postgres via docker compose for tests.
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
// Spec: plans/01-runtime.md 6.1. Bounded pool for blocking work.
|
||||
#include "executor.h"
|
||||
|
||||
#include "caps.h"
|
||||
|
||||
namespace molo::runtime {
|
||||
|
||||
ThreadPool::ThreadPool(std::size_t threads, std::size_t queue_cap)
|
||||
: queue_cap_(queue_cap) {
|
||||
if (threads == 0) threads = 1;
|
||||
if (threads > caps::kThreadPoolThreadsCeiling) {
|
||||
threads = caps::kThreadPoolThreadsCeiling;
|
||||
}
|
||||
if (queue_cap_ == 0) queue_cap_ = 1;
|
||||
for (std::size_t i = 0; i < threads; ++i) {
|
||||
workers_.emplace_back([this] { Worker(); });
|
||||
}
|
||||
}
|
||||
|
||||
ThreadPool::~ThreadPool() { shutdown(); }
|
||||
|
||||
bool ThreadPool::try_submit(Job job) {
|
||||
if (!job) return false;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
if (stop_) return false;
|
||||
if (queue_.size() >= queue_cap_) return false;
|
||||
queue_.push_back(std::move(job));
|
||||
}
|
||||
cv_.notify_one();
|
||||
return true;
|
||||
}
|
||||
|
||||
void ThreadPool::shutdown() {
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
if (stop_) {
|
||||
// Fall through to join below; workers may still need joining.
|
||||
}
|
||||
stop_ = true;
|
||||
}
|
||||
cv_.notify_all();
|
||||
for (auto& w : workers_) {
|
||||
if (w.joinable()) w.join();
|
||||
}
|
||||
workers_.clear();
|
||||
}
|
||||
|
||||
std::size_t ThreadPool::pending() const {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
return queue_.size();
|
||||
}
|
||||
|
||||
bool ThreadPool::stopped() const {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
return stop_;
|
||||
}
|
||||
|
||||
void ThreadPool::Worker() {
|
||||
for (;;) {
|
||||
Job job;
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
cv_.wait(lock, [this] { return stop_ || !queue_.empty(); });
|
||||
if (queue_.empty()) {
|
||||
if (stop_) return;
|
||||
continue;
|
||||
}
|
||||
job = std::move(queue_.front());
|
||||
queue_.pop_front();
|
||||
}
|
||||
job();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace molo::runtime
|
||||
Reference in New Issue
Block a user