diff options
author | /C=EU/ST=EU/CN=Pablo Neira Ayuso/emailAddress=pablo@netfilter.org </C=EU/ST=EU/CN=Pablo Neira Ayuso/emailAddress=pablo@netfilter.org> | 2008-01-14 15:22:24 +0000 |
---|---|---|
committer | /C=EU/ST=EU/CN=Pablo Neira Ayuso/emailAddress=pablo@netfilter.org </C=EU/ST=EU/CN=Pablo Neira Ayuso/emailAddress=pablo@netfilter.org> | 2008-01-14 15:22:24 +0000 |
commit | 6e5b6c91625fd431ac3d1339f55a4aa278ff2604 (patch) | |
tree | 79e087eebf878ff86bc4dd74292c97748f593b15 /src | |
parent | 94aa8b70f2e8cf7cf664d7c0e86a9ebc287010ef (diff) | |
download | conntrack-tools-6e5b6c91625fd431ac3d1339f55a4aa278ff2604.tar.gz conntrack-tools-6e5b6c91625fd431ac3d1339f55a4aa278ff2604.zip |
improve alarm framework based on suggestions from Max Duempel
Diffstat (limited to 'src')
-rw-r--r-- | src/alarm.c | 33 | ||||
-rw-r--r-- | src/cache_timer.c | 2 | ||||
-rw-r--r-- | src/run.c | 15 | ||||
-rw-r--r-- | src/sync-alarm.c | 10 | ||||
-rw-r--r-- | src/sync-ftfw.c | 4 |
5 files changed, 35 insertions, 29 deletions
diff --git a/src/alarm.c b/src/alarm.c index e1d2e24..eb2226b 100644 --- a/src/alarm.c +++ b/src/alarm.c @@ -28,16 +28,6 @@ static LIST_HEAD(alarm_list); -void set_alarm_expiration_secs(struct alarm_list *t, unsigned long expires) -{ - t->tv.tv_sec = expires; -} - -void set_alarm_expiration_usecs(struct alarm_list *t, unsigned long expires) -{ - t->tv.tv_usec = expires; -} - void set_alarm_function(struct alarm_list *t, void (*fcn)(struct alarm_list *a, void *data)) { @@ -51,8 +41,6 @@ void set_alarm_data(struct alarm_list *t, void *data) void init_alarm(struct alarm_list *t) { - INIT_LIST_HEAD(&t->head); - timerclear(&t->tv); t->data = 0; t->function = NULL; @@ -77,14 +65,26 @@ void mod_alarm(struct alarm_list *alarm, unsigned long sc, unsigned long usc) struct timeval tv; list_del(&alarm->head); - INIT_LIST_HEAD(&alarm->head); gettimeofday(&tv, NULL); alarm->tv.tv_sec = tv.tv_sec + sc; alarm->tv.tv_usec = tv.tv_usec + usc; list_add_tail(&alarm->head, &alarm_list); } -void do_alarm_run(struct timeval *next_alarm) +int get_next_alarm(struct timeval *tv, struct timeval *next_alarm) +{ + struct list_head *i; + struct alarm_list *t; + + list_for_each(i, &alarm_list) { + t = (struct alarm_list *) i; + timersub(&t->tv, tv, next_alarm); + return 1; + } + return 0; +} + +int do_alarm_run(struct timeval *next_alarm) { struct list_head *i, *tmp; struct alarm_list *t; @@ -97,10 +97,13 @@ void do_alarm_run(struct timeval *next_alarm) if (timercmp(&t->tv, &tv, >)) { timersub(&t->tv, &tv, next_alarm); - break; + return 1; } del_alarm(t); t->function(t, t->data); } + + /* check for refreshed alarms to get the next one */ + return get_next_alarm(&tv, next_alarm); } diff --git a/src/cache_timer.c b/src/cache_timer.c index c0075f5..02f3ae4 100644 --- a/src/cache_timer.c +++ b/src/cache_timer.c @@ -36,7 +36,7 @@ static void timer_add(struct us_conntrack *u, void *data) struct alarm_list *alarm = data; init_alarm(alarm); - set_alarm_expiration_secs(alarm, CONFIG(cache_timeout)); + set_alarm_expiration(alarm, CONFIG(cache_timeout), 0); set_alarm_data(alarm, u); set_alarm_function(alarm, timeout); add_alarm(alarm); @@ -224,19 +224,20 @@ static int __run(struct timeval *next_alarm) void run(void) { - struct timeval next_alarm = { - .tv_sec = 1, - .tv_usec = 0 - }; + struct timeval next_alarm; struct timeval *next = &next_alarm; + struct timeval tv; - if (CONFIG(flags) & CTD_STATS_MODE) - next = NULL; + /* initialization: get the first alarm available */ + gettimeofday(&tv, NULL); + get_next_alarm(&tv, next); while(1) { if (__run(next)) { sigprocmask(SIG_BLOCK, &STATE(block), NULL); - do_alarm_run(next); + next = &next_alarm; + if (!do_alarm_run(next)) + next = NULL; /* no next alarms */ sigprocmask(SIG_UNBLOCK, &STATE(block), NULL); } } diff --git a/src/sync-alarm.c b/src/sync-alarm.c index 632eff2..3d20867 100644 --- a/src/sync-alarm.c +++ b/src/sync-alarm.c @@ -31,8 +31,9 @@ static void refresher(struct alarm_list *a, void *data) debug_ct(u->ct, "persistence update"); init_alarm(a); - set_alarm_expiration_secs(a, random() % CONFIG(refresh) + 1); - set_alarm_expiration_usecs(a, random() % 999999 + 1); + set_alarm_expiration(a, + random() % CONFIG(refresh) + 1, + random() % 999999 + 1); set_alarm_data(a, u); set_alarm_function(a, refresher); @@ -48,8 +49,9 @@ static void cache_alarm_add(struct us_conntrack *u, void *data) struct alarm_list *alarm = data; init_alarm(alarm); - set_alarm_expiration_secs(alarm, random() % CONFIG(refresh) + 1); - set_alarm_expiration_usecs(alarm, random() % 999999 + 1); + set_alarm_expiration(alarm, + random() % CONFIG(refresh) + 1, + random() % 999999 + 1); set_alarm_data(alarm, u); set_alarm_function(alarm, refresher); add_alarm(alarm); diff --git a/src/sync-ftfw.c b/src/sync-ftfw.c index ac1b8b6..125e82e 100644 --- a/src/sync-ftfw.c +++ b/src/sync-ftfw.c @@ -88,7 +88,7 @@ static void do_alive_alarm(struct alarm_list *a, void *data) tx_queue_add_ctlmsg(NET_F_ALIVE, 0, 0); init_alarm(&alive_alarm); - set_alarm_expiration_secs(&alive_alarm, 1); + set_alarm_expiration(&alive_alarm, 1, 0); set_alarm_function(&alive_alarm, do_alive_alarm); add_alarm(&alive_alarm); } @@ -112,7 +112,7 @@ static int ftfw_init() /* XXX: alive message expiration configurable */ init_alarm(&alive_alarm); - set_alarm_expiration_secs(&alive_alarm, 1); + set_alarm_expiration(&alive_alarm, 1, 0); set_alarm_function(&alive_alarm, do_alive_alarm); add_alarm(&alive_alarm); |