From bb3a48e42835fdbb456fdf0d0f74050fa61ff7d5 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Sat, 15 Jun 2019 21:38:24 +0100 Subject: [Project] Refactor more, use ev_stat for cdb watching --- contrib/cdb/cdb.h | 8 +++++--- contrib/cdb/cdb_init.c | 47 +++++++++++++++++------------------------------ 2 files changed, 22 insertions(+), 33 deletions(-) (limited to 'contrib') diff --git a/contrib/cdb/cdb.h b/contrib/cdb/cdb.h index f8071fa38..8774799a4 100644 --- a/contrib/cdb/cdb.h +++ b/contrib/cdb/cdb.h @@ -10,6 +10,7 @@ #include "config.h" #include "unix-std.h" +#include "contrib/libev/ev.h" /* * OpenBSD fix @@ -34,8 +35,9 @@ struct cdb int cdb_fd; /* file descriptor */ char *filename; /* file name */ time_t mtime; /* mtime of cdb file */ - struct event *check_timer_ev; /* event structure for checking cdb for modifications */ - struct timeval *check_timer_tv; + struct ev_loop *loop; + ev_stat stat_ev; /* event structure for checking cdb for modifications */ + ev_tstamp check_ts; /* private members */ unsigned cdb_fsize; /* datafile size */ unsigned cdb_dend; /* end of data ptr */ @@ -53,7 +55,7 @@ struct cdb #define cdb_fileno(c) ((c)->cdb_fd) int cdb_init(struct cdb *cdbp, int fd); -void cdb_add_timer(struct cdb *cdbp, unsigned seconds); +void cdb_add_timer(struct cdb *cdbp, EV_P_ ev_tstamp seconds); void cdb_free(struct cdb *cdbp); int cdb_read(const struct cdb *cdbp, void *buf, unsigned len, unsigned pos); diff --git a/contrib/cdb/cdb_init.c b/contrib/cdb/cdb_init.c index 0c0b5e353..bfc6dd0c2 100644 --- a/contrib/cdb/cdb_init.c +++ b/contrib/cdb/cdb_init.c @@ -83,10 +83,8 @@ cdb_free(struct cdb *cdbp) } cdbp->cdb_fsize = 0; - if (cdbp->check_timer_ev) { - evtimer_del (cdbp->check_timer_ev); - g_free (cdbp->check_timer_ev); - g_free (cdbp->check_timer_tv); + if (cdbp->loop) { + ev_stat_stop (cdbp->loop, &cdbp->stat_ev); } } @@ -111,43 +109,32 @@ cdb_read(const struct cdb *cdbp, void *buf, unsigned len, unsigned pos) } static void -cdb_timer_callback (int fd, short what, gpointer ud) +cdb_timer_callback (EV_P_ ev_stat *w, int revents) { - struct cdb *cdbp = ud; + struct cdb *cdbp = w->data; gint nfd; - struct stat st; /* Check cdb file for modifications */ - if (stat (cdbp->filename, &st) != -1) { - if (st.st_mtime > cdbp->mtime) { - if ((nfd = open (cdbp->filename, O_RDONLY)) != -1) { - if (cdbp->cdb_mem) { + if ((nfd = open (cdbp->filename, O_RDONLY)) != -1) { + if (cdbp->cdb_mem) { #ifdef _WIN32 - UnmapViewOfFile((void*) cdbp->cdb_mem); + UnmapViewOfFile((void*) cdbp->cdb_mem); #else - munmap ((void*) cdbp->cdb_mem, cdbp->cdb_fsize); + munmap ((void*) cdbp->cdb_mem, cdbp->cdb_fsize); #endif /* _WIN32 */ - cdbp->cdb_mem = NULL; - } - (void)close (cdbp->cdb_fd); - cdbp->cdb_fsize = 0; - (void)cdb_init (cdbp, nfd); - } + cdbp->cdb_mem = NULL; } + (void)close (cdbp->cdb_fd); + cdbp->cdb_fsize = 0; + (void)cdb_init (cdbp, nfd); } - - evtimer_add (cdbp->check_timer_ev, cdbp->check_timer_tv); } void -cdb_add_timer(struct cdb *cdbp, unsigned seconds) +cdb_add_timer (struct cdb *cdbp, EV_P_ ev_tstamp seconds) { - cdbp->check_timer_ev = g_malloc (sizeof (struct event)); - cdbp->check_timer_tv = g_malloc (sizeof (struct timeval)); - - cdbp->check_timer_tv->tv_sec = seconds; - cdbp->check_timer_tv->tv_usec = 0; - - evtimer_set (cdbp->check_timer_ev, cdb_timer_callback, cdbp); - evtimer_add (cdbp->check_timer_ev, cdbp->check_timer_tv); + cdbp->loop = loop; + ev_stat_init (&cdbp->stat_ev, cdb_timer_callback, cdbp->filename, seconds); + cdbp->stat_ev.data = cdbp; + ev_stat_start (EV_A_ &cdbp->stat_ev); } -- cgit v1.2.3