]> git.proxmox.com Git - mirror_frr.git/blame - lib/thread.h
2004-07-23 Paul Jakma <paul@dishone.st>
[mirror_frr.git] / lib / thread.h
CommitLineData
718e3744 1/* 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
25#ifdef HAVE_RUSAGE
26#define RUSAGE_T struct rusage
27#define GETRUSAGE(X) getrusage (RUSAGE_SELF, X);
28#else
29#define RUSAGE_T struct timeval
30#define GETRUSAGE(X) gettimeofday (X, NULL);
31#endif /* HAVE_RUSAGE */
32
33/* Linked list of thread. */
34struct thread_list
35{
36 struct thread *head;
37 struct thread *tail;
38 int count;
39};
40
41/* Master of the theads. */
42struct thread_master
43{
44 struct thread_list read;
45 struct thread_list write;
46 struct thread_list timer;
47 struct thread_list event;
48 struct thread_list ready;
49 struct thread_list unuse;
50 fd_set readfd;
51 fd_set writefd;
52 fd_set exceptfd;
53 unsigned long alloc;
54};
55
56/* Thread itself. */
57struct thread
58{
59 unsigned char type; /* thread type */
e04ab74d 60 unsigned char add_type; /* thread type */
718e3744 61 struct thread *next; /* next pointer of the thread */
62 struct thread *prev; /* previous pointer of the thread */
63 struct thread_master *master; /* pointer to the struct thread_master. */
64 int (*func) (struct thread *); /* event function */
65 void *arg; /* event argument */
66 union {
67 int val; /* second argument of the event. */
68 int fd; /* file descriptor in case of read/write. */
69 struct timeval sands; /* rest of time sands value. */
70 } u;
71 RUSAGE_T ru; /* Indepth usage info. */
e04ab74d 72 char* funcname;
73};
74
75struct cpu_thread_history {
76 int (*func)(struct thread *);
77 char *funcname;
78 unsigned int total_calls;
79 unsigned long total, max;
80 unsigned char types;
718e3744 81};
82
83/* Thread types. */
84#define THREAD_READ 0
85#define THREAD_WRITE 1
86#define THREAD_TIMER 2
87#define THREAD_EVENT 3
88#define THREAD_READY 4
89#define THREAD_UNUSED 5
e04ab74d 90#define THREAD_EXECUTE 6
718e3744 91
92/* Thread yield time. */
93#define THREAD_YIELD_TIME_SLOT 100 * 1000L /* 100ms */
94
95/* Macros. */
96#define THREAD_ARG(X) ((X)->arg)
97#define THREAD_FD(X) ((X)->u.fd)
98#define THREAD_VAL(X) ((X)->u.val)
99
100#define THREAD_READ_ON(master,thread,func,arg,sock) \
101 do { \
102 if (! thread) \
103 thread = thread_add_read (master, func, arg, sock); \
104 } while (0)
105
106#define THREAD_WRITE_ON(master,thread,func,arg,sock) \
107 do { \
108 if (! thread) \
109 thread = thread_add_write (master, func, arg, sock); \
110 } while (0)
111
112#define THREAD_TIMER_ON(master,thread,func,arg,time) \
113 do { \
114 if (! thread) \
115 thread = thread_add_timer (master, func, arg, time); \
116 } while (0)
117
118#define THREAD_OFF(thread) \
119 do { \
120 if (thread) \
121 { \
122 thread_cancel (thread); \
123 thread = NULL; \
124 } \
125 } while (0)
126
127#define THREAD_READ_OFF(thread) THREAD_OFF(thread)
128#define THREAD_WRITE_OFF(thread) THREAD_OFF(thread)
129#define THREAD_TIMER_OFF(thread) THREAD_OFF(thread)
130
e04ab74d 131#define thread_add_read(m,f,a,v) funcname_thread_add_read(m,f,a,v,#f)
132#define thread_add_write(m,f,a,v) funcname_thread_add_write(m,f,a,v,#f)
133#define thread_add_timer(m,f,a,v) funcname_thread_add_timer(m,f,a,v,#f)
9e867fe6 134#define thread_add_timer_msec(m,f,a,v) funcname_thread_add_timer_msec(m,f,a,v,#f)
e04ab74d 135#define thread_add_event(m,f,a,v) funcname_thread_add_event(m,f,a,v,#f)
136#define thread_execute(m,f,a,v) funcname_thread_execute(m,f,a,v,#f)
137
718e3744 138/* Prototypes. */
139struct thread_master *thread_master_create ();
e04ab74d 140struct thread *funcname_thread_add_read (struct thread_master *,
141 int (*)(struct thread *), void *, int, char*);
142struct thread *funcname_thread_add_write (struct thread_master *,
143 int (*)(struct thread *), void *, int, char*);
144struct thread *funcname_thread_add_timer (struct thread_master *,
145 int (*)(struct thread *), void *, long, char*);
9e867fe6 146struct thread *funcname_thread_add_timer_msec (struct thread_master *,
147 int (*)(struct thread *), void *, long, char*);
e04ab74d 148struct thread *funcname_thread_add_event (struct thread_master *,
149 int (*)(struct thread *), void *, int, char*);
718e3744 150void thread_cancel (struct thread *);
151void thread_cancel_event (struct thread_master *, void *);
152
153struct thread *thread_fetch (struct thread_master *, struct thread *);
e04ab74d 154struct thread *funcname_thread_execute (struct thread_master *,
155 int (*)(struct thread *), void *, int, char *);
718e3744 156void thread_call (struct thread *);
157unsigned long thread_timer_remain_second (struct thread *);
158
e04ab74d 159extern struct cmd_element show_thread_cpu_cmd;
160
718e3744 161#endif /* _ZEBRA_THREAD_H */