timer.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. int openais_timer_init (
  114. void (*serialize_lock_fn) (void),
  115. void (*serialize_unlock_fn) (void))
  116. {
  117. int res;
  118. timer_serialize_lock_fn = serialize_lock_fn;
  119. timer_serialize_unlock_fn = serialize_unlock_fn;
  120. timerlist_init (&timers_timerlist);
  121. pthread_mutex_lock (&timer_mutex);
  122. pthread_attr_init (&thread_attr);
  123. pthread_attr_setstacksize (&thread_attr, 8192);
  124. pthread_attr_setdetachstate (&thread_attr, PTHREAD_CREATE_DETACHED);
  125. res = pthread_create (&thread, &thread_attr, prioritized_timer_thread,
  126. NULL);
  127. return (res);
  128. }
  129. int openais_timer_add (
  130. unsigned int msec_in_future,
  131. void *data,
  132. void (*timer_fn) (void *data),
  133. timer_handle *handle)
  134. {
  135. int res;
  136. pthread_mutex_lock (&timer_mutex);
  137. res = timerlist_add_future (
  138. &timers_timerlist,
  139. timer_fn,
  140. data,
  141. msec_in_future,
  142. handle);
  143. pthread_mutex_unlock (&timer_mutex);
  144. pthread_kill (thread, SIGUSR1);
  145. return (res);
  146. }
  147. void openais_timer_delete (
  148. timer_handle timer_handle)
  149. {
  150. if (timer_handle == 0) {
  151. return;
  152. }
  153. pthread_mutex_lock (&timer_mutex);
  154. timerlist_del (&timers_timerlist, timer_handle);
  155. pthread_mutex_unlock (&timer_mutex);
  156. }
  157. void openais_timer_delete_data (
  158. timer_handle timer_handle)
  159. {
  160. if (timer_handle == 0) {
  161. return;
  162. }
  163. pthread_mutex_lock (&timer_mutex);
  164. timerlist_del_data (&timers_timerlist, timer_handle);
  165. pthread_mutex_unlock (&timer_mutex);
  166. }
  167. void openais_timer_lock (void)
  168. {
  169. pthread_mutex_lock (&timer_mutex);
  170. }
  171. void openais_timer_unlock (void)
  172. {
  173. pthread_mutex_unlock (&timer_mutex);
  174. }