luat_timer_linux.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #define _POSIX_C_SOURCE 199309L
  2. #include "luat_base.h"
  3. #include "luat_malloc.h"
  4. #include "luat_timer.h"
  5. #include "luat_msgbus.h"
  6. #include <pthread.h>
  7. #define LUAT_LOG_TAG "timer"
  8. #include "luat_log.h"
  9. #include <time.h>
  10. #include <math.h>
  11. #include <unistd.h>
  12. #define TIMER_COUNT 128
  13. typedef struct sysp_timer {
  14. luat_timer_t* timer;
  15. uint32_t starttime;
  16. uint32_t nexttime;
  17. } sysp_timer_t;
  18. static sysp_timer_t timers[TIMER_COUNT] = {0};
  19. // 获取当前时间
  20. uint32_t get_timestamp(void) {
  21. struct timespec _t = {0};
  22. clock_gettime(CLOCK_REALTIME, &_t);
  23. uint32_t timenow = _t.tv_sec*1000 + lround(_t.tv_nsec/1e6);
  24. //printf("time now > %u\n", timenow);
  25. return timenow;
  26. }
  27. void luat_timer_check(void) {
  28. //LLOGD("timer callback");
  29. uint32_t timenow = get_timestamp();
  30. rtos_msg_t msg;
  31. for (size_t i = 0; i < TIMER_COUNT; i++)
  32. {
  33. if (timers[i].timer == NULL) continue;
  34. //LLOGD("%d %d\n", timers[i].nexttime, timenow);
  35. if (timers[i].nexttime < timenow) {
  36. luat_timer_t *timer = timers[i].timer;
  37. msg.handler = timer->func;
  38. msg.ptr = NULL;
  39. msg.arg1 = timer->id;
  40. msg.arg2 = 0;
  41. luat_msgbus_put(&msg, 0);
  42. if (timer->repeat) {
  43. timers[i].nexttime += timer->timeout;
  44. }
  45. else {
  46. timers[i].nexttime = 0;
  47. }
  48. }
  49. }
  50. //LLOGD("end of luat_timer_check\n");
  51. }
  52. static int nextTimerSlot() {
  53. for (size_t i = 0; i < TIMER_COUNT; i++)
  54. {
  55. if (timers[i].timer == NULL) {
  56. return i;
  57. }
  58. }
  59. return -1;
  60. }
  61. int luat_timer_start(luat_timer_t* timer) {
  62. // int os_timer;
  63. int timerIndex;
  64. //LLOGD(">>luat_timer_start timeout=%ld", timer->timeout);
  65. timerIndex = nextTimerSlot();
  66. //LLOGD("timer id=%ld", timerIndex);
  67. if (timerIndex < 0) {
  68. return 1; // too many timer!!
  69. }
  70. // os_timer = timerIndex;
  71. //LLOGD("timer id=%ld, osTimerNew=%p", timerIndex, os_timer);
  72. timers[timerIndex].timer = timer;
  73. timers[timerIndex].starttime = get_timestamp();
  74. timers[timerIndex].nexttime = timers[timerIndex].starttime + timer->timeout;
  75. //timer->os_timer = os_timer;
  76. return 0;
  77. }
  78. int luat_timer_stop(luat_timer_t* timer) {
  79. if (!timer)
  80. return 1;
  81. for (size_t i = 0; i < TIMER_COUNT; i++)
  82. {
  83. if (timers[i].timer == timer) {
  84. timers[i].timer = NULL;
  85. break;
  86. }
  87. }
  88. return 0;
  89. };
  90. luat_timer_t* luat_timer_get(size_t timer_id) {
  91. for (size_t i = 0; i < TIMER_COUNT; i++)
  92. {
  93. if (timers[i].timer && timers[i].timer->id == timer_id) {
  94. return timers[i].timer;
  95. }
  96. }
  97. return NULL;
  98. }
  99. int luat_timer_mdelay(size_t ms) {
  100. if (ms > 0)
  101. usleep(ms*1000);
  102. return 0;
  103. }
  104. void luat_timer_us_delay(size_t us) {
  105. if (us)
  106. usleep(us);
  107. }
  108. static pthread_mutex_t mp;
  109. static pthread_cond_t cv;
  110. void *timer_thread_start(void *args) {
  111. // printf("timer thread started\r\n");
  112. struct timespec to = {0};
  113. pthread_mutex_lock(&mp);
  114. to.tv_sec = 0;
  115. to.tv_nsec = 100;
  116. while (1) {
  117. pthread_cond_timedwait(&cv, &mp, &to);
  118. luat_timer_check();
  119. }
  120. return NULL;
  121. }