MiniDLNA update: 1.0.19.1 to 1.0.20
[tomato.git] / release / src / router / zebra / lib / thread.h
blobcd62df0ffb65e2535fec3133b86b444d676a5125
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 #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 */
33 /* Linked list of thread. */
34 struct thread_list
36 struct thread *head;
37 struct thread *tail;
38 int count;
41 /* Master of the theads. */
42 struct thread_master
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;
56 /* Thread itself. */
57 struct thread
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 */
65 union {
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. */
69 } u;
70 RUSAGE_T ru; /* Indepth usage info. */
73 /* Thread types. */
74 #define THREAD_READ 0
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 */
84 /* Macros. */
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) \
90 do { \
91 if (! thread) \
92 thread = thread_add_read (master, func, arg, sock); \
93 } while (0)
95 #define THREAD_WRITE_ON(master,thread,func,arg,sock) \
96 do { \
97 if (! thread) \
98 thread = thread_add_write (master, func, arg, sock); \
99 } while (0)
101 #define THREAD_TIMER_ON(master,thread,func,arg,time) \
102 do { \
103 if (! thread) \
104 thread = thread_add_timer (master, func, arg, time); \
105 } while (0)
107 #define THREAD_OFF(thread) \
108 do { \
109 if (thread) \
111 thread_cancel (thread); \
112 thread = NULL; \
114 } while (0)
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)
120 /* Prototypes. */
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 */