Commit 273e79a5 authored by william's avatar william

fix some issues

parent 7a7f63a8
......@@ -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);
......
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