timer.inl 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 =
  91. (struct ttimer *)mg_realloc_ctx(ctx->timers->timers,
  92. capacity * sizeof(struct ttimer),
  93. ctx);
  94. if (timers) {
  95. ctx->timers->timers = timers;
  96. ctx->timers->timer_capacity = capacity;
  97. } else {
  98. error = 1;
  99. }
  100. }
  101. if (!error) {
  102. /* Insert new timer into a sorted list. */
  103. /* The linear list is still most efficient for short lists (small
  104. * number of timers) - if there are many timers, different
  105. * algorithms will work better. */
  106. unsigned u = ctx->timers->timer_count;
  107. for (; (u > 0) && (ctx->timers->timers[u - 1].time > next_time); u--) {
  108. ctx->timers->timers[u] = ctx->timers->timers[u - 1];
  109. }
  110. ctx->timers->timers[u].time = next_time;
  111. ctx->timers->timers[u].period = period;
  112. ctx->timers->timers[u].action = action;
  113. ctx->timers->timers[u].arg = arg;
  114. ctx->timers->timer_count++;
  115. }
  116. pthread_mutex_unlock(&ctx->timers->mutex);
  117. return error;
  118. }
  119. static void
  120. timer_thread_run(void *thread_func_param)
  121. {
  122. struct mg_context *ctx = (struct mg_context *)thread_func_param;
  123. double d;
  124. unsigned u;
  125. int re_schedule;
  126. struct ttimer t;
  127. mg_set_thread_name("timer");
  128. if (ctx->callbacks.init_thread) {
  129. /* Timer thread */
  130. ctx->callbacks.init_thread(ctx, 2);
  131. }
  132. d = timer_getcurrenttime(ctx);
  133. while (ctx->stop_flag == 0) {
  134. pthread_mutex_lock(&ctx->timers->mutex);
  135. if ((ctx->timers->timer_count > 0)
  136. && (d >= ctx->timers->timers[0].time)) {
  137. t = ctx->timers->timers[0];
  138. for (u = 1; u < ctx->timers->timer_count; u++) {
  139. ctx->timers->timers[u - 1] = ctx->timers->timers[u];
  140. }
  141. ctx->timers->timer_count--;
  142. pthread_mutex_unlock(&ctx->timers->mutex);
  143. re_schedule = t.action(t.arg);
  144. if (re_schedule && (t.period > 0)) {
  145. timer_add(ctx, t.time + t.period, t.period, 0, t.action, t.arg);
  146. }
  147. continue;
  148. } else {
  149. pthread_mutex_unlock(&ctx->timers->mutex);
  150. }
  151. /* 10 ms seems reasonable.
  152. * A faster loop (smaller sleep value) increases CPU load,
  153. * a slower loop (higher sleep value) decreases timer accuracy.
  154. */
  155. #if defined(_WIN32)
  156. Sleep(10);
  157. #else
  158. usleep(10000);
  159. #endif
  160. d = timer_getcurrenttime(ctx);
  161. }
  162. }
  163. #if defined(_WIN32)
  164. static unsigned __stdcall timer_thread(void *thread_func_param)
  165. {
  166. timer_thread_run(thread_func_param);
  167. return 0;
  168. }
  169. #else
  170. static void *
  171. timer_thread(void *thread_func_param)
  172. {
  173. struct sigaction sa;
  174. /* Ignore SIGPIPE */
  175. memset(&sa, 0, sizeof(sa));
  176. sa.sa_handler = SIG_IGN;
  177. sigaction(SIGPIPE, &sa, NULL);
  178. timer_thread_run(thread_func_param);
  179. return NULL;
  180. }
  181. #endif /* _WIN32 */
  182. TIMER_API int
  183. timers_init(struct mg_context *ctx)
  184. {
  185. /* Initialize timers data structure */
  186. ctx->timers =
  187. (struct ttimers *)mg_calloc_ctx(sizeof(struct ttimers), 1, ctx);
  188. if (!ctx->timers) {
  189. return -1;
  190. }
  191. ctx->timers->timers = NULL;
  192. /* Initialize mutex */
  193. if (0 != pthread_mutex_init(&ctx->timers->mutex, NULL)) {
  194. mg_free(ctx->timers);
  195. ctx->timers = NULL;
  196. return -1;
  197. }
  198. /* For some systems timer_getcurrenttime does some initialization
  199. * during the first call. Call it once now, ignore the result. */
  200. (void)timer_getcurrenttime(ctx);
  201. /* Start timer thread */
  202. if (mg_start_thread_with_id(timer_thread, ctx, &ctx->timers->threadid)
  203. != 0) {
  204. (void)pthread_mutex_destroy(&ctx->timers->mutex);
  205. mg_free(ctx->timers);
  206. ctx->timers = NULL;
  207. return -1;
  208. }
  209. return 0;
  210. }
  211. TIMER_API void
  212. timers_exit(struct mg_context *ctx)
  213. {
  214. if (ctx->timers) {
  215. mg_join_thread(ctx->timers->threadid);
  216. (void)pthread_mutex_destroy(&ctx->timers->mutex);
  217. mg_free(ctx->timers->timers);
  218. mg_free(ctx->timers);
  219. ctx->timers = NULL;
  220. }
  221. }
  222. /* End of timer.inl */