aos_posix_timer.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*
  2. * Copyright (C) 2015-2017 Alibaba Group Holding Limited
  3. */
  4. #include <stdio.h>
  5. #include <stdint.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <string.h>
  9. #include <assert.h>
  10. #include <time.h>
  11. #include <errno.h>
  12. #include <signal.h>
  13. #define _GNU_SOURCE
  14. #include <pthread.h>
  15. #include <semaphore.h>
  16. #include "aos/list.h"
  17. #include <aos/kernel.h>
  18. #include <ulog/ulog.h>
  19. #define TAG "TIMER"
  20. #define CLOCKID CLOCK_MONOTONIC
  21. // #include <aos/aos.h>
  22. static timer_t s_timerid;
  23. static dlist_t s_aos_timer_listhead;
  24. static pthread_mutex_t s_list_mutex;
  25. static sem_t s_timer_sem;
  26. typedef void (*timer_cb_t)(void *timer, void *args);
  27. typedef struct timer_s {
  28. timer_cb_t handler;
  29. void *args;
  30. uint64_t timeout;
  31. uint64_t start_ms;
  32. int repeat;
  33. } p_timer_t;
  34. typedef struct timer_list {
  35. dlist_t node;
  36. p_timer_t *timer;
  37. } timer_list_t ;
  38. static void timer_callback(void *ptr) {
  39. sem_post(&s_timer_sem);
  40. }
  41. static uint64_t now(void)
  42. {
  43. struct timespec ts;
  44. if (clock_gettime(CLOCK_BOOTTIME, &ts) == -1) {
  45. LOGE(TAG, "%s unable to get current time: %s",
  46. __func__, strerror(errno));
  47. return 0;
  48. }
  49. return (ts.tv_sec * 1000LL) + (ts.tv_nsec / 1000000LL);
  50. }
  51. // static void start_timer(uint64_t timerout_ms)
  52. // {
  53. // struct itimerspec wakeup_time;
  54. // memset(&wakeup_time, 0, sizeof(wakeup_time));
  55. // // LOGD(TAG, "%s timer start %d\n", __func__, timerout_ms);
  56. // wakeup_time.it_value.tv_sec = (timerout_ms / 1000);
  57. // wakeup_time.it_value.tv_nsec = (timerout_ms % 1000) * 1000000LL;
  58. // if (timer_settime(s_timerid, TIMER_ABSTIME, &wakeup_time, NULL) == -1)
  59. // LOGE(TAG, "%s unable to set timer: %s",
  60. // __func__, strerror(errno));
  61. // }
  62. static void restart_timer(uint64_t timerout_ms)
  63. {
  64. struct itimerspec wakeup_time;
  65. memset(&wakeup_time, 0, sizeof(wakeup_time));
  66. // LOGD(TAG, "%s timeout %d\n", __func__, timerout_ms);
  67. /* stop the timer */
  68. timer_settime(s_timerid, TIMER_ABSTIME, &wakeup_time, NULL);
  69. wakeup_time.it_value.tv_sec = (timerout_ms / 1000);
  70. wakeup_time.it_value.tv_nsec = (timerout_ms % 1000) * 1000000LL;
  71. if (timer_settime(s_timerid, TIMER_ABSTIME, &wakeup_time, NULL) == -1)
  72. LOGE(TAG, "%s unable to set timer: %s",
  73. __func__, strerror(errno));
  74. }
  75. static void add_node_to_list(p_timer_t *timer)
  76. {
  77. timer_list_t* node = malloc(sizeof(timer_list_t));
  78. timer_list_t* tmp_node;
  79. if (node == NULL) {
  80. LOGE(TAG, "OOM\n");
  81. return;
  82. }
  83. timer->start_ms = now() + timer->timeout;
  84. node->timer = timer;
  85. if (!dlist_empty(&s_aos_timer_listhead)) {
  86. dlist_for_each_entry(&s_aos_timer_listhead, tmp_node, timer_list_t, node) {
  87. if (tmp_node->timer->start_ms > timer->start_ms) {
  88. break;
  89. }
  90. }
  91. dlist_add_tail(&node->node, &tmp_node->node);
  92. } else {
  93. dlist_add_tail(&node->node, &s_aos_timer_listhead);
  94. }
  95. }
  96. static int is_in_list(p_timer_t *timer)
  97. {
  98. timer_list_t* tmp_node;
  99. dlist_for_each_entry(&s_aos_timer_listhead, tmp_node, timer_list_t, node) {
  100. if (tmp_node->timer == timer) {
  101. return 1;
  102. }
  103. }
  104. return 0;
  105. }
  106. static void* timer_dispatch(void *args)
  107. {
  108. while(1) {
  109. sem_wait(&s_timer_sem);
  110. // LOGD(TAG, "%s entry 1\n", __func__);
  111. if (dlist_empty(&s_aos_timer_listhead)) {
  112. continue;
  113. }
  114. timer_list_t* tmp_node;
  115. // LOGD(TAG, "%s entry 2\n", __func__);
  116. pthread_mutex_lock(&s_list_mutex);
  117. while (1) {
  118. int del = 0;
  119. p_timer_t *tmp_timer;
  120. uint64_t timeout_ms = now();
  121. dlist_for_each_entry(&s_aos_timer_listhead, tmp_node, timer_list_t, node) {
  122. if (tmp_node->timer->start_ms <= timeout_ms) {
  123. tmp_timer = tmp_node->timer;
  124. tmp_node->timer->handler(tmp_node->timer, tmp_node->timer->args);
  125. // LOGD(TAG, "%s handler\n", __func__);
  126. del = 1;
  127. break;
  128. }
  129. }
  130. if (del == 0) {
  131. break;
  132. } else {
  133. /* check the list for avoid call aos_timer_stop in timer->handle */
  134. if (is_in_list(tmp_timer) && tmp_timer->start_ms <= timeout_ms) {
  135. dlist_del(&tmp_node->node);
  136. if (tmp_timer->repeat == 1) {
  137. add_node_to_list(tmp_timer);
  138. }
  139. free(tmp_node);
  140. }
  141. }
  142. }
  143. if (!dlist_empty(&s_aos_timer_listhead)) {
  144. tmp_node = dlist_first_entry(&s_aos_timer_listhead, timer_list_t, node);
  145. // LOGD(TAG, "%s timer start %p\n", __func__, tmp_node->timer);
  146. restart_timer(tmp_node->timer->start_ms);
  147. }
  148. pthread_mutex_unlock(&s_list_mutex);
  149. }
  150. return NULL;
  151. }
  152. static void lazy_init()
  153. {
  154. static int init = 0;
  155. if (init == 0) {
  156. struct sigevent sigevent;
  157. memset(&sigevent, 0, sizeof(sigevent));
  158. sigevent.sigev_notify = SIGEV_THREAD;
  159. sigevent.sigev_notify_function = (void (*)(union sigval))timer_callback;
  160. if (timer_create(CLOCKID, &sigevent, &s_timerid) == -1) {
  161. LOGE(TAG, "%s unable to create timer with clock %d: %s\n", __func__, CLOCKID, strerror(errno));
  162. assert(NULL);
  163. }
  164. dlist_init(&s_aos_timer_listhead);
  165. pthread_mutexattr_t attr;
  166. pthread_mutexattr_init(&attr);
  167. pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK_NP);
  168. pthread_mutex_init(&s_list_mutex, &attr);
  169. sem_init(&s_timer_sem, 0, 0);
  170. pthread_t tid = 0;
  171. pthread_create(&tid, NULL, timer_dispatch, NULL);
  172. pthread_setname_np(tid, "timer_task");
  173. }
  174. init = 1;
  175. }
  176. int aos_timer_new(aos_timer_t *timer, void (*fn)(void *, void *),
  177. void *arg, int ms, int repeat)
  178. {
  179. lazy_init();
  180. p_timer_t *t = malloc(sizeof(p_timer_t));
  181. if (t == NULL) {
  182. LOGE(TAG, "%s OOM", __func__);
  183. return -1;
  184. }
  185. t->handler = fn;
  186. t->args = arg;
  187. t->start_ms = 0;
  188. t->repeat = repeat;
  189. t->timeout = ms;
  190. timer->hdl = t;
  191. return 0;
  192. }
  193. int aos_timer_new_ext(aos_timer_t *timer, void (*fn)(void *, void *),
  194. void *arg, int ms, int repeat, unsigned char auto_run)
  195. {
  196. aos_timer_new(timer, fn, arg, ms, repeat);
  197. if (auto_run == 1) {
  198. return aos_timer_start(timer);
  199. } else {
  200. return 0;
  201. }
  202. }
  203. int aos_timer_start(aos_timer_t *timer)
  204. {
  205. timer_list_t* tmp_node;
  206. assert(timer);
  207. p_timer_t *t = timer->hdl;
  208. // LOGD(TAG, "%s timer %p,timeout %u\n", __func__, timer, timeout);
  209. t->start_ms = now() + t->timeout;
  210. timer_list_t* node = malloc(sizeof(timer_list_t));
  211. if (node == NULL) {
  212. LOGE(TAG, "OOM\n");
  213. return -1;
  214. }
  215. node->timer = t;
  216. pthread_mutex_lock(&s_list_mutex);
  217. /* rm the timer if the timer is in the list */
  218. dlist_for_each_entry(&s_aos_timer_listhead, tmp_node, timer_list_t, node) {
  219. if (tmp_node->timer == t) {
  220. dlist_del(&tmp_node->node);
  221. free(tmp_node);
  222. break;
  223. }
  224. }
  225. if (!dlist_empty(&s_aos_timer_listhead)) {
  226. int first = 0;
  227. dlist_for_each_entry(&s_aos_timer_listhead, tmp_node, timer_list_t, node) {
  228. if (tmp_node->timer->start_ms > t->start_ms) {
  229. first ++;
  230. break;
  231. }
  232. }
  233. dlist_add_tail(&node->node, &tmp_node->node);
  234. /* if the timer is earliest, restart posix timer */
  235. if (first == 1) {
  236. restart_timer(t->start_ms);
  237. }
  238. } else {
  239. dlist_add_tail(&node->node, &s_aos_timer_listhead);
  240. restart_timer(node->timer->start_ms);
  241. }
  242. pthread_mutex_unlock(&s_list_mutex);
  243. return 0;
  244. }
  245. int aos_timer_stop(aos_timer_t *timer)
  246. {
  247. timer_list_t* tmp_node;
  248. assert(timer);
  249. p_timer_t *t = timer->hdl;
  250. pthread_mutex_lock(&s_list_mutex);
  251. /* rm the timer if the timer is in the list */
  252. dlist_for_each_entry(&s_aos_timer_listhead, tmp_node, timer_list_t, node) {
  253. if (tmp_node->timer == t) {
  254. dlist_del(&tmp_node->node);
  255. free(tmp_node);
  256. break;
  257. }
  258. }
  259. pthread_mutex_unlock(&s_list_mutex);
  260. return 0;
  261. }
  262. void aos_timer_free(aos_timer_t *timer)
  263. {
  264. free(timer->hdl);
  265. }
  266. int aos_timer_change(aos_timer_t *timer, int ms)
  267. {
  268. assert(timer);
  269. p_timer_t *t = timer->hdl;
  270. pthread_mutex_lock(&s_list_mutex);
  271. t->timeout = ms;
  272. pthread_mutex_unlock(&s_list_mutex);
  273. return aos_timer_start(timer);
  274. }
  275. int aos_timer_change_once(aos_timer_t *timer, int ms)
  276. {
  277. assert(timer);
  278. p_timer_t *t = timer->hdl;
  279. pthread_mutex_lock(&s_list_mutex);
  280. t->repeat = 0;
  281. t->timeout = ms;
  282. pthread_mutex_unlock(&s_list_mutex);
  283. return aos_timer_start(timer);
  284. }
  285. int aos_timer_is_valid(aos_timer_t *timer)
  286. {
  287. p_timer_t *t;
  288. if (timer == NULL) {
  289. return 0;
  290. }
  291. t = timer->hdl;
  292. if (t == NULL) {
  293. return 0;
  294. }
  295. if (t->handler == NULL) {
  296. return 0;
  297. }
  298. return 1;
  299. }