timer.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * Copyright (c) 2002-2006 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2008 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/list.h>
  61. #include <corosync/queue.h>
  62. #include <corosync/lcr/lcr_ifact.h>
  63. #include <corosync/totem/coropoll.h>
  64. #include <corosync/totem/totempg.h>
  65. #include <corosync/engine/objdb.h>
  66. #include <corosync/engine/config.h>
  67. #define LOG_SERVICE LOG_SERVICE_IPC
  68. #include <corosync/engine/logsys.h>
  69. #include "poll.h"
  70. #include "totemsrp.h"
  71. #include "mempool.h"
  72. #include "mainconfig.h"
  73. #include "totemconfig.h"
  74. #include "main.h"
  75. #include "sync.h"
  76. #include "tlist.h"
  77. #include "util.h"
  78. #define SERVER_BACKLOG 5
  79. static pthread_mutex_t timer_mutex = PTHREAD_MUTEX_INITIALIZER;
  80. static pthread_t expiry_thread;
  81. static pthread_attr_t thread_attr;
  82. static struct timerlist timers_timerlist;
  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(TS_CLASS) && (defined(COROSYNC_BSD) || defined(COROSYNC_LINUX) || defined(COROSYNC_SOLARIS))
  95. struct sched_param sched_param;
  96. int res;
  97. sched_param.sched_priority = 2;
  98. res = pthread_setschedparam (expiry_thread, SCHED_RR, &sched_param);
  99. #endif
  100. pthread_mutex_unlock (&timer_mutex);
  101. for (;;) {
  102. retry_poll:
  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 == -1) {
  111. goto retry_poll;
  112. }
  113. pthread_mutex_lock (&timer_mutex);
  114. timer_serialize_lock_fn ();
  115. timerlist_expire (&timers_timerlist);
  116. timer_serialize_unlock_fn ();
  117. pthread_mutex_unlock (&timer_mutex);
  118. }
  119. pthread_exit (0);
  120. }
  121. static void sigusr1_handler (int num) {
  122. #ifdef COROSYNC_SOLARIS
  123. /* Rearm the signal facility */
  124. signal (num, sigusr1_handler);
  125. #endif
  126. }
  127. int corosync_timer_init (
  128. void (*serialize_lock_fn) (void),
  129. void (*serialize_unlock_fn) (void))
  130. {
  131. int res;
  132. timer_serialize_lock_fn = serialize_lock_fn;
  133. timer_serialize_unlock_fn = serialize_unlock_fn;
  134. timerlist_init (&timers_timerlist);
  135. signal (SIGUSR1, sigusr1_handler);
  136. pthread_mutex_lock (&timer_mutex);
  137. pthread_attr_init (&thread_attr);
  138. pthread_attr_setstacksize (&thread_attr, 100000);
  139. pthread_attr_setdetachstate (&thread_attr, PTHREAD_CREATE_DETACHED);
  140. res = pthread_create (&expiry_thread, &thread_attr,
  141. prioritized_timer_thread, NULL);
  142. return (res);
  143. }
  144. int corosync_timer_add_absolute (
  145. unsigned long long nanosec_from_epoch,
  146. void *data,
  147. void (*timer_fn) (void *data),
  148. timer_handle *handle)
  149. {
  150. int res;
  151. int unlock;
  152. if (pthread_equal (pthread_self(), expiry_thread) != 0) {
  153. unlock = 0;
  154. } else {
  155. unlock = 1;
  156. pthread_mutex_lock (&timer_mutex);
  157. }
  158. res = timerlist_add_absolute (
  159. &timers_timerlist,
  160. timer_fn,
  161. data,
  162. nanosec_from_epoch,
  163. handle);
  164. if (unlock) {
  165. pthread_mutex_unlock (&timer_mutex);
  166. }
  167. pthread_kill (expiry_thread, SIGUSR1);
  168. return (res);
  169. }
  170. int corosync_timer_add_duration (
  171. unsigned long long nanosec_duration,
  172. void *data,
  173. void (*timer_fn) (void *data),
  174. timer_handle *handle)
  175. {
  176. int res;
  177. int unlock;
  178. if (pthread_equal (pthread_self(), expiry_thread) != 0) {
  179. unlock = 0;
  180. } else {
  181. unlock = 1;
  182. pthread_mutex_lock (&timer_mutex);
  183. }
  184. res = timerlist_add_duration (
  185. &timers_timerlist,
  186. timer_fn,
  187. data,
  188. nanosec_duration,
  189. handle);
  190. if (unlock) {
  191. pthread_mutex_unlock (&timer_mutex);
  192. }
  193. pthread_kill (expiry_thread, SIGUSR1);
  194. return (res);
  195. }
  196. void corosync_timer_delete (
  197. timer_handle timer_handle)
  198. {
  199. int unlock;
  200. if (timer_handle == 0) {
  201. return;
  202. }
  203. if (pthread_equal (pthread_self(), expiry_thread) != 0) {
  204. unlock = 0;
  205. } else {
  206. unlock = 1;
  207. pthread_mutex_lock (&timer_mutex);
  208. }
  209. timerlist_del (&timers_timerlist, timer_handle);
  210. if (unlock) {
  211. pthread_mutex_unlock (&timer_mutex);
  212. }
  213. }
  214. void corosync_timer_lock (void)
  215. {
  216. pthread_mutex_lock (&timer_mutex);
  217. }
  218. void corosync_timer_unlock (void)
  219. {
  220. pthread_mutex_unlock (&timer_mutex);
  221. }
  222. unsigned long long corosync_timer_time_get (void)
  223. {
  224. return (timerlist_nano_from_epoch());
  225. }
  226. unsigned long long corosync_timer_expire_time_get (
  227. timer_handle timer_handle)
  228. {
  229. int unlock;
  230. unsigned long long expire;
  231. if (timer_handle == 0) {
  232. return (0);
  233. }
  234. if (pthread_equal (pthread_self(), expiry_thread) != 0) {
  235. unlock = 0;
  236. } else {
  237. unlock = 1;
  238. pthread_mutex_lock (&timer_mutex);
  239. }
  240. expire = timerlist_expire_time (&timers_timerlist, timer_handle);
  241. if (unlock) {
  242. pthread_mutex_unlock (&timer_mutex);
  243. }
  244. return (expire);
  245. }