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 */
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.  */
71};
72
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
80
81/* Thread yield time.  */
82#define THREAD_YIELD_TIME_SLOT     100 * 1000L /* 100ms */
83
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)
88
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)
94
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)
100
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)
106
107#define THREAD_OFF(thread) \
108  do { \
109    if (thread) \
110      { \
111        thread_cancel (thread); \
112        thread = NULL; \
113      } \
114  } while (0)
115
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)
119
120/* Prototypes. */
121struct thread_master *thread_master_create ();
122struct thread *thread_add_read (struct thread_master *,
123				int (*)(struct thread *), void *, int);
124struct thread *thread_add_write (struct thread_master *,
125				 int (*)(struct thread *), void *, int);
126struct thread *thread_add_timer (struct thread_master *,
127				 int (*)(struct thread *), void *, long);
128struct thread *thread_add_event (struct thread_master *,
129				 int (*)(struct thread *), void *, int );
130void thread_cancel (struct thread *);
131void thread_cancel_event (struct thread_master *, void *);
132
133struct thread *thread_fetch (struct thread_master *, struct thread *);
134struct thread *thread_execute (struct thread_master *,
135			       int (*)(struct thread *), void *, int);
136void thread_call (struct thread *);
137unsigned long thread_timer_remain_second (struct thread *);
138
139#endif /* _ZEBRA_THREAD_H */
140