timer.inl 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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, int is_relative, taction action, void * arg)
  18. {
  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. {
  53. struct mg_context *ctx = (struct mg_context *) thread_func_param;
  54. struct timespec now;
  55. double d;
  56. unsigned u;
  57. int re_schedule;
  58. struct ttimer t;
  59. #if defined(HAVE_CLOCK_NANOSLEEP) /* Linux with librt */
  60. /* TODO */
  61. while (clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &request, &request)==EINTR) {/*nop*/;}
  62. #else
  63. clock_gettime(CLOCK_MONOTONIC, &now);
  64. d = (double)now.tv_sec + (double)now.tv_nsec * 1.0E-9;
  65. while (ctx->stop_flag == 0) {
  66. pthread_mutex_lock(&ctx->timers->mutex);
  67. if (ctx->timers->timer_count > 0 && d >= ctx->timers->timers[0].time) {
  68. t = ctx->timers->timers[0];
  69. for (u=1; u<ctx->timers->timer_count; u++) {
  70. ctx->timers->timers[u-1] = ctx->timers->timers[u];
  71. }
  72. ctx->timers->timer_count--;
  73. pthread_mutex_unlock(&ctx->timers->mutex);
  74. re_schedule = t.action(t.arg);
  75. if (re_schedule && (t.period>0)) {
  76. timer_add(ctx, t.time+t.period, t.period, 0, t.action, t.arg);
  77. }
  78. continue;
  79. } else {
  80. pthread_mutex_unlock(&ctx->timers->mutex);
  81. }
  82. mg_sleep(1);
  83. clock_gettime(CLOCK_MONOTONIC, &now);
  84. d = (double)now.tv_sec + (double)now.tv_nsec * 1.0E-9;
  85. }
  86. #endif
  87. }
  88. #ifdef _WIN32
  89. static unsigned __stdcall timer_thread(void *thread_func_param)
  90. {
  91. timer_thread_run(thread_func_param);
  92. return 0;
  93. }
  94. #else
  95. static void *timer_thread(void *thread_func_param)
  96. {
  97. timer_thread_run(thread_func_param);
  98. return NULL;
  99. }
  100. #endif /* _WIN32 */
  101. static int timers_init(struct mg_context * ctx)
  102. {
  103. ctx->timers = (struct ttimers*) mg_calloc(sizeof(struct ttimers), 1);
  104. (void) pthread_mutex_init(&ctx->timers->mutex, NULL);
  105. /* Start timer thread */
  106. mg_start_thread_with_id(timer_thread, ctx, &ctx->timers->threadid);
  107. return 0;
  108. }
  109. static void timers_exit(struct mg_context * ctx)
  110. {
  111. if (ctx->timers) {
  112. (void) pthread_mutex_destroy(&ctx->timers->mutex);
  113. mg_free(ctx->timers);
  114. }
  115. }