Browse Source

lib/thread: get rid of the shallow-copy thread_fetch add a sane thread_main

* thread.h: (thread_{fetch,call}) unexport these functions. thread_fetch
  has a funny "return a cloned, shallow-copied thread struct" semantics that
  are needless.
  thread_call has no users other than the usual main thread loop, which
  should be replaced with:
  (thread_main) encapsulated main thread loop.
* thread.c: (thread_run) no need for this shallow-copy anymore.
  (thread_add_unuse) no need for a separate master argument. Update
  all callers to match. Setting type to THREAD_UNUSED can be done here.
  (thread_fetch) no need to copy the thread or add to unused _before_
  running it, so return the chosen thread directly.
  (thread_call) This runs the thread, so add_unuse best done here, at end.
  (thread_main) Simple main loop for public use.
* */*main.c: update to thread_main
Paul Jakma 2 years ago
parent
commit
5e13840d7f

+ 1 - 3
bgpd/bgp_main.c

@@ -346,7 +346,6 @@ main (int argc, char **argv)
   int daemon_mode = 0;
   int dryrun = 0;
   char *progname;
-  struct thread thread;
   int tmp_port;
   int skip_runas = 0;
 
@@ -485,8 +484,7 @@ main (int argc, char **argv)
 	       getpid ());
 
   /* Start finite state machine, here we go! */
-  while (thread_fetch (bm->master, &thread))
-    thread_call (&thread);
+  thread_main (bm->master);
 
   /* Not reached. */
   return (0);

+ 1 - 3
isisd/isis_main.c

@@ -236,7 +236,6 @@ main (int argc, char **argv, char **envp)
 {
   char *p;
   int opt, vty_port = ISISD_VTY_PORT;
-  struct thread thread;
   char *config_file = NULL;
   char *vty_addr = NULL;
   int dryrun = 0;
@@ -375,8 +374,7 @@ main (int argc, char **argv, char **envp)
   zlog_notice ("Quagga-ISISd %s starting: vty@%d", QUAGGA_VERSION, vty_port);
 
   /* Start finite state machine. */
-  while (thread_fetch (master, &thread))
-    thread_call (&thread);
+  thread_main (master);
 
   /* Not reached. */
   exit (0);

+ 25 - 28
lib/thread.c

@@ -601,13 +601,13 @@ thread_add_fd (struct thread **thread_array, struct thread *thread)
 
 /* Move thread to unuse list. */
 static void
-thread_add_unuse (struct thread_master *m, struct thread *thread)
+thread_add_unuse (struct thread *thread)
 {
-  assert (m != NULL && thread != NULL);
+  thread->type = THREAD_UNUSED;
+  assert (thread->master != NULL && thread != NULL);
   assert (thread->next == NULL);
   assert (thread->prev == NULL);
-  assert (thread->type == THREAD_UNUSED);
-  thread_list_add (&m->unuse, thread);
+  thread_list_add (&thread->master->unuse, thread);
 }
 
 /* Free all unused thread. */
@@ -995,8 +995,7 @@ thread_cancel (struct thread *thread)
       assert(!"Thread should be either in queue or list or array!");
     }
 
-  thread->type = THREAD_UNUSED;
-  thread_add_unuse (thread->master, thread);
+  thread_add_unuse (thread);
 }
 
 /* Delete all events which has argument value arg. */
@@ -1018,8 +1017,7 @@ thread_cancel_event (struct thread_master *m, void *arg)
         {
           ret++;
           thread_list_delete (&m->event, t);
-          t->type = THREAD_UNUSED;
-          thread_add_unuse (m, t);
+          thread_add_unuse (t);
         }
     }
 
@@ -1036,8 +1034,7 @@ thread_cancel_event (struct thread_master *m, void *arg)
         {
           ret++;
           thread_list_delete (&m->ready, t);
-          t->type = THREAD_UNUSED;
-          thread_add_unuse (m, t);
+          thread_add_unuse (t);
         }
     }
   return ret;
@@ -1055,16 +1052,6 @@ thread_timer_wait (struct pqueue *queue, struct timeval *timer_val)
   return NULL;
 }
 
