blob: 1c324d86bfa84f2e982f8d1211901bfcd31908b6 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/* Thread management routine header.
2 * Copyright (C) 1998 Kunihiro Ishiguro
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with GNU Zebra; see the file COPYING. If not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 * 02111-1307, USA.
20 */
21
22#ifndef _ZEBRA_THREAD_H
23#define _ZEBRA_THREAD_H
24
ajs8b70d0b2005-04-28 01:31:13 +000025struct rusage_t
26{
paul718e3742002-12-13 20:15:29 +000027#ifdef HAVE_RUSAGE
ajs8b70d0b2005-04-28 01:31:13 +000028 struct rusage cpu;
29#endif
30 struct timeval real;
31};
32#define RUSAGE_T struct rusage_t
33
Paul Jakmadb9c0df2006-08-27 06:44:02 +000034#define GETRUSAGE(X) thread_getrusage(X)
paul718e3742002-12-13 20:15:29 +000035
36/* Linked list of thread. */
37struct thread_list
38{
39 struct thread *head;
40 struct thread *tail;
41 int count;
42};
43
44/* Master of the theads. */
45struct thread_master
46{
47 struct thread_list read;
48 struct thread_list write;
49 struct thread_list timer;
50 struct thread_list event;
51 struct thread_list ready;
52 struct thread_list unuse;
paula48b4e62005-04-22 00:43:47 +000053 struct thread_list background;
paul718e3742002-12-13 20:15:29 +000054 fd_set readfd;
55 fd_set writefd;
56 fd_set exceptfd;
57 unsigned long alloc;
58};
59
60/* Thread itself. */
61struct thread
62{
63 unsigned char type; /* thread type */
ajsfb9e46b2005-04-22 14:23:34 +000064 unsigned char add_type; /* thread type */
paula48b4e62005-04-22 00:43:47 +000065 struct thread *next; /* next pointer of the thread */
paul718e3742002-12-13 20:15:29 +000066 struct thread *prev; /* previous pointer of the thread */
67 struct thread_master *master; /* pointer to the struct thread_master. */
68 int (*func) (struct thread *); /* event function */
69 void *arg; /* event argument */
70 union {
71 int val; /* second argument of the event. */
72 int fd; /* file descriptor in case of read/write. */
73 struct timeval sands; /* rest of time sands value. */
74 } u;
75 RUSAGE_T ru; /* Indepth usage info. */
Paul Jakmacc8b13a2006-07-25 20:40:40 +000076 struct cpu_thread_history *hist; /* cache pointer to cpu_history */
paule04ab742003-01-17 23:47:00 +000077 char* funcname;
78};
79
ajs8b70d0b2005-04-28 01:31:13 +000080struct cpu_thread_history
81{
paule04ab742003-01-17 23:47:00 +000082 int (*func)(struct thread *);
hasso8c328f12004-10-05 21:01:23 +000083 const char *funcname;
paule04ab742003-01-17 23:47:00 +000084 unsigned int total_calls;
ajs8b70d0b2005-04-28 01:31:13 +000085 struct time_stats
86 {
87 unsigned long total, max;
88 } real;
89#ifdef HAVE_RUSAGE
90 struct time_stats cpu;
91#endif
paule04ab742003-01-17 23:47:00 +000092 unsigned char types;
paul718e3742002-12-13 20:15:29 +000093};
94
Paul Jakmadb9c0df2006-08-27 06:44:02 +000095/* Clocks supported by Quagga */
96enum quagga_clkid {
97 QUAGGA_CLK_REALTIME = 0, /* ala gettimeofday() */
98 QUAGGA_CLK_MONOTONIC, /* monotonic, against an indeterminate base */
99 QUAGGA_CLK_REALTIME_STABILISED, /* like realtime, but non-decrementing */
100};
101
paul718e3742002-12-13 20:15:29 +0000102/* Thread types. */
103#define THREAD_READ 0
104#define THREAD_WRITE 1
105#define THREAD_TIMER 2
106#define THREAD_EVENT 3
107#define THREAD_READY 4
paula48b4e62005-04-22 00:43:47 +0000108#define THREAD_BACKGROUND 5
109#define THREAD_UNUSED 6
110#define THREAD_EXECUTE 7
paul718e3742002-12-13 20:15:29 +0000111
112/* Thread yield time. */
paul17fc1282005-04-22 00:57:03 +0000113#define THREAD_YIELD_TIME_SLOT 10 * 1000L /* 10ms */
paul718e3742002-12-13 20:15:29 +0000114
115/* Macros. */
116#define THREAD_ARG(X) ((X)->arg)
117#define THREAD_FD(X) ((X)->u.fd)
118#define THREAD_VAL(X) ((X)->u.val)
119
120#define THREAD_READ_ON(master,thread,func,arg,sock) \
121 do { \
122 if (! thread) \
123 thread = thread_add_read (master, func, arg, sock); \
124 } while (0)
125
126#define THREAD_WRITE_ON(master,thread,func,arg,sock) \
127 do { \
128 if (! thread) \
129 thread = thread_add_write (master, func, arg, sock); \
130 } while (0)
131
132#define THREAD_TIMER_ON(master,thread,func,arg,time) \
133 do { \
134 if (! thread) \
135 thread = thread_add_timer (master, func, arg, time); \
136 } while (0)
137
138#define THREAD_OFF(thread) \
139 do { \
140 if (thread) \
141 { \
142 thread_cancel (thread); \
143 thread = NULL; \
144 } \
145 } while (0)
146
147#define THREAD_READ_OFF(thread) THREAD_OFF(thread)
148#define THREAD_WRITE_OFF(thread) THREAD_OFF(thread)
149#define THREAD_TIMER_OFF(thread) THREAD_OFF(thread)
150
paule04ab742003-01-17 23:47:00 +0000151#define thread_add_read(m,f,a,v) funcname_thread_add_read(m,f,a,v,#f)
152#define thread_add_write(m,f,a,v) funcname_thread_add_write(m,f,a,v,#f)
153#define thread_add_timer(m,f,a,v) funcname_thread_add_timer(m,f,a,v,#f)
jardin9e867fe2003-12-23 08:56:18 +0000154#define thread_add_timer_msec(m,f,a,v) funcname_thread_add_timer_msec(m,f,a,v,#f)
paule04ab742003-01-17 23:47:00 +0000155#define thread_add_event(m,f,a,v) funcname_thread_add_event(m,f,a,v,#f)
156#define thread_execute(m,f,a,v) funcname_thread_execute(m,f,a,v,#f)
ajsfb9e46b2005-04-22 14:23:34 +0000157
158/* The 4th arg to thread_add_background is the # of milliseconds to delay. */
paula48b4e62005-04-22 00:43:47 +0000159#define thread_add_background(m,f,a,v) funcname_thread_add_background(m,f,a,v,#f)
paule04ab742003-01-17 23:47:00 +0000160
paul718e3742002-12-13 20:15:29 +0000161/* Prototypes. */
paul8cc41982005-05-06 21:25:49 +0000162extern struct thread_master *thread_master_create (void);
163extern void thread_master_free (struct thread_master *);
164
165extern struct thread *funcname_thread_add_read (struct thread_master *,
166 int (*)(struct thread *),
167 void *, int, const char*);
168extern struct thread *funcname_thread_add_write (struct thread_master *,
169 int (*)(struct thread *),
170 void *, int, const char*);
171extern struct thread *funcname_thread_add_timer (struct thread_master *,
172 int (*)(struct thread *),
173 void *, long, const char*);
174extern struct thread *funcname_thread_add_timer_msec (struct thread_master *,
175 int (*)(struct thread *),
176 void *, long, const char*);
177extern struct thread *funcname_thread_add_event (struct thread_master *,
178 int (*)(struct thread *),
179 void *, int, const char*);
180extern struct thread *funcname_thread_add_background (struct thread_master *,
181 int (*func)(struct thread *),
ajsfb9e46b2005-04-22 14:23:34 +0000182 void *arg,
183 long milliseconds_to_delay,
184 const char *funcname);
paul8cc41982005-05-06 21:25:49 +0000185extern struct thread *funcname_thread_execute (struct thread_master *,
186 int (*)(struct thread *),
187 void *, int, const char *);
188extern void thread_cancel (struct thread *);
pauldc818072005-05-19 01:30:53 +0000189extern unsigned int thread_cancel_event (struct thread_master *, void *);
paul8cc41982005-05-06 21:25:49 +0000190extern struct thread *thread_fetch (struct thread_master *, struct thread *);
191extern void thread_call (struct thread *);
192extern unsigned long thread_timer_remain_second (struct thread *);
193extern int thread_should_yield (struct thread *);
paul718e3742002-12-13 20:15:29 +0000194
Paul Jakmadb9c0df2006-08-27 06:44:02 +0000195/* Internal libzebra exports */
196extern void thread_getrusage (RUSAGE_T *);
paule04ab742003-01-17 23:47:00 +0000197extern struct cmd_element show_thread_cpu_cmd;
198
Paul Jakmadb9c0df2006-08-27 06:44:02 +0000199/* replacements for the system gettimeofday(), clock_gettime() and
200 * time() functions, providing support for non-decrementing clock on
201 * all systems, and fully monotonic on /some/ systems.
202 */
203extern int quagga_gettime (enum quagga_clkid, struct timeval *);
204extern time_t quagga_time (time_t *);
205
ajs8b70d0b2005-04-28 01:31:13 +0000206/* Returns elapsed real (wall clock) time. */
207extern unsigned long thread_consumed_time(RUSAGE_T *after, RUSAGE_T *before,
208 unsigned long *cpu_time_elapsed);
209
210/* Global variable containing a recent result from gettimeofday. This can
211 be used instead of calling gettimeofday if a recent value is sufficient.
212 This is guaranteed to be refreshed before a thread is called. */
213extern struct timeval recent_time;
Paul Jakmadb9c0df2006-08-27 06:44:02 +0000214/* Similar to recent_time, but a monotonically increasing time value */
215extern struct timeval recent_relative_time (void);
paul718e3742002-12-13 20:15:29 +0000216#endif /* _ZEBRA_THREAD_H */