timer.c 5.7 KB

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