#ifndef TIMER_LEDGER_H #define TIMER_LEDGER_H #include #define TL_TIME_MAX UINT64_C(9223372036854775807) enum tl_rc { TL_OK = 0, TL_EMPTY = 1, TL_WAIT = 2, TL_DONE = 3, TL_ARG = -1, TL_ID = -2, TL_BUSY = -3, TL_STALE = -4, TL_RANGE = -5 }; enum tl_phase { TL_IDLE = 0, TL_ARMED = 1, TL_READY = 2, TL_RUNNING = 3, TL_DRAINING = 4 }; enum tl_cancel_state { TL_CANCEL_NONE = 0, TL_CANCEL_WAIT = 1, TL_CANCEL_DONE = 2 }; enum tl_end_mode { TL_STOP = 0, TL_REARM = 1 }; typedef struct tl_bank tl_bank; typedef struct { uint32_t timer_id; uint64_t generation; uint64_t ticket; } tl_fire; typedef struct { uint32_t timer_id; uint64_t generation; uint64_t serial; } tl_cancel; typedef struct { uint32_t timer_id; uint64_t generation; uint64_t deadline; uint64_t period; uint64_t tag; } tl_due; typedef struct { uint32_t phase; uint32_t cancel_state; uint64_t generation; uint64_t deadline; uint64_t period; uint64_t tag; uint64_t ticket; uint64_t cancel_generation; uint64_t cancel_serial; uint64_t arms; uint64_t begins; uint64_t ends; uint64_t rearms; uint64_t cancelled_armed; uint64_t cancelled_ready; } tl_timer_view; typedef struct { uint64_t now; uint32_t slots; uint32_t phase_count[5]; } tl_bank_view; tl_bank *tl_create(uint32_t slots); void tl_destroy(tl_bank *bank); int tl_advance(tl_bank *bank, uint64_t now); int tl_arm(tl_bank *bank, uint32_t id, uint64_t deadline, uint64_t period, uint64_t tag); int tl_next(tl_bank *bank, tl_due *out); int tl_take(tl_bank *bank, tl_fire *out); int tl_begin(tl_bank *bank, tl_fire fire); int tl_end(tl_bank *bank, tl_fire fire, uint32_t mode); int tl_cancel_sync(tl_bank *bank, uint32_t id, tl_cancel *out); int tl_cancel_poll(tl_bank *bank, tl_cancel cookie); int tl_view(tl_bank *bank, uint32_t id, tl_timer_view *out); int tl_bank_snapshot(tl_bank *bank, tl_bank_view *out); #endif