From 8dadcae7301a2b38cbf2f539cc9c727d5477edac Mon Sep 17 00:00:00 2001 From: Donald Sharp Date: Fri, 4 Mar 2016 02:14:13 +0000 Subject: [PATCH] lib: Refactor read/write functionality Both the read and write functions used the same code slightly modified for reading and writing. Combine this code together. Signed-off-by: Donald Sharp --- lib/thread.c | 57 +++++++++++++++------------------------------------- lib/thread.h | 9 +++------ 2 files changed, 19 insertions(+), 47 deletions(-) diff --git a/lib/thread.c b/lib/thread.c index 9a43257b3f..b2c321c6d1 100644 --- a/lib/thread.c +++ b/lib/thread.c @@ -46,7 +46,6 @@ extern int agentx_enabled; #include #endif - /* Recent absolute time of day */ struct timeval recent_time; static struct timeval last_recent_time; @@ -798,17 +797,6 @@ fd_is_set (int fd, thread_fd_set *fdset) return FD_ISSET (fd, fdset); } -static int -fd_set_read_write (int fd, thread_fd_set *fdset) -{ - if (FD_ISSET (fd, fdset)) - return 0; - - FD_SET (fd, fdset); - - return 1; -} - static int fd_clear_read_write (int fd, thread_fd_set *fdset) { @@ -821,44 +809,31 @@ fd_clear_read_write (int fd, thread_fd_set *fdset) /* Add new read thread. */ struct thread * -funcname_thread_add_read (struct thread_master *m, - int (*func) (struct thread *), void *arg, int fd, const char* funcname) +funcname_thread_add_read_write (int dir, struct thread_master *m, + int (*func) (struct thread *), void *arg, int fd, const char* funcname) { - struct thread *thread; + struct thread *thread = NULL; + thread_fd_set *fdset = NULL; - assert (m != NULL); + if (dir == THREAD_READ) + fdset = &m->readfd; + else + fdset = &m->writefd; - if (!fd_set_read_write (fd, &m->readfd)) + if (FD_ISSET (fd, fdset)) { - zlog (NULL, LOG_WARNING, "There is already read fd [%d]", fd); + zlog (NULL, LOG_WARNING, "There is already %s fd [%d]", (dir = THREAD_READ) ? "read" : "write", fd); return NULL; } - thread = thread_get (m, THREAD_READ, func, arg, funcname); - thread->u.fd = fd; - thread_add_fd (m->read, thread); - - return thread; -} - -/* Add new write thread. */ -struct thread * -funcname_thread_add_write (struct thread_master *m, - int (*func) (struct thread *), void *arg, int fd, const char* funcname) -{ - struct thread *thread; - - assert (m != NULL); - - if (!fd_set_read_write (fd, &m->writefd)) - { - zlog (NULL, LOG_WARNING, "There is already write fd [%d]", fd); - return NULL; - } + FD_SET (fd, fdset); - thread = thread_get (m, THREAD_WRITE, func, arg, funcname); + thread = thread_get (m, dir, func, arg, funcname); thread->u.fd = fd; - thread_add_fd (m->write, thread); + if (dir == THREAD_READ) + thread_add_fd (m->read, thread); + else + thread_add_fd (m->write, thread); return thread; } diff --git a/lib/thread.h b/lib/thread.h index 29de0f117e..cd5cf16d55 100644 --- a/lib/thread.h +++ b/lib/thread.h @@ -176,8 +176,8 @@ enum quagga_clkid { #define THREAD_WRITE_OFF(thread) THREAD_OFF(thread) #define THREAD_TIMER_OFF(thread) THREAD_OFF(thread) -#define thread_add_read(m,f,a,v) funcname_thread_add_read(m,f,a,v,#f) -#define thread_add_write(m,f,a,v) funcname_thread_add_write(m,f,a,v,#f) +#define thread_add_read(m,f,a,v) funcname_thread_add_read_write(THREAD_READ,m,f,a,v,#f) +#define thread_add_write(m,f,a,v) funcname_thread_add_read_write(THREAD_WRITE,m,f,a,v,#f) #define thread_add_timer(m,f,a,v) funcname_thread_add_timer(m,f,a,v,#f) #define thread_add_timer_msec(m,f,a,v) funcname_thread_add_timer_msec(m,f,a,v,#f) #define thread_add_event(m,f,a,v) funcname_thread_add_event(m,f,a,v,#f) @@ -191,12 +191,9 @@ extern struct thread_master *thread_master_create (void); extern void thread_master_free (struct thread_master *); extern void thread_master_free_unused(struct thread_master *); -extern struct thread *funcname_thread_add_read (struct thread_master *, +extern struct thread *funcname_thread_add_read_write (int dir, struct thread_master *, int (*)(struct thread *), void *, int, const char*); -extern struct thread *funcname_thread_add_write (struct thread_master *, - int (*)(struct thread *), - void *, int, const char*); extern struct thread *funcname_thread_add_timer (struct thread_master *, int (*)(struct thread *), void *, long, const char*); -- 2.39.5