-static struct thread *
-thread_run (struct thread_master *m, struct thread *thread,
-	    struct thread *fetch)
-{
-  *fetch = *thread;
-  thread->type = THREAD_UNUSED;
-  thread_add_unuse (m, thread);
-  return fetch;
-}
-
 static int
 thread_process_fds_helper (struct thread_master *m, struct thread *thread, thread_fd_set *fdset)
 {
@@ -1148,10 +1135,9 @@ thread_process (struct thread_list *list)
   return ready;
 }
 
-
 /* Fetch next ready thread. */
-struct thread *
-thread_fetch (struct thread_master *m, struct thread *fetch)
+static struct thread *
+thread_fetch (struct thread_master *m)
 {
   struct thread *thread;
   thread_fd_set readfd;
@@ -1173,7 +1159,7 @@ thread_fetch (struct thread_master *m, struct thread *fetch)
        * more.
        */
       if ((thread = thread_trim_head (&m->ready)) != NULL)
-        return thread_run (m, thread, fetch);
+        return thread;
       
       /* To be fair to all kinds of threads, and avoid starvation, we
        * need to be careful to consider all thread types for scheduling
@@ -1227,14 +1213,14 @@ thread_fetch (struct thread_master *m, struct thread *fetch)
 	 list at this time.  If this is code is uncommented, then background
 	 timer threads will not run unless there is nothing else to do. */
       if ((thread = thread_trim_head (&m->ready)) != NULL)
-        return thread_run (m, thread, fetch);
+        return thread;
 #endif
 
       /* Background timer/events, lowest priority */
       thread_timer_process (m->background, &relative_time);
       
       if ((thread = thread_trim_head (&m->ready)) != NULL)
-        return thread_run (m, thread, fetch);
+        return thread;
     }
 }
 
@@ -1291,12 +1277,12 @@ struct thread *thread_current = NULL;
 /* We check thread consumed time. If the system has getrusage, we'll
    use that to get in-depth stats on the performance of the thread in addition
    to wall clock time stats from gettimeofday. */
-void
+static void
 thread_call (struct thread *thread)
 {
   unsigned long realtime, cputime;
   RUSAGE_T before, after;
-
+ 
  /* Cache a pointer to the relevant cpu history thread, if the thread
   * does not have it yet.
   *
@@ -1350,6 +1336,8 @@ thread_call (struct thread *thread)
 		 realtime/1000, cputime/1000);
     }
 #endif /* CONSUMED_TIME_CHECK */
+  
+  thread_add_unuse (thread);
 }
 
 /* Execute thread */
@@ -1379,3 +1367,12 @@ funcname_thread_execute (struct thread_master *m,
 
   return NULL;
 }
+
+/* Co-operative thread main loop */
+void
+thread_main (struct thread_master *master)
+{
+  struct thread *t;
+  while ((t = thread_fetch (master)))
+    thread_call (t);
+}

+ 1 - 2
lib/thread.h

