From: Jeff Garzik <jgarzik@xxxxxxxxxx> Move timer_init and friends so that they can be used by test/*. Signed-Off-By: Pete Zaitcev <zaitcev@xxxxxxxxxx> --- include/Makefile.am | 2 include/libtimer.h | 34 ++++++++++++++++ lib/.gitignore | 2 lib/Makefile.am | 2 lib/libtimer.c | 83 ++++++++++++++++++++++++++++++++++++++++ server/Makefile.am | 3 - server/cld.h | 25 ------------ server/util.c | 87 ------------------------------------------ 8 files changed, 125 insertions(+), 113 deletions(-) commit 92da6100e5803526ce8acc907c23a5dba3aee758 Author: Master <zaitcev@xxxxxxxxxxxxxxxxxx> Date: Sat Nov 28 23:12:31 2009 -0700 The libtimer by jgarzik. diff --git a/include/Makefile.am b/include/Makefile.am index 06e35a6..f7539dd 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -1,5 +1,5 @@ -EXTRA_DIST = cld-private.h +EXTRA_DIST = cld-private.h libtimer.h include_HEADERS = cldc.h cld_msg.h diff --git a/include/libtimer.h b/include/libtimer.h new file mode 100644 index 0000000..aedaea0 --- /dev/null +++ b/include/libtimer.h @@ -0,0 +1,34 @@ + +#ifndef __LIBTIMER_H__ +#define __LIBTIMER_H__ + +#include <stdbool.h> +#include <string.h> +#include <time.h> + +struct timer { + bool fired; + bool on_list; + void (*cb)(struct timer *); + void *userdata; + time_t expires; + char name[32]; +}; + +extern void timer_add(struct timer *timer, time_t expires); +extern void timer_del(struct timer *timer); +extern time_t timers_run(void); + +static inline void timer_init(struct timer *timer, const char *name, + void (*cb)(struct timer *), + void *userdata) +{ + memset(timer, 0, sizeof(*timer)); + timer->cb = cb; + timer->userdata = userdata; + strncpy(timer->name, name, sizeof(timer->name)); + timer->name[sizeof(timer->name) - 1] = 0; +} + +#endif /* __LIBTIMER_H__ */ + diff --git a/lib/.gitignore b/lib/.gitignore index 9eaa296..dddcc4c 100644 --- a/lib/.gitignore +++ b/lib/.gitignore @@ -6,5 +6,7 @@ libcldc.la libcldc-uninstalled.pc libcldc.pc +libtimer.a + .libs diff --git a/lib/Makefile.am b/lib/Makefile.am index 5b042ef..68be429 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -16,6 +16,8 @@ libcldc_la_LDFLAGS = \ -no-undefined \ -export-symbols-regex "^[^_].*" +noinst_LIBRARIES = libtimer.a + pkgconfigdir = $(libdir)/pkgconfig pkgconfig_DATA = libcldc.pc diff --git a/lib/libtimer.c b/lib/libtimer.c new file mode 100644 index 0000000..c1bcaf1 --- /dev/null +++ b/lib/libtimer.c @@ -0,0 +1,83 @@ + +#include <glib.h> +#include <libtimer.h> + +static GList *timer_list; + +static gint timer_cmp(gconstpointer a_, gconstpointer b_) +{ + const struct timer *a = a_; + const struct timer *b = b_; + + if (a->expires > b->expires) + return 1; + if (a->expires == b->expires) + return 0; + return -1; +} + +void timer_add(struct timer *timer, time_t expires) +{ + if (timer->on_list) + timer_list = g_list_remove(timer_list, timer); + + timer->on_list = true; + timer->fired = false; + timer->expires = expires; + + timer_list = g_list_insert_sorted(timer_list, timer, timer_cmp); +} + +void timer_del(struct timer *timer) +{ + if (!timer->on_list) + return; + + timer_list = g_list_remove(timer_list, timer); + + timer->on_list = false; +} + +time_t timers_run(void) +{ + struct timer *timer; + time_t now = time(NULL); + time_t next_timeout = 0; + GList *tmp, *cur; + GList *exec_list = NULL; + + tmp = timer_list; + while (tmp) { + timer = tmp->data; + cur = tmp; + tmp = tmp->next; + + if (timer->expires > now) + break; + + timer_list = g_list_remove_link(timer_list, cur); + exec_list = g_list_concat(exec_list, cur); + + timer->on_list = false; + } + + tmp = exec_list; + while (tmp) { + timer = tmp->data; + tmp = tmp->next; + + timer->fired = true; + timer->cb(timer); + } + + if (timer_list) { + timer = timer_list->data; + if (timer->expires > now) + next_timeout = (timer->expires - now); + else + next_timeout = 1; + } + + return next_timeout; +} + diff --git a/server/Makefile.am b/server/Makefile.am index 929a3fd..d099b4a 100644 --- a/server/Makefile.am +++ b/server/Makefile.am @@ -8,7 +8,8 @@ sbin_PROGRAMS = cld cldbadm cld_SOURCES = cldb.h cld.h \ ../lib/common.c \ cldb.c msg.c server.c session.c util.c -cld_LDADD = @CRYPTO_LIBS@ @GLIB_LIBS@ @DB4_LIBS@ @ARGP_LIBS@ +cld_LDADD = ../lib/libtimer.a \ + @CRYPTO_LIBS@ @GLIB_LIBS@ @DB4_LIBS@ @ARGP_LIBS@ cldbadm_SOURCES = cldb.h cldbadm.c cldbadm_LDADD = @CRYPTO_LIBS@ @GLIB_LIBS@ @DB4_LIBS@ @ARGP_LIBS@ diff --git a/server/cld.h b/server/cld.h index da26862..05c93ad 100644 --- a/server/cld.h +++ b/server/cld.h @@ -26,8 +26,8 @@ #include <glib.h> #include "cldb.h" #include <cld_msg.h> +#include <libtimer.h> -struct timer; struct client; struct session_outpkt; @@ -40,15 +40,6 @@ enum { SFL_FOREGROUND = (1 << 0), /* run in foreground */ }; -struct timer { - bool fired; - bool on_list; - void (*cb)(struct timer *); - void *userdata; - time_t expires; - char name[32]; -}; - struct client { struct sockaddr_in6 addr; /* inet address */ socklen_t addr_len; /* inet address len */ @@ -176,20 +167,6 @@ extern void applog(int prio, const char *fmt, ...); extern int write_pid_file(const char *pid_fn); extern void syslogerr(const char *prefix); extern int fsetflags(const char *prefix, int fd, int or_flags); -extern void timer_add(struct timer *timer, time_t expires); -extern void timer_del(struct timer *timer); -extern time_t timers_run(void); - -static inline void timer_init(struct timer *timer, const char *name, - void (*cb)(struct timer *), - void *userdata) -{ - memset(timer, 0, sizeof(*timer)); - timer->cb = cb; - timer->userdata = userdata; - strncpy(timer->name, name, sizeof(timer->name)); - timer->name[sizeof(timer->name) - 1] = 0; -} #ifndef HAVE_STRNLEN extern size_t strnlen(const char *s, size_t maxlen); diff --git a/server/util.c b/server/util.c index 3cd072f..36fa219 100644 --- a/server/util.c +++ b/server/util.c @@ -32,8 +32,6 @@ #include <syslog.h> #include "cld.h" -static GList *timer_list; - int write_pid_file(const char *pid_fn) { char str[32], *s; @@ -133,91 +131,6 @@ int fsetflags(const char *prefix, int fd, int or_flags) return rc; } -static gint timer_cmp(gconstpointer a_, gconstpointer b_) -{ - const struct timer *a = a_; - const struct timer *b = b_; - - if (a->expires > b->expires) - return 1; - if (a->expires == b->expires) - return 0; - return -1; -} - -void timer_add(struct timer *timer, time_t expires) -{ - if (timer->on_list) { - timer_list = g_list_remove(timer_list, timer); - - if (debugging) - applog(LOG_WARNING, "BUG? timer %s added twice " - "(expires: old %llu, new %llu)", - timer->name, - (unsigned long long) timer->expires, - (unsigned long long) expires); - } - - timer->on_list = true; - timer->fired = false; - timer->expires = expires; - - timer_list = g_list_insert_sorted(timer_list, timer, timer_cmp); -} - -void timer_del(struct timer *timer) -{ - if (!timer->on_list) - return; - - timer_list = g_list_remove(timer_list, timer); - - timer->on_list = false; -} - -time_t timers_run(void) -{ - struct timer *timer; - time_t now = time(NULL); - time_t next_timeout = 0; - GList *tmp, *cur; - GList *exec_list = NULL; - - tmp = timer_list; - while (tmp) { - timer = tmp->data; - cur = tmp; - tmp = tmp->next; - - if (timer->expires > now) - break; - - timer_list = g_list_remove_link(timer_list, cur); - exec_list = g_list_concat(exec_list, cur); - - timer->on_list = false; - } - - tmp = exec_list; - while (tmp) { - timer = tmp->data; - tmp = tmp->next; - - timer->fired = true; - timer->cb(timer); - } - - if (timer_list) { - timer = timer_list->data; - if (timer->expires > now) - next_timeout = (timer->expires - now); - else - next_timeout = 1; - } - - return next_timeout; -} - #ifndef HAVE_STRNLEN size_t strnlen(const char *s, size_t maxlen) { -- To unsubscribe from this list: send the line "unsubscribe hail-devel" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html