timer.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearmand client and server library.
  4. *
  5. * Copyright (C) 2012 Data Differential, http://datadifferential.com/ All
  6. * rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are
  10. * met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following disclaimer
  17. * in the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * * The names of its contributors may not be used to endorse or
  21. * promote products derived from this software without specific prior
  22. * written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  27. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  28. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  29. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  30. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  31. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  32. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. */
  37. #include "gear_config.h"
  38. #include "libgearman-server/common.h"
  39. #include <libgearman-server/gearmand.h>
  40. #include <libgearman/pipe.h>
  41. #include <libgearman-server/timer.h>
  42. #include <cerrno>
  43. #include <cstring>
  44. #include <ctime>
  45. #include <pthread.h>
  46. #include <signal.h>
  47. static pthread_once_t start_key_once= PTHREAD_ONCE_INIT;
  48. static pthread_t thread_id;
  49. static struct timeval current_epoch;
  50. static int wakeup_fd[2];
  51. static __attribute__((noreturn)) void* current_epoch_handler(void*)
  52. {
  53. gearmand_debug("starting up Epoch thread");
  54. pollfd fds[2];
  55. while (true)
  56. {
  57. memset(fds, 0, sizeof(fds));
  58. fds[0].fd= -1; //STDIN_FILENO;
  59. fds[0].events= POLLIN;
  60. fds[0].revents= 0;
  61. fds[1].fd= wakeup_fd[0]; // wakeup fd
  62. fds[1].events= POLLIN;
  63. fds[1].revents= 0;
  64. int active_fd;
  65. if ((active_fd= poll(fds, 2, 1000)) == -1)
  66. {
  67. gearmand_perror(errno, "poll");
  68. }
  69. else
  70. {
  71. if (fds[1].revents)
  72. {
  73. pthread_exit(NULL);
  74. }
  75. gettimeofday(&current_epoch, NULL);
  76. }
  77. }
  78. pthread_exit(NULL);
  79. }
  80. namespace libgearman {
  81. namespace server {
  82. static void startup(void)
  83. {
  84. wakeup_fd[0]= -1;
  85. wakeup_fd[1]= -1;
  86. if (setup_shutdown_pipe(wakeup_fd) == false)
  87. {
  88. fprintf(stderr, "Could not setup pipe\n");
  89. exit(1);
  90. }
  91. gettimeofday(&current_epoch, NULL);
  92. int error;
  93. if ((error= pthread_create(&thread_id, NULL, current_epoch_handler, NULL)))
  94. {
  95. fprintf(stderr, "pthread_create() failed: %s\n", strerror(error));
  96. exit(1);
  97. }
  98. }
  99. Epoch::Epoch()
  100. {
  101. (void) pthread_once(&start_key_once, startup);
  102. }
  103. Epoch::~Epoch()
  104. {
  105. gearmand_debug("shutting down Epoch thread");
  106. int count= 5;
  107. for (size_t x= 0; x < 2; x++)
  108. {
  109. while (--count)
  110. {
  111. if (close(wakeup_fd[x]) == -1)
  112. {
  113. switch(errno)
  114. {
  115. case EAGAIN:
  116. continue;
  117. default:
  118. break;
  119. }
  120. }
  121. break; // close()
  122. }
  123. }
  124. #if 0
  125. int error;
  126. if ((error= pthread_kill(thread_id, SIGPIPE)) != 0)
  127. {
  128. gearmand_perror(error, "pthread_kill(thread_id, SIGTERM)");
  129. }
  130. #endif
  131. pthread_join(thread_id, 0);
  132. gearmand_debug("shutdown of Epoch completed");
  133. }
  134. struct timeval Epoch::current()
  135. {
  136. return current_epoch;
  137. }
  138. } // server
  139. } // libgearman