]> git.puffer.fish Git - mirror/frr.git/commitdiff
lib: Refactor read/write functionality
authorDonald Sharp <sharpd@cumulusnetworks.com>
Fri, 4 Mar 2016 02:14:13 +0000 (02:14 +0000)
committerDonald Sharp <sharpd@cumulusnetworks.com>
Fri, 4 Mar 2016 17:33:56 +0000 (12:33 -0500)
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 <sharpd@cumulusnetworks.com>
lib/thread.c
lib/thread.h

index 9a43257b3f915a7658be9d28513ec9b59aec8b7c..b2c321c6d17d40a7b6b457137c8165d31adeccde 100644 (file)
@@ -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;
@@ -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;
 }
index 29de0f117e92a759cc145f3e1cb1fb454dd72c78..cd5cf16d55834badc76395affe1b14fbe63f9f97 100644 (file)
@@ -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*);