thread.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /* Thread management routine header.
  2. * Copyright (C) 1998 Kunihiro Ishiguro
  3. * Portions Copyright (c) 2008 Everton da Silva Marques <everton.marques@gmail.com>
  4. *
  5. * This file is part of GNU Zebra.
  6. *
  7. * GNU Zebra is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2, or (at your option) any
  10. * later version.
  11. *
  12. * GNU Zebra is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with GNU Zebra; see the file COPYING. If not, write to the Free
  19. * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  20. * 02111-1307, USA.
  21. */
  22. #ifndef _ZEBRA_THREAD_H
  23. #define _ZEBRA_THREAD_H
  24. #include <zebra.h>
  25. struct rusage_t
  26. {
  27. #ifdef HAVE_RUSAGE
  28. struct rusage cpu;
  29. #endif
  30. struct timeval real;
  31. };
  32. #define RUSAGE_T struct rusage_t
  33. #define GETRUSAGE(X) thread_getrusage(X)
  34. /* Linked list of thread. */
  35. struct thread_list
  36. {
  37. struct thread *head;
  38. struct thread *tail;
  39. int count;
  40. };
  41. struct pqueue;
  42. /* Master of the theads. */
  43. struct thread_master
  44. {
  45. struct thread_list read;
  46. struct thread_list write;
  47. struct pqueue *timer;
  48. struct thread_list event;
  49. struct thread_list ready;
  50. struct thread_list unuse;
  51. struct pqueue *background;
  52. fd_set readfd;
  53. fd_set writefd;
  54. fd_set exceptfd;
  55. unsigned long alloc;
  56. };
  57. typedef unsigned char thread_type;
  58. /* ISO C99 maximum function name length is 63 */
  59. #define FUNCNAME_LEN 64
  60. /* Thread itself. */
  61. struct thread
  62. {
  63. thread_type type; /* thread type */
  64. thread_type add_type; /* thread type */
  65. struct thread *next; /* next pointer of the thread */
  66. struct thread *prev; /* previous pointer of the thread */
  67. struct thread_master *master; /* pointer to the struct thread_master. */
  68. int (*func) (struct thread *); /* event function */
  69. void *arg; /* event argument */
  70. union {
  71. int val; /* second argument of the event. */
  72. int fd; /* file descriptor in case of read/write. */
  73. struct timeval sands; /* rest of time sands value. */
  74. } u;
  75. int index; /* used for timers to store position in queue */
  76. struct timeval real;
  77. struct cpu_thread_history *hist; /* cache pointer to cpu_history */
  78. char funcname[FUNCNAME_LEN];
  79. };
  80. struct cpu_thread_history
  81. {
  82. int (*func)(struct thread *);
  83. unsigned int total_calls;
  84. struct time_stats
  85. {
  86. unsigned long total, max;
  87. } real;
  88. #ifdef HAVE_RUSAGE
  89. struct time_stats cpu;
  90. #endif
  91. thread_type types;
  92. char funcname[FUNCNAME_LEN];
  93. };
  94. /* Clocks supported by Quagga */
  95. enum quagga_clkid {
  96. QUAGGA_CLK_REALTIME = 0, /* ala gettimeofday() */
  97. QUAGGA_CLK_MONOTONIC, /* monotonic, against an indeterminate base */
  98. QUAGGA_CLK_REALTIME_STABILISED, /* like realtime, but non-decrementing */
  99. };
  100. /* Thread types. */
  101. #define THREAD_READ 0
  102. #define THREAD_WRITE 1
  103. #define THREAD_TIMER 2
  104. #define THREAD_EVENT 3
  105. #define THREAD_READY 4
  106. #define THREAD_BACKGROUND 5
  107. #define THREAD_UNUSED 6
  108. #define THREAD_EXECUTE 7
  109. /* Thread yield time. */
  110. #define THREAD_YIELD_TIME_SLOT 10 * 1000L /* 10ms */
  111. /* Macros. */
  112. #define THREAD_ARG(X) ((X)->arg)
  113. #define THREAD_FD(X) ((X)->u.fd)
  114. #define THREAD_VAL(X) ((X)->u.val)
  115. #define THREAD_READ_ON(master,thread,func,arg,sock) \
  116. do { \
  117. if (! thread) \
  118. thread = thread_add_read (master, func, arg, sock); \
  119. } while (0)
  120. #define THREAD_WRITE_ON(master,thread,func,arg,sock) \
  121. do { \
  122. if (! thread) \
  123. thread = thread_add_write (master, func, arg, sock); \
  124. } while (0)
  125. #define THREAD_TIMER_ON(master,thread,func,arg,time) \
  126. do { \
  127. if (! thread) \
  128. thread = thread_add_timer (master, func, arg, time); \
  129. } while (0)
  130. #define THREAD_TIMER_MSEC_ON(master,thread,func,arg,time) \
  131. do { \
  132. if (! thread) \
  133. thread = thread_add_timer_msec (master, func, arg, time); \
  134. } while (0)
  135. #define THREAD_OFF(thread) \
  136. do { \
  137. if (thread) \
  138. { \
  139. thread_cancel (thread); \
  140. thread = NULL; \
  141. } \
  142. } while (0)
  143. #define THREAD_READ_OFF(thread) THREAD_OFF(thread)
  144. #define THREAD_WRITE_OFF(thread) THREAD_OFF(thread)
  145. #define THREAD_TIMER_OFF(thread) THREAD_OFF(thread)
  146. #define thread_add_read(m,f,a,v) funcname_thread_add_read(m,f,a,v,#f)
  147. #define thread_add_write(m,f,a,v) funcname_thread_add_write(m,f,a,v,#f)
  148. #define thread_add_timer(m,f,a,v) funcname_thread_add_timer(m,f,a,v,#f)
  149. #define thread_add_timer_msec(m,f,a,v) funcname_thread_add_timer_msec(m,f,a,v,#f)
  150. #define thread_add_event(m,f,a,v) funcname_thread_add_event(m,f,a,v,#f)
  151. #define thread_execute(m,f,a,v) funcname_thread_execute(m,f,a,v,#f)
  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)
  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, const char*);
  160. extern struct thread *funcname_thread_add_write (struct thread_master *,
  161. int (*)(struct thread *),
  162. void *, int, const char*);
  163. extern struct thread *funcname_thread_add_timer (struct thread_master *,
  164. int (*)(struct thread *),
  165. void *, long, const char*);
  166. extern struct thread *funcname_thread_add_timer_msec (struct thread_master *,
  167. int (*)(struct thread *),
  168. void *, long, const char*);
  169. extern struct thread *funcname_thread_add_event (struct thread_master *,
  170. int (*)(struct thread *),
  171. void *, int, const char*);
  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. const char *funcname);
  177. extern struct thread *funcname_thread_execute (struct thread_master *,
  178. int (*)(struct thread *),
  179. void *, int, const char *);
  180. extern void thread_cancel (struct thread *);
  181. extern unsigned int thread_cancel_event (struct thread_master *, void *);
  182. extern struct thread *thread_fetch (struct thread_master *, struct thread *);
  183. extern void thread_call (struct thread *);
  184. extern unsigned long thread_timer_remain_second (struct thread *);
  185. extern int thread_should_yield (struct thread *);
  186. /* Internal libzebra exports */
  187. extern void thread_getrusage (RUSAGE_T *);
  188. extern struct cmd_element show_thread_cpu_cmd;
  189. extern struct cmd_element clear_thread_cpu_cmd;
  190. /* replacements for the system gettimeofday(), clock_gettime() and
  191. * time() functions, providing support for non-decrementing clock on
  192. * all systems, and fully monotonic on /some/ systems.
  193. */
  194. extern int quagga_gettime (enum quagga_clkid, struct timeval *);
  195. extern time_t quagga_time (time_t *);
  196. /* Returns elapsed real (wall clock) time. */
  197. extern unsigned long thread_consumed_time(RUSAGE_T *after, RUSAGE_T *before,
  198. unsigned long *cpu_time_elapsed);
  199. /* Global variable containing a recent result from gettimeofday. This can
  200. be used instead of calling gettimeofday if a recent value is sufficient.
  201. This is guaranteed to be refreshed before a thread is called. */
  202. extern struct timeval recent_time;
  203. /* Similar to recent_time, but a monotonically increasing time value */
  204. extern struct timeval recent_relative_time (void);
  205. #endif /* _ZEBRA_THREAD_H */