// twheel.h — Tickless hierarchical timing wheel (adapted from timeout.h) // Original: Copyright (c) 2013-2014 William Ahern, MIT license. #ifndef TWHEEL_H #define TWHEEL_H #include #include #include #include #define TWHEEL_VERSION 0x20160226 typedef uint64_t twheel_t; #define TWHEEL_C(n) UINT64_C(n) #define TWHEEL_mHZ TWHEEL_C(1000) #define TWHEEL_MAX ((TWHEEL_C(1) << 24) - 1) enum twheel_flags { TWHEEL_INT = 0x01, TWHEEL_ABS = 0x02, }; #define TWHEEL_INITIALIZER(flags) { (flags) } struct twheel { int flags; twheel_t expires; struct twheel_list *pending; TAILQ_ENTRY(twheel) tqe; twheel_t interval; }; TAILQ_HEAD(twheel_list, twheel); struct twheels { struct twheel_list wheel[4][64], expired; uint64_t pending[4]; twheel_t curtime; twheel_t hertz; }; struct twheel *twheel_init(struct twheel *, int flags); bool twheel_pending(struct twheel *); struct twheels *twheels_open(twheel_t hz); void twheels_close(struct twheels *); twheel_t twheels_hz(struct twheels *); void twheels_update(struct twheels *, twheel_t curtime); twheel_t twheels_timeout(struct twheels *); void twheels_add(struct twheels *, struct twheel *, twheel_t timeout); void twheels_del(struct twheels *, struct twheel *); struct twheel *twheels_get(struct twheels *); bool twheels_pending(struct twheels *); bool twheels_expired(struct twheels *); #endif