timer.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 "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 "print.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 int 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_timeout_msec (&timers_timerlist);
  106. timer_serialize_unlock_fn ();
  107. fds = poll (NULL, 0, timeout);
  108. if (fds == -1) {
  109. goto retry_poll;
  110. }
  111. pthread_mutex_lock (&timer_mutex);
  112. timer_serialize_lock_fn ();
  113. timerlist_expire (&timers_timerlist);
  114. timer_serialize_unlock_fn ();
  115. pthread_mutex_unlock (&timer_mutex);
  116. }
  117. pthread_exit (0);
  118. }
  119. static void sigusr1_handler (int num) {
  120. #ifdef OPENAIS_SOLARIS
  121. /* Rearm the signal facility */
  122. signal (num, sigusr1_handler);
  123. #endif
  124. }
  125. int openais_timer_init (
  126. void (*serialize_lock_fn) (void),
  127. void (*serialize_unlock_fn) (void))
  128. {
  129. int res;
  130. timer_serialize_lock_fn = serialize_lock_fn;
  131. timer_serialize_unlock_fn = serialize_unlock_fn;
  132. timerlist_init (&timers_timerlist);
  133. signal (SIGUSR1, sigusr1_handler);
  134. pthread_mutex_lock (&timer_mutex);
  135. pthread_attr_init (&thread_attr);
  136. pthread_attr_setstacksize (&thread_attr, 100000);
  137. pthread_attr_setdetachstate (&thread_attr, PTHREAD_CREATE_DETACHED);
  138. res = pthread_create (&expiry_thread, &thread_attr,
  139. prioritized_timer_thread, NULL);
  140. return (res);
  141. }
  142. int openais_timer_add (
  143. unsigned int msec_in_future,
  144. void *data,
  145. void (*timer_fn) (void *data),
  146. timer_handle *handle)
  147. {
  148. int res;
  149. int unlock;
  150. if (pthread_equal (pthread_self(), expiry_thread) == 0) {
  151. unlock = 0;
  152. } else {
  153. unlock = 1;
  154. pthread_mutex_lock (&timer_mutex);
  155. }
  156. res = timerlist_add_future (
  157. &timers_timerlist,
  158. timer_fn,
  159. data,
  160. msec_in_future,
  161. handle);
  162. if (unlock) {
  163. pthread_mutex_unlock (&timer_mutex);
  164. }
  165. pthread_kill (expiry_thread, SIGUSR1);
  166. return (res);
  167. }
  168. void openais_timer_delete (
  169. timer_handle timer_handle)
  170. {
  171. int unlock;
  172. if (timer_handle == 0) {
  173. return;
  174. }
  175. if (pthread_equal (pthread_self(), expiry_thread) == 0) {
  176. unlock = 0;
  177. } else {
  178. unlock = 1;
  179. pthread_mutex_lock (&timer_mutex);
  180. }
  181. timerlist_del (&timers_timerlist, timer_handle);
  182. if (unlock) {
  183. pthread_mutex_unlock (&timer_mutex);
  184. }
  185. }
  186. void openais_timer_delete_data (
  187. timer_handle timer_handle)
  188. {
  189. int unlock;
  190. if (timer_handle == 0) {
  191. return;
  192. }
  193. if (pthread_equal (pthread_self(), expiry_thread) == 0) {
  194. unlock = 0;
  195. } else {
  196. unlock = 1;
  197. pthread_mutex_lock (&timer_mutex);
  198. }
  199. timerlist_del_data (&timers_timerlist, timer_handle);
  200. if (unlock) {
  201. pthread_mutex_unlock (&timer_mutex);
  202. }
  203. }
  204. void openais_timer_lock (void)
  205. {
  206. pthread_mutex_lock (&timer_mutex);
  207. }
  208. void openais_timer_unlock (void)
  209. {
  210. pthread_mutex_unlock (&timer_mutex);
  211. }