timer.inl 7.2 KB

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