Skip to content

Commit

Permalink
lib: optimize time wheel code
Browse files Browse the repository at this point in the history
  • Loading branch information
pymumu committed Nov 29, 2023
1 parent 066c472 commit d094a70
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 36 deletions.
4 changes: 2 additions & 2 deletions src/dns_cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,13 @@ struct dns_cache_data *dns_cache_new_data_packet(void *packet, size_t packet_len
return (struct dns_cache_data *)cache_packet;
}

static void dns_cache_timer_release(struct tw_timer_list *timer, void *data)
static void dns_cache_timer_release(struct tw_base *base, struct tw_timer_list *timer, void *data)
{
struct dns_cache *dns_cache = data;
dns_cache_release(dns_cache);
}

static void dns_cache_expired(struct tw_timer_list *timer, void *data, unsigned long timestamp)
static void dns_cache_expired(struct tw_base *base, struct tw_timer_list *timer, void *data, unsigned long timestamp)
{
struct dns_cache *dns_cache = data;

Expand Down
11 changes: 9 additions & 2 deletions src/include/timer_wheel.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@

#include "list.h"

#ifdef __cplusplus
extern "C" {
#endif

struct tw_base;
struct tw_timer_list;

typedef void (*tw_func)(struct tw_timer_list *, void *, unsigned long);
typedef void (*tw_del_func)(struct tw_timer_list *, void *);
typedef void (*tw_func)(struct tw_base *, struct tw_timer_list *, void *, unsigned long);
typedef void (*tw_del_func)(struct tw_base *, struct tw_timer_list *, void *);

struct tw_timer_list {
void *data;
Expand All @@ -47,4 +51,7 @@ int tw_mod_timer_pending(struct tw_base *, struct tw_timer_list *, unsigned long

int tw_mod_timer(struct tw_base *, struct tw_timer_list *, unsigned long);

#ifdef __cplusplus
}
#endif
#endif
35 changes: 3 additions & 32 deletions src/lib/timer_wheel.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,31 +94,6 @@ static inline void _tw_add_timer(struct tw_base *base, struct tw_timer_list *tim
list_add_tail(&timer->entry, vec);
}

static inline unsigned long _apply_slack(struct tw_base *base, struct tw_timer_list *timer)
{
long delta;
unsigned long mask, expires, expires_limit;

expires = timer->expires;

delta = expires - base->jiffies;
if (delta < 256) {
return expires;
}

expires_limit = expires + delta / 256;
mask = expires ^ expires_limit;
if (mask == 0) {
return expires;
}

int bit = fls_long(mask);
mask = (1UL << bit) - 1;

expires_limit = expires_limit & ~(mask);
return expires_limit;
}

static inline void _tw_detach_timer(struct tw_timer_list *timer)
{
struct list_head *entry = &timer->entry;
Expand Down Expand Up @@ -184,7 +159,6 @@ void tw_add_timer(struct tw_base *base, struct tw_timer_list *timer)
pthread_spin_lock(&base->lock);
{
timer->expires += base->jiffies;
timer->expires = _apply_slack(base, timer);
_tw_add_timer(base, timer);
}
pthread_spin_unlock(&base->lock);
Expand All @@ -204,7 +178,7 @@ int tw_del_timer(struct tw_base *base, struct tw_timer_list *timer)
pthread_spin_unlock(&base->lock);

if (ret == 1 && timer->del_function) {
timer->del_function(timer, timer->data);
timer->del_function(base, timer, timer->data);
}

return ret;
Expand All @@ -217,8 +191,6 @@ int tw_mod_timer_pending(struct tw_base *base, struct tw_timer_list *timer, unsi
pthread_spin_lock(&base->lock);
{
timer->expires = expires + base->jiffies;
timer->expires = _apply_slack(base, timer);

ret = __mod_timer(base, timer, 1);
}
pthread_spin_unlock(&base->lock);
Expand All @@ -237,7 +209,6 @@ int tw_mod_timer(struct tw_base *base, struct tw_timer_list *timer, unsigned lon
}

timer->expires = expires + base->jiffies;
timer->expires = _apply_slack(base, timer);

ret = __mod_timer(base, timer, 0);
}
Expand Down Expand Up @@ -305,13 +276,13 @@ static inline void run_timers(struct tw_base *base)
_tw_detach_timer(timer);
pthread_spin_unlock(&base->lock);
{
fn(timer, data, call_time);
fn(base, timer, data, call_time);
}

pthread_spin_lock(&base->lock);
if ((timer_pending(timer) == 0 && timer->del_function)) {
pthread_spin_unlock(&base->lock);
timer->del_function(timer, timer->data);
timer->del_function(base, timer, timer->data);
pthread_spin_lock(&base->lock);
}
}
Expand Down

0 comments on commit d094a70

Please sign in to comment.