timer.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * Copyright (c) 2002-2006 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2008 Red Hat, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Steven Dake (sdake@redhat.com)
  8. *
  9. * This software licensed under BSD license, the text of which follows:
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * - Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  20. * contributors may be used to endorse or promote products derived from this
  21. * software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  33. * THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. #include <pthread.h>
  36. #include <assert.h>
  37. #include <pwd.h>
  38. #include <grp.h>
  39. #include <sys/types.h>
  40. #include <sys/poll.h>
  41. #include <sys/uio.h>
  42. #include <sys/mman.h>
  43. #include <sys/socket.h>
  44. #include <sys/un.h>
  45. #include <sys/time.h>
  46. #include <sys/resource.h>
  47. #include <netinet/in.h>
  48. #include <arpa/inet.h>
  49. #include <unistd.h>
  50. #include <fcntl.h>
  51. #include <stdlib.h>
  52. #include <stdio.h>
  53. #include <errno.h>
  54. #include <signal.h>
  55. #include <sched.h>
  56. #include <time.h>
  57. #include <corosync/swab.h>
  58. #include <corosync/corotypes.h>
  59. #include <corosync/list.h>
  60. #include <corosync/queue.h>
  61. #include <corosync/lcr/lcr_ifact.h>
  62. #include <corosync/totem/coropoll.h>
  63. #include <corosync/totem/totempg.h>
  64. #include <corosync/engine/objdb.h>
  65. #include <corosync/engine/config.h>
  66. #define LOG_SERVICE LOG_SERVICE_IPC
  67. #include <corosync/engine/logsys.h>
  68. #include "poll.h"
  69. #include "totemsrp.h"
  70. #include "mempool.h"
  71. #include "mainconfig.h"
  72. #include "totemconfig.h"
  73. #include "main.h"
  74. #include "sync.h"
  75. #include "tlist.h"
  76. #include "util.h"
  77. #define SERVER_BACKLOG 5
  78. static pthread_mutex_t timer_mutex = PTHREAD_MUTEX_INITIALIZER;
  79. static pthread_t expiry_thread;
  80. static pthread_attr_t thread_attr;
  81. static struct timerlist timers_timerlist;
  82. static void (*timer_serialize_lock_fn) (void);
  83. static void (*timer_serialize_unlock_fn) (void);
  84. static void *prioritized_timer_thread (void *data);
  85. extern void pthread_exit(void *) __attribute__((noreturn));
  86. /*
  87. * This thread runs at the highest priority to run system wide timers
  88. */
  89. static void *prioritized_timer_thread (void *data)
  90. {
  91. int fds;
  92. unsigned long long timeout;
  93. #if ! defined(TS_CLASS) && (defined(COROSYNC_BSD) || defined(COROSYNC_LINUX) || defined(COROSYNC_SOLARIS))
  94. struct sched_param sched_param;
  95. int res;
  96. sched_param.sched_priority = 2;
  97. res = pthread_setschedparam (expiry_thread, SCHED_RR, &sched_param);
  98. #endif
  99. pthread_mutex_unlock (&timer_mutex);
  100. for (;;) {
  101. retry_poll:
  102. timer_serialize_lock_fn ();
  103. timeout = timerlist_msec_duration_to_expire (&timers_timerlist);
  104. if (timeout != -1 && timeout > 0xFFFFFFFF) {
  105. timeout = 0xFFFFFFFE;
  106. }
  107. timer_serialize_unlock_fn ();
  108. fds = poll (NULL, 0, timeout);
  109. if (fds == -1) {
  110. goto retry_poll;
  111. }
  112. pthread_mutex_lock (&timer_mutex);
  113. timer_serialize_lock_fn ();
  114. timerlist_expire (&timers_timerlist);
  115. timer_serialize_unlock_fn ();
  116. pthread_mutex_unlock (&timer_mutex);
  117. }
  118. pthread_exit (0);
  119. }
  120. static void sigusr1_handler (int num) {
  121. #ifdef COROSYNC_SOLARIS
  122. /* Rearm the signal facility */
  123. signal (num, sigusr1_handler);
  124. #endif
  125. }
  126. int corosync_timer_init (
  127. void (*serialize_lock_fn) (void),
  128. void (*serialize_unlock_fn) (void))
  129. {
  130. int res;
  131. timer_serialize_lock_fn = serialize_lock_fn;
  132. timer_serialize_unlock_fn = serialize_unlock_fn;
  133. timerlist_init (&timers_timerlist);
  134. signal (SIGUSR1, sigusr1_handler);
  135. pthread_mutex_lock (&timer_mutex);
  136. pthread_attr_init (&thread_attr);
  137. pthread_attr_setstacksize (&thread_attr, 100000);
  138. pthread_attr_setdetachstate (&thread_attr, PTHREAD_CREATE_DETACHED);
  139. res = pthread_create (&expiry_thread, &thread_attr,
  140. prioritized_timer_thread, NULL);
  141. return (res);
  142. }
  143. int corosync_timer_add_absolute (
  144. unsigned long long nanosec_from_epoch,
  145. void *data,
  146. void (*timer_fn) (void *data),
  147. timer_handle *handle)
  148. {
  149. int res;
  150. int unlock;
  151. if (pthread_equal (pthread_self(), expiry_thread) != 0) {
  152. unlock = 0;
  153. } else {
  154. unlock = 1;
  155. pthread_mutex_lock (&timer_mutex);
  156. }
  157. res = timerlist_add_absolute (
  158. &timers_timerlist,
  159. timer_fn,
  160. data,
  161. nanosec_from_epoch,
  162. handle);
  163. if (unlock) {
  164. pthread_mutex_unlock (&timer_mutex);
  165. }
  166. pthread_kill (expiry_thread, SIGUSR1);
  167. return (res);
  168. }
  169. int corosync_timer_add_duration (
  170. unsigned long long nanosec_duration,
  171. void *data,
  172. void (*timer_fn) (void *data),
  173. timer_handle *handle)
  174. {
  175. int res;
  176. int unlock;
  177. if (pthread_equal (pthread_self(), expiry_thread) != 0) {
  178. unlock = 0;
  179. } else {
  180. unlock = 1;
  181. pthread_mutex_lock (&timer_mutex);
  182. }
  183. res = timerlist_add_duration (
  184. &timers_timerlist,
  185. timer_fn,
  186. data,
  187. nanosec_duration,
  188. handle);
  189. if (unlock) {
  190. pthread_mutex_unlock (&timer_mutex);
  191. }
  192. pthread_kill (expiry_thread, SIGUSR1);
  193. return (res);
  194. }
  195. void corosync_timer_delete (
  196. timer_handle timer_handle)
  197. {
  198. int unlock;
  199. if (timer_handle == 0) {
  200. return;
  201. }
  202. if (pthread_equal (pthread_self(), expiry_thread) != 0) {
  203. unlock = 0;
  204. } else {
  205. unlock = 1;
  206. pthread_mutex_lock (&timer_mutex);
  207. }
  208. timerlist_del (&timers_timerlist, timer_handle);
  209. if (unlock) {
  210. pthread_mutex_unlock (&timer_mutex);
  211. }
  212. }
  213. void corosync_timer_lock (void)
  214. {
  215. pthread_mutex_lock (&timer_mutex);
  216. }
  217. void corosync_timer_unlock (void)
  218. {
  219. pthread_mutex_unlock (&timer_mutex);
  220. }