daemon: Phase 0 platform seams (ADR 0020) — eventfd/SO_PEERCRED/instance-lock/XDG behind platform/, zero behaviour change
Introduces the four rpc-layer seams docs/08-porting.md calls for this lane: platform::Wakeup (eventfd), platform::peer_of (SO_PEERCRED/struct ucred, same-UID check preserved unchanged), platform::acquire_instance_lock (abstract-namespace socket), platform::runtime_base_dir/data_base_dir (XDG lookups). Today's Linux code moves unchanged into daemon/src/rpc/platform/linux/; the seam headers carry no OS types and no #ifdef. No fifth interface for timerfd: EventLoop gains a portable add_timer() that folds the next deadline into poll()'s own timeout, replacing both timerfd instances in main.cpp — the loop already computes a deadline, so this needs no per-OS backend at all. Full suite 59/59 green; no #ifdef outside platform/linux/, no behaviour change. Co-Authored-By: Claude Sonnet 5 <[email protected]>
This commit is contained in:
@@ -10,12 +10,15 @@
|
||||
// from any thread or a signal handler — they only write() a byte to an internal eventfd.
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <mutex>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#include "rpc/platform/wakeup.hpp"
|
||||
|
||||
namespace velox::daemon::rpc {
|
||||
|
||||
enum Interest : unsigned {
|
||||
@@ -58,22 +61,43 @@ public:
|
||||
// marshal an engine-thread callback back onto the RPC loop.
|
||||
void post(std::function<void()> fn);
|
||||
|
||||
using TimerId = std::uint64_t;
|
||||
|
||||
// Fire `cb` roughly every `interval` for as long as the loop runs, expressed as a
|
||||
// poll(2) timeout rather than a platform timer fd (docs/08-porting.md: "the loop
|
||||
// already has a deadline set" — no per-OS backend needed for this one). Not
|
||||
// reentrant-safe to call from inside a timer callback other than the one running.
|
||||
TimerId add_timer(std::chrono::milliseconds interval, std::function<void()> cb);
|
||||
void remove_timer(TimerId id);
|
||||
|
||||
private:
|
||||
struct Entry {
|
||||
unsigned interest;
|
||||
Callback cb;
|
||||
};
|
||||
|
||||
void drain_wakeup() noexcept;
|
||||
void drain_posts();
|
||||
struct Timer {
|
||||
std::chrono::steady_clock::time_point next;
|
||||
std::chrono::milliseconds interval;
|
||||
std::function<void()> cb;
|
||||
};
|
||||
|
||||
int wake_fd_; // eventfd, always registered
|
||||
void drain_posts();
|
||||
// Milliseconds until the next timer is due, or -1 if there are none (poll()'s "block
|
||||
// forever" convention).
|
||||
int next_timeout_ms() const;
|
||||
void run_due_timers();
|
||||
|
||||
platform::Wakeup wakeup_;
|
||||
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_;
|
||||
|
||||
TimerId next_timer_id_ = 1;
|
||||
std::unordered_map<TimerId, Timer> timers_;
|
||||
};
|
||||
|
||||
} // namespace velox::daemon::rpc
|
||||
|
||||
Reference in New Issue
Block a user