timerfd.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-09-20 zmq810150896 first version
  9. */
  10. #include <rtthread.h>
  11. #include <dfs_file.h>
  12. #include <stdint.h>
  13. #include <poll.h>
  14. #include <sys/timerfd.h>
  15. #define INIT_PERIODIC 0
  16. #define OPEN_PERIODIC 1
  17. #define ENTER_PERIODIC 2
  18. #define SEC_TO_MSEC 1000
  19. #define MSEC_TO_NSEC 1000000
  20. #define SEC_TO_NSEC 1000000000
  21. #define TIME_INT32_MAX 0x7FFFFFFF
  22. #define TIMERFD_MUTEX_NAME "TIMERFD"
  23. #define TFD_SHARED_FCNTL_FLAGS (TFD_CLOEXEC | TFD_NONBLOCK)
  24. struct rt_timerfd
  25. {
  26. rt_wqueue_t timerfd_queue;
  27. struct itimerspec ittimer;
  28. rt_timer_t timer;
  29. struct rt_mutex lock;
  30. struct timespec pre_time;
  31. rt_atomic_t timeout_num;
  32. struct rt_wqueue_node wqn;
  33. rt_atomic_t ticks;
  34. int clockid;
  35. int isperiodic;
  36. int tick_out;
  37. };
  38. static int timerfd_close(struct dfs_file *file);
  39. static int timerfd_poll(struct dfs_file *file, struct rt_pollreq *req);
  40. #ifndef RT_USING_DFS_V2
  41. static ssize_t timerfd_read(struct dfs_file *file, void *buf, size_t count);
  42. #else
  43. static ssize_t timerfd_read(struct dfs_file *file, void *buf, size_t count, off_t *pos);
  44. #endif
  45. static const struct dfs_file_ops timerfd_fops =
  46. {
  47. .close = timerfd_close,
  48. .poll = timerfd_poll,
  49. .read = timerfd_read,
  50. };
  51. static int timerfd_close(struct dfs_file *file)
  52. {
  53. struct rt_timerfd *tfd;
  54. if (file->vnode->ref_count != 1)
  55. return 0;
  56. tfd = file->vnode->data;
  57. if (tfd)
  58. {
  59. if (tfd->timer != RT_NULL)
  60. {
  61. rt_timer_stop(tfd->timer);
  62. rt_timer_delete(tfd->timer);
  63. tfd->timer = RT_NULL;
  64. }
  65. if (tfd->wqn.wqueue)
  66. {
  67. rt_wqueue_remove(&tfd->wqn);
  68. }
  69. rt_mutex_detach(&tfd->lock);
  70. rt_free(tfd);
  71. }
  72. return 0;
  73. }
  74. static int timerfd_poll(struct dfs_file *file, struct rt_pollreq *req)
  75. {
  76. struct rt_timerfd *tfd;
  77. int events = 0;
  78. tfd = file->vnode->data;
  79. rt_mutex_take(&tfd->lock, RT_WAITING_FOREVER);
  80. rt_poll_add(&tfd->timerfd_queue, req);
  81. rt_mutex_release(&tfd->lock);
  82. if (rt_atomic_load(&(tfd->ticks)) > 0)
  83. {
  84. events |= POLLIN;
  85. }
  86. return events;
  87. }
  88. #ifndef RT_USING_DFS_V2
  89. static ssize_t timerfd_read(struct dfs_file *file, void *buf, size_t count)
  90. #else
  91. static ssize_t timerfd_read(struct dfs_file *file, void *buf, size_t count, off_t *pos)
  92. #endif
  93. {
  94. struct rt_timerfd *tfd;
  95. rt_uint64_t *buffer;
  96. int ret = 0;
  97. buffer = (rt_uint64_t *)buf;
  98. if (sizeof(buffer) > count)
  99. {
  100. rt_set_errno(EINVAL);
  101. return -1;
  102. }
  103. tfd = file->vnode->data;
  104. if (!tfd)
  105. {
  106. rt_set_errno(EINVAL);
  107. return -1;
  108. }
  109. if ((rt_atomic_load(&(tfd->ticks)) == 0) && (file->flags & O_NONBLOCK))
  110. {
  111. rt_set_errno(EAGAIN);
  112. return -EAGAIN;
  113. }
  114. else
  115. {
  116. if (rt_atomic_load(&(tfd->ticks)) == 0)
  117. {
  118. tfd->wqn.polling_thread = rt_thread_self();
  119. if (tfd->wqn.wqueue)
  120. {
  121. rt_wqueue_remove(&tfd->wqn);
  122. }
  123. rt_wqueue_add(&tfd->timerfd_queue, &tfd->wqn);
  124. ret = rt_thread_suspend_with_flag(tfd->wqn.polling_thread, RT_INTERRUPTIBLE);
  125. if (ret == RT_EOK)
  126. {
  127. rt_schedule();
  128. }
  129. else
  130. {
  131. return ret;
  132. }
  133. }
  134. (*buffer) = rt_atomic_load(&(tfd->timeout_num));
  135. rt_atomic_store(&(tfd->ticks), 0);
  136. }
  137. return sizeof(buffer);
  138. }
  139. static int timerfd_wqueue_callback(struct rt_wqueue_node *wait, void *key)
  140. {
  141. return 0;
  142. }
  143. static int timerfd_do_create(int clockid, int flags)
  144. {
  145. struct rt_timerfd *tfd = RT_NULL;
  146. struct dfs_file *df;
  147. rt_err_t ret = -1;
  148. int fd = -1;
  149. if ((flags & ~TFD_SHARED_FCNTL_FLAGS) ||
  150. (clockid != CLOCK_MONOTONIC &&
  151. clockid != CLOCK_REALTIME &&
  152. clockid != CLOCK_REALTIME_ALARM &&
  153. clockid != CLOCK_BOOTTIME &&
  154. clockid != CLOCK_BOOTTIME_ALARM))
  155. {
  156. rt_set_errno(EINVAL);
  157. return -1;
  158. }
  159. if ((clockid == CLOCK_REALTIME_ALARM ||
  160. clockid == CLOCK_BOOTTIME_ALARM))
  161. {
  162. rt_set_errno(EPERM);
  163. return -1;
  164. }
  165. fd = fd_new();
  166. if (fd < 0)
  167. {
  168. rt_set_errno(EINVAL);
  169. return -1;
  170. }
  171. ret = fd;
  172. df = fd_get(fd);
  173. if (df)
  174. {
  175. df->flags |= flags;
  176. tfd = (struct rt_timerfd *)rt_calloc(1, sizeof(struct rt_timerfd));
  177. if (tfd)
  178. {
  179. rt_mutex_init(&tfd->lock, TIMERFD_MUTEX_NAME, RT_IPC_FLAG_FIFO);
  180. rt_wqueue_init(&tfd->timerfd_queue);
  181. tfd->isperiodic = INIT_PERIODIC;
  182. tfd->ticks = 0;
  183. tfd->timeout_num = 0;
  184. tfd->tick_out = 0;
  185. tfd->clockid = clockid;
  186. tfd->timer = RT_NULL;
  187. tfd->pre_time.tv_sec = 0;
  188. tfd->pre_time.tv_nsec = 0;
  189. tfd->wqn.polling_thread = rt_thread_self();
  190. rt_list_init(&(tfd->wqn.list));
  191. tfd->wqn.wakeup = timerfd_wqueue_callback;
  192. df->vnode = (struct dfs_vnode *)rt_malloc(sizeof(struct dfs_vnode));
  193. if (df->vnode)
  194. {
  195. dfs_vnode_init(df->vnode, FT_REGULAR, &timerfd_fops);
  196. df->vnode->data = tfd;
  197. #ifdef RT_USING_DFS_V2
  198. df->fops = &timerfd_fops;
  199. #endif
  200. }
  201. else
  202. {
  203. rt_free(tfd);
  204. fd_release(fd);
  205. rt_set_errno(ENOMEM);
  206. ret = -1;
  207. }
  208. }
  209. else
  210. {
  211. fd_release(fd);
  212. rt_set_errno(ENOMEM);
  213. ret = -1;
  214. }
  215. }
  216. else
  217. {
  218. fd_release(fd);
  219. ret = -1;
  220. }
  221. return ret;
  222. }
  223. static int get_current_time(struct rt_timerfd *tfd, struct timespec *time)
  224. {
  225. int ret = 0;
  226. struct timespec *cur_time = RT_NULL;
  227. if (time == RT_NULL)
  228. {
  229. cur_time = &tfd->pre_time;
  230. }
  231. else
  232. {
  233. cur_time = time;
  234. }
  235. if (tfd->clockid >= 0)
  236. {
  237. ret = clock_gettime(tfd->clockid, cur_time);
  238. }
  239. else
  240. {
  241. ret = clock_gettime(CLOCK_MONOTONIC, cur_time);
  242. }
  243. return ret;
  244. }
  245. static void timerfd_timeout(void *parameter)
  246. {
  247. struct rt_timerfd *tfd = RT_NULL;
  248. tfd = (struct rt_timerfd *)parameter;
  249. if (tfd == RT_NULL)
  250. {
  251. return ;
  252. }
  253. rt_wqueue_wakeup(&tfd->timerfd_queue, (void *)POLLIN);
  254. rt_atomic_store(&(tfd->ticks), 1);
  255. rt_atomic_add(&(tfd->timeout_num), 1);
  256. rt_mutex_take(&tfd->lock, RT_WAITING_FOREVER);
  257. get_current_time(tfd, RT_NULL);
  258. if (tfd->isperiodic == OPEN_PERIODIC)
  259. {
  260. if (tfd->timer)
  261. {
  262. rt_timer_stop(tfd->timer);
  263. rt_timer_delete(tfd->timer);
  264. tfd->timer = RT_NULL;
  265. }
  266. tfd->isperiodic = ENTER_PERIODIC;
  267. tfd->timer = rt_timer_create(TIMERFD_MUTEX_NAME, timerfd_timeout,
  268. tfd, tfd->tick_out,
  269. RT_TIMER_FLAG_PERIODIC | RT_TIMER_FLAG_SOFT_TIMER);
  270. RT_ASSERT(tfd->timer);
  271. rt_timer_start(tfd->timer);
  272. }
  273. rt_mutex_release(&tfd->lock);
  274. }
  275. static void timerfd_time_operation(time_t *sec, long *nsec)
  276. {
  277. if (*nsec < 0)
  278. {
  279. if (*sec > 0)
  280. {
  281. *sec -= 1;
  282. *nsec = 1 * SEC_TO_NSEC + *nsec;
  283. }
  284. }
  285. if (*sec < 0 || *nsec < 0)
  286. {
  287. *sec = 0;
  288. *nsec = 0;
  289. }
  290. }
  291. static int timerfd_do_settime(int fd, int flags, const struct itimerspec *new, struct itimerspec *old)
  292. {
  293. int ret = 0;
  294. struct rt_timerfd *tfd;
  295. struct dfs_file *df;
  296. struct timespec current_time;
  297. int tick_out;
  298. rt_int64_t value_msec;
  299. rt_int64_t interval_msec;
  300. rt_int64_t cur_time = 0;
  301. if (fd < 0)
  302. {
  303. rt_set_errno(EINVAL);
  304. return -EINVAL;
  305. }
  306. df = fd_get(fd);
  307. if (!df)
  308. return -EINVAL;
  309. tfd = df->vnode->data;
  310. rt_atomic_store(&(tfd->ticks), 0);
  311. rt_atomic_store(&(tfd->timeout_num), 0);
  312. rt_mutex_take(&tfd->lock, RT_WAITING_FOREVER);
  313. tfd->isperiodic = INIT_PERIODIC;
  314. if (old)
  315. {
  316. old->it_interval.tv_nsec = tfd->ittimer.it_interval.tv_nsec;
  317. old->it_interval.tv_sec = tfd->ittimer.it_interval.tv_sec;
  318. old->it_value.tv_nsec = tfd->ittimer.it_value.tv_nsec;
  319. old->it_value.tv_sec = tfd->ittimer.it_value.tv_sec;
  320. }
  321. if (new)
  322. {
  323. if (tfd->timer != RT_NULL)
  324. {
  325. rt_timer_stop(tfd->timer);
  326. rt_timer_delete(tfd->timer);
  327. tfd->timer = RT_NULL;
  328. }
  329. if (new->it_value.tv_nsec == 0 && new->it_value.tv_sec == 0)
  330. {
  331. rt_mutex_release(&tfd->lock);
  332. return 0;
  333. }
  334. value_msec = (new->it_value.tv_nsec / MSEC_TO_NSEC) + (new->it_value.tv_sec * SEC_TO_MSEC);
  335. interval_msec = (new->it_interval.tv_nsec / MSEC_TO_NSEC) + (new->it_interval.tv_sec * SEC_TO_MSEC);
  336. current_time.tv_nsec = 0;
  337. current_time.tv_sec = 0;
  338. if (flags == TFD_TIMER_ABSTIME)
  339. {
  340. ret = get_current_time(tfd, &current_time);
  341. if (ret < 0)
  342. {
  343. rt_mutex_release(&tfd->lock);
  344. return ret;
  345. }
  346. cur_time = current_time.tv_sec * SEC_TO_MSEC + (current_time.tv_nsec / MSEC_TO_NSEC);
  347. value_msec = value_msec - cur_time;
  348. }
  349. tfd->ittimer.it_interval.tv_nsec = new->it_interval.tv_nsec;
  350. tfd->ittimer.it_interval.tv_sec = new->it_interval.tv_sec;
  351. tfd->ittimer.it_value.tv_sec = new->it_value.tv_sec - current_time.tv_sec;
  352. tfd->ittimer.it_value.tv_nsec = new->it_value.tv_nsec - current_time.tv_nsec;
  353. timerfd_time_operation(&tfd->ittimer.it_value.tv_sec, &tfd->ittimer.it_value.tv_nsec);
  354. if ((interval_msec > 0) && (interval_msec <= TIME_INT32_MAX))
  355. {
  356. tfd->tick_out = rt_tick_from_millisecond(interval_msec);
  357. if (tfd->tick_out < 0)
  358. {
  359. rt_mutex_release(&tfd->lock);
  360. return -EINVAL;
  361. }
  362. tfd->isperiodic = OPEN_PERIODIC;
  363. }
  364. get_current_time(tfd, RT_NULL);
  365. if (value_msec > 0)
  366. {
  367. if (value_msec > TIME_INT32_MAX)
  368. {
  369. rt_mutex_release(&tfd->lock);
  370. return -EINVAL;
  371. }
  372. tick_out = rt_tick_from_millisecond(value_msec);
  373. if (tick_out < 0)
  374. {
  375. rt_mutex_release(&tfd->lock);
  376. return -EINVAL;
  377. }
  378. tfd->timer = rt_timer_create(TIMERFD_MUTEX_NAME, timerfd_timeout,
  379. tfd, tick_out,
  380. RT_TIMER_FLAG_ONE_SHOT | RT_TIMER_FLAG_SOFT_TIMER);
  381. RT_ASSERT(tfd->timer);
  382. rt_timer_start(tfd->timer);
  383. }
  384. else
  385. {
  386. timerfd_timeout(tfd);
  387. }
  388. }
  389. else
  390. {
  391. rt_set_errno(EINVAL);
  392. ret = -1;
  393. }
  394. rt_mutex_release(&tfd->lock);
  395. return ret;
  396. }
  397. static int timerfd_do_gettime(int fd, struct itimerspec *cur)
  398. {
  399. struct rt_timerfd *tfd;
  400. struct dfs_file *df = RT_NULL;
  401. struct timespec cur_time;
  402. rt_int64_t tv_sec = 0;
  403. rt_int64_t tv_nsec = 0;
  404. df = fd_get(fd);
  405. if (df == RT_NULL)
  406. {
  407. rt_set_errno(EINVAL);
  408. return -1;
  409. }
  410. tfd = df->vnode->data;
  411. get_current_time(tfd, &cur_time);
  412. rt_mutex_take(&tfd->lock, RT_WAITING_FOREVER);
  413. tv_sec = cur_time.tv_sec - tfd->pre_time.tv_sec;
  414. tv_nsec = cur_time.tv_nsec - tfd->pre_time.tv_nsec;
  415. timerfd_time_operation(&tv_sec, &tv_nsec);
  416. cur->it_interval.tv_nsec = tfd->ittimer.it_interval.tv_nsec;
  417. cur->it_interval.tv_sec = tfd->ittimer.it_interval.tv_sec;
  418. if (tfd->isperiodic == ENTER_PERIODIC)
  419. {
  420. cur->it_value.tv_nsec = tfd->ittimer.it_interval.tv_nsec - tv_nsec;
  421. cur->it_value.tv_sec = tfd->ittimer.it_interval.tv_sec - tv_sec;
  422. timerfd_time_operation(&cur->it_value.tv_sec, &cur->it_value.tv_nsec);
  423. }
  424. else
  425. {
  426. if (rt_atomic_load(&(tfd->timeout_num)) == 1)
  427. {
  428. cur->it_value.tv_nsec = 0;
  429. cur->it_value.tv_sec = 0;
  430. }
  431. else
  432. {
  433. cur->it_value.tv_nsec = tfd->ittimer.it_value.tv_nsec - tv_nsec;
  434. cur->it_value.tv_sec = tfd->ittimer.it_value.tv_sec - tv_sec;
  435. timerfd_time_operation(&cur->it_value.tv_sec, &cur->it_value.tv_nsec);
  436. }
  437. }
  438. rt_mutex_release(&tfd->lock);
  439. return 0;
  440. }
  441. int timerfd_create(int clockid, int flags)
  442. {
  443. return timerfd_do_create(clockid, flags);
  444. }
  445. int timerfd_settime(int fd, int flags, const struct itimerspec *new, struct itimerspec *old)
  446. {
  447. return timerfd_do_settime(fd, flags, new, old);
  448. }
  449. int timerfd_gettime(int fd, struct itimerspec *cur)
  450. {
  451. return timerfd_do_gettime(fd, cur);
  452. }