thread.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. #ifndef _ZEBRA_THREAD_H
  22. #define _ZEBRA_THREAD_H
  23. struct rusage_t
  24. {
  25. #ifdef HAVE_RUSAGE
  26. struct rusage cpu;
  27. #endif
  28. struct timeval real;
  29. };
  30. #define RUSAGE_T struct rusage_t
  31. #define GETRUSAGE(X) thread_getrusage(X)
  32. /* Linked list of thread. */
  33. struct thread_list
  34. {
  35. struct thread *head;
  36. struct thread *tail;
  37. int count;
  38. };
  39. /* Master of the theads. */
  40. struct thread_master
  41. {
  42. struct thread_list read;
  43. struct thread_list write;
  44. struct thread_list timer;
  45. struct thread_list event;
  46. struct thread_list ready;
  47. struct thread_list unuse;
  48. struct thread_list background;
  49. fd_set readfd;
  50. fd_set writefd;
  51. fd_set exceptfd;
  52. unsigned long alloc;
  53. };
  54. typedef unsigned char thread_type;
  55. /* Thread itself. */
  56. struct thread
  57. {
  58. thread_type type; /* thread type */
  59. thread_type add_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. struct cpu_thread_history *hist; /* cache pointer to cpu_history */
  72. char* funcname;
  73. };
  74. struct cpu_thread_history
  75. {
  76. int (*func)(struct thread *);
  77. char *funcname;
  78. unsigned int total_calls;
  79. struct time_stats
  80. {
  81. unsigned long total, max;
  82. } real;
  83. #ifdef HAVE_RUSAGE
  84. struct time_stats cpu;
  85. #endif
  86. thread_type types;
  87. };
  88. /* Clocks supported by Quagga */
  89. enum quagga_clkid {
  90. QUAGGA_CLK_REALTIME = 0, /* ala gettimeofday() */
  91. QUAGGA_CLK_MONOTONIC, /* monotonic, against an indeterminate base */
  92. QUAGGA_CLK_REALTIME_STABILISED, /* like realtime, but non-decrementing */
  93. };
  94. /* Thread types. */
  95. #define THREAD_READ 0
  96. #define THREAD_WRITE 1
  97. #define THREAD_TIMER 2
  98. #define THREAD_EVENT 3
  99. #define THREAD_READY 4
  100. #define THREAD_BACKGROUND 5
  101. #define THREAD_UNUSED 6
  102. #define THREAD_EXECUTE 7
  103. /* Thread yield time. */
  104. #define THREAD_YIELD_TIME_SLOT 10 * 1000L /* 10ms */
  105. /* Macros. */
  106. #define THREAD_ARG(X) ((X)->arg)
  107. #define THREAD_FD(X) ((X)->u.fd)
  108. #define THREAD_VAL(X) ((X)->u.val)
  109. #define THREAD_READ_ON(master,thread,func,arg,sock) \
  110. do { \
  111. if (! thread) \
  112. thread = thread_add_read (master, func, arg, sock); \
  113. } while (0)
  114. #define THREAD_WRITE_ON(master,thread,func,arg,sock) \
  115. do { \
  116. if (! thread) \
  117. thread = thread_add_write (master, func, arg, sock); \
  118. } while (0)
  119. #define THREAD_TIMER_ON(master,thread,func,arg,time) \
  120. do { \
  121. if (! thread) \
  122. thread = thread_add_timer (master, func, arg, time); \
  123. } while (0)
  124. #define THREAD_OFF(thread) \
  125. do { \
  126. if (thread) \
  127. { \
  128. thread_cancel (thread); \
  129. thread = NULL; \
  130. } \
  131. } while (0)
  132. #define THREAD_READ_OFF(thread) THREAD_OFF(thread)
  133. #define THREAD_WRITE_OFF(thread) THREAD_OFF(thread)
  134. #define THREAD_TIMER_OFF(thread) THREAD_OFF(thread)
  135. #define thread_add_read(m,f,a,v) funcname_thread_add_read(m,f,a,v,#f)
  136. #define thread_add_write(m,f,a,v) funcname_thread_add_write(m,f,a,v,#f)
  137. #define thread_add_timer(m,f,a,v) funcname_thread_add_timer(m,f,a,v,#f)
  138. #define thread_add_timer_msec(m,f,a,v) funcname_thread_add_timer_msec(m,f,a,v,#f)
  139. #define thread_add_event(m,f,a,v) funcname_thread_add_event(m,f,a,v,#f)
  140. #define thread_execute(m,f,a,v) funcname_thread_execute(m,f,a,v,#f)
  141. /* The 4th arg to thread_add_background is the # of milliseconds to delay. */
  142. #define thread_add_background(m,f,a,v) funcname_thread_add_background(m,f,a,v,#f)
  143. /* Prototypes. */
  144. extern struct thread_master *thread_master_create (void);
  145. extern void thread_master_free (struct thread_master *);
  146. extern struct thread *funcname_thread_add_read (struct thread_master *,
  147. int (*)(struct thread *),
  148. void *, int, const char*);
  149. extern struct thread *funcname_thread_add_write (struct thread_master *,
  150. int (*)(struct thread *),
  151. void *, int, const char*);
  152. extern struct thread *funcname_thread_add_timer (struct thread_master *,
  153. int (*)(struct thread *),
  154. void *, long, const char*);
  155. extern struct thread *funcname_thread_add_timer_msec (struct thread_master *,
  156. int (*)(struct thread *),
  157. void *, long, const char*);
  158. extern struct thread *funcname_thread_add_event (struct thread_master *,
  159. int (*)(struct thread *),
  160. void *, int, const char*);
  161. extern struct thread *funcname_thread_add_background (struct thread_master *,
  162. int (*func)(struct thread *),
  163. void *arg,
  164. long milliseconds_to_delay,
  165. const char *funcname);
  166. extern struct thread *funcname_thread_execute (struct thread_master *,
  167. int (*)(struct thread *),
  168. void *, int, const char *);
  169. extern void thread_cancel (struct thread *);
  170. extern unsigned int thread_cancel_event (struct thread_master *, void *);
  171. extern struct thread *thread_fetch (struct thread_master *, struct thread *);
  172. extern void thread_call (struct thread *);
  173. extern unsigned long thread_timer_remain_second (struct thread *);
  174. extern int thread_should_yield (struct thread *);
  175. /* Internal libzebra exports */
  176. extern void thread_getrusage (RUSAGE_T *);
  177. extern struct cmd_element show_thread_cpu_cmd;
  178. extern struct cmd_element clear_thread_cpu_cmd;
  179. /* replacements for the system gettimeofday(), clock_gettime() and
  180. * time() functions, providing support for non-decrementing clock on
  181. * all systems, and fully monotonic on /some/ systems.
  182. */
  183. extern int quagga_gettime (enum quagga_clkid, struct timeval *);
  184. extern time_t quagga_time (time_t *);
  185. /* Returns elapsed real (wall clock) time. */
  186. extern unsigned long thread_consumed_time(RUSAGE_T *after, RUSAGE_T *before,
  187. unsigned long *cpu_time_elapsed);
  188. /* Global variable containing a recent result from gettimeofday. This can
  189. be used instead of calling gettimeofday if a recent value is sufficient.
  190. This is guaranteed to be refreshed before a thread is called. */
  191. extern struct timeval recent_time;
  192. /* Similar to recent_time, but a monotonically increasing time value */
  193. extern struct timeval recent_relative_time (void);
  194. #endif /* _ZEBRA_THREAD_H */