Commit 896a50f1 authored by william's avatar william

use timeouts_ prefix instead of timer_, which is in the POSIX namespace (they...

use timeouts_ prefix instead of timer_, which is in the POSIX namespace (they get all the good prefixes!
parent 7d60763b
......@@ -3,10 +3,26 @@ all: timer
WHEEL_BIT = 6
WHEEL_NUM = 4
CPPFLAGS = -DTIMER_DEBUG -DTIMER_MAIN -DWHEEL_BIT=$(WHEEL_BIT) -DWHEEL_NUM=$(WHEEL_NUM)
CPPFLAGS = -DTIMER_DEBUG -DTIMER_MAIN
CFLAGS = -O2 -g -Wall -Wextra
timer: CPPFLAGS+=-DWHEEL_BIT=$(WHEEL_BIT) -DWHEEL_NUM=$(WHEEL_NUM)
timer8: CPPFLAGS+=-DWHEEL_BIT=3 -DWHEEL_NUM=$(WHEEL_NUM)
timer16: CPPFLAGS+=-DWHEEL_BIT=4 -DWHEEL_NUM=$(WHEEL_NUM)
timer32: CPPFLAGS+=-DWHEEL_BIT=5 -DWHEEL_NUM=$(WHEEL_NUM)
timer64: CPPFLAGS+=-DWHEEL_BIT=6 -DWHEEL_NUM=$(WHEEL_NUM)
timer64 timer32 timer16 timer8 timer: timer.c
$(CC) $(CFLAGS) -o $@ $^ $(CPPFLAGS)
.PHONY: clean clean!
clean:
......
/*
* D E B U G R O U T I N E S
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#if TIMER_DEBUG - 0 || TIMER_MAIN - 0
#include <stdlib.h>
#include <stdio.h>
#undef TIMER_DEBUG
#define TIMER_DEBUG 1
#define DEBUG_LEVEL timer_debug
static int timer_debug;
#define SAYit_(lvl, fmt, ...) do { \
if (DEBUG_LEVEL >= (lvl)) \
fprintf(stderr, fmt "%s", __FILE__, __LINE__, __func__, __VA_ARGS__); \
} while (0)
#define SAYit(lvl, ...) SAYit_((lvl), "%s:%d:%s: " __VA_ARGS__, "\n")
#define PANIC(...) do { \
SAYit(0, __VA_ARGS__); \
_Exit(EXIT_FAILURE); \
} while (0)
#else
#undef TIMER_DEBUG
#define TIMER_DEBUG 0
#define DEBUG_LEVEL 0
#define SAYit(...) (void)0
#endif
#define SAY(...) SAYit(1, __VA_ARGS__)
#define HAI SAY("HAI")
static inline char *fmt_(char *buf, uint64_t ts, int wheel_bit, int wheel_num) {
char *p = buf;
int wheel, n, i;
for (wheel = wheel_num - 2; wheel >= 0; wheel--) {
n = ((1 << wheel_bit) - 1) & (ts >> (wheel * WHEEL_BIT));
for (i = wheel_bit - 1; i >= 0; i--) {
*p++ = '0' + !!(n & (1 << i));
}
if (wheel != 0)
*p++ = ':';
}
*p = 0;
return buf;
} /* fmt_() */
#define fmt(ts) fmt_(((char[((1 << WHEEL_BIT) * WHEEL_NUM) + WHEEL_NUM + 1]){ 0 }), (ts), WHEEL_BIT, WHEEL_NUM)
static inline char *bin64_(char *buf, uint64_t n, int wheel_bit) {
char *p = buf;
int i;
for (i = 0; i < (1 << wheel_bit); i++) {
*p++ = "01"[0x1 & (n >> (((1 << wheel_bit) - 1) - i))];
}
*p = 0;
return buf;
} /* bin64_() */
#define bin64(ts) bin64_(((char[((1 << WHEEL_BIT) * WHEEL_NUM) + 1]){ 0 }), (ts), WHEEL_BIT)
This diff is collapsed.
......@@ -35,36 +35,31 @@
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#define TIMER_mHZ TIMER_C(1000)
#define TIMER_uHZ TIMER_C(1000000)
#define TIMER_nHZ TIMER_C(1000000000)
#define TIMEOUT_C(n) UINT64_C(n)
#define TIMEOUT_PRIu PRIu64
#define TIMEOUT_PRIx PRIx64
#define TIMEOUT_PRIX PRIX64
#define TIMER_C(n) UINT64_C(n)
#define TIMER_PRIu PRIu64
#define TIMER_PRIx PRIx64
#define TIMER_PRIX PRIX64
#define TIMEOUT_mHZ TIMEOUT_C(1000)
#define TIMEOUT_uHZ TIMEOUT_C(1000000)
#define TIMEOUT_nHZ TIMEOUT_C(1000000000)
#define TIMEOUT_C(n) TIMER_C(n)
#define TIMEOUT_PRIu TIMER_PRIu
#define TIMEOUT_PRIx TIMER_PRIx
#define TIMEOUT_PRIX TIMER_PRIX
typedef uint64_t timeout_t;
typedef uint64_t timer_t; /* absolute times */
typedef timer_t timeout_t; /* relative times */
struct timer;
struct timeouts;
struct timeout;
void timer_add(struct timer *, struct timeout *, timeout_t);
void timer_del(struct timer *, struct timeout *);
struct timeouts *timeouts_open(timeout_t);
bool timer_pending(struct timer *);
void timeouts_close(struct timeouts *);
timeout_t timer_timeout(struct timer *);
void timeouts_add(struct timeouts *, struct timeout *, timeout_t);
void timeouts_del(struct timeouts *, struct timeout *);
bool timeouts_pending(struct timeouts *);
timeout_t timeouts_timeout(struct timeouts *);
/*
......@@ -73,13 +68,14 @@ timeout_t timer_timeout(struct timer *);
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#define TIMEOUT_PERIODIC 0x01
#define TIMEOUT_ABSTIME 0x02
struct timeout *timeout_init(struct timeout *, int);
bool timeout_pending(struct timeout *);
/* true if pending in a timing wheel or on expired queue, false otherwise */
/* true if on timing wheel or expired queue, false otherwise */
bool timeout_expired(struct timeout *);
/* true if currently or previously on expired queue, false otherwise */
/* true if on expired queue, false otherwise */
#endif /* TIMER_H */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment