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
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
22 #ifndef _ZEBRA_THREAD_H
23 #define _ZEBRA_THREAD_H
26 #define RUSAGE_T struct rusage
27 #define GETRUSAGE(X) getrusage (RUSAGE_SELF, X);
29 #define RUSAGE_T struct timeval
30 #define GETRUSAGE(X) gettimeofday (X, NULL);
31 #endif /* HAVE_RUSAGE */
33 /* Linked list of thread. */
41 /* Master of the theads. */
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
;
59 unsigned char type
; /* thread type */
60 struct thread
*next
; /* next pointer of the thread */
61 struct thread
*prev
; /* previous pointer of the thread */
62 struct thread_master
*master
; /* pointer to the struct thread_master. */
63 int (*func
) (struct thread
*); /* event function */
64 void *arg
; /* event argument */
66 int val
; /* second argument of the event. */
67 int fd
; /* file descriptor in case of read/write. */
68 struct timeval sands
; /* rest of time sands value. */
70 RUSAGE_T ru
; /* Indepth usage info. */
75 #define THREAD_WRITE 1
76 #define THREAD_TIMER 2
77 #define THREAD_EVENT 3
78 #define THREAD_READY 4
79 #define THREAD_UNUSED 5
81 /* Thread yield time. */
82 #define THREAD_YIELD_TIME_SLOT 100 * 1000L /* 100ms */
85 #define THREAD_ARG(X) ((X)->arg)
86 #define THREAD_FD(X) ((X)->u.fd)
87 #define THREAD_VAL(X) ((X)->u.val)
89 #define THREAD_READ_ON(master,thread,func,arg,sock) \
92 thread = thread_add_read (master, func, arg, sock); \
95 #define THREAD_WRITE_ON(master,thread,func,arg,sock) \
98 thread = thread_add_write (master, func, arg, sock); \
101 #define THREAD_TIMER_ON(master,thread,func,arg,time) \
104 thread = thread_add_timer (master, func, arg, time); \
107 #define THREAD_OFF(thread) \
111 thread_cancel (thread); \
116 #define THREAD_READ_OFF(thread) THREAD_OFF(thread)
117 #define THREAD_WRITE_OFF(thread) THREAD_OFF(thread)
118 #define THREAD_TIMER_OFF(thread) THREAD_OFF(thread)
121 struct thread_master
*thread_master_create ();
122 struct thread
*thread_add_read (struct thread_master
*,
123 int (*)(struct thread
*), void *, int);
124 struct thread
*thread_add_write (struct thread_master
*,
125 int (*)(struct thread
*), void *, int);
126 struct thread
*thread_add_timer (struct thread_master
*,
127 int (*)(struct thread
*), void *, long);
128 struct thread
*thread_add_event (struct thread_master
*,
129 int (*)(struct thread
*), void *, int );
130 void thread_cancel (struct thread
*);
131 void thread_cancel_event (struct thread_master
*, void *);
133 struct thread
*thread_fetch (struct thread_master
*, struct thread
*);
134 struct thread
*thread_execute (struct thread_master
*,
135 int (*)(struct thread
*), void *, int);
136 void thread_call (struct thread
*);
137 char *thread_timer_remain_second (struct thread
*);
139 #endif /* _ZEBRA_THREAD_H */