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 | |
| 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. */ |
| 34 | struct thread_list |
| 35 | { |
| 36 | struct thread *head; |
| 37 | struct thread *tail; |
| 38 | int count; |
| 39 | }; |
| 40 | |
| 41 | /* Master of the theads. */ |
| 42 | struct 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; |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 50 | struct thread_list background; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 51 | fd_set readfd; |
| 52 | fd_set writefd; |
| 53 | fd_set exceptfd; |
| 54 | unsigned long alloc; |
| 55 | }; |
| 56 | |
| 57 | /* Thread itself. */ |
| 58 | struct thread |
| 59 | { |
| 60 | unsigned char type; /* thread type */ |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 61 | unsigned add_type; /* thread type */ |
| 62 | struct thread *next; /* next pointer of the thread */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 63 | struct thread *prev; /* previous pointer of the thread */ |
| 64 | struct thread_master *master; /* pointer to the struct thread_master. */ |
| 65 | int (*func) (struct thread *); /* event function */ |
| 66 | void *arg; /* event argument */ |
| 67 | union { |
| 68 | int val; /* second argument of the event. */ |
| 69 | int fd; /* file descriptor in case of read/write. */ |
| 70 | struct timeval sands; /* rest of time sands value. */ |
| 71 | } u; |
| 72 | RUSAGE_T ru; /* Indepth usage info. */ |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 73 | char* funcname; |
| 74 | }; |
| 75 | |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 76 | struct cpu_thread_history { |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 77 | int (*func)(struct thread *); |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 78 | const char *funcname; |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 79 | unsigned int total_calls; |
| 80 | unsigned long total, max; |
| 81 | unsigned char types; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | /* Thread types. */ |
| 85 | #define THREAD_READ 0 |
| 86 | #define THREAD_WRITE 1 |
| 87 | #define THREAD_TIMER 2 |
| 88 | #define THREAD_EVENT 3 |
| 89 | #define THREAD_READY 4 |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 90 | #define THREAD_BACKGROUND 5 |
| 91 | #define THREAD_UNUSED 6 |
| 92 | #define THREAD_EXECUTE 7 |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 93 | |
| 94 | /* Thread yield time. */ |
paul | 17fc128 | 2005-04-22 00:57:03 +0000 | [diff] [blame^] | 95 | #define THREAD_YIELD_TIME_SLOT 10 * 1000L /* 10ms */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 96 | |
| 97 | /* Macros. */ |
| 98 | #define THREAD_ARG(X) ((X)->arg) |
| 99 | #define THREAD_FD(X) ((X)->u.fd) |
| 100 | #define THREAD_VAL(X) ((X)->u.val) |
| 101 | |
| 102 | #define THREAD_READ_ON(master,thread,func,arg,sock) \ |
| 103 | do { \ |
| 104 | if (! thread) \ |
| 105 | thread = thread_add_read (master, func, arg, sock); \ |
| 106 | } while (0) |
| 107 | |
| 108 | #define THREAD_WRITE_ON(master,thread,func,arg,sock) \ |
| 109 | do { \ |
| 110 | if (! thread) \ |
| 111 | thread = thread_add_write (master, func, arg, sock); \ |
| 112 | } while (0) |
| 113 | |
| 114 | #define THREAD_TIMER_ON(master,thread,func,arg,time) \ |
| 115 | do { \ |
| 116 | if (! thread) \ |
| 117 | thread = thread_add_timer (master, func, arg, time); \ |
| 118 | } while (0) |
| 119 | |
| 120 | #define THREAD_OFF(thread) \ |
| 121 | do { \ |
| 122 | if (thread) \ |
| 123 | { \ |
| 124 | thread_cancel (thread); \ |
| 125 | thread = NULL; \ |
| 126 | } \ |
| 127 | } while (0) |
| 128 | |
| 129 | #define THREAD_READ_OFF(thread) THREAD_OFF(thread) |
| 130 | #define THREAD_WRITE_OFF(thread) THREAD_OFF(thread) |
| 131 | #define THREAD_TIMER_OFF(thread) THREAD_OFF(thread) |
| 132 | |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 133 | #define thread_add_read(m,f,a,v) funcname_thread_add_read(m,f,a,v,#f) |
| 134 | #define thread_add_write(m,f,a,v) funcname_thread_add_write(m,f,a,v,#f) |
| 135 | #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] | 136 | #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] | 137 | #define thread_add_event(m,f,a,v) funcname_thread_add_event(m,f,a,v,#f) |
| 138 | #define thread_execute(m,f,a,v) funcname_thread_execute(m,f,a,v,#f) |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 139 | #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] | 140 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 141 | /* Prototypes. */ |
| 142 | struct thread_master *thread_master_create (); |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 143 | struct thread *funcname_thread_add_read (struct thread_master *, |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 144 | int (*)(struct thread *), void *, int, const char*); |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 145 | struct thread *funcname_thread_add_write (struct thread_master *, |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 146 | int (*)(struct thread *), void *, int, const char*); |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 147 | struct thread *funcname_thread_add_timer (struct thread_master *, |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 148 | int (*)(struct thread *), void *, long, const char*); |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 149 | struct thread *funcname_thread_add_timer_msec (struct thread_master *, |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 150 | int (*)(struct thread *), void *, long, const char*); |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 151 | struct thread *funcname_thread_add_event (struct thread_master *, |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 152 | int (*)(struct thread *), void *, int, const char*); |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 153 | struct thread *funcname_thread_add_background (struct thread_master *, |
| 154 | int (*)(struct thread *), |
| 155 | void *, |
| 156 | long, const char*); |
| 157 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 158 | void thread_cancel (struct thread *); |
| 159 | void thread_cancel_event (struct thread_master *, void *); |
| 160 | |
| 161 | struct thread *thread_fetch (struct thread_master *, struct thread *); |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 162 | struct thread *funcname_thread_execute (struct thread_master *, |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 163 | int (*)(struct thread *), void *, int, const char *); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 164 | void thread_call (struct thread *); |
| 165 | unsigned long thread_timer_remain_second (struct thread *); |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 166 | int thread_should_yield (struct thread *); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 167 | |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 168 | extern struct cmd_element show_thread_cpu_cmd; |
paul | a48b4e6 | 2005-04-22 00:43:47 +0000 | [diff] [blame] | 169 | extern struct cmd_element show_thread_work_queues_cmd; |
paul | e04ab74 | 2003-01-17 23:47:00 +0000 | [diff] [blame] | 170 | |
ajs | 924b922 | 2005-04-16 17:11:24 +0000 | [diff] [blame] | 171 | extern unsigned long thread_consumed_time(RUSAGE_T *after, RUSAGE_T *before); |
| 172 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 173 | #endif /* _ZEBRA_THREAD_H */ |