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 <[email protected]>
---
 lib/thread.c | 57 ++++++++++++++++-----------------------------------------
 lib/thread.h | 13 +++++--------
 2 files changed, 21 insertions(+), 49 deletions(-)

diff --git a/lib/thread.c b/lib/thread.c
index 9b201de..92bdf11 100644
--- a/lib/thread.c
+++ b/lib/thread.c
@@ -46,7 +46,6 @@ extern int agentx_enabled;
 #include <mach/mach_time.h>
 #endif
 
-
 /* Recent absolute time of day */
 struct timeval recent_time;
 static struct timeval last_recent_time;
@@ -769,17 +768,6 @@ fd_is_set (int fd, thread_fd_set *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)
 {
   if (!FD_ISSET (fd, fdset))
@@ -791,46 +779,33 @@ fd_clear_read_write (int fd, thread_fd_set *fdset)
 
 /* Add new read thread. */
 struct thread *
-funcname_thread_add_read (struct thread_master *m, 
+funcname_thread_add_read_write (int dir, struct thread_master *m, 
                 int (*func) (struct thread *), void *arg, int fd,
                 debugargdef)
 {
-  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, &m->readfd))
     {
-      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, debugargpass);
-  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,
-                debugargdef)
-{
-  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, debugargpass);
+  thread = thread_get (m, dir, func, arg, debugargpass);
   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 dafd438..12d64bb 100644
--- a/lib/thread.h
+++ b/lib/thread.h
@@ -172,8 +172,8 @@ enum quagga_clkid {
 
 #define debugargdef  const char *funcname, const char *schedfrom, int fromln
 
-#define thread_add_read(m,f,a,v) 
funcname_thread_add_read(m,f,a,v,#f,__FILE__,__LINE__)
-#define thread_add_write(m,f,a,v) 
funcname_thread_add_write(m,f,a,v,#f,__FILE__,__LINE__)
+#define thread_add_read(m,f,a,v) 
funcname_thread_add_read_write(THREAD_READ,m,f,a,v,#f,__FILE__,__LINE__)
+#define thread_add_write(m,f,a,v) 
funcname_thread_add_read_write(THREAD_WRITE,m,f,a,v,#f,__FILE__,__LINE__)
 #define thread_add_timer(m,f,a,v) 
funcname_thread_add_timer(m,f,a,v,#f,__FILE__,__LINE__)
 #define thread_add_timer_msec(m,f,a,v) 
funcname_thread_add_timer_msec(m,f,a,v,#f,__FILE__,__LINE__)
 #define thread_add_event(m,f,a,v) 
funcname_thread_add_event(m,f,a,v,#f,__FILE__,__LINE__)
@@ -186,12 +186,9 @@ enum quagga_clkid {
 extern struct thread_master *thread_master_create (void);
 extern void thread_master_free (struct thread_master *);
 
-extern struct thread *funcname_thread_add_read (struct thread_master *, 
-                                               int (*)(struct thread *),
-                                               void *, int, debugargdef);
-extern struct thread *funcname_thread_add_write (struct thread_master *,
-                                                int (*)(struct thread *),
-                                                void *, int, debugargdef);
+extern struct thread *funcname_thread_add_read_write (int dir, struct 
thread_master *,
+                                                     int (*)(struct thread *),
+                                                     void *, int, debugargdef);
 extern struct thread *funcname_thread_add_timer (struct thread_master *,
                                                 int (*)(struct thread *),
                                                 void *, long, debugargdef);
-- 
1.9.1


_______________________________________________
Quagga-dev mailing list
[email protected]
https://lists.quagga.net/mailman/listinfo/quagga-dev

Reply via email to