@@ -218,8 +218,7 @@ extern struct thread *funcname_thread_execute (struct thread_master *,
 
 extern void thread_cancel (struct thread *);
 extern unsigned int thread_cancel_event (struct thread_master *, void *);
-extern struct thread *thread_fetch (struct thread_master *, struct thread *);
-extern void thread_call (struct thread *);
+extern void thread_main (struct thread_master *);
 extern unsigned long thread_timer_remain_second (struct thread *);
 extern struct timeval thread_timer_remain(struct thread*);
 extern int thread_should_yield (struct thread *);

+ 1 - 3
nhrpd/nhrp_main.c

@@ -184,7 +184,6 @@ static struct quagga_signal_t sighandlers[] = {
 
 int main(int argc, char **argv)
 {
-	struct thread thread;
 	const char *progname;
 
 	/* Set umask before anything for security */
@@ -240,8 +239,7 @@ int main(int argc, char **argv)
 	zlog_notice("nhrpd starting: vty@%d", vty_port);
 
 	/* Main loop */
-	while (thread_fetch(master, &thread))
-		thread_call(&thread);
+	thread_main (master);
 
 	return 0;
 }

+ 2 - 4
ospf6d/ospf6_main.c

@@ -229,7 +229,6 @@ main (int argc, char *argv[], char *envp[])
   char *vty_addr = NULL;
   int vty_port = 0;
   char *config_file = NULL;
-  struct thread thread;
   int dryrun = 0;
 
   /* Set umask before anything for security */
@@ -352,9 +351,8 @@ main (int argc, char *argv[], char *envp[])
                QUAGGA_VERSION, OSPF6_DAEMON_VERSION,vty_port);
 
   /* Start finite state machine, here we go! */
-  while (thread_fetch (master, &thread))
-    thread_call (&thread);
-
+  thread_main (master);
+  
   /* Log in case thread failed */
   zlog_warn ("Thread failed");
 

+ 1 - 7
ospfclient/ospfclient.c

@@ -284,8 +284,6 @@ static int usage()
 int
 main (int argc, char *argv[])
 {
-  struct thread thread;
-
   args = argv;
 
   /* ospfclient should be started with the following arguments:
@@ -340,11 +338,7 @@ main (int argc, char *argv[])
   thread_add_read (master, lsa_read, oclient, oclient->fd_async);
 
   /* Now connection is established, run loop */
-  while (1)
-    {
-      thread_fetch (master, &thread);
-      thread_call (&thread);
-    }
+  thread_main (master);
 
   /* Never reached */
   return 0;

+ 2 - 5
ospfd/ospf_main.c

@@ -186,7 +186,6 @@ main (int argc, char **argv)
   int daemon_mode = 0;
   char *config_file = NULL;
   char *progname;
-  struct thread thread;
   int dryrun = 0;
 
   /* Set umask before anything for security */
@@ -334,10 +333,8 @@ main (int argc, char **argv)
   /* Print banner. */
   zlog_notice ("OSPFd %s starting: vty@%d", QUAGGA_VERSION, vty_port);
 
-  /* Fetch next active thread. */
-  while (thread_fetch (master, &thread))
-    thread_call (&thread);
-
+  thread_main (master);
+  
   /* Not reached. */
   return (0);
 }

+ 1 - 3
pimd/pim_main.c

@@ -129,7 +129,6 @@ int main(int argc, char** argv, char** envp) {
   int daemon_mode = 0;
   char *config_file = NULL;
   char *zebra_sock_path = NULL;
-  struct thread thread;
           
   umask(0027);
  
@@ -273,8 +272,7 @@ int main(int argc, char** argv, char** envp) {
   zlog_notice("!HAVE_CLOCK_MONOTONIC");
 #endif
 
-  while (thread_fetch(master, &thread))
-    thread_call(&thread);
+  thread_main (master);
 
   zlog_err("%s %s: thread_fetch() returned NULL, exiting",
 	   __FILE__, __PRETTY_FUNCTION__);

+ 1 - 3
ripd/rip_main.c

@@ -193,7 +193,6 @@ main (int argc, char **argv)
   int daemon_mode = 0;
   int dryrun = 0;
   char *progname;
-  struct thread thread;
 
   /* Set umask before anything for security */
   umask (0027);
@@ -313,8 +312,7 @@ main (int argc, char **argv)
   zlog_notice ("RIPd %s starting: vty@%d", QUAGGA_VERSION, vty_port);
 
   /* Execute each thread. */
-  while (thread_fetch (master, &thread))
-    thread_call (&thread);
+  thread_main (master);
 
   /* Not reached. */
   return (0);

+ 1 - 3
ripngd/ripng_main.c

@@ -192,7 +192,6 @@ main (int argc, char **argv)
   int vty_port = RIPNG_VTY_PORT;
   int daemon_mode = 0;
   char *progname;
-  struct thread thread;
   int dryrun = 0;
 
   /* Set umask before anything for security */
@@ -308,8 +307,7 @@ main (int argc, char **argv)
   zlog_notice ("RIPNGd %s starting: vty@%d", QUAGGA_VERSION, vty_port);
 
   /* Fetch next active thread. */
-  while (thread_fetch (master, &thread))
-    thread_call (&thread);
+  thread_main (master);
 
   /* Not reached. */
   return 0;

+ 1 - 4
tests/common-cli.c

@@ -55,8 +55,6 @@ static void vty_do_exit(void)
 int
 main (int argc, char **argv)
 {
-  struct thread thread;
-
   /* Set umask before anything for security */
   umask (0027);
 
@@ -81,8 +79,7 @@ main (int argc, char **argv)
   vty_stdio (vty_do_exit);
 
   /* Fetch next active thread. */
-  while (thread_fetch (master, &thread))
-    thread_call (&thread);
+  thread_main (master);
 
   /* Not reached. */
   exit (0);

+ 1 - 3
tests/main.c

@@ -106,7 +106,6 @@ main (int argc, char **argv)
   int vty_port = 4000;
   int daemon_mode = 0;
   char *progname;
-  struct thread thread;
   char *config_file = NULL;
   
   /* Set umask before anything for security */
@@ -191,8 +190,7 @@ main (int argc, char **argv)
   test_init();  
   
   /* Fetch next active thread. */
-  while (thread_fetch (master, &thread))
-    thread_call (&thread);
+  thread_main (master);
 
   /* Not reached. */
   exit (0);

+ 1 - 3
tests/test-sig.c

@@ -57,7 +57,6 @@ struct quagga_signal_t sigs[] =
 };
 
 struct thread_master *master;
-struct thread t;
 
 int
 main (void)
@@ -71,8 +70,7 @@ main (void)
   zlog_set_level (NULL, ZLOG_DEST_STDOUT, LOG_DEBUG);
   zlog_set_level (NULL, ZLOG_DEST_MONITOR, ZLOG_DISABLED);
   
-  while (thread_fetch (master, &t))
-    thread_call (&t);
+  thread_main (master);
 
   exit (0);
 }

+ 1 - 3
tests/test-timer-correctness.c

@@ -113,7 +113,6 @@ static int cmp_timeval(const void* a, const void *b)
 int main(int argc, char **argv)
 {
   int i, j;
-  struct thread t;
   struct timeval **alarms;
 
   master = thread_master_create();
@@ -190,8 +189,7 @@ int main(int argc, char **argv)
     }
   XFREE(MTYPE_TMP, alarms);
 
-  while (thread_fetch(master, &t))
-    thread_call(&t);
+  thread_main (master);
 
   return 0;
 }

+ 3 - 8
watchquagga/watchquagga.c

@@ -1386,14 +1386,9 @@ main(int argc, char **argv)
       		  progname, QUAGGA_VERSION, buf, mode_str[gs.mode]);
     }
   }
