timer.inl 3.3 KB

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