timer.inl 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /* This file is part of the CivetWeb web server.
  2. * See https://github.com/civetweb/civetweb/
  3. * (C) 2014-2018 by the CivetWeb authors, MIT license.
  4. */
  5. #if !defined(MAX_TIMERS)
  6. #define MAX_TIMERS MAX_WORKER_THREADS
  7. #endif
  8. typedef int (*taction)(void *arg);
  9. struct ttimer {
  10. double time;
  11. double period;
  12. taction action;
  13. void *arg;
  14. };
  15. struct ttimers {
  16. pthread_t threadid; /* Timer thread ID */
  17. pthread_mutex_t mutex; /* Protects timer lists */
  18. struct ttimer *timers; /* List of timers */
  19. unsigned timer_count; /* Current size of timer list */
  20. unsigned timer_capacity; /* Capacity of timer list */
  21. #if defined(_WIN32)
  22. DWORD last_tick;
  23. uint64_t now_tick64;
  24. #endif
  25. };
  26. TIMER_API double
  27. timer_getcurrenttime(struct mg_context *ctx)
  28. {
  29. #if defined(_WIN32)
  30. /* GetTickCount returns milliseconds since system start as
  31. * unsigned 32 bit value. It will wrap around every 49.7 days.
  32. * We need to use a 64 bit counter (will wrap in 500 mio. years),
  33. * by adding the 32 bit difference since the last call to a
  34. * 64 bit counter. This algorithm will only work, if this
  35. * function is called at least once every 7 weeks. */
  36. uint64_t now_tick64 = 0;
  37. DWORD now_tick = GetTickCount();
  38. if (ctx->timers) {
  39. pthread_mutex_lock(&ctx->timers->mutex);
  40. ctx->timers->now_tick64 += now_tick - ctx->timers->last_tick;
  41. now_tick64 = ctx->timers->now_tick64;
  42. ctx->timers->last_tick = now_tick;
  43. pthread_mutex_unlock(&ctx->timers->mutex);
  44. }
  45. return (double)now_tick64 * 1.0E-3;
  46. #else
  47. struct timespec now_ts;
  48. (void)ctx;
  49. clock_gettime(CLOCK_MONOTONIC, &now_ts);
  50. return (double)now_ts.tv_sec + (double)now_ts.tv_nsec * 1.0E-9;
  51. #endif
  52. }
  53. TIMER_API int
  54. timer_add(struct mg_context *ctx,
  55. double next_time,
  56. double period,
  57. int is_relative,
  58. taction action,
  59. void *arg)
  60. {
  61. int error = 0;
  62. double now;
  63. if (!ctx->timers) {
  64. return 1;
  65. }
  66. now = timer_getcurrenttime(ctx);
  67. /* HCP24: if is_relative = 0 and next_time < now
  68. * action will be called so fast as possible
  69. * if additional period > 0
  70. * action will be called so fast as possible
  71. * n times until (next_time + (n * period)) > now
  72. * then the period is working
  73. * Solution:
  74. * if next_time < now then we set next_time = now.
  75. * The first callback will be so fast as possible (now)
  76. * but the next callback on period
  77. */
  78. if (is_relative) {
  79. next_time += now;
  80. }
  81. /* You can not set timers into the past */
  82. if (next_time < now) {
  83. next_time = now;
  84. }
  85. pthread_mutex_lock(&ctx->timers->mutex);
  86. if (ctx->timers->timer_count == MAX_TIMERS) {
  87. error = 1;
  88. } else if (ctx->timers->timer_count == ctx->timers->timer_capacity) {
  89. unsigned capacity = (ctx->timers->timer_capacity * 2) + 1;
  90. struct ttimer *timers = (struct ttimer *)mg_realloc_ctx(
  91. ctx->timers->timers, capacity * sizeof(struct ttimer), ctx);
  92. if (timers) {
  93. ctx->timers->timers = timers;
  94. ctx->timers->timer_capacity = capacity;
  95. } else {
  96. error = 1;
  97. }
  98. }
  99. if (!error) {
  100. /* Insert new timer into a sorted list. */
  101. /* The linear list is still most efficient for short lists (small
  102. * number of timers) - if there are many timers, different
  103. * algorithms will work better. */
  104. unsigned u = ctx->timers->timer_count;
  105. for (; (u > 0) && (ctx->timers->timers[u - 1].time > next_time); u--) {
  106. ctx->timers->timers[u] = ctx->timers->timers[u - 1];
  107. }
  108. ctx->timers->timers[u].time = next_time;
  109. ctx->timers->timers[u].period = period;
  110. ctx->timers->timers[u].action = action;
  111. ctx->timers->timers[u].arg = arg;
  112. ctx->timers->timer_count++;
  113. }
  114. pthread_mutex_unlock(&ctx->timers->mutex);
  115. return error;
  116. }
  117. static void
  118. timer_thread_run(void *thread_func_param)
  119. {
  120. struct mg_context *ctx = (struct mg_context *)thread_func_param;
  121. double d;
  122. unsigned u;
  123. int re_schedule;
  124. struct ttimer t;
  125. mg_set_thread_name("timer");
  126. if (ctx->callbacks.init_thread) {
  127. /* Timer thread */
  128. ctx->callbacks.init_thread(ctx, 2);
  129. }
  130. d = timer_getcurrenttime(ctx);
  131. while (ctx->stop_flag == 0) {
  132. pthread_mutex_lock(&ctx->timers->mutex);
  133. if ((ctx->timers->timer_count > 0)
  134. && (d >= ctx->timers->timers[0].time)) {
  135. t = ctx->timers->timers[0];
  136. for (u = 1; u < ctx->timers->timer_count; u++) {
  137. ctx->timers->timers[u - 1] = ctx->timers->timers[u];
  138. }
  139. ctx->timers->timer_count--;
  140. pthread_mutex_unlock(&ctx->timers->mutex);
  141. re_schedule = t.action(t.arg);
  142. if (re_schedule && (t.period > 0)) {
  143. timer_add(ctx, t.time + t.period, t.period, 0, t.action, t.arg);
  144. }
  145. continue;
  146. } else {
  147. pthread_mutex_unlock(&ctx->timers->mutex);
  148. }
  149. /* 10 ms seems reasonable.
  150. * A faster loop (smaller sleep value) increases CPU load,
  151. * a slower loop (higher sleep value) decreases timer accuracy.
  152. */
  153. #if defined(_WIN32)
  154. Sleep(10);
  155. #else
  156. usleep(10000);
  157. #endif
  158. d = timer_getcurrenttime(ctx);
  159. }
  160. }
  161. #if defined(_WIN32)
  162. static unsigned __stdcall timer_thread(void *thread_func_param)
  163. {
  164. timer_thread_run(thread_func_param);
  165. return 0;
  166. }
  167. #else
  168. static void *
  169. timer_thread(void *thread_func_param)
  170. {
  171. struct sigaction sa;
  172. /* Ignore SIGPIPE */
  173. memset(&sa, 0, sizeof(sa));
  174. sa.sa_handler = SIG_IGN;
  175. sigaction(SIGPIPE, &sa, NULL);
  176. timer_thread_run(thread_func_param);
  177. return NULL;
  178. }
  179. #endif /* _WIN32 */
  180. TIMER_API int
  181. timers_init(struct mg_context *ctx)
  182. {
  183. /* Initialize timers data structure */
  184. ctx->timers =
  185. (struct ttimers *)mg_calloc_ctx(sizeof(struct ttimers), 1, ctx);
  186. if (!ctx->timers) {
  187. return -1;
  188. }
  189. ctx->timers->timers = NULL;
  190. /* Initialize mutex */
  191. if (0 != pthread_mutex_init(&ctx->timers->mutex, NULL)) {
  192. mg_free(ctx->timers);
  193. ctx->timers = NULL;
  194. return -1;
  195. }
  196. /* For some systems timer_getcurrenttime does some initialization
  197. * during the first call. Call it once now, ignore the result. */
  198. (void)timer_getcurrenttime(ctx);
  199. /* Start timer thread */
  200. if (mg_start_thread_with_id(timer_thread, ctx, &ctx->timers->threadid)
  201. != 0) {
  202. (void)pthread_mutex_destroy(&ctx->timers->mutex);
  203. mg_free(ctx->timers);
  204. ctx->timers = NULL;
  205. return -1;
  206. }
  207. return 0;
  208. }
  209. TIMER_API void
  210. timers_exit(struct mg_context *ctx)
  211. {
  212. if (ctx->timers) {
  213. mg_join_thread(ctx->timers->threadid);
  214. (void)pthread_mutex_destroy(&ctx->timers->mutex);
  215. mg_free(ctx->timers->timers);
  216. mg_free(ctx->timers);
  217. ctx->timers = NULL;
  218. }
  219. }
  220. /* End of timer.inl */