CORE stage 8 merged, so vdm::Engine is linkable. This closes D4a and narrows D4b: `velox add <url>` now actually downloads. - sched/engine_port_core.hpp — the real EnginePort: forwards to a live vdm::Engine, keeps the DownloadHandle per task for pause/resume/ cancel/provide_auth/decide/refresh_url, drives set_task_order / set_max_active_segments / set_host_segment_cap via engine.segment_budget(). CORE confirmed the admission model: DAEMON decides when to start(); the engine's own download_task calls register_task/set_want internally — DAEMON never touches per-task budget calls. EnginePort gains release(TaskId) so the port drops a handle when the task goes terminal. - rpc/event_loop — EventLoop::post(fn): thread-safe, runs fn on the loop thread next iteration. The marshaller for engine-thread callbacks. - main.cpp — constructs vdm::Engine + EnginePortCore + Scheduler (post_to_loop = loop.post). At startup: reconcile_after_restart() (ADR 0013 §5), reload_config(), tick(). A 1 s timerfd on the loop re-runs tick() (schedule windows, missed nudges); download.add nudges via dispatcher.set_on_mutation. End-to-end verified against tools/testserver: `velox add http://127.0.0.1:.../file/512K` -> task queued -> scheduler admits -> engine downloads 524288 bytes -> complete, file on disk. First byte-path all the way through the project. safepath-adversarial.md: re-verified per its own note — CORE landed O_NOFOLLOW on the target open (core/src/io/sparse_file.cpp), so the leaf-symlink TOCTOU is now closed; residual is down to one intermediate-dir gap (documented post-M1 chase). 36 daemon/cli tests green; scheduler + uds_roundtrip TSan-clean. Co-Authored-By: Claude Sonnet 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Upd9WhG9oppieig5nRDLig
80 lines
2.6 KiB
C++
80 lines
2.6 KiB
C++
#pragma once
|
|
|
|
// A single-threaded poll(2) reactor. Every RPC listener and connection registers its fd
|
|
// here; the loop never blocks on disk or DNS (AGENT-DAEMON.md build step 1 — "Never block
|
|
// the RPC loop"). Long work is handed to CORE's pools later; this class only multiplexes
|
|
// readiness.
|
|
//
|
|
// Thread model: run() executes on one thread. add_fd/mod_fd/del_fd are called from
|
|
// callbacks on that same thread. stop() and wake() are async-signal-safe and safe to call
|
|
// from any thread or a signal handler — they only write() a byte to an internal eventfd.
|
|
|
|
#include <atomic>
|
|
#include <cstdint>
|
|
#include <functional>
|
|
#include <mutex>
|
|
#include <unordered_map>
|
|
#include <vector>
|
|
|
|
namespace velox::daemon::rpc {
|
|
|
|
enum Interest : unsigned {
|
|
kNone = 0,
|
|
kRead = 1u << 0,
|
|
kWrite = 1u << 1,
|
|
};
|
|
|
|
class EventLoop {
|
|
public:
|
|
// Called when the fd is readable and/or writable. `events` is the subset of the fd's
|
|
// registered Interest that fired. A callback may add/modify/remove any fd, including
|
|
// its own, and may call stop().
|
|
using Callback = std::function<void(int fd, unsigned events)>;
|
|
|
|
EventLoop();
|
|
~EventLoop();
|
|
|
|
EventLoop(const EventLoop&) = delete;
|
|
EventLoop& operator=(const EventLoop&) = delete;
|
|
|
|
// Register `fd` (must be non-blocking) for `interest`. Replaces any prior registration.
|
|
void add_fd(int fd, unsigned interest, Callback cb);
|
|
// Change the interest mask for an already-registered fd.
|
|
void mod_fd(int fd, unsigned interest);
|
|
// Stop watching `fd`. Does not close it — ownership stays with the caller.
|
|
void del_fd(int fd);
|
|
|
|
// Run until stop() is called. Re-entrant calls are not supported.
|
|
void run();
|
|
|
|
// Ask run() to return after the current poll wakeup. Async-signal-safe.
|
|
void stop() noexcept;
|
|
|
|
// Force one poll() wakeup without stopping — used when interest changed from outside a
|
|
// callback. Async-signal-safe.
|
|
void wake() noexcept;
|
|
|
|
// Run `fn` on the loop thread at the next iteration. Thread-safe; the intended way to
|
|
// marshal an engine-thread callback back onto the RPC loop.
|
|
void post(std::function<void()> fn);
|
|
|
|
private:
|
|
struct Entry {
|
|
unsigned interest;
|
|
Callback cb;
|
|
};
|
|
|
|
void drain_wakeup() noexcept;
|
|
void drain_posts();
|
|
|
|
int wake_fd_; // eventfd, always registered
|
|
bool running_ = false;
|
|
std::atomic<bool> stop_requested_ = false; // set from stop(), read by run()
|
|
std::unordered_map<int, Entry> fds_;
|
|
|
|
std::mutex post_mu_;
|
|
std::vector<std::function<void()>> posts_;
|
|
};
|
|
|
|
} // namespace velox::daemon::rpc
|