forked from Mirror/frr

* thread.h: Add background thread type and thread_add_background macro and accompanying funcname_... function. export thread_should_yield, background threads can use it. Lower thread yield time to 10ms, 100ms is noticeable lag and a thread would only be /starting/ to finish sometime afterward. * thread.c: (general) Add background thread type and schedule nearly all thread types through the ready list for fairness. (timeval_adjust) static qualifier missing (vty_out_cpu_thread_history) add support for printout of background threads (show_thread_cpu) ditto. (thread_master_debug) add debug of background list (thread_master_create) fixup long line (thread_add_unuse) add asserts for required state. (thread_master_free) free background thread list (funcname_thread_add_timer_timeval) make generic, able to support arbitrary timer-like thread types. (funcname_thread_add_timer) pass thread type to .._add_timer_timeval (funcname_thread_add_timer_msec) ditto (funcname_thread_add_background) Add a background thread, with an optional millisecond delay factor, using .._add_timer_timeval. (thread_cancel) Add background thread type. Move the thread_list_delete common to all cases to bottom of function, after the switch statement.. (thread_cancel_event) indent (thread_timer_wait) Static qualifier, and make it able to cope with arbitrary timer-like thread lists, so its of use to background threads too. (thread_process_fd) static qualifier. Again, make it take a list reference rather than thread_master. Fix indentation. (thread_timer_process) Check for ready timer-like threads in the given list and move them on to the ready list - code originally embedded in thread_fetch. (thread_fetch) Schedule all threads, other than events, through the ready list, to ensure fairness. Timer readying code moved to thread_timer_process so it can be reused for background threads. Remove the unneeded quagga_sigevent_process, as pointed out by John Lin <john.ch.lin@gmail.com>. (thread_should_yield) make this available.
174 lines
5.6 KiB
C
174 lines
5.6 KiB
C
/* Thread management routine header.
|
|
* Copyright (C) 1998 Kunihiro Ishiguro
|
|
*
|
|
* This file is part of GNU Zebra.
|
|
*
|
|
* GNU Zebra is free software; you can redistribute it and/or modify it
|
|
* under the terms of the GNU General Public License as published by the
|
|
* Free Software Foundation; either version 2, or (at your option) any
|
|
* later version.
|
|
*
|
|
* GNU Zebra is distributed in the hope that it will be useful, but
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
* General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with GNU Zebra; see the file COPYING. If not, write to the Free
|
|
* Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|
* 02111-1307, USA.
|
|
*/
|
|
|
|
#ifndef _ZEBRA_THREAD_H
|
|
#define _ZEBRA_THREAD_H
|
|
|
|
#ifdef HAVE_RUSAGE
|
|
#define RUSAGE_T struct rusage
|
|
#define GETRUSAGE(X) getrusage (RUSAGE_SELF, X);
|
|
#else
|
|
#define RUSAGE_T struct timeval
|
|
#define GETRUSAGE(X) gettimeofday (X, NULL);
|
|
#endif /* HAVE_RUSAGE */
|
|
|
|
/* Linked list of thread. */
|
|
struct thread_list
|
|
{
|
|
struct thread *head;
|
|
struct thread *tail;
|
|
int count;
|
|
};
|
|
|
|
/* Master of the theads. */
|
|
struct thread_master
|
|
{
|
|
struct thread_list read;
|
|
struct thread_list write;
|
|
struct thread_list timer;
|
|
struct thread_list event;
|
|
struct thread_list ready;
|
|
struct thread_list unuse;
|
|
struct thread_list background;
|
|
fd_set readfd;
|
|
fd_set writefd;
|
|
fd_set exceptfd;
|
|
unsigned long alloc;
|
|
};
|
|
|
|
/* Thread itself. */
|
|
struct thread
|
|
{
|
|
unsigned char type; /* thread type */
|
|
unsigned add_type; /* thread type */
|
|
struct thread *next; /* next pointer of the thread */
|
|
struct thread *prev; /* previous pointer of the thread */
|
|
struct thread_master *master; /* pointer to the struct thread_master. */
|
|
int (*func) (struct thread *); /* event function */
|
|
void *arg; /* event argument */
|
|
union {
|
|
int val; /* second argument of the event. */
|
|
int fd; /* file descriptor in case of read/write. */
|
|
struct timeval sands; /* rest of time sands value. */
|
|
} u;
|
|
RUSAGE_T ru; /* Indepth usage info. */
|
|
char* funcname;
|
|
};
|
|
|
|
struct cpu_thread_history {
|
|
int (*func)(struct thread *);
|
|
const char *funcname;
|
|
unsigned int total_calls;
|
|
unsigned long total, max;
|
|
unsigned char types;
|
|
};
|
|
|
|
/* Thread types. */
|
|
#define THREAD_READ 0
|
|
#define THREAD_WRITE 1
|
|
#define THREAD_TIMER 2
|
|
#define THREAD_EVENT 3
|
|
#define THREAD_READY 4
|
|
#define THREAD_BACKGROUND 5
|
|
#define THREAD_UNUSED 6
|
|
#define THREAD_EXECUTE 7
|
|
|
|
/* Thread yield time. */
|
|
#define THREAD_YIELD_TIME_SLOT 10 * 1000L /* 100ms */
|
|
|
|
/* Macros. */
|
|
#define THREAD_ARG(X) ((X)->arg)
|
|
#define THREAD_FD(X) ((X)->u.fd)
|
|
#define THREAD_VAL(X) ((X)->u.val)
|
|
|
|
#define THREAD_READ_ON(master,thread,func,arg,sock) \
|
|
do { \
|
|
if (! thread) \
|
|
thread = thread_add_read (master, func, arg, sock); \
|
|
} while (0)
|
|
|
|
#define THREAD_WRITE_ON(master,thread,func,arg,sock) \
|
|
do { \
|
|
if (! thread) \
|
|
thread = thread_add_write (master, func, arg, sock); \
|
|
} while (0)
|
|
|
|
#define THREAD_TIMER_ON(master,thread,func,arg,time) \
|
|
do { \
|
|
if (! thread) \
|
|
thread = thread_add_timer (master, func, arg, time); \
|
|
} while (0)
|
|
|
|
#define THREAD_OFF(thread) \
|
|
do { \
|
|
if (thread) \
|
|
{ \
|
|
thread_cancel (thread); \
|
|
thread = NULL; \
|
|
} \
|
|
} while (0)
|
|
|
|
#define THREAD_READ_OFF(thread) THREAD_OFF(thread)
|
|
#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_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)
|
|
#define thread_execute(m,f,a,v) funcname_thread_execute(m,f,a,v,#f)
|
|
#define thread_add_background(m,f,a,v) funcname_thread_add_background(m,f,a,v,#f)
|
|
|
|
/* Prototypes. */
|
|
struct thread_master *thread_master_create ();
|
|
struct thread *funcname_thread_add_read (struct thread_master *,
|
|
int (*)(struct thread *), void *, int, const char*);
|
|
struct thread *funcname_thread_add_write (struct thread_master *,
|
|
int (*)(struct thread *), void *, int, const char*);
|
|
struct thread *funcname_thread_add_timer (struct thread_master *,
|
|
int (*)(struct thread *), void *, long, const char*);
|
|
struct thread *funcname_thread_add_timer_msec (struct thread_master *,
|
|
int (*)(struct thread *), void *, long, const char*);
|
|
struct thread *funcname_thread_add_event (struct thread_master *,
|
|
int (*)(struct thread *), void *, int, const char*);
|
|
struct thread *funcname_thread_add_background (struct thread_master *,
|
|
int (*)(struct thread *),
|
|
void *,
|
|
long, const char*);
|
|
|
|
void thread_cancel (struct thread *);
|
|
void thread_cancel_event (struct thread_master *, void *);
|
|
|
|
struct thread *thread_fetch (struct thread_master *, struct thread *);
|
|
struct thread *funcname_thread_execute (struct thread_master *,
|
|
int (*)(struct thread *), void *, int, const char *);
|
|
void thread_call (struct thread *);
|
|
unsigned long thread_timer_remain_second (struct thread *);
|
|
int thread_should_yield (struct thread *);
|
|
|
|
extern struct cmd_element show_thread_cpu_cmd;
|
|
extern struct cmd_element show_thread_work_queues_cmd;
|
|
|
|
extern unsigned long thread_consumed_time(RUSAGE_T *after, RUSAGE_T *before);
|
|
|
|
#endif /* _ZEBRA_THREAD_H */
|