timer.c 5.8 KB

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