Commit 273e79a5 authored by william's avatar william

fix some issues

parent 7a7f63a8
...@@ -316,16 +316,17 @@ struct rbtimeouts { ...@@ -316,16 +316,17 @@ struct rbtimeouts {
static int timeoutcmp(struct rbtimeout *a, struct rbtimeout *b) { static int timeoutcmp(struct rbtimeout *a, struct rbtimeout *b) {
if (a->expires < b->expires) if (a->expires < b->expires) {
return -1; return -1;
else if (a->expires > b->expires) } else if (a->expires > b->expires) {
return 1; return 1;
else if (a < b) } else if (a < b) {
return -1; return -1;
else if (a > b) } else if (a > b) {
return 1; return 1;
else } else {
return 0; return 0;
}
} /* timeoutcmp() */ } /* timeoutcmp() */
LLRB_GENERATE_STATIC(tree, rbtimeout, rbe, timeoutcmp) LLRB_GENERATE_STATIC(tree, rbtimeout, rbe, timeoutcmp)
...@@ -335,6 +336,7 @@ static void *init(struct timeout *timeout, size_t count, int verbose) { ...@@ -335,6 +336,7 @@ static void *init(struct timeout *timeout, size_t count, int verbose) {
size_t i; size_t i;
T = malloc(sizeof *T); T = malloc(sizeof *T);
T->curtime = 0;
LLRB_INIT(&T->tree); LLRB_INIT(&T->tree);
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
...@@ -351,9 +353,12 @@ static void add(void *ctx, struct timeout *_to, timeout_t expires) { ...@@ -351,9 +353,12 @@ static void add(void *ctx, struct timeout *_to, timeout_t expires) {
struct rbtimeouts *T = ctx; struct rbtimeouts *T = ctx;
struct rbtimeout *to = (void *)_to; struct rbtimeout *to = (void *)_to;
if (to->pending)
LLRB_REMOVE(tree, &T->tree, to);
to->expires = T->curtime + expires; to->expires = T->curtime + expires;
LLRB_INSERT(tree, &T->tree, to); LLRB_INSERT(tree, &T->tree, to);
to->pending = 0; to->pending = 1;
} /* add() */ } /* add() */
...@@ -363,6 +368,7 @@ static void del(void *ctx, struct timeout *_to) { ...@@ -363,6 +368,7 @@ static void del(void *ctx, struct timeout *_to) {
LLRB_REMOVE(tree, &T->tree, to); LLRB_REMOVE(tree, &T->tree, to);
to->pending = 0; to->pending = 0;
to->expires = 0;
} /* del() */ } /* del() */
...@@ -373,6 +379,7 @@ static struct timeout *get(void *ctx) { ...@@ -373,6 +379,7 @@ static struct timeout *get(void *ctx) {
if ((to = LLRB_MIN(tree, &T->tree)) && to->expires <= T->curtime) { if ((to = LLRB_MIN(tree, &T->tree)) && to->expires <= T->curtime) {
LLRB_REMOVE(tree, &T->tree, to); LLRB_REMOVE(tree, &T->tree, to);
to->pending = 0; to->pending = 0;
to->expires = 0;
return (void *)to; return (void *)to;
} }
......
...@@ -29,19 +29,19 @@ struct bench { ...@@ -29,19 +29,19 @@ struct bench {
struct benchops ops; struct benchops ops;
timeout_t curtime; timeout_t curtime;
#if __APPLE__
mach_timebase_info_data_t timebase;
#endif
}; /* struct bench */ }; /* struct bench */
#if __APPLE__
static mach_timebase_info_data_t timebase;
#endif
static int bench_clock(lua_State *L) { static int bench_clock(lua_State *L) {
#if __APPLE__ #if __APPLE__
struct bench *B = lua_touserdata(L, 1);
unsigned long long abt; unsigned long long abt;
abt = mach_absolute_time(); abt = mach_absolute_time();
abt = abt * B->timebase.numer / B->timebase.denom; abt = abt * timebase.numer / timebase.denom;
lua_pushnumber(L, (double)abt / 1000000000L); lua_pushnumber(L, (double)abt / 1000000000L);
#else #else
...@@ -67,10 +67,6 @@ static int bench_new(lua_State *L) { ...@@ -67,10 +67,6 @@ static int bench_new(lua_State *L) {
B = lua_newuserdata(L, sizeof *B); B = lua_newuserdata(L, sizeof *B);
memset(B, 0, sizeof *B); memset(B, 0, sizeof *B);
#if __APPLE__
mach_timebase_info(&B->timebase);
#endif
luaL_getmetatable(L, "BENCH*"); luaL_getmetatable(L, "BENCH*");
lua_setmetatable(L, -2); lua_setmetatable(L, -2);
...@@ -160,7 +156,7 @@ static int bench__gc(lua_State *L) { ...@@ -160,7 +156,7 @@ static int bench__gc(lua_State *L) {
} }
return 0; return 0;
} /* bench_expire() */ } /* bench__gc() */
static const luaL_Reg bench_methods[] = { static const luaL_Reg bench_methods[] = {
...@@ -184,6 +180,10 @@ static const luaL_Reg bench_globals[] = { ...@@ -184,6 +180,10 @@ static const luaL_Reg bench_globals[] = {
}; };
int luaopen_bench(lua_State *L) { int luaopen_bench(lua_State *L) {
#if __APPLE__
mach_timebase_info(&timebase);
#endif
if (luaL_newmetatable(L, "BENCH*")) { if (luaL_newmetatable(L, "BENCH*")) {
luaL_register(L, NULL, bench_metatable); luaL_register(L, NULL, bench_metatable);
lua_newtable(L); lua_newtable(L);
......
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