timer.c 6.3 KB

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