paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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 | |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 25 | struct rusage_t |
| 26 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 27 | #ifdef HAVE_RUSAGE |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 28 | struct rusage cpu; |
| 29 | #endif |
| 30 | struct timeval real; |
| 31 | }; |
| 32 | #define RUSAGE_T struct rusage_t |
| 33 | |
| 34 | #ifdef HAVE_RUSAGE |
| 35 | #define GETRUSAGE(X) \ |
| 36 | getrusage(RUSAGE_SELF, &((X)->cpu)); \ |
| 37 | gettimeofday(&recent_time, NULL); (X)->real = recent_time |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 38 | #else |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 39 | #define GETRUSAGE(X) \ |
| 40 | gettimeofday(&recent_time, NULL); (X)->real = recent_time |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 41 | #endif /* HAVE_RUSAGE */ |
| 42 | |
| 43 | /* Linked list of thread. */ |
| 44 | struct thread_list |
| 45 | { |
| 46 | struct thread *head; |
| 47 | struct thread *tail; |
| 48 | int count; |
| 49 | }; |
| 50 | |
| 51 | /* Master of the theads. */ |
| 52 | struct thread_master |
| 53 | { |
| 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; |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 60 | struct thread_list background; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 61 | fd_set readfd; |
| 62 | fd_set writefd; |
| 63 | fd_set exceptfd; |
| 64 | unsigned long alloc; |
| 65 | }; |
| 66 | |
| 67 | /* Thread itself. */ |
| 68 | struct thread |
| 69 | { |
| 70 | unsigned char type; /* thread type */ |
ajs | fb9e46b | 2005-04-22 14:23:34 +0000 | [diff] [blame] | 71 | unsigned char add_type; /* thread type */ |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 72 | struct thread *next; /* next pointer of the thread */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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. */ |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 83 | char* funcname; |
| 84 | }; |
| 85 | |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 86 | struct cpu_thread_history |
| 87 | { |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 88 | int (*func)(struct thread *); |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 89 | const char *funcname; |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 90 | unsigned int total_calls; |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 91 | struct time_stats |
| 92 | { |
| 93 | unsigned long total, max; |
| 94 | } real; |
| 95 | #ifdef HAVE_RUSAGE |
| 96 | struct time_stats cpu; |
| 97 | #endif |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 98 | unsigned char types; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | /* Thread types. */ |
| 102 | #define THREAD_READ 0 |
| 103 | #define THREAD_WRITE 1 |
| 104 | #define THREAD_TIMER 2 |
| 105 | #define THREAD_EVENT 3 |
| 106 | #define THREAD_READY 4 |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 107 | #define THREAD_BACKGROUND 5 |
| 108 | #define THREAD_UNUSED 6 |
| 109 | #define THREAD_EXECUTE 7 |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 110 | |
| 111 | /* Thread yield time. */ |
paul | 17fc128 | 2005-04-22 00:57:03 +0000 | [diff] [blame] | 112 | #define THREAD_YIELD_TIME_SLOT 10 * 1000L /* 10ms */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 113 | |
| 114 | /* Macros. */ |
| 115 | #define THREAD_ARG(X) ((X)->arg) |
| 116 | #define THREAD_FD(X) ((X)->u.fd) |
| 117 | #define THREAD_VAL(X) ((X)->u.val) |
| 118 | |
| 119 | #define THREAD_READ_ON(master,thread,func,arg,sock) \ |
| 120 | do { \ |
| 121 | if (! thread) \ |
| 122 | thread = thread_add_read (master, func, arg, sock); \ |
| 123 | } while (0) |
| 124 | |
| 125 | #define THREAD_WRITE_ON(master,thread,func,arg,sock) \ |
| 126 | do { \ |
| 127 | if (! thread) \ |
| 128 | thread = thread_add_write (master, func, arg, sock); \ |
| 129 | } while (0) |
| 130 | |
| 131 | #define THREAD_TIMER_ON(master,thread,func,arg,time) \ |
| 132 | do { \ |
| 133 | if (! thread) \ |
| 134 | thread = thread_add_timer (master, func, arg, time); \ |
| 135 | } while (0) |
| 136 | |
| 137 | #define THREAD_OFF(thread) \ |
| 138 | do { \ |
| 139 | if (thread) \ |
| 140 | { \ |
| 141 | thread_cancel (thread); \ |
| 142 | thread = NULL; \ |
| 143 | } \ |
| 144 | } while (0) |
| 145 | |
| 146 | #define THREAD_READ_OFF(thread) THREAD_OFF(thread) |
| 147 | #define THREAD_WRITE_OFF(thread) THREAD_OFF(thread) |
| 148 | #define THREAD_TIMER_OFF(thread) THREAD_OFF(thread) |
| 149 | |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 150 | #define thread_add_read(m,f,a,v) funcname_thread_add_read(m,f,a,v,#f) |
| 151 | #define thread_add_write(m,f,a,v) funcname_thread_add_write(m,f,a,v,#f) |
| 152 | #define thread_add_timer(m,f,a,v) funcname_thread_add_timer(m,f,a,v,#f) |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 153 | #define thread_add_timer_msec(m,f,a,v) funcname_thread_add_timer_msec(m,f,a,v,#f) |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 154 | #define thread_add_event(m,f,a,v) funcname_thread_add_event(m,f,a,v,#f) |
| 155 | #define thread_execute(m,f,a,v) funcname_thread_execute(m,f,a,v,#f) |
ajs | fb9e46b | 2005-04-22 14:23:34 +0000 | [diff] [blame] | 156 | |
| 157 | /* The 4th arg to thread_add_background is the # of milliseconds to delay. */ |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 158 | #define thread_add_background(m,f,a,v) funcname_thread_add_background(m,f,a,v,#f) |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 159 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 160 | /* Prototypes. */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 161 | extern struct thread_master *thread_master_create (void); |
| 162 | extern void thread_master_free (struct thread_master *); |
| 163 | |
| 164 | extern struct thread *funcname_thread_add_read (struct thread_master *, |
| 165 | int (*)(struct thread *), |
| 166 | void *, int, const char*); |
| 167 | extern struct thread *funcname_thread_add_write (struct thread_master *, |
| 168 | int (*)(struct thread *), |
| 169 | void *, int, const char*); |
| 170 | extern struct thread *funcname_thread_add_timer (struct thread_master *, |
| 171 | int (*)(struct thread *), |
| 172 | void *, long, const char*); |
| 173 | extern struct thread *funcname_thread_add_timer_msec (struct thread_master *, |
| 174 | int (*)(struct thread *), |
| 175 | void *, long, const char*); |
| 176 | extern struct thread *funcname_thread_add_event (struct thread_master *, |
| 177 | int (*)(struct thread *), |
| 178 | void *, int, const char*); |
| 179 | extern struct thread *funcname_thread_add_background (struct thread_master *, |
| 180 | int (*func)(struct thread *), |
ajs | fb9e46b | 2005-04-22 14:23:34 +0000 | [diff] [blame] | 181 | void *arg, |
| 182 | long milliseconds_to_delay, |
| 183 | const char *funcname); |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame] | 184 | extern struct thread *funcname_thread_execute (struct thread_master *, |
| 185 | int (*)(struct thread *), |
| 186 | void *, int, const char *); |
| 187 | extern void thread_cancel (struct thread *); |
| 188 | extern void thread_cancel_event (struct thread_master *, void *); |
| 189 | extern struct thread *thread_fetch (struct thread_master *, struct thread *); |
| 190 | extern void thread_call (struct thread *); |
| 191 | extern unsigned long thread_timer_remain_second (struct thread *); |
| 192 | extern int thread_should_yield (struct thread *); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 193 | |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 194 | extern struct cmd_element show_thread_cpu_cmd; |
| 195 | |
ajs | 8b70d0b | 2005-04-28 01:31:13 +0000 | [diff] [blame] | 196 | /* Returns elapsed real (wall clock) time. */ |
| 197 | extern unsigned long thread_consumed_time(RUSAGE_T *after, RUSAGE_T *before, |
| 198 | unsigned long *cpu_time_elapsed); |
| 199 | |
| 200 | /* Global variable containing a recent result from gettimeofday. This can |
| 201 | be used instead of calling gettimeofday if a recent value is sufficient. |
| 202 | This is guaranteed to be refreshed before a thread is called. */ |
| 203 | extern struct timeval recent_time; |
ajs | 924b922 | 2005-04-16 17:11:24 +0000 | [diff] [blame] | 204 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 205 | #endif /* _ZEBRA_THREAD_H */ |