timer.inl 3.6 KB

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