Re: [Patch 2/2] CLD: drop dependency on libevent from libcldc

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, Nov 27, 2009 at 08:52:48PM -0700, Pete Zaitcev wrote:
> diff --git a/test/timer.c b/test/timer.c
> new file mode 100644
> index 0000000..ce05196
> --- /dev/null
> +++ b/test/timer.c
> @@ -0,0 +1,44 @@
> +
> +/*
> + * Basic timers for CLD test suite.
> + */
> +#include <string.h>
> +#include "timer.h"
> +
> +void timer_init(struct timer *timer, void (*func)(void *), void *priv)
> +{
> +	memset(timer, 0, sizeof(struct timer));
> +	timer->func = func;
> +	timer->priv = priv;
> +}
> +
> +void timer_add(struct timer *timer, time_t expires)
> +{
> +	timer->expires = expires;
> +}
> +
> +void timer_del(struct timer *timer)
> +{
> +	timer->expires = 0;
> +}
> +
> +/*
> + * Return true if timer was run.
> + * Otherwise, modify *pset if our expected expiration time is closer.
> + */
> +bool timer_poke(struct timer *timer, time_t now, time_t *pset)
> +{
> +	if (!timer->expires)
> +		return false;
> +
> +	if (now >= timer->expires) {
> +		timer->expires = 0;
> +		(*timer->func)(timer->priv);
> +		return true;
> +	}
> +
> +	if (*pset == 0 || timer->expires < *pset)
> +		*pset = timer->expires;
> +	return false;
> +}
> +
> diff --git a/test/timer.h b/test/timer.h
> new file mode 100644
> index 0000000..e0ddcc5
> --- /dev/null
> +++ b/test/timer.h
> @@ -0,0 +1,18 @@
> +#ifndef _TEST_TIMER_H_
> +#define _TEST_TIMER_H_
> +
> +#include <stdbool.h>
> +#include <time.h>
> +
> +struct timer {
> +	time_t expires;
> +	void (*func)(void *);
> +	void *priv;
> +};
> +
> +extern void timer_init(struct timer *timer, void (*func)(void *), void *priv);
> +extern void timer_add(struct timer *timer, time_t expires);
> +extern void timer_del(struct timer *timer);
> +extern bool timer_poke(struct timer *timer, time_t now, time_t *pset);
> +
> +#endif

hmmm, you are re-creating the self-contained timer facilities that
already exist in CLD, which are designed to be used with any of
select(2), poll(2), or epoll_wait(2).  I don't think we need two
hand-coded timer implementations.

I would suggest using some variant of the changes below as patch #1,
then add these other changes on top of that.  I have not committed
these changes, pending your comments.

If you choose to take the changes below and submit them as part
of your own patch series, make sure to add "From: Jeff Garzik
<jgarzik@xxxxxxxxxx>" as the first line of the email body.
This so-called "Andrew Morton rule" triggers 'git am' and other
utilities to differentiate patch author from email author.


 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(-)

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

[Index of Archives]     [Fedora Clound]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [XFree86]

  Powered by Linux