lib: optimize time wheel code
This commit is contained in:
@@ -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;
|
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;
|
struct dns_cache *dns_cache = data;
|
||||||
dns_cache_release(dns_cache);
|
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;
|
struct dns_cache *dns_cache = data;
|
||||||
|
|
||||||
|
|||||||
@@ -21,11 +21,15 @@
|
|||||||
|
|
||||||
#include "list.h"
|
#include "list.h"
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
struct tw_base;
|
struct tw_base;
|
||||||
struct tw_timer_list;
|
struct tw_timer_list;
|
||||||
|
|
||||||
typedef void (*tw_func)(struct tw_timer_list *, void *, unsigned long);
|
typedef void (*tw_func)(struct tw_base *, struct tw_timer_list *, void *, unsigned long);
|
||||||
typedef void (*tw_del_func)(struct tw_timer_list *, void *);
|
typedef void (*tw_del_func)(struct tw_base *, struct tw_timer_list *, void *);
|
||||||
|
|
||||||
struct tw_timer_list {
|
struct tw_timer_list {
|
||||||
void *data;
|
void *data;
|
||||||
@@ -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);
|
int tw_mod_timer(struct tw_base *, struct tw_timer_list *, unsigned long);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -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);
|
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)
|
static inline void _tw_detach_timer(struct tw_timer_list *timer)
|
||||||
{
|
{
|
||||||
struct list_head *entry = &timer->entry;
|
struct list_head *entry = &timer->entry;
|
||||||
@@ -184,7 +159,6 @@ void tw_add_timer(struct tw_base *base, struct tw_timer_list *timer)
|
|||||||
pthread_spin_lock(&base->lock);
|
pthread_spin_lock(&base->lock);
|
||||||
{
|
{
|
||||||
timer->expires += base->jiffies;
|
timer->expires += base->jiffies;
|
||||||
timer->expires = _apply_slack(base, timer);
|
|
||||||
_tw_add_timer(base, timer);
|
_tw_add_timer(base, timer);
|
||||||
}
|
}
|
||||||
pthread_spin_unlock(&base->lock);
|
pthread_spin_unlock(&base->lock);
|
||||||
@@ -204,7 +178,7 @@ int tw_del_timer(struct tw_base *base, struct tw_timer_list *timer)
|
|||||||
pthread_spin_unlock(&base->lock);
|
pthread_spin_unlock(&base->lock);
|
||||||
|
|
||||||
if (ret == 1 && timer->del_function) {
|
if (ret == 1 && timer->del_function) {
|
||||||
timer->del_function(timer, timer->data);
|
timer->del_function(base, timer, timer->data);
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
@@ -217,8 +191,6 @@ int tw_mod_timer_pending(struct tw_base *base, struct tw_timer_list *timer, unsi
|
|||||||
pthread_spin_lock(&base->lock);
|
pthread_spin_lock(&base->lock);
|
||||||
{
|
{
|
||||||
timer->expires = expires + base->jiffies;
|
timer->expires = expires + base->jiffies;
|
||||||
timer->expires = _apply_slack(base, timer);
|
|
||||||
|
|
||||||
ret = __mod_timer(base, timer, 1);
|
ret = __mod_timer(base, timer, 1);
|
||||||
}
|
}
|
||||||
pthread_spin_unlock(&base->lock);
|
pthread_spin_unlock(&base->lock);
|
||||||
@@ -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 = expires + base->jiffies;
|
||||||
timer->expires = _apply_slack(base, timer);
|
|
||||||
|
|
||||||
ret = __mod_timer(base, timer, 0);
|
ret = __mod_timer(base, timer, 0);
|
||||||
}
|
}
|
||||||
@@ -305,13 +276,13 @@ static inline void run_timers(struct tw_base *base)
|
|||||||
_tw_detach_timer(timer);
|
_tw_detach_timer(timer);
|
||||||
pthread_spin_unlock(&base->lock);
|
pthread_spin_unlock(&base->lock);
|
||||||
{
|
{
|
||||||
fn(timer, data, call_time);
|
fn(base, timer, data, call_time);
|
||||||
}
|
}
|
||||||
|
|
||||||
pthread_spin_lock(&base->lock);
|
pthread_spin_lock(&base->lock);
|
||||||
if ((timer_pending(timer) == 0 && timer->del_function)) {
|
if ((timer_pending(timer) == 0 && timer->del_function)) {
|
||||||
pthread_spin_unlock(&base->lock);
|
pthread_spin_unlock(&base->lock);
|
||||||
timer->del_function(timer, timer->data);
|
timer->del_function(base, timer, timer->data);
|
||||||
pthread_spin_lock(&base->lock);
|
pthread_spin_lock(&base->lock);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user