timer.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 <assert.h>
  38. #include <pwd.h>
  39. #include <grp.h>
  40. #include <sys/types.h>
  41. #include <sys/poll.h>
  42. #include <sys/uio.h>
  43. #include <sys/mman.h>
  44. #include <sys/socket.h>
  45. #include <sys/un.h>
  46. #include <sys/time.h>
  47. #include <sys/resource.h>
  48. #include <netinet/in.h>
  49. #include <arpa/inet.h>
  50. #include <unistd.h>
  51. #include <fcntl.h>
  52. #include <stdlib.h>
  53. #include <stdio.h>
  54. #include <errno.h>
  55. #include <signal.h>
  56. #include <sched.h>
  57. #include <time.h>
  58. #include <corosync/swab.h>
  59. #include <corosync/corotypes.h>
  60. #include <corosync/coroipc_types.h>
  61. #include <corosync/list.h>
  62. #include <corosync/queue.h>
  63. #include <corosync/lcr/lcr_ifact.h>
  64. #include <corosync/totem/coropoll.h>
  65. #include <corosync/totem/totempg.h>
  66. #include <corosync/engine/objdb.h>
  67. #include <corosync/engine/config.h>
  68. #define LOG_SERVICE LOG_SERVICE_IPC
  69. #include <corosync/engine/logsys.h>
  70. #include "poll.h"
  71. #include "totemsrp.h"
  72. #include "mempool.h"
  73. #include "mainconfig.h"
  74. #include "totemconfig.h"
  75. #include "main.h"
  76. #include "sync.h"
  77. #include "tlist.h"
  78. #include "util.h"
  79. #include "timer.h"
  80. #define SERVER_BACKLOG 5
  81. static pthread_mutex_t timer_mutex = PTHREAD_MUTEX_INITIALIZER;
  82. static pthread_t expiry_thread;
  83. static pthread_attr_t thread_attr;
  84. static struct timerlist timers_timerlist;
  85. static int sched_priority = 0;
  86. static void (*timer_serialize_lock_fn) (void);
  87. static void (*timer_serialize_unlock_fn) (void);
  88. static void *prioritized_timer_thread (void *data);
  89. extern void pthread_exit(void *) __attribute__((noreturn));
  90. /*
  91. * This thread runs at the highest priority to run system wide timers
  92. */
  93. static void *prioritized_timer_thread (void *data)
  94. {
  95. int fds;
  96. unsigned long long timeout;
  97. #if ! defined(TS_CLASS) && (defined(COROSYNC_BSD) || defined(COROSYNC_LINUX) || defined(COROSYNC_SOLARIS))
  98. if (sched_priority != 0) {
  99. struct sched_param sched_param;
  100. sched_param.sched_priority = sched_priority;
  101. pthread_setschedparam (expiry_thread, SCHED_RR, &sched_param);
  102. }
  103. #endif
  104. pthread_mutex_unlock (&timer_mutex);
  105. for (;;) {
  106. timer_serialize_lock_fn ();
  107. timeout = timerlist_msec_duration_to_expire (&timers_timerlist);
  108. if (timeout != -1 && timeout > 0xFFFFFFFF) {
  109. timeout = 0xFFFFFFFE;
  110. }
  111. timer_serialize_unlock_fn ();
  112. fds = poll (NULL, 0, timeout);
  113. if (fds < 0 && errno == EINTR) {
  114. continue;
  115. }
  116. if (fds < 0) {
  117. return NULL;
  118. }
  119. pthread_mutex_lock (&timer_mutex);
  120. timer_serialize_lock_fn ();
  121. timerlist_expire (&timers_timerlist);
  122. timer_serialize_unlock_fn ();
  123. pthread_mutex_unlock (&timer_mutex);
  124. }
  125. }
  126. static void sigusr1_handler (int num) {
  127. #ifdef COROSYNC_SOLARIS
  128. /* Rearm the signal facility */
  129. signal (num, sigusr1_handler);
  130. #endif
  131. }
  132. int corosync_timer_init (
  133. void (*serialize_lock_fn) (void),
  134. void (*serialize_unlock_fn) (void),
  135. int sched_priority_in)
  136. {
  137. int res;
  138. timer_serialize_lock_fn = serialize_lock_fn;
  139. timer_serialize_unlock_fn = serialize_unlock_fn;
  140. sched_priority = sched_priority_in;
  141. timerlist_init (&timers_timerlist);
  142. signal (SIGUSR1, sigusr1_handler);
  143. pthread_mutex_lock (&timer_mutex);
  144. pthread_attr_init (&thread_attr);
  145. pthread_attr_setstacksize (&thread_attr, 100000);
  146. pthread_attr_setdetachstate (&thread_attr, PTHREAD_CREATE_DETACHED);
  147. res = pthread_create (&expiry_thread, &thread_attr,
  148. prioritized_timer_thread, NULL);
  149. return (res);
  150. }
  151. int corosync_timer_add_absolute (
  152. unsigned long long nanosec_from_epoch,
  153. void *data,
  154. void (*timer_fn) (void *data),
  155. timer_handle *handle)
  156. {
  157. int res;
  158. int unlock;
  159. if (pthread_equal (pthread_self(), expiry_thread) != 0) {
  160. unlock = 0;
  161. } else {
  162. unlock = 1;
  163. pthread_mutex_lock (&timer_mutex);
  164. }
  165. res = timerlist_add_absolute (
  166. &timers_timerlist,
  167. timer_fn,
  168. data,
  169. nanosec_from_epoch,
  170. handle);
  171. if (unlock) {
  172. pthread_mutex_unlock (&timer_mutex);
  173. }
  174. pthread_kill (expiry_thread, SIGUSR1);
  175. return (res);
  176. }
  177. int corosync_timer_add_duration (
  178. unsigned long long nanosec_duration,
  179. void *data,
  180. void (*timer_fn) (void *data),
  181. timer_handle *handle)
  182. {
  183. int res;
  184. int unlock;
  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. res = timerlist_add_duration (
  192. &timers_timerlist,
  193. timer_fn,
  194. data,
  195. nanosec_duration,
  196. handle);
  197. if (unlock) {
  198. pthread_mutex_unlock (&timer_mutex);
  199. }
  200. pthread_kill (expiry_thread, SIGUSR1);
  201. return (res);
  202. }
  203. void corosync_timer_delete (
  204. timer_handle th)
  205. {
  206. int unlock;
  207. if (th == 0) {
  208. return;
  209. }
  210. if (pthread_equal (pthread_self(), expiry_thread) != 0) {
  211. unlock = 0;
  212. } else {
  213. unlock = 1;
  214. pthread_mutex_lock (&timer_mutex);
  215. }
  216. timerlist_del (&timers_timerlist, th);
  217. if (unlock) {
  218. pthread_mutex_unlock (&timer_mutex);
  219. }
  220. }
  221. void corosync_timer_lock (void)
  222. {
  223. pthread_mutex_lock (&timer_mutex);
  224. }
  225. void corosync_timer_unlock (void)
  226. {
  227. pthread_mutex_unlock (&timer_mutex);
  228. }
  229. unsigned long long corosync_timer_time_get (void)
  230. {
  231. return (timerlist_nano_from_epoch());
  232. }
  233. unsigned long long corosync_timer_expire_time_get (
  234. timer_handle th)
  235. {
  236. int unlock;
  237. unsigned long long expire;
  238. if (th == 0) {
  239. return (0);
  240. }
  241. if (pthread_equal (pthread_self(), expiry_thread) != 0) {
  242. unlock = 0;
  243. } else {
  244. unlock = 1;
  245. pthread_mutex_lock (&timer_mutex);
  246. }
  247. expire = timerlist_expire_time (&timers_timerlist, th);
  248. if (unlock) {
  249. pthread_mutex_unlock (&timer_mutex);
  250. }
  251. return (expire);
  252. }