timer.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Copyright (c) 2002-2006 MontaVista Software, Inc.
  3. * Copyright (c) 2006 Red Hat, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Steven Dake (sdake@mvista.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 "../include/saAis.h"
  58. #include "../include/list.h"
  59. #include "../include/queue.h"
  60. #include "../lcr/lcr_ifact.h"
  61. #include "poll.h"
  62. #include "totempg.h"
  63. #include "totemsrp.h"
  64. #include "mempool.h"
  65. #include "mainconfig.h"
  66. #include "totemconfig.h"
  67. #include "main.h"
  68. #include "service.h"
  69. #include "sync.h"
  70. #include "swab.h"
  71. #include "objdb.h"
  72. #include "config.h"
  73. #include "tlist.h"
  74. #define LOG_SERVICE LOG_SERVICE_IPC
  75. #include "print.h"
  76. #include "util.h"
  77. #define SERVER_BACKLOG 5
  78. static pthread_mutex_t timer_mutex = PTHREAD_MUTEX_INITIALIZER;
  79. static pthread_t 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. /*
  86. * This thread runs at the highest priority to run system wide timers
  87. */
  88. static void *prioritized_timer_thread (void *data)
  89. {
  90. int fds;
  91. struct sched_param sched_param;
  92. int res;
  93. unsigned int timeout;
  94. sched_param.sched_priority = 2;
  95. res = pthread_setschedparam (thread, SCHED_RR, &sched_param);
  96. pthread_mutex_unlock (&timer_mutex);
  97. for (;;) {
  98. retry_poll:
  99. timeout = timerlist_timeout_msec (&timers_timerlist);
  100. fds = poll (NULL, 0, timeout);
  101. if (fds == -1) {
  102. goto retry_poll;
  103. }
  104. pthread_mutex_lock (&timer_mutex);
  105. timer_serialize_lock_fn ();
  106. timerlist_expire (&timers_timerlist);
  107. timer_serialize_unlock_fn ();
  108. pthread_mutex_unlock (&timer_mutex);
  109. }
  110. pthread_exit (0);
  111. return (0);
  112. }
  113. static void sigusr1_handler (int num) {
  114. }
  115. int openais_timer_init (
  116. void (*serialize_lock_fn) (void),
  117. void (*serialize_unlock_fn) (void))
  118. {
  119. int res;
  120. timer_serialize_lock_fn = serialize_lock_fn;
  121. timer_serialize_unlock_fn = serialize_unlock_fn;
  122. timerlist_init (&timers_timerlist);
  123. signal (SIGUSR1, sigusr1_handler);
  124. pthread_mutex_lock (&timer_mutex);
  125. pthread_attr_init (&thread_attr);
  126. pthread_attr_setstacksize (&thread_attr, 8192);
  127. pthread_attr_setdetachstate (&thread_attr, PTHREAD_CREATE_DETACHED);
  128. res = pthread_create (&thread, &thread_attr, prioritized_timer_thread,
  129. NULL);
  130. return (res);
  131. }
  132. int openais_timer_add (
  133. unsigned int msec_in_future,
  134. void *data,
  135. void (*timer_fn) (void *data),
  136. timer_handle *handle)
  137. {
  138. int res;
  139. pthread_mutex_lock (&timer_mutex);
  140. res = timerlist_add_future (
  141. &timers_timerlist,
  142. timer_fn,
  143. data,
  144. msec_in_future,
  145. handle);
  146. pthread_mutex_unlock (&timer_mutex);
  147. pthread_kill (thread, SIGUSR1);
  148. return (res);
  149. }
  150. void openais_timer_delete (
  151. timer_handle timer_handle)
  152. {
  153. if (timer_handle == 0) {
  154. return;
  155. }
  156. pthread_mutex_lock (&timer_mutex);
  157. timerlist_del (&timers_timerlist, timer_handle);
  158. pthread_mutex_unlock (&timer_mutex);
  159. }
  160. void openais_timer_delete_data (
  161. timer_handle timer_handle)
  162. {
  163. if (timer_handle == 0) {
  164. return;
  165. }
  166. pthread_mutex_lock (&timer_mutex);
  167. timerlist_del_data (&timers_timerlist, timer_handle);
  168. pthread_mutex_unlock (&timer_mutex);
  169. }
  170. void openais_timer_lock (void)
  171. {
  172. pthread_mutex_lock (&timer_mutex);
  173. }
  174. void openais_timer_unlock (void)
  175. {
  176. pthread_mutex_unlock (&timer_mutex);
  177. }