timer.inl 4.9 KB

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