-
-  {
-    struct thread thread;
-
-    while (thread_fetch (master, &thread))
-      thread_call (&thread);
-  }
-
+  
+  thread_main (master);
+  
   /* Not reached. */
   return 0;
 }

+ 2 - 5
zebra/main.c

@@ -295,7 +295,6 @@ main (int argc, char **argv)
   int daemon_mode = 0;
   char *config_file = NULL;
   char *progname;
-  struct thread thread;
   char *zserv_path = NULL;
   char *fpm_format = NULL;
 
@@ -489,10 +488,8 @@ main (int argc, char **argv)
 
   /* Print banner. */
   zlog_notice ("Zebra %s starting: vty@%d", QUAGGA_VERSION, vty_port);
+  
+  thread_main (zebrad.master);
 
-  while (thread_fetch (zebrad.master, &thread))
-    thread_call (&thread);
-
-  /* Not reached... */
   return 0;
 }

+ 1 - 3
zebra/test_main.c

@@ -273,7 +273,6 @@ main (int argc, char **argv)
   int daemon_mode = 0;
   char *config_file = NULL;
   char *progname;
-  struct thread thread;
 
   /* Set umask before anything for security */
   umask (0027);
@@ -388,8 +387,7 @@ main (int argc, char **argv)
   /* Print banner. */
   zlog_notice ("Zebra %s starting: vty@%d", QUAGGA_VERSION, vty_port);
 
-  while (thread_fetch (zebrad.master, &thread))
-    thread_call (&thread);
+  thread_main (zebrad.master);
 
   /* Not reached... */
   return 0;