Add session timer

This commit is contained in:
luwenpeng
2023-12-12 18:41:53 +08:00
parent 006315fb7c
commit 2d3e182b5a
11 changed files with 612 additions and 234 deletions

View File

@@ -26,13 +26,12 @@
#ifndef TIMEOUT_H
#define TIMEOUT_H
#include <stdbool.h> /* bool */
#include <stdio.h> /* FILE */
#include <stdbool.h> /* bool */
#include <stdio.h> /* FILE */
#include <inttypes.h> /* PRIu64 PRIx64 PRIX64 uint64_t */
#include <sys/queue.h> /* TAILQ(3) */
#include <inttypes.h> /* PRIu64 PRIx64 PRIX64 uint64_t */
#include <sys/queue.h> /* TAILQ(3) */
/*
* V E R S I O N I N T E R F A C E S
@@ -44,7 +43,7 @@
#endif
#define TIMEOUT_VERSION TIMEOUT_V_REL
#define TIMEOUT_VENDOR "william@25thandClement.com"
#define TIMEOUT_VENDOR "william@25thandClement.com"
#define TIMEOUT_V_REL 0x20160226
#define TIMEOUT_V_ABI 0x20160224
@@ -60,7 +59,6 @@ TIMEOUT_PUBLIC int timeout_v_abi(void);
TIMEOUT_PUBLIC int timeout_v_api(void);
/*
* I N T E G E R T Y P E I N T E R F A C E S
*
@@ -79,7 +77,6 @@ typedef uint64_t timeout_t;
#define timeout_error_t int /* for documentation purposes */
/*
* C A L L B A C K I N T E R F A C E
*
@@ -89,7 +86,8 @@ typedef uint64_t timeout_t;
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#ifndef TIMEOUT_CB_OVERRIDE
struct timeout_cb {
struct timeout_cb
{
void (*fn)();
void *arg;
}; /* struct timeout_cb */
@@ -105,14 +103,20 @@ struct timeout_cb {
#endif
#define TIMEOUT_ABS 0x02 /* treat timeout values as absolute */
#define TIMEOUT_INITIALIZER(flags) { (flags) }
#define TIMEOUT_INITIALIZER(flags) \
{ \
(flags) \
}
#define timeout_setcb(to, fn, arg) do { \
(to)->callback.fn = (fn); \
(to)->callback.arg = (arg); \
} while (0)
#define timeout_setcb(to, _fn, _arg) \
do \
{ \
(to)->callback.fn = (_fn); \
(to)->callback.arg = (_arg); \
} while (0)
struct timeout {
struct timeout
{
int flags;
timeout_t expires;
@@ -121,7 +125,8 @@ struct timeout {
struct timeout_list *pending;
/* timeout list if pending on wheel or expiry queue */
TAILQ_ENTRY(timeout) tqe;
TAILQ_ENTRY(timeout)
tqe;
/* entry member for struct timeout_list lists */
#ifndef TIMEOUT_DISABLE_CALLBACKS
@@ -140,14 +145,13 @@ struct timeout {
#endif
}; /* struct timeout */
TIMEOUT_PUBLIC struct timeout *timeout_init(struct timeout *, int);
/* initialize timeout structure (same as TIMEOUT_INITIALIZER) */
#ifndef TIMEOUT_DISABLE_RELATIVE_ACCESS
TIMEOUT_PUBLIC bool timeout_pending(struct timeout *);
/* true if on timing wheel, false otherwise */
TIMEOUT_PUBLIC bool timeout_expired(struct timeout *);
/* true if on expired queue, false otherwise */
@@ -200,17 +204,23 @@ TIMEOUT_PUBLIC bool timeouts_check(struct timeouts *, FILE *);
#define TIMEOUTS_PENDING 0x10
#define TIMEOUTS_EXPIRED 0x20
#define TIMEOUTS_ALL (TIMEOUTS_PENDING|TIMEOUTS_EXPIRED)
#define TIMEOUTS_CLEAR 0x40
#define TIMEOUTS_ALL (TIMEOUTS_PENDING | TIMEOUTS_EXPIRED)
#define TIMEOUTS_CLEAR 0x40
#define TIMEOUTS_IT_INITIALIZER(flags) { (flags), 0, 0, 0, 0 }
#define TIMEOUTS_IT_INITIALIZER(flags) \
{ \
(flags), 0, 0, 0, 0 \
}
#define TIMEOUTS_IT_INIT(cur, _flags) do { \
(cur)->flags = (_flags); \
(cur)->pc = 0; \
} while (0)
#define TIMEOUTS_IT_INIT(cur, _flags) \
do \
{ \
(cur)->flags = (_flags); \
(cur)->pc = 0; \
} while (0)
struct timeouts_it {
struct timeouts_it
{
int flags;
unsigned pc, i, j;
struct timeout *to;
@@ -223,11 +233,10 @@ TIMEOUT_PUBLIC struct timeout *timeouts_next(struct timeouts *, struct timeouts_
* could invalidate cursor state and trigger a use-after-free.
*/
#define TIMEOUTS_FOREACH(var, T, flags) \
struct timeouts_it _it = TIMEOUTS_IT_INITIALIZER((flags)); \
#define TIMEOUTS_FOREACH(var, T, flags) \
struct timeouts_it _it = TIMEOUTS_IT_INITIALIZER((flags)); \
while (((var) = timeouts_next((T), &_it)))
/*
* B O N U S W H E E L I N T E R F A C E S
*