timer.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * Copyright (c) 2002-2006 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2009 Red Hat, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Steven Dake (sdake@redhat.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 <config.h>
  36. #include <pthread.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 <corosync/swab.h>
  58. #include <corosync/corotypes.h>
  59. #include <corosync/coroipc_types.h>
  60. #include <corosync/list.h>
  61. #include <corosync/lcr/lcr_ifact.h>
  62. #include <corosync/totem/coropoll.h>
  63. #include <corosync/totem/totempg.h>
  64. #include <corosync/engine/objdb.h>
  65. #include <corosync/engine/config.h>
  66. #define LOG_SERVICE LOG_SERVICE_IPC
  67. #include <corosync/engine/logsys.h>
  68. #include "poll.h"
  69. #include "totemsrp.h"
  70. #include "mainconfig.h"
  71. #include "totemconfig.h"
  72. #include "main.h"
  73. #include "sync.h"
  74. #include "tlist.h"
  75. #include "util.h"
  76. #include "timer.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 sched_priority = 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. extern void pthread_exit(void *) __attribute__((noreturn));
  87. /*
  88. * This thread runs at the highest priority to run system wide timers
  89. */
  90. static void *prioritized_timer_thread (void *data)
  91. {
  92. int fds;
  93. unsigned long long timeout;
  94. #if defined(HAVE_PTHREAD_SETSCHEDPARAM) && defined(HAVE_SCHED_GET_PRIORITY_MAX)
  95. if (sched_priority != 0) {
  96. struct sched_param sched_param;
  97. sched_param.sched_priority = sched_priority;
  98. pthread_setschedparam (expiry_thread, SCHED_RR, &sched_param);
  99. }
  100. #endif
  101. pthread_mutex_unlock (&timer_mutex);
  102. for (;;) {
  103. timer_serialize_lock_fn ();
  104. timeout = timerlist_msec_duration_to_expire (&timers_timerlist);
  105. if (timeout != -1 && timeout > 0xFFFFFFFF) {
  106. timeout = 0xFFFFFFFE;
  107. }
  108. timer_serialize_unlock_fn ();
  109. fds = poll (NULL, 0, timeout);
  110. if (fds < 0 && errno == EINTR) {
  111. continue;
  112. }
  113. if (fds < 0) {
  114. return NULL;
  115. }
  116. timer_serialize_lock_fn ();
  117. pthread_mutex_lock (&timer_mutex);
  118. timerlist_expire (&timers_timerlist);
  119. pthread_mutex_unlock (&timer_mutex);
  120. timer_serialize_unlock_fn ();
  121. }
  122. }
  123. static void sigusr1_handler (int num) {
  124. #ifdef COROSYNC_SOLARIS
  125. /* Rearm the signal facility */
  126. signal (num, sigusr1_handler);
  127. #endif
  128. }
  129. int corosync_timer_init (
  130. void (*serialize_lock_fn) (void),
  131. void (*serialize_unlock_fn) (void),
  132. int sched_priority_in)
  133. {
  134. int res;
  135. timer_serialize_lock_fn = serialize_lock_fn;
  136. timer_serialize_unlock_fn = serialize_unlock_fn;
  137. sched_priority = sched_priority_in;
  138. timerlist_init (&timers_timerlist);
  139. signal (SIGUSR1, sigusr1_handler);
  140. pthread_mutex_lock (&timer_mutex);
  141. pthread_attr_init (&thread_attr);
  142. pthread_attr_setstacksize (&thread_attr, 100000);
  143. pthread_attr_setdetachstate (&thread_attr, PTHREAD_CREATE_DETACHED);
  144. res = pthread_create (&expiry_thread, &thread_attr,
  145. prioritized_timer_thread, NULL);
  146. return (res);
  147. }
  148. int corosync_timer_add_absolute (
  149. unsigned long long nanosec_from_epoch,
  150. void *data,
  151. void (*timer_fn) (void *data),
  152. timer_handle *handle)
  153. {
  154. int res;
  155. int unlock;
  156. if (pthread_equal (pthread_self(), expiry_thread) != 0) {
  157. unlock = 0;
  158. } else {
  159. unlock = 1;
  160. pthread_mutex_lock (&timer_mutex);
  161. }
  162. res = timerlist_add_absolute (
  163. &timers_timerlist,
  164. timer_fn,
  165. data,
  166. nanosec_from_epoch,
  167. handle);
  168. if (unlock) {
  169. pthread_mutex_unlock (&timer_mutex);
  170. }
  171. pthread_kill (expiry_thread, SIGUSR1);
  172. return (res);
  173. }
  174. int corosync_timer_add_duration (
  175. unsigned long long nanosec_duration,
  176. void *data,
  177. void (*timer_fn) (void *data),
  178. timer_handle *handle)
  179. {
  180. int res;
  181. int unlock;
  182. if (pthread_equal (pthread_self(), expiry_thread) != 0) {
  183. unlock = 0;
  184. } else {
  185. unlock = 1;
  186. pthread_mutex_lock (&timer_mutex);
  187. }
  188. res = timerlist_add_duration (
  189. &timers_timerlist,
  190. timer_fn,
  191. data,
  192. nanosec_duration,
  193. handle);
  194. if (unlock) {
  195. pthread_mutex_unlock (&timer_mutex);
  196. }
  197. pthread_kill (expiry_thread, SIGUSR1);
  198. return (res);
  199. }
  200. void corosync_timer_delete (
  201. timer_handle th)
  202. {
  203. int unlock;
  204. if (th == 0) {
  205. return;
  206. }
  207. if (pthread_equal (pthread_self(), expiry_thread) != 0) {
  208. unlock = 0;
  209. } else {
  210. unlock = 1;
  211. pthread_mutex_lock (&timer_mutex);
  212. }
  213. timerlist_del (&timers_timerlist, th);
  214. if (unlock) {
  215. pthread_mutex_unlock (&timer_mutex);
  216. }
  217. }
  218. void corosync_timer_lock (void)
  219. {
  220. pthread_mutex_lock (&timer_mutex);
  221. }
  222. void corosync_timer_unlock (void)
  223. {
  224. pthread_mutex_unlock (&timer_mutex);
  225. }
  226. unsigned long long corosync_timer_time_get (void)
  227. {
  228. return (timerlist_nano_from_epoch());
  229. }
  230. unsigned long long corosync_timer_expire_time_get (
  231. timer_handle th)
  232. {
  233. int unlock;
  234. unsigned long long expire;
  235. if (th == 0) {
  236. return (0);
  237. }
  238. if (pthread_equal (pthread_self(), expiry_thread) != 0) {
  239. unlock = 0;
  240. } else {
  241. unlock = 1;
  242. pthread_mutex_lock (&timer_mutex);
  243. }
  244. expire = timerlist_expire_time (&timers_timerlist, th);
  245. if (unlock) {
  246. pthread_mutex_unlock (&timer_mutex);
  247. }
  248. return (expire);
  249. }