diff options
| author | william <[email protected]> | 2014-02-02 19:06:31 -0800 |
|---|---|---|
| committer | william <[email protected]> | 2014-02-02 19:06:31 -0800 |
| commit | 273e79a56d362e408d8cdf3f23cc72fa592ab5aa (patch) | |
| tree | 0a28e257239c2219b0a46f36f45eadbbb42855ae | |
| parent | 7a7f63a81e6739f7e6a7168d67e8a641c0be694d (diff) | |
fix some issues
| -rw-r--r-- | bench-llrb.c | 19 | ||||
| -rw-r--r-- | bench.c | 20 |
2 files changed, 23 insertions, 16 deletions
diff --git a/bench-llrb.c b/bench-llrb.c index f72ee05..bdb02f0 100644 --- a/bench-llrb.c +++ b/bench-llrb.c @@ -316,16 +316,17 @@ struct rbtimeouts { static int timeoutcmp(struct rbtimeout *a, struct rbtimeout *b) { - if (a->expires < b->expires) + if (a->expires < b->expires) { return -1; - else if (a->expires > b->expires) + } else if (a->expires > b->expires) { return 1; - else if (a < b) + } else if (a < b) { return -1; - else if (a > b) + } else if (a > b) { return 1; - else + } else { return 0; + } } /* timeoutcmp() */ LLRB_GENERATE_STATIC(tree, rbtimeout, rbe, timeoutcmp) @@ -335,6 +336,7 @@ static void *init(struct timeout *timeout, size_t count, int verbose) { size_t i; T = malloc(sizeof *T); + T->curtime = 0; LLRB_INIT(&T->tree); for (i = 0; i < count; i++) { @@ -351,9 +353,12 @@ static void add(void *ctx, struct timeout *_to, timeout_t expires) { struct rbtimeouts *T = ctx; struct rbtimeout *to = (void *)_to; + if (to->pending) + LLRB_REMOVE(tree, &T->tree, to); + to->expires = T->curtime + expires; LLRB_INSERT(tree, &T->tree, to); - to->pending = 0; + to->pending = 1; } /* add() */ @@ -363,6 +368,7 @@ static void del(void *ctx, struct timeout *_to) { LLRB_REMOVE(tree, &T->tree, to); to->pending = 0; + to->expires = 0; } /* del() */ @@ -373,6 +379,7 @@ static struct timeout *get(void *ctx) { if ((to = LLRB_MIN(tree, &T->tree)) && to->expires <= T->curtime) { LLRB_REMOVE(tree, &T->tree, to); to->pending = 0; + to->expires = 0; return (void *)to; } @@ -29,19 +29,19 @@ struct bench { struct benchops ops; timeout_t curtime; -#if __APPLE__ - mach_timebase_info_data_t timebase; -#endif }; /* struct bench */ +#if __APPLE__ +static mach_timebase_info_data_t timebase; +#endif + static int bench_clock(lua_State *L) { #if __APPLE__ - struct bench *B = lua_touserdata(L, 1); unsigned long long abt; abt = mach_absolute_time(); - abt = abt * B->timebase.numer / B->timebase.denom; + abt = abt * timebase.numer / timebase.denom; lua_pushnumber(L, (double)abt / 1000000000L); #else @@ -67,10 +67,6 @@ static int bench_new(lua_State *L) { B = lua_newuserdata(L, sizeof *B); memset(B, 0, sizeof *B); -#if __APPLE__ - mach_timebase_info(&B->timebase); -#endif - luaL_getmetatable(L, "BENCH*"); lua_setmetatable(L, -2); @@ -160,7 +156,7 @@ static int bench__gc(lua_State *L) { } return 0; -} /* bench_expire() */ +} /* bench__gc() */ static const luaL_Reg bench_methods[] = { @@ -184,6 +180,10 @@ static const luaL_Reg bench_globals[] = { }; int luaopen_bench(lua_State *L) { +#if __APPLE__ + mach_timebase_info(&timebase); +#endif + if (luaL_newmetatable(L, "BENCH*")) { luaL_register(L, NULL, bench_metatable); lua_newtable(L); |
