thread.h 8.4 KB

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