From: Pete Zaitcev <zaitcev@redhat.com>
To: Jeff Garzik <jeff@garzik.org>
Cc: Project Hail List <hail-devel@vger.kernel.org>
Subject: [Patch 1/2] CLD: factor timers out into a library
Date: Sat, 28 Nov 2009 23:35:05 -0700 [thread overview]
Message-ID: <20091128233505.6d02b131@redhat.com> (raw)
From: Jeff Garzik <jgarzik@redhat.com>
Move timer_init and friends so that they can be used by test/*.
Signed-Off-By: Pete Zaitcev <zaitcev@redhat.com>
---
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@lembas.zaitcev.lan>
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)
{
next reply other threads:[~2009-11-29 6:35 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-11-29 6:35 Pete Zaitcev [this message]
2009-11-29 10:47 ` [Patch 1/2] CLD: factor timers out into a library Jeff Garzik
2009-11-29 11:11 ` Jeff Garzik
2009-11-29 19:34 ` Pete Zaitcev
2009-11-29 20:36 ` Jeff Garzik
2009-11-29 23:50 ` Pete Zaitcev
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20091128233505.6d02b131@redhat.com \
--to=zaitcev@redhat.com \
--cc=hail-devel@vger.kernel.org \
--cc=jeff@garzik.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.