timer.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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_cond_t timer_mutex_cond = PTHREAD_COND_INITIALIZER;
  80. static pthread_t expiry_thread;
  81. static pthread_attr_t thread_attr;
  82. static struct timerlist timers_timerlist;
  83. static int sched_priority = 0;
  84. static void (*timer_serialize_lock_fn) (void);
  85. static void (*timer_serialize_unlock_fn) (void);
  86. static void *prioritized_timer_thread (void *data);
  87. extern void pthread_exit(void *) __attribute__((noreturn));
  88. /*
  89. * This thread runs at the highest priority to run system wide timers
  90. */
  91. static void *prioritized_timer_thread (void *data)
  92. {
  93. int fds;
  94. unsigned long long timeout;
  95. #if defined(HAVE_PTHREAD_SETSCHEDPARAM) && defined(HAVE_SCHED_GET_PRIORITY_MAX)
  96. if (sched_priority != 0) {
  97. struct sched_param sched_param;
  98. sched_param.sched_priority = sched_priority;
  99. pthread_setschedparam (expiry_thread, SCHED_RR, &sched_param);
  100. }
  101. #endif
  102. pthread_mutex_lock (&timer_mutex);
  103. pthread_cond_signal (&timer_mutex_cond);
  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. timer_serialize_lock_fn ();
  120. pthread_mutex_lock (&timer_mutex);
  121. timerlist_expire (&timers_timerlist);
  122. pthread_mutex_unlock (&timer_mutex);
  123. timer_serialize_unlock_fn ();
  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. /*
  150. * Wait for thread to really exec
  151. */
  152. pthread_cond_wait (&timer_mutex_cond, &timer_mutex);
  153. pthread_mutex_unlock (&timer_mutex);
  154. return (res);
  155. }
  156. int corosync_timer_add_absolute (
  157. unsigned long long nanosec_from_epoch,
  158. void *data,
  159. void (*timer_fn) (void *data),
  160. timer_handle *handle)
  161. {
  162. int res;
  163. int unlock;
  164. if (pthread_equal (pthread_self(), expiry_thread) != 0) {
  165. unlock = 0;
  166. } else {
  167. unlock = 1;
  168. pthread_mutex_lock (&timer_mutex);
  169. }
  170. res = timerlist_add_absolute (
  171. &timers_timerlist,
  172. timer_fn,
  173. data,
  174. nanosec_from_epoch,
  175. handle);
  176. if (unlock) {
  177. pthread_mutex_unlock (&timer_mutex);
  178. }
  179. pthread_kill (expiry_thread, SIGUSR1);
  180. return (res);
  181. }
  182. int corosync_timer_add_duration (
  183. unsigned long long nanosec_duration,
  184. void *data,
  185. void (*timer_fn) (void *data),
  186. timer_handle *handle)
  187. {
  188. int res;
  189. int unlock;
  190. if (pthread_equal (pthread_self(), expiry_thread) != 0) {
  191. unlock = 0;
  192. } else {
  193. unlock = 1;
  194. pthread_mutex_lock (&timer_mutex);
  195. }
  196. res = timerlist_add_duration (
  197. &timers_timerlist,
  198. timer_fn,
  199. data,
  200. nanosec_duration,
  201. handle);
  202. if (unlock) {
  203. pthread_mutex_unlock (&timer_mutex);
  204. }
  205. pthread_kill (expiry_thread, SIGUSR1);
  206. return (res);
  207. }
  208. void corosync_timer_delete (
  209. timer_handle th)
  210. {
  211. int unlock;
  212. if (th == 0) {
  213. return;
  214. }
  215. if (pthread_equal (pthread_self(), expiry_thread) != 0) {
  216. unlock = 0;
  217. } else {
  218. unlock = 1;
  219. pthread_mutex_lock (&timer_mutex);
  220. }
  221. timerlist_del (&timers_timerlist, th);
  222. if (unlock) {
  223. pthread_mutex_unlock (&timer_mutex);
  224. }
  225. }
  226. void corosync_timer_lock (void)
  227. {
  228. pthread_mutex_lock (&timer_mutex);
  229. }
  230. void corosync_timer_unlock (void)
  231. {
  232. pthread_mutex_unlock (&timer_mutex);
  233. }
  234. unsigned long long corosync_timer_time_get (void)
  235. {
  236. return (timerlist_nano_from_epoch());
  237. }
  238. unsigned long long corosync_timer_expire_time_get (
  239. timer_handle th)
  240. {
  241. int unlock;
  242. unsigned long long expire;
  243. if (th == 0) {
  244. return (0);
  245. }
  246. if (pthread_equal (pthread_self(), expiry_thread) != 0) {
  247. unlock = 0;
  248. } else {
  249. unlock = 1;
  250. pthread_mutex_lock (&timer_mutex);
  251. }
  252. expire = timerlist_expire_time (&timers_timerlist, th);
  253. if (unlock) {
  254. pthread_mutex_unlock (&timer_mutex);
  255. }
  256. return (expire);
  257. }