72 lines
2.0 KiB
C++
72 lines
2.0 KiB
C++
#pragma once
|
|
|
|
#include <Void/Functional/Result.hpp>
|
|
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <functional>
|
|
#include <memory>
|
|
#include <mutex>
|
|
#include <optional>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace ReloadableHost
|
|
{
|
|
struct ReleaseDescriptor {
|
|
std::string id;
|
|
std::string path;
|
|
std::uint64_t generation{0};
|
|
};
|
|
|
|
/// Host-supplied trust/checksum validation for a candidate release.
|
|
using ReleaseValidator =
|
|
std::function<Void::Result::Result<void>(const ReleaseDescriptor&)>;
|
|
|
|
struct ReleaseLimits {
|
|
/// Maximum unpinned-reclaimable older generations retained.
|
|
std::size_t max_retained_generations{2};
|
|
};
|
|
|
|
/// Immutable pinned release handle. Holding it keeps its generation alive
|
|
/// across reloads, exactly like an old read handle in the reference host.
|
|
using ReleaseHandle = std::shared_ptr<const ReleaseDescriptor>;
|
|
|
|
/// Immutable release generations with pin-based retention.
|
|
///
|
|
/// Publishing never invalidates an acquired handle. Older generations are
|
|
/// retired only after every pin is released, so the retained set stays
|
|
/// bounded by the pin count and the configured cap.
|
|
class ReleaseRegistry
|
|
{
|
|
public:
|
|
|
|
explicit ReleaseRegistry(ReleaseLimits limits = {});
|
|
|
|
/// Validate and publish a candidate. Returns its generation.
|
|
[[nodiscard]] Void::Result::Result<std::uint64_t>
|
|
publish(ReleaseDescriptor candidate,
|
|
const ReleaseValidator& validate);
|
|
|
|
/// Pin the current generation. Empty when nothing is published.
|
|
[[nodiscard]] ReleaseHandle acquire() const;
|
|
|
|
[[nodiscard]] std::optional<ReleaseDescriptor> current() const;
|
|
|
|
/// Retire older generations with no outstanding pins.
|
|
std::size_t collect() noexcept;
|
|
|
|
[[nodiscard]] std::size_t retained() const noexcept;
|
|
|
|
private:
|
|
|
|
std::size_t collect_locked() noexcept;
|
|
|
|
mutable std::mutex m_mutex;
|
|
ReleaseLimits m_limits;
|
|
std::uint64_t m_next_generation{1};
|
|
ReleaseHandle m_current;
|
|
std::vector<ReleaseHandle> m_retained;
|
|
};
|
|
} // namespace ReloadableHost
|