4
0

timer.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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/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 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(TS_CLASS) && (defined(COROSYNC_BSD) || defined(COROSYNC_LINUX) || defined(COROSYNC_SOLARIS))
  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_unlock (&timer_mutex);
  103. for (;;) {
  104. timer_serialize_lock_fn ();
  105. timeout = timerlist_msec_duration_to_expire (&timers_timerlist);
  106. if (timeout != -1 && timeout > 0xFFFFFFFF) {
  107. timeout = 0xFFFFFFFE;
  108. }
  109. timer_serialize_unlock_fn ();
  110. fds = poll (NULL, 0, timeout);
  111. if (fds == 0 || (fds < 0 && errno == EINTR)) {
  112. continue;
  113. }
  114. if (fds < 0) {
  115. return NULL;
  116. }
  117. pthread_mutex_lock (&timer_mutex);
  118. timer_serialize_lock_fn ();
  119. timerlist_expire (&timers_timerlist);
  120. timer_serialize_unlock_fn ();
  121. pthread_mutex_unlock (&timer_mutex);
  122. }
  123. }
  124. static void sigusr1_handler (int num) {
  125. #ifdef COROSYNC_SOLARIS
  126. /* Rearm the signal facility */
  127. signal (num, sigusr1_handler);
  128. #endif
  129. }
  130. int corosync_timer_init (
  131. void (*serialize_lock_fn) (void),
  132. void (*serialize_unlock_fn) (void),
  133. int sched_priority_in)
  134. {
  135. int res;
  136. timer_serialize_lock_fn = serialize_lock_fn;
  137. timer_serialize_unlock_fn = serialize_unlock_fn;
  138. sched_priority = sched_priority_in;
  139. timerlist_init (&timers_timerlist);
  140. signal (SIGUSR1, sigusr1_handler);
  141. pthread_mutex_lock (&timer_mutex);
  142. pthread_attr_init (&thread_attr);
  143. pthread_attr_setstacksize (&thread_attr, 100000);
  144. pthread_attr_setdetachstate (&thread_attr, PTHREAD_CREATE_DETACHED);
  145. res = pthread_create (&expiry_thread, &thread_attr,
  146. prioritized_timer_thread, NULL);
  147. return (res);
  148. }
  149. int corosync_timer_add_absolute (
  150. unsigned long long nanosec_from_epoch,
  151. void *data,
  152. void (*timer_fn) (void *data),
  153. timer_handle *handle)
  154. {
  155. int res;
  156. int unlock;
  157. if (pthread_equal (pthread_self(), expiry_thread) != 0) {
  158. unlock = 0;
  159. } else {
  160. unlock = 1;
  161. pthread_mutex_lock (&timer_mutex);
  162. }
  163. res = timerlist_add_absolute (
  164. &timers_timerlist,
  165. timer_fn,
  166. data,
  167. nanosec_from_epoch,
  168. handle);
  169. if (unlock) {
  170. pthread_mutex_unlock (&timer_mutex);
  171. }
  172. pthread_kill (expiry_thread, SIGUSR1);
  173. return (res);
  174. }
  175. int corosync_timer_add_duration (
  176. unsigned long long nanosec_duration,
  177. void *data,
  178. void (*timer_fn) (void *data),
  179. timer_handle *handle)
  180. {
  181. int res;
  182. int unlock;
  183. if (pthread_equal (pthread_self(), expiry_thread) != 0) {
  184. unlock = 0;
  185. } else {
  186. unlock = 1;
  187. pthread_mutex_lock (&timer_mutex);
  188. }
  189. res = timerlist_add_duration (
  190. &timers_timerlist,
  191. timer_fn,
  192. data,
  193. nanosec_duration,
  194. handle);
  195. if (unlock) {
  196. pthread_mutex_unlock (&timer_mutex);
  197. }
  198. pthread_kill (expiry_thread, SIGUSR1);
  199. return (res);
  200. }
  201. void corosync_timer_delete (
  202. timer_handle timer_handle)
  203. {
  204. int unlock;
  205. if (timer_handle == 0) {
  206. return;
  207. }
  208. if (pthread_equal (pthread_self(), expiry_thread) != 0) {
  209. unlock = 0;
  210. } else {
  211. unlock = 1;
  212. pthread_mutex_lock (&timer_mutex);
  213. }
  214. timerlist_del (&timers_timerlist, timer_handle);
  215. if (unlock) {
  216. pthread_mutex_unlock (&timer_mutex);
  217. }
  218. }
  219. void corosync_timer_lock (void)
  220. {
  221. pthread_mutex_lock (&timer_mutex);
  222. }
  223. void corosync_timer_unlock (void)
  224. {
  225. pthread_mutex_unlock (&timer_mutex);
  226. }
  227. unsigned long long corosync_timer_time_get (void)
  228. {
  229. return (timerlist_nano_from_epoch());
  230. }
  231. unsigned long long corosync_timer_expire_time_get (
  232. timer_handle timer_handle)
  233. {
  234. int unlock;
  235. unsigned long long expire;
  236. if (timer_handle == 0) {
  237. return (0);
  238. }
  239. if (pthread_equal (pthread_self(), expiry_thread) != 0) {
  240. unlock = 0;
  241. } else {
  242. unlock = 1;
  243. pthread_mutex_lock (&timer_mutex);
  244. }
  245. expire = timerlist_expire_time (&timers_timerlist, timer_handle);
  246. if (unlock) {
  247. pthread_mutex_unlock (&timer_mutex);
  248. }
  249. return (expire);
  250. }