summaryrefslogtreecommitdiff
path: root/lib/thread.h
diff options
context:
space:
mode:
Diffstat (limited to 'lib/thread.h')
-rw-r--r--lib/thread.h172
1 files changed, 59 insertions, 113 deletions
diff --git a/lib/thread.h b/lib/thread.h
index 35a11cb77e..753aa41ffd 100644
--- a/lib/thread.h
+++ b/lib/thread.h
@@ -13,16 +13,18 @@
* 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.
+ * You should have received a copy of the GNU General Public License along
+ * with this program; see the file COPYING; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef _ZEBRA_THREAD_H
#define _ZEBRA_THREAD_H
#include <zebra.h>
+#include <pthread.h>
+#include <poll.h>
+#include "monotime.h"
struct rusage_t
{
@@ -43,14 +45,6 @@ struct thread_list
struct pqueue;
-/*
- * Abstract it so we can use different methodologies to
- * select on data.
- */
-typedef fd_set thread_fd_set;
-
-#if defined(HAVE_POLL_CALL)
-#include <poll.h>
struct fd_handler
{
/* number of pfd stored in pfds */
@@ -61,14 +55,6 @@ struct fd_handler
nfds_t pfdsize;
struct pollfd *pfds;
};
-#else
-struct fd_handler
-{
- fd_set readfd;
- fd_set writefd;
- fd_set exceptfd;
-};
-#endif
/* Master of the theads. */
struct thread_master
@@ -80,9 +66,15 @@ struct thread_master
struct thread_list ready;
struct thread_list unuse;
struct pqueue *background;
+ int io_pipe[2];
int fd_limit;
struct fd_handler handler;
unsigned long alloc;
+ long selectpoll_timeout;
+ bool spin;
+ bool handle_signals;
+ pthread_mutex_t mtx;
+ pthread_t owner;
};
typedef unsigned char thread_type;
@@ -90,31 +82,34 @@ typedef unsigned char thread_type;
/* Thread itself. */
struct thread
{
- thread_type type; /* thread type */
- thread_type 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 */
+ thread_type type; /* thread type */
+ thread_type add_type; /* thread type */
+ struct thread *next; /* next pointer of the thread */
+ struct thread *prev; /* previous pointer of the thread */
+ struct thread **ref; /* external reference (if given) */
+ 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. */
+ int val; /* second argument of the event. */
+ int fd; /* file descriptor in case of r/w */
+ struct timeval sands; /* rest of time sands value. */
} u;
- int index; /* used for timers to store position in queue */
+ int index; /* queue position for timers */
struct timeval real;
- struct cpu_thread_history *hist; /* cache pointer to cpu_history */
- unsigned long yield; /* yield time in us */
- const char *funcname;
- const char *schedfrom;
- int schedfrom_line;
+ struct cpu_thread_history *hist; /* cache pointer to cpu_history */
+ unsigned long yield; /* yield time in microseconds */
+ const char *funcname; /* name of thread function */
+ const char *schedfrom; /* source file thread was scheduled from */
+ int schedfrom_line; /* line number of source file */
+ pthread_mutex_t mtx; /* mutex for thread.c functions */
};
struct cpu_thread_history
{
int (*func)(struct thread *);
unsigned int total_calls;
+ unsigned int total_active;
struct time_stats
{
unsigned long total, max;
@@ -124,11 +119,6 @@ struct cpu_thread_history
const char *funcname;
};
-/* Clocks supported by Quagga */
-enum quagga_clkid {
- QUAGGA_CLK_MONOTONIC = 1, /* monotonic, against an indeterminate base */
-};
-
/* Struct timeval's tv_usec one second value. */
#define TIMER_SECOND_MICRO 1000000L
@@ -150,30 +140,6 @@ enum quagga_clkid {
#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_TIMER_MSEC_ON(master,thread,func,arg,time) \
- do { \
- if (! thread) \
- thread = thread_add_timer_msec (master, func, arg, time); \
- } while (0)
-
#define THREAD_OFF(thread) \
do { \
if (thread) \
@@ -189,46 +155,42 @@ 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_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_timer_tv(m,f,a,v) funcname_thread_add_timer_tv(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__)
+#define thread_add_read(m,f,a,v,t) funcname_thread_add_read_write(THREAD_READ,m,f,a,v,t,#f,__FILE__,__LINE__)
+#define thread_add_write(m,f,a,v,t) funcname_thread_add_read_write(THREAD_WRITE,m,f,a,v,t,#f,__FILE__,__LINE__)
+#define thread_add_timer(m,f,a,v,t) funcname_thread_add_timer(m,f,a,v,t,#f,__FILE__,__LINE__)
+#define thread_add_timer_msec(m,f,a,v,t) funcname_thread_add_timer_msec(m,f,a,v,t,#f,__FILE__,__LINE__)
+#define thread_add_timer_tv(m,f,a,v,t) funcname_thread_add_timer_tv(m,f,a,v,t,#f,__FILE__,__LINE__)
+#define thread_add_event(m,f,a,v,t) funcname_thread_add_event(m,f,a,v,t,#f,__FILE__,__LINE__)
#define thread_execute(m,f,a,v) funcname_thread_execute(m,f,a,v,#f,__FILE__,__LINE__)
/* The 4th arg to thread_add_background is the # of milliseconds to delay. */
-#define thread_add_background(m,f,a,v) funcname_thread_add_background(m,f,a,v,#f,__FILE__,__LINE__)
+#define thread_add_background(m,f,a,v,t) funcname_thread_add_background(m,f,a,v,t,#f,__FILE__,__LINE__)
/* Prototypes. */
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_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);
-extern struct thread *funcname_thread_add_timer_msec (struct thread_master *,
- int (*)(struct thread *),
- void *, long, debugargdef);
-extern struct thread *funcname_thread_add_timer_tv (struct thread_master *,
- int (*)(struct thread *),
- void *, struct timeval *,
- debugargdef);
-extern struct thread *funcname_thread_add_event (struct thread_master *,
- int (*)(struct thread *),
- void *, int, debugargdef);
-extern struct thread *funcname_thread_add_background (struct thread_master *,
- int (*func)(struct thread *),
- void *arg,
- long milliseconds_to_delay,
- debugargdef);
-extern struct thread *funcname_thread_execute (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, struct thread **, debugargdef);
+
+extern struct thread * funcname_thread_add_timer (struct thread_master *,
+ int (*)(struct thread *), void *, long, struct thread **, debugargdef);
+
+extern struct thread * funcname_thread_add_timer_msec (struct thread_master *,
+ int (*)(struct thread *), void *, long, struct thread **, debugargdef);
+
+extern struct thread * funcname_thread_add_timer_tv (struct thread_master *,
+ int (*)(struct thread *), void *, struct timeval *, struct thread **, debugargdef);
+
+extern struct thread * funcname_thread_add_event (struct thread_master *,
+ int (*)(struct thread *), void *, int, struct thread **, debugargdef);
+
+extern struct thread * funcname_thread_add_background (struct thread_master *,
+ int (*)(struct thread *), void *, long, struct thread **, debugargdef);
+
+extern void funcname_thread_execute (struct thread_master *,
+ int (*)(struct thread *), void *, int, debugargdef);
#undef debugargdef
extern void thread_cancel (struct thread *);
@@ -238,33 +200,17 @@ extern void thread_call (struct thread *);
extern unsigned long thread_timer_remain_second (struct thread *);
extern struct timeval thread_timer_remain(struct thread*);
extern int thread_should_yield (struct thread *);
-extern unsigned long timeval_elapsed (struct timeval a, struct timeval b);
/* set yield time for thread */
extern void thread_set_yield_time (struct thread *, unsigned long);
/* Internal libfrr exports */
extern void thread_getrusage (RUSAGE_T *);
-extern struct cmd_element show_thread_cpu_cmd;
-extern struct cmd_element clear_thread_cpu_cmd;
-
-/* replacements for the system gettimeofday(), clock_gettime() and
- * time() functions, providing support for non-decrementing clock on
- * all systems, and fully monotonic on /some/ systems.
- */
-extern int quagga_gettime (enum quagga_clkid, struct timeval *);
-extern time_t quagga_monotime (void);
+extern void thread_cmd_init (void);
/* Returns elapsed real (wall clock) time. */
extern unsigned long thread_consumed_time(RUSAGE_T *after, RUSAGE_T *before,
unsigned long *cpu_time_elapsed);
-/* Global variable containing a recent result from gettimeofday. This can
- be used instead of calling gettimeofday if a recent value is sufficient.
- This is guaranteed to be refreshed before a thread is called. */
-extern struct timeval recent_time;
-/* Similar to recent_time, but a monotonically increasing time value */
-extern struct timeval recent_relative_time (void);
-
/* only for use in logging functions! */
extern struct thread *thread_current;