blob: 69bb8d6c5e864bf4fd057d945fc2bf75c8942369 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/* Thread management routine header.
2 * Copyright (C) 1998 Kunihiro Ishiguro
Everton Marquese8540952012-02-16 06:14:54 +01003 * Portions Copyright (c) 2008 Everton da Silva Marques <everton.marques@gmail.com>
paul718e3742002-12-13 20:15:29 +00004 *
5 * This file is part of GNU Zebra.
6 *
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
11 *
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Zebra; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 * 02111-1307, USA.
21 */
22
23#ifndef _ZEBRA_THREAD_H
24#define _ZEBRA_THREAD_H
25
ajs8b70d0b2005-04-28 01:31:13 +000026struct rusage_t
27{
paul718e3742002-12-13 20:15:29 +000028#ifdef HAVE_RUSAGE
ajs8b70d0b2005-04-28 01:31:13 +000029 struct rusage cpu;
30#endif
31 struct timeval real;
32};
33#define RUSAGE_T struct rusage_t
34
Paul Jakmadb9c0df2006-08-27 06:44:02 +000035#define GETRUSAGE(X) thread_getrusage(X)
paul718e3742002-12-13 20:15:29 +000036
37/* Linked list of thread. */
38struct thread_list
39{
40 struct thread *head;
41 struct thread *tail;
42 int count;
43};
44
45/* Master of the theads. */
46struct thread_master
47{
48 struct thread_list read;
49 struct thread_list write;
50 struct thread_list timer;
51 struct thread_list event;
52 struct thread_list ready;
53 struct thread_list unuse;
paula48b4e62005-04-22 00:43:47 +000054 struct thread_list background;
paul718e3742002-12-13 20:15:29 +000055 fd_set readfd;
56 fd_set writefd;
57 fd_set exceptfd;
58 unsigned long alloc;
59};
60
Paul Jakma41b23732009-06-30 16:12:49 +010061typedef unsigned char thread_type;
62
paul718e3742002-12-13 20:15:29 +000063/* Thread itself. */
64struct thread
65{
Paul Jakma41b23732009-06-30 16:12:49 +010066 thread_type type; /* thread type */
67 thread_type add_type; /* thread type */
paula48b4e62005-04-22 00:43:47 +000068 struct thread *next; /* next pointer of the thread */
paul718e3742002-12-13 20:15:29 +000069 struct thread *prev; /* previous pointer of the thread */
70 struct thread_master *master; /* pointer to the struct thread_master. */
71 int (*func) (struct thread *); /* event function */
72 void *arg; /* event argument */
73 union {
74 int val; /* second argument of the event. */
75 int fd; /* file descriptor in case of read/write. */
76 struct timeval sands; /* rest of time sands value. */
77 } u;
78 RUSAGE_T ru; /* Indepth usage info. */
Paul Jakmacc8b13a2006-07-25 20:40:40 +000079 struct cpu_thread_history *hist; /* cache pointer to cpu_history */
paule04ab742003-01-17 23:47:00 +000080 char* funcname;
81};
82
ajs8b70d0b2005-04-28 01:31:13 +000083struct cpu_thread_history
84{
paule04ab742003-01-17 23:47:00 +000085 int (*func)(struct thread *);
Paul Jakma85261002010-01-11 16:30:45 +000086 char *funcname;
paule04ab742003-01-17 23:47:00 +000087 unsigned int total_calls;
ajs8b70d0b2005-04-28 01:31:13 +000088 struct time_stats
89 {
90 unsigned long total, max;
91 } real;
92#ifdef HAVE_RUSAGE
93 struct time_stats cpu;
94#endif
Paul Jakma41b23732009-06-30 16:12:49 +010095 thread_type types;
paul718e3742002-12-13 20:15:29 +000096};
97
Paul Jakmadb9c0df2006-08-27 06:44:02 +000098/* Clocks supported by Quagga */
99enum quagga_clkid {
100 QUAGGA_CLK_REALTIME = 0, /* ala gettimeofday() */
101 QUAGGA_CLK_MONOTONIC, /* monotonic, against an indeterminate base */
102 QUAGGA_CLK_REALTIME_STABILISED, /* like realtime, but non-decrementing */
103};
104
paul718e3742002-12-13 20:15:29 +0000105/* Thread types. */
106#define THREAD_READ 0
107#define THREAD_WRITE 1
108#define THREAD_TIMER 2
109#define THREAD_EVENT 3
110#define THREAD_READY 4
paula48b4e62005-04-22 00:43:47 +0000111#define THREAD_BACKGROUND 5
112#define THREAD_UNUSED 6
113#define THREAD_EXECUTE 7
paul718e3742002-12-13 20:15:29 +0000114
115/* Thread yield time. */
paul17fc1282005-04-22 00:57:03 +0000116#define THREAD_YIELD_TIME_SLOT 10 * 1000L /* 10ms */
paul718e3742002-12-13 20:15:29 +0000117
118/* Macros. */
119#define THREAD_ARG(X) ((X)->arg)
120#define THREAD_FD(X) ((X)->u.fd)
121#define THREAD_VAL(X) ((X)->u.val)
122
123#define THREAD_READ_ON(master,thread,func,arg,sock) \
124 do { \
125 if (! thread) \
126 thread = thread_add_read (master, func, arg, sock); \
127 } while (0)
128
129#define THREAD_WRITE_ON(master,thread,func,arg,sock) \
130 do { \
131 if (! thread) \
132 thread = thread_add_write (master, func, arg, sock); \
133 } while (0)
134
135#define THREAD_TIMER_ON(master,thread,func,arg,time) \
136 do { \
137 if (! thread) \
138 thread = thread_add_timer (master, func, arg, time); \
139 } while (0)
140
Everton Marquese8540952012-02-16 06:14:54 +0100141#define THREAD_TIMER_MSEC_ON(master,thread,func,arg,time) \
142 do { \
143 if (! thread) \
144 thread = thread_add_timer_msec (master, func, arg, time); \
145 } while (0)
146
paul718e3742002-12-13 20:15:29 +0000147#define THREAD_OFF(thread) \
148 do { \
149 if (thread) \
150 { \
151 thread_cancel (thread); \
152 thread = NULL; \
153 } \
154 } while (0)
155
156#define THREAD_READ_OFF(thread) THREAD_OFF(thread)
157#define THREAD_WRITE_OFF(thread) THREAD_OFF(thread)
158#define THREAD_TIMER_OFF(thread) THREAD_OFF(thread)
159
paule04ab742003-01-17 23:47:00 +0000160#define thread_add_read(m,f,a,v) funcname_thread_add_read(m,f,a,v,#f)
161#define thread_add_write(m,f,a,v) funcname_thread_add_write(m,f,a,v,#f)
162#define thread_add_timer(m,f,a,v) funcname_thread_add_timer(m,f,a,v,#f)
jardin9e867fe2003-12-23 08:56:18 +0000163#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 +0000164#define thread_add_event(m,f,a,v) funcname_thread_add_event(m,f,a,v,#f)
165#define thread_execute(m,f,a,v) funcname_thread_execute(m,f,a,v,#f)
ajsfb9e46b2005-04-22 14:23:34 +0000166
167/* The 4th arg to thread_add_background is the # of milliseconds to delay. */
paula48b4e62005-04-22 00:43:47 +0000168#define thread_add_background(m,f,a,v) funcname_thread_add_background(m,f,a,v,#f)
paule04ab742003-01-17 23:47:00 +0000169
paul718e3742002-12-13 20:15:29 +0000170/* Prototypes. */
paul8cc41982005-05-06 21:25:49 +0000171extern struct thread_master *thread_master_create (void);
172extern void thread_master_free (struct thread_master *);
173
174extern struct thread *funcname_thread_add_read (struct thread_master *,
175 int (*)(struct thread *),
176 void *, int, const char*);
177extern struct thread *funcname_thread_add_write (struct thread_master *,
178 int (*)(struct thread *),
179 void *, int, const char*);
180extern struct thread *funcname_thread_add_timer (struct thread_master *,
181 int (*)(struct thread *),
182 void *, long, const char*);
183extern struct thread *funcname_thread_add_timer_msec (struct thread_master *,
184 int (*)(struct thread *),
185 void *, long, const char*);
186extern struct thread *funcname_thread_add_event (struct thread_master *,
187 int (*)(struct thread *),
188 void *, int, const char*);
189extern struct thread *funcname_thread_add_background (struct thread_master *,
190 int (*func)(struct thread *),
ajsfb9e46b2005-04-22 14:23:34 +0000191 void *arg,
192 long milliseconds_to_delay,
193 const char *funcname);
paul8cc41982005-05-06 21:25:49 +0000194extern struct thread *funcname_thread_execute (struct thread_master *,
195 int (*)(struct thread *),
196 void *, int, const char *);
197extern void thread_cancel (struct thread *);
pauldc818072005-05-19 01:30:53 +0000198extern unsigned int thread_cancel_event (struct thread_master *, void *);
paul8cc41982005-05-06 21:25:49 +0000199extern struct thread *thread_fetch (struct thread_master *, struct thread *);
200extern void thread_call (struct thread *);
201extern unsigned long thread_timer_remain_second (struct thread *);
202extern int thread_should_yield (struct thread *);
paul718e3742002-12-13 20:15:29 +0000203
Paul Jakmadb9c0df2006-08-27 06:44:02 +0000204/* Internal libzebra exports */
205extern void thread_getrusage (RUSAGE_T *);
paule04ab742003-01-17 23:47:00 +0000206extern struct cmd_element show_thread_cpu_cmd;
Paul Jakmae276eb82010-01-09 16:15:00 +0000207extern struct cmd_element clear_thread_cpu_cmd;
paule04ab742003-01-17 23:47:00 +0000208
Paul Jakmadb9c0df2006-08-27 06:44:02 +0000209/* replacements for the system gettimeofday(), clock_gettime() and
210 * time() functions, providing support for non-decrementing clock on
211 * all systems, and fully monotonic on /some/ systems.
212 */
213extern int quagga_gettime (enum quagga_clkid, struct timeval *);
214extern time_t quagga_time (time_t *);
215
ajs8b70d0b2005-04-28 01:31:13 +0000216/* Returns elapsed real (wall clock) time. */
217extern unsigned long thread_consumed_time(RUSAGE_T *after, RUSAGE_T *before,
218 unsigned long *cpu_time_elapsed);
219
220/* Global variable containing a recent result from gettimeofday. This can
221 be used instead of calling gettimeofday if a recent value is sufficient.
222 This is guaranteed to be refreshed before a thread is called. */
223extern struct timeval recent_time;
Paul Jakmadb9c0df2006-08-27 06:44:02 +0000224/* Similar to recent_time, but a monotonically increasing time value */
225extern struct timeval recent_relative_time (void);
paul718e3742002-12-13 20:15:29 +0000226#endif /* _ZEBRA_THREAD_H */