timer.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * Copyright (c) 2002-2006 MontaVista Software, Inc.
  3. * Copyright (c) 2006 Red Hat, Inc.
  4. * Copyright (c) 2006 Sun Microsystems, 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 "../include/saAis.h"
  59. #include "../include/list.h"
  60. #include "../include/queue.h"
  61. #include "../lcr/lcr_ifact.h"
  62. #include "poll.h"
  63. #include "totempg.h"
  64. #include "totemsrp.h"
  65. #include "mempool.h"
  66. #include "mainconfig.h"
  67. #include "totemconfig.h"
  68. #include "main.h"
  69. #include "service.h"
  70. #include "sync.h"
  71. #include "swab.h"
  72. #include "objdb.h"
  73. #include "config.h"
  74. #include "tlist.h"
  75. #define LOG_SERVICE LOG_SERVICE_IPC
  76. #include "print.h"
  77. #include "util.h"
  78. #define SERVER_BACKLOG 5
  79. static pthread_mutex_t timer_mutex = PTHREAD_MUTEX_INITIALIZER;
  80. static pthread_t expiry_thread;
  81. static pthread_attr_t thread_attr;
  82. static struct timerlist timers_timerlist;
  83. static void (*timer_serialize_lock_fn) (void);
  84. static void (*timer_serialize_unlock_fn) (void);
  85. static void *prioritized_timer_thread (void *data);
  86. extern void pthread_exit(void *) __attribute__((noreturn));
  87. /*
  88. * This thread runs at the highest priority to run system wide timers
  89. */
  90. static void *prioritized_timer_thread (void *data)
  91. {
  92. int fds;
  93. unsigned int timeout;
  94. #if ! defined(TS_CLASS) && (defined(OPENAIS_BSD) || defined(OPENAIS_LINUX) || defined(OPENAIS_SOLARIS))
  95. struct sched_param sched_param;
  96. int res;
  97. sched_param.sched_priority = 2;
  98. res = pthread_setschedparam (expiry_thread, SCHED_RR, &sched_param);
  99. #endif
  100. pthread_mutex_unlock (&timer_mutex);
  101. for (;;) {
  102. retry_poll:
  103. timer_serialize_lock_fn ();
  104. timeout = timerlist_timeout_msec (&timers_timerlist);
  105. timer_serialize_unlock_fn ();
  106. fds = poll (NULL, 0, timeout);
  107. if (fds == -1) {
  108. goto retry_poll;
  109. }
  110. pthread_mutex_lock (&timer_mutex);
  111. timer_serialize_lock_fn ();
  112. timerlist_expire (&timers_timerlist);
  113. timer_serialize_unlock_fn ();
  114. pthread_mutex_unlock (&timer_mutex);
  115. }
  116. pthread_exit (0);
  117. }
  118. static void sigusr1_handler (int num) {
  119. #ifdef OPENAIS_SOLARIS
  120. /* Rearm the signal facility */
  121. signal (num, sigusr1_handler);
  122. #endif
  123. }
  124. int openais_timer_init (
  125. void (*serialize_lock_fn) (void),
  126. void (*serialize_unlock_fn) (void))
  127. {
  128. int res;
  129. timer_serialize_lock_fn = serialize_lock_fn;
  130. timer_serialize_unlock_fn = serialize_unlock_fn;
  131. timerlist_init (&timers_timerlist);
  132. signal (SIGUSR1, sigusr1_handler);
  133. pthread_mutex_lock (&timer_mutex);
  134. pthread_attr_init (&thread_attr);
  135. pthread_attr_setstacksize (&thread_attr, 100000);
  136. pthread_attr_setdetachstate (&thread_attr, PTHREAD_CREATE_DETACHED);
  137. res = pthread_create (&expiry_thread, &thread_attr,
  138. prioritized_timer_thread, NULL);
  139. return (res);
  140. }
  141. int openais_timer_add (
  142. unsigned int msec_in_future,
  143. void *data,
  144. void (*timer_fn) (void *data),
  145. timer_handle *handle)
  146. {
  147. int res;
  148. int unlock;
  149. if (pthread_equal (pthread_self(), expiry_thread) == 0) {
  150. unlock = 0;
  151. } else {
  152. unlock = 1;
  153. pthread_mutex_lock (&timer_mutex);
  154. }
  155. res = timerlist_add_future (
  156. &timers_timerlist,
  157. timer_fn,
  158. data,
  159. msec_in_future,
  160. handle);
  161. if (unlock) {
  162. pthread_mutex_unlock (&timer_mutex);
  163. }
  164. pthread_kill (expiry_thread, SIGUSR1);
  165. return (res);
  166. }
  167. void openais_timer_delete (
  168. timer_handle timer_handle)
  169. {
  170. int unlock;
  171. if (timer_handle == 0) {
  172. return;
  173. }
  174. if (pthread_equal (pthread_self(), expiry_thread) == 0) {
  175. unlock = 0;
  176. } else {
  177. unlock = 1;
  178. pthread_mutex_lock (&timer_mutex);
  179. }
  180. timerlist_del (&timers_timerlist, timer_handle);
  181. if (unlock) {
  182. pthread_mutex_unlock (&timer_mutex);
  183. }
  184. }
  185. void openais_timer_delete_data (
  186. timer_handle timer_handle)
  187. {
  188. int unlock;
  189. if (timer_handle == 0) {
  190. return;
  191. }
  192. if (pthread_equal (pthread_self(), expiry_thread) == 0) {
  193. unlock = 0;
  194. } else {
  195. unlock = 1;
  196. pthread_mutex_lock (&timer_mutex);
  197. }
  198. timerlist_del_data (&timers_timerlist, timer_handle);
  199. if (unlock) {
  200. pthread_mutex_unlock (&timer_mutex);
  201. }
  202. }
  203. void openais_timer_lock (void)
  204. {
  205. pthread_mutex_lock (&timer_mutex);
  206. }
  207. void openais_timer_unlock (void)
  208. {
  209. pthread_mutex_unlock (&timer_mutex);
  210. }