Skip to content

Commit

Permalink
lib/thread: get rid of the shallow-copy thread_fetch add a sane threa…
Browse files Browse the repository at this point in the history
…d_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
  • Loading branch information
pjakma committed Dec 5, 2017
1 parent 671444d commit 5e13840
Show file tree
Hide file tree
Showing 18 changed files with 47 additions and 93 deletions.
4 changes: 1 addition & 3 deletions bgpd/bgp_main.c
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down
4 changes: 1 addition & 3 deletions isisd/isis_main.c
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
53 changes: 25 additions & 28 deletions lib/thread.c
Expand Up @@ -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. */
Expand Down Expand Up @@ -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. */
Expand All @@ -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);
}
}

Expand All @@ -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;
Expand All @@ -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)
{
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -1350,6 +1336,8 @@ thread_call (struct thread *thread)
realtime/1000, cputime/1000);
}
#endif /* CONSUMED_TIME_CHECK */

thread_add_unuse (thread);
}

/* Execute thread */
Expand Down Expand Up @@ -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);
}
3 changes: 1 addition & 2 deletions lib/thread.h
Expand Up @@ -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 *);
Expand Down
4 changes: 1 addition & 3 deletions nhrpd/nhrp_main.c
Expand Up @@ -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 */
Expand Down Expand Up @@ -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;
}
6 changes: 2 additions & 4 deletions ospf6d/ospf6_main.c
Expand Up @@ -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 */
Expand Down Expand Up @@ -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");

Expand Down
8 changes: 1 addition & 7 deletions ospfclient/ospfclient.c
Expand Up @@ -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:
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 2 additions & 5 deletions ospfd/ospf_main.c
Expand Up @@ -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 */
Expand Down Expand Up @@ -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);
}
Expand Down
4 changes: 1 addition & 3 deletions pimd/pim_main.c
Expand Up @@ -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);

Expand Down Expand Up @@ -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__);
Expand Down
4 changes: 1 addition & 3 deletions ripd/rip_main.c
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 1 addition & 3 deletions ripngd/ripng_main.c
Expand Up @@ -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 */
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 1 addition & 4 deletions tests/common-cli.c
Expand Up @@ -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);

Expand All @@ -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);
Expand Down
4 changes: 1 addition & 3 deletions tests/main.c
Expand Up @@ -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 */
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 1 addition & 3 deletions tests/test-sig.c
Expand Up @@ -57,7 +57,6 @@ struct quagga_signal_t sigs[] =
};

struct thread_master *master;
struct thread t;

int
main (void)
Expand All @@ -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);
}
4 changes: 1 addition & 3 deletions tests/test-timer-correctness.c
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}

0 comments on commit 5e13840

Please sign in to comment.