timer.inl 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 timer_add(struct mg_context *ctx,
  18. double next_time,
  19. double period,
  20. int is_relative,
  21. taction action,
  22. void *arg)
  23. {
  24. unsigned u, v;
  25. int error = 0;
  26. struct timespec now;
  27. if (ctx->stop_flag) {
  28. return 0;
  29. }
  30. if (is_relative) {
  31. clock_gettime(CLOCK_MONOTONIC, &now);
  32. next_time += now.tv_sec;
  33. next_time += now.tv_nsec * 1.0E-9;
  34. }
  35. pthread_mutex_lock(&ctx->timers->mutex);
  36. if (ctx->timers->timer_count == MAX_TIMERS) {
  37. error = 1;
  38. } else {
  39. for (u = 0; u < ctx->timers->timer_count; u++) {
  40. if (ctx->timers->timers[u].time < next_time) {
  41. for (v = ctx->timers->timer_count; v > u; v--) {
  42. ctx->timers->timers[v] = ctx->timers->timers[v - 1];
  43. }
  44. break;
  45. }
  46. }
  47. ctx->timers->timers[u].time = next_time;
  48. ctx->timers->timers[u].period = period;
  49. ctx->timers->timers[u].action = action;
  50. ctx->timers->timers[u].arg = arg;
  51. ctx->timers->timer_count++;
  52. }
  53. pthread_mutex_unlock(&ctx->timers->mutex);
  54. return error;
  55. }
  56. static void timer_thread_run(void *thread_func_param)
  57. {
  58. struct mg_context *ctx = (struct mg_context *)thread_func_param;
  59. struct timespec now;
  60. double d;
  61. unsigned u;
  62. int re_schedule;
  63. struct ttimer t;
  64. #if defined(HAVE_CLOCK_NANOSLEEP) /* Linux with librt */
  65. /* TODO */
  66. while (
  67. clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &request, &request) ==
  68. EINTR) { /*nop*/
  69. ;
  70. }
  71. #else
  72. clock_gettime(CLOCK_MONOTONIC, &now);
  73. d = (double)now.tv_sec + (double)now.tv_nsec * 1.0E-9;
  74. while (ctx->stop_flag == 0) {
  75. pthread_mutex_lock(&ctx->timers->mutex);
  76. if (ctx->timers->timer_count > 0 && d >= ctx->timers->timers[0].time) {
  77. t = ctx->timers->timers[0];
  78. for (u = 1; u < ctx->timers->timer_count; u++) {
  79. ctx->timers->timers[u - 1] = ctx->timers->timers[u];
  80. }
  81. ctx->timers->timer_count--;
  82. pthread_mutex_unlock(&ctx->timers->mutex);
  83. re_schedule = t.action(t.arg);
  84. if (re_schedule && (t.period > 0)) {
  85. timer_add(ctx, t.time + t.period, t.period, 0, t.action, t.arg);
  86. }
  87. continue;
  88. } else {
  89. pthread_mutex_unlock(&ctx->timers->mutex);
  90. }
  91. mg_sleep(1);
  92. clock_gettime(CLOCK_MONOTONIC, &now);
  93. d = (double)now.tv_sec + (double)now.tv_nsec * 1.0E-9;
  94. }
  95. #endif
  96. }
  97. #ifdef _WIN32
  98. static unsigned __stdcall timer_thread(void *thread_func_param)
  99. {
  100. timer_thread_run(thread_func_param);
  101. return 0;
  102. }
  103. #else
  104. static void *timer_thread(void *thread_func_param)
  105. {
  106. timer_thread_run(thread_func_param);
  107. return NULL;
  108. }
  109. #endif /* _WIN32 */
  110. static int timers_init(struct mg_context *ctx)
  111. {
  112. ctx->timers = (struct ttimers *)mg_calloc(sizeof(struct ttimers), 1);
  113. (void)pthread_mutex_init(&ctx->timers->mutex, NULL);
  114. /* Start timer thread */
  115. mg_start_thread_with_id(timer_thread, ctx, &ctx->timers->threadid);
  116. return 0;
  117. }
  118. static void timers_exit(struct mg_context *ctx)
  119. {
  120. if (ctx->timers) {
  121. (void)pthread_mutex_destroy(&ctx->timers->mutex);
  122. mg_free(ctx->timers);
  123. }
  124. }