thread.h 8.0 KB

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