[lib] Optimise thread_call by caching pointer to thread history in the thread
[jleu-quagga.git] / lib / thread.h
blob0670a890f1aef5647d45eb8684d4646d8e0b2aaa
1 /* Thread management routine header.
2 * Copyright (C) 1998 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
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.
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.
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.
22 #ifndef _ZEBRA_THREAD_H
23 #define _ZEBRA_THREAD_H
25 struct rusage_t
27 #ifdef HAVE_RUSAGE
28 struct rusage cpu;
29 #endif
30 struct timeval real;
32 #define RUSAGE_T struct rusage_t
34 #ifdef HAVE_RUSAGE
35 #define GETRUSAGE(X) \
36 getrusage(RUSAGE_SELF, &((X)->cpu)); \
37 gettimeofday(&recent_time, NULL); (X)->real = recent_time
38 #else
39 #define GETRUSAGE(X) \
40 gettimeofday(&recent_time, NULL); (X)->real = recent_time
41 #endif /* HAVE_RUSAGE */
43 /* Linked list of thread. */
44 struct thread_list
46 struct thread *head;
47 struct thread *tail;
48 int count;
51 /* Master of the theads. */
52 struct thread_master
54 struct thread_list read;
55 struct thread_list write;
56 struct thread_list timer;
57 struct thread_list event;
58 struct thread_list ready;
59 struct thread_list unuse;
60 struct thread_list background;
61 fd_set readfd;
62 fd_set writefd;
63 fd_set exceptfd;
64 unsigned long alloc;
67 /* Thread itself. */
68 struct thread
70 unsigned char type; /* thread type */
71 unsigned char add_type; /* thread type */
72 struct thread *next; /* next pointer of the thread */
73 struct thread *prev; /* previous pointer of the thread */
74 struct thread_master *master; /* pointer to the struct thread_master. */
75 int (*func) (struct thread *); /* event function */
76 void *arg; /* event argument */
77 union {
78 int val; /* second argument of the event. */
79 int fd; /* file descriptor in case of read/write. */
80 struct timeval sands; /* rest of time sands value. */
81 } u;
82 RUSAGE_T ru; /* Indepth usage info. */
83 struct cpu_thread_history *hist; /* cache pointer to cpu_history */
84 char* funcname;
87 struct cpu_thread_history
89 int (*func)(struct thread *);
90 const char *funcname;
91 unsigned int total_calls;
92 struct time_stats
94 unsigned long total, max;
95 } real;
96 #ifdef HAVE_RUSAGE
97 struct time_stats cpu;
98 #endif
99 unsigned char types;
102 /* 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
108 #define THREAD_BACKGROUND 5
109 #define THREAD_UNUSED 6
110 #define THREAD_EXECUTE 7
112 /* Thread yield time. */
113 #define THREAD_YIELD_TIME_SLOT 10 * 1000L /* 10ms */
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)
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)
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)
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)
138 #define THREAD_OFF(thread) \
139 do { \
140 if (thread) \
142 thread_cancel (thread); \
143 thread = NULL; \
145 } while (0)
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)
151 #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)
154 #define thread_add_timer_msec(m,f,a,v) funcname_thread_add_timer_msec(m,f,a,v,#f)
155 #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)
158 /* The 4th arg to thread_add_background is the # of milliseconds to delay. */
159 #define thread_add_background(m,f,a,v) funcname_thread_add_background(m,f,a,v,#f)
161 /* Prototypes. */
162 extern struct thread_master *thread_master_create (void);
163 extern void thread_master_free (struct thread_master *);
165 extern struct thread *funcname_thread_add_read (struct thread_master *,
166 int (*)(struct thread *),
167 void *, int, const char*);
168 extern struct thread *funcname_thread_add_write (struct thread_master *,
169 int (*)(struct thread *),
170 void *, int, const char*);
171 extern struct thread *funcname_thread_add_timer (struct thread_master *,
172 int (*)(struct thread *),
173 void *, long, const char*);
174 extern struct thread *funcname_thread_add_timer_msec (struct thread_master *,
175 int (*)(struct thread *),
176 void *, long, const char*);
177 extern struct thread *funcname_thread_add_event (struct thread_master *,
178 int (*)(struct thread *),
179 void *, int, const char*);
180 extern struct thread *funcname_thread_add_background (struct thread_master *,
181 int (*func)(struct thread *),
182 void *arg,
183 long milliseconds_to_delay,
184 const char *funcname);
185 extern struct thread *funcname_thread_execute (struct thread_master *,
186 int (*)(struct thread *),
187 void *, int, const char *);
188 extern void thread_cancel (struct thread *);
189 extern unsigned int thread_cancel_event (struct thread_master *, void *);
190 extern struct thread *thread_fetch (struct thread_master *, struct thread *);
191 extern void thread_call (struct thread *);
192 extern unsigned long thread_timer_remain_second (struct thread *);
193 extern int thread_should_yield (struct thread *);
195 extern struct cmd_element show_thread_cpu_cmd;
197 /* Returns elapsed real (wall clock) time. */
198 extern unsigned long thread_consumed_time(RUSAGE_T *after, RUSAGE_T *before,
199 unsigned long *cpu_time_elapsed);
201 /* Global variable containing a recent result from gettimeofday. This can
202 be used instead of calling gettimeofday if a recent value is sufficient.
203 This is guaranteed to be refreshed before a thread is called. */
204 extern struct timeval recent_time;
206 #endif /* _ZEBRA_THREAD_H */