timer.inl 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /* This file is part of the CivetWeb web server.
  2. * See https://github.com/civetweb/civetweb/
  3. * (C) 2014-2017 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[MAX_TIMERS]; /* List of timers */
  19. unsigned timer_count; /* Current size of timer list */
  20. };
  21. TIMER_API double
  22. timer_getcurrenttime(void)
  23. {
  24. #if defined(_WIN32)
  25. /* GetTickCount returns milliseconds since system start as
  26. * unsigned 32 bit value. It will wrap around every 49.7 days.
  27. * We need to use a 64 bit counter (will wrap in 500 mio. years),
  28. * by adding the 32 bit difference since the last call to a
  29. * 64 bit counter. This algorithm will only work, if this
  30. * function is called at least once every 7 weeks. */
  31. static DWORD last_tick;
  32. static uint64_t now_tick64;
  33. DWORD now_tick = GetTickCount();
  34. now_tick64 += ((DWORD)(now_tick - last_tick));
  35. last_tick = now_tick;
  36. return (double)now_tick64 * 1.0E-3;
  37. #else
  38. struct timespec now_ts;
  39. clock_gettime(CLOCK_MONOTONIC, &now_ts);
  40. return (double)now_ts.tv_sec + (double)now_ts.tv_nsec * 1.0E-9;
  41. #endif
  42. }
  43. TIMER_API int
  44. timer_add(struct mg_context *ctx,
  45. double next_time,
  46. double period,
  47. int is_relative,
  48. taction action,
  49. void *arg)
  50. {
  51. unsigned u, v;
  52. int error = 0;
  53. double now;
  54. if (ctx->stop_flag) {
  55. return 0;
  56. }
  57. now = timer_getcurrenttime();
  58. /* HCP24: if is_relative = 0 and next_time < now
  59. * action will be called so fast as possible
  60. * if additional period > 0
  61. * action will be called so fast as possible
  62. * n times until (next_time + (n * period)) > now
  63. * then the period is working
  64. * Solution:
  65. * if next_time < now then we set next_time = now.
  66. * The first callback will be so fast as possible (now)
  67. * but the next callback on period
  68. */
  69. if (is_relative) {
  70. next_time += now;
  71. }
  72. /* You can not set timers into the past */
  73. if (next_time < now) {
  74. next_time = now;
  75. }
  76. pthread_mutex_lock(&ctx->timers->mutex);
  77. if (ctx->timers->timer_count == MAX_TIMERS) {
  78. error = 1;
  79. } else {
  80. /* Insert new timer into a sorted list. */
  81. /* The linear list is still most efficient for short lists (small
  82. * number of timers) - if there are many timers, different
  83. * algorithms will work better. */
  84. for (u = 0; u < ctx->timers->timer_count; u++) {
  85. if (ctx->timers->timers[u].time > next_time) {
  86. /* HCP24: moving all timers > next_time */
  87. for (v = ctx->timers->timer_count; v > u; v--) {
  88. ctx->timers->timers[v] = ctx->timers->timers[v - 1];
  89. }
  90. break;
  91. }
  92. }
  93. ctx->timers->timers[u].time = next_time;
  94. ctx->timers->timers[u].period = period;
  95. ctx->timers->timers[u].action = action;
  96. ctx->timers->timers[u].arg = arg;
  97. ctx->timers->timer_count++;
  98. }
  99. pthread_mutex_unlock(&ctx->timers->mutex);
  100. return error;
  101. }
  102. static void
  103. timer_thread_run(void *thread_func_param)
  104. {
  105. struct mg_context *ctx = (struct mg_context *)thread_func_param;
  106. double d;
  107. unsigned u;
  108. int re_schedule;
  109. struct ttimer t;
  110. mg_set_thread_name("timer");
  111. if (ctx->callbacks.init_thread) {
  112. /* Timer thread */
  113. ctx->callbacks.init_thread(ctx, 2);
  114. }
  115. d = timer_getcurrenttime();
  116. while (ctx->stop_flag == 0) {
  117. pthread_mutex_lock(&ctx->timers->mutex);
  118. if ((ctx->timers->timer_count > 0)
  119. && (d >= ctx->timers->timers[0].time)) {
  120. t = ctx->timers->timers[0];
  121. for (u = 1; u < ctx->timers->timer_count; u++) {
  122. ctx->timers->timers[u - 1] = ctx->timers->timers[u];
  123. }
  124. ctx->timers->timer_count--;
  125. pthread_mutex_unlock(&ctx->timers->mutex);
  126. re_schedule = t.action(t.arg);
  127. if (re_schedule && (t.period > 0)) {
  128. timer_add(ctx, t.time + t.period, t.period, 0, t.action, t.arg);
  129. }
  130. continue;
  131. } else {
  132. pthread_mutex_unlock(&ctx->timers->mutex);
  133. }
  134. /* 10 ms seems reasonable.
  135. * A faster loop (smaller sleep value) increases CPU load,
  136. * a slower loop (higher sleep value) decreases timer accuracy.
  137. */
  138. #ifdef _WIN32
  139. Sleep(10);
  140. #else
  141. usleep(10000);
  142. #endif
  143. d = timer_getcurrenttime();
  144. }
  145. pthread_mutex_lock(&ctx->timers->mutex);
  146. ctx->timers->timer_count = 0;
  147. pthread_mutex_unlock(&ctx->timers->mutex);
  148. }
  149. #ifdef _WIN32
  150. static unsigned __stdcall timer_thread(void *thread_func_param)
  151. {
  152. timer_thread_run(thread_func_param);
  153. return 0;
  154. }
  155. #else
  156. static void *
  157. timer_thread(void *thread_func_param)
  158. {
  159. timer_thread_run(thread_func_param);
  160. return NULL;
  161. }
  162. #endif /* _WIN32 */
  163. TIMER_API int
  164. timers_init(struct mg_context *ctx)
  165. {
  166. ctx->timers =
  167. (struct ttimers *)mg_calloc_ctx(sizeof(struct ttimers), 1, ctx);
  168. (void)pthread_mutex_init(&ctx->timers->mutex, NULL);
  169. (void)timer_getcurrenttime();
  170. /* Start timer thread */
  171. mg_start_thread_with_id(timer_thread, ctx, &ctx->timers->threadid);
  172. return 0;
  173. }
  174. TIMER_API void
  175. timers_exit(struct mg_context *ctx)
  176. {
  177. if (ctx->timers) {
  178. pthread_mutex_lock(&ctx->timers->mutex);
  179. ctx->timers->timer_count = 0;
  180. mg_join_thread(ctx->timers->threadid);
  181. /* TODO: Do we really need to unlock the mutex, before
  182. * destroying it, if it's destroyed by the thread currently
  183. * owning the mutex? */
  184. pthread_mutex_unlock(&ctx->timers->mutex);
  185. (void)pthread_mutex_destroy(&ctx->timers->mutex);
  186. mg_free(ctx->timers);
  187. }
  188. }
  189. /* End of timer.inl */