timer.inl 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 int
  18. timer_add(struct mg_context *ctx,
  19. double next_time,
  20. double period,
  21. int is_relative,
  22. taction action,
  23. void *arg)
  24. {
  25. unsigned u, v;
  26. int error = 0;
  27. struct timespec now;
  28. double dt; /* double time */
  29. if (ctx->stop_flag) {
  30. return 0;
  31. }
  32. clock_gettime(CLOCK_MONOTONIC, &now);
  33. dt = (double)(now.tv_sec);
  34. dt += (double)(now.tv_nsec) * 1.0E-9;
  35. /* HCP24: if is_relative = 0 and next_time < now
  36. * action will be called so fast as possible
  37. * if additional period > 0
  38. * action will be called so fast as possible
  39. * n times until (next_time + (n * period)) > now
  40. * then the period is working
  41. * Solution:
  42. * if next_time < now then we set next_time = now.
  43. * The first callback will be so fast as possible (now)
  44. * but the next callback on period
  45. */
  46. if (is_relative) {
  47. next_time += dt;
  48. } else if (next_time < dt) {
  49. next_time = dt;
  50. }
  51. pthread_mutex_lock(&ctx->timers->mutex);
  52. if (ctx->timers->timer_count == MAX_TIMERS) {
  53. error = 1;
  54. } else {
  55. for (u = 0; u < ctx->timers->timer_count; u++) {
  56. if (ctx->timers->timers[u].time > next_time) {
  57. /* HCP24: moving all timers > next_time */
  58. for (v = ctx->timers->timer_count; v > u; v--) {
  59. ctx->timers->timers[v] = ctx->timers->timers[v - 1];
  60. }
  61. break;
  62. }
  63. }
  64. ctx->timers->timers[u].time = next_time;
  65. ctx->timers->timers[u].period = period;
  66. ctx->timers->timers[u].action = action;
  67. ctx->timers->timers[u].arg = arg;
  68. ctx->timers->timer_count++;
  69. }
  70. pthread_mutex_unlock(&ctx->timers->mutex);
  71. return error;
  72. }
  73. static void
  74. timer_thread_run(void *thread_func_param)
  75. {
  76. struct mg_context *ctx = (struct mg_context *)thread_func_param;
  77. struct timespec now;
  78. double d;
  79. unsigned u;
  80. int re_schedule;
  81. struct ttimer t;
  82. mg_set_thread_name("timer");
  83. if (ctx->callbacks.init_thread) {
  84. /* Timer thread */
  85. ctx->callbacks.init_thread(ctx, 2);
  86. }
  87. #if defined(HAVE_CLOCK_NANOSLEEP) /* Linux with librt */
  88. /* TODO */
  89. while (clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &request, &request)
  90. == EINTR) { /*nop*/
  91. ;
  92. }
  93. #else
  94. clock_gettime(CLOCK_MONOTONIC, &now);
  95. d = (double)now.tv_sec + (double)now.tv_nsec * 1.0E-9;
  96. while (ctx->stop_flag == 0) {
  97. pthread_mutex_lock(&ctx->timers->mutex);
  98. if (ctx->timers->timer_count > 0 && d >= ctx->timers->timers[0].time) {
  99. t = ctx->timers->timers[0];
  100. for (u = 1; u < ctx->timers->timer_count; u++) {
  101. ctx->timers->timers[u - 1] = ctx->timers->timers[u];
  102. }
  103. ctx->timers->timer_count--;
  104. pthread_mutex_unlock(&ctx->timers->mutex);
  105. re_schedule = t.action(t.arg);
  106. if (re_schedule && (t.period > 0)) {
  107. timer_add(ctx, t.time + t.period, t.period, 0, t.action, t.arg);
  108. }
  109. continue;
  110. } else {
  111. pthread_mutex_unlock(&ctx->timers->mutex);
  112. }
  113. mg_sleep(1);
  114. clock_gettime(CLOCK_MONOTONIC, &now);
  115. d = (double)now.tv_sec + (double)now.tv_nsec * 1.0E-9;
  116. }
  117. ctx->timers->timer_count = 0;
  118. #endif
  119. }
  120. #ifdef _WIN32
  121. static unsigned __stdcall timer_thread(void *thread_func_param)
  122. {
  123. timer_thread_run(thread_func_param);
  124. return 0;
  125. }
  126. #else
  127. static void *
  128. timer_thread(void *thread_func_param)
  129. {
  130. timer_thread_run(thread_func_param);
  131. return NULL;
  132. }
  133. #endif /* _WIN32 */
  134. static int
  135. timers_init(struct mg_context *ctx)
  136. {
  137. ctx->timers = (struct ttimers *)mg_calloc(sizeof(struct ttimers), 1);
  138. (void)pthread_mutex_init(&ctx->timers->mutex, NULL);
  139. /* Start timer thread */
  140. mg_start_thread_with_id(timer_thread, ctx, &ctx->timers->threadid);
  141. return 0;
  142. }
  143. static void
  144. timers_exit(struct mg_context *ctx)
  145. {
  146. if (ctx->timers) {
  147. ctx->timers->timer_count = 0;
  148. (void)pthread_mutex_destroy(&ctx->timers->mutex);
  149. mg_free(ctx->timers);
  150. }
  151. }