timer.inl 3.3 KB

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