timerfd.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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. rt_wqueue_remove(&tfd->wqn);
  66. rt_mutex_detach(&tfd->lock);
  67. rt_free(tfd);
  68. }
  69. return 0;
  70. }
  71. static int timerfd_poll(struct dfs_file *file, struct rt_pollreq *req)
  72. {
  73. struct rt_timerfd *tfd;
  74. int events = 0;
  75. tfd = file->vnode->data;
  76. rt_mutex_take(&tfd->lock, RT_WAITING_FOREVER);
  77. rt_poll_add(&tfd->timerfd_queue, req);
  78. rt_mutex_release(&tfd->lock);
  79. if (rt_atomic_load(&(tfd->ticks)) > 0)
  80. events |= POLLIN;
  81. return events;
  82. }
  83. #ifndef RT_USING_DFS_V2
  84. static ssize_t timerfd_read(struct dfs_file *file, void *buf, size_t count)
  85. #else
  86. static ssize_t timerfd_read(struct dfs_file *file, void *buf, size_t count, off_t *pos)
  87. #endif
  88. {
  89. struct rt_timerfd *tfd;
  90. rt_uint64_t *buffer;
  91. int ret = 0;
  92. buffer = (rt_uint64_t *)buf;
  93. if (sizeof(buffer) > count)
  94. {
  95. rt_set_errno(EINVAL);
  96. return -1;
  97. }
  98. tfd = file->vnode->data;
  99. if (!tfd)
  100. {
  101. rt_set_errno(EINVAL);
  102. return -1;
  103. }
  104. if ((rt_atomic_load(&(tfd->ticks)) == 0) && (file->flags & O_NONBLOCK))
  105. {
  106. rt_set_errno(EAGAIN);
  107. return -EAGAIN;
  108. }
  109. else
  110. {
  111. if (rt_atomic_load(&(tfd->ticks)) == 0)
  112. {
  113. tfd->wqn.polling_thread = rt_thread_self();
  114. rt_wqueue_remove(&tfd->wqn);
  115. rt_wqueue_add(&tfd->timerfd_queue, &tfd->wqn);
  116. ret = rt_thread_suspend_with_flag(tfd->wqn.polling_thread, RT_INTERRUPTIBLE);
  117. if (ret == RT_EOK)
  118. {
  119. rt_schedule();
  120. }
  121. else
  122. {
  123. return ret;
  124. }
  125. }
  126. (*buffer) = rt_atomic_load(&(tfd->timeout_num));
  127. rt_atomic_store(&(tfd->ticks), 0);
  128. }
  129. return sizeof(buffer);
  130. }
  131. static int timerfd_wqueue_callback(struct rt_wqueue_node *wait, void *key)
  132. {
  133. return 0;
  134. }
  135. static int timerfd_do_create(int clockid, int flags)
  136. {
  137. struct rt_timerfd *tfd = RT_NULL;
  138. struct dfs_file *df;
  139. rt_err_t ret = -1;
  140. int fd = -1;
  141. if ((flags & ~TFD_SHARED_FCNTL_FLAGS) ||
  142. (clockid != CLOCK_MONOTONIC &&
  143. clockid != CLOCK_REALTIME &&
  144. clockid != CLOCK_REALTIME_ALARM &&
  145. clockid != CLOCK_BOOTTIME &&
  146. clockid != CLOCK_BOOTTIME_ALARM))
  147. {
  148. rt_set_errno(EINVAL);
  149. return -1;
  150. }
  151. if ((clockid == CLOCK_REALTIME_ALARM ||
  152. clockid == CLOCK_BOOTTIME_ALARM))
  153. {
  154. rt_set_errno(EPERM);
  155. return -1;
  156. }
  157. fd = fd_new();
  158. if (fd < 0)
  159. {
  160. rt_set_errno(EINVAL);
  161. return -1;
  162. }
  163. ret = fd;
  164. df = fd_get(fd);
  165. if (df)
  166. {
  167. df->flags |= flags;
  168. tfd = (struct rt_timerfd *)rt_malloc(sizeof(struct rt_timerfd));
  169. if (tfd)
  170. {
  171. rt_mutex_init(&tfd->lock, TIMERFD_MUTEX_NAME, RT_IPC_FLAG_FIFO);
  172. rt_wqueue_init(&tfd->timerfd_queue);
  173. tfd->isperiodic = INIT_PERIODIC;
  174. tfd->ticks = 0;
  175. tfd->timeout_num = 0;
  176. tfd->tick_out = 0;
  177. tfd->clockid = clockid;
  178. tfd->timer = RT_NULL;
  179. tfd->pre_time.tv_sec = 0;
  180. tfd->pre_time.tv_nsec = 0;
  181. tfd->wqn.polling_thread = rt_thread_self();
  182. rt_list_init(&(tfd->wqn.list));
  183. tfd->wqn.wakeup = timerfd_wqueue_callback;
  184. df->vnode = (struct dfs_vnode *)rt_malloc(sizeof(struct dfs_vnode));
  185. if (df->vnode)
  186. {
  187. dfs_vnode_init(df->vnode, FT_REGULAR, &timerfd_fops);
  188. df->vnode->data = tfd;
  189. #ifdef RT_USING_DFS_V2
  190. df->fops = &timerfd_fops;
  191. #endif
  192. }
  193. else
  194. {
  195. rt_free(tfd);
  196. fd_release(fd);
  197. rt_set_errno(ENOMEM);
  198. ret = -1;
  199. }
  200. }
  201. else
  202. {
  203. fd_release(fd);
  204. rt_set_errno(ENOMEM);
  205. ret = -1;
  206. }
  207. }
  208. else
  209. {
  210. fd_release(fd);
  211. ret = -1;
  212. }
  213. return ret;
  214. }
  215. static int get_current_time(struct rt_timerfd *tfd, struct timespec *time)
  216. {
  217. int ret = 0;
  218. struct timespec *cur_time = RT_NULL;
  219. if (time == RT_NULL)
  220. {
  221. cur_time = &tfd->pre_time;
  222. }
  223. else
  224. {
  225. cur_time = time;
  226. }
  227. if (tfd->clockid >= 0)
  228. {
  229. ret = clock_gettime(tfd->clockid, cur_time);
  230. }
  231. else
  232. {
  233. ret = clock_gettime(CLOCK_MONOTONIC, cur_time);
  234. }
  235. return ret;
  236. }
  237. static void timerfd_timeout(void *parameter)
  238. {
  239. struct rt_timerfd *tfd = RT_NULL;
  240. tfd = (struct rt_timerfd *)parameter;
  241. if (tfd == RT_NULL)
  242. {
  243. return ;
  244. }
  245. rt_wqueue_wakeup(&tfd->timerfd_queue, (void *)POLLIN);
  246. rt_atomic_store(&(tfd->ticks), 1);
  247. rt_atomic_add(&(tfd->timeout_num), 1);
  248. rt_mutex_take(&tfd->lock, RT_WAITING_FOREVER);
  249. get_current_time(tfd, RT_NULL);
  250. if (tfd->isperiodic == OPEN_PERIODIC)
  251. {
  252. if (tfd->timer)
  253. {
  254. rt_timer_stop(tfd->timer);
  255. rt_timer_delete(tfd->timer);
  256. tfd->timer = RT_NULL;
  257. }
  258. tfd->isperiodic = ENTER_PERIODIC;
  259. tfd->timer = rt_timer_create(TIMERFD_MUTEX_NAME, timerfd_timeout,
  260. tfd, tfd->tick_out,
  261. RT_TIMER_FLAG_PERIODIC | RT_TIMER_FLAG_SOFT_TIMER);
  262. rt_timer_start(tfd->timer);
  263. }
  264. rt_mutex_release(&tfd->lock);
  265. }
  266. static void timerfd_time_operation(time_t *sec, long *nsec)
  267. {
  268. if (*nsec < 0)
  269. {
  270. if (*sec > 0)
  271. {
  272. *sec -= 1;
  273. *nsec = 1 * SEC_TO_NSEC + *nsec;
  274. }
  275. }
  276. if (*sec < 0 || *nsec < 0)
  277. {
  278. *sec = 0;
  279. *nsec = 0;
  280. }
  281. }
  282. static int timerfd_do_settime(int fd, int flags, const struct itimerspec *new, struct itimerspec *old)
  283. {
  284. int ret = 0;
  285. struct rt_timerfd *tfd;
  286. struct dfs_file *df;
  287. struct timespec current_time;
  288. int tick_out;
  289. rt_int64_t value_msec;
  290. rt_int64_t interval_msec;
  291. rt_int64_t cur_time = 0;
  292. if (fd < 0)
  293. {
  294. rt_set_errno(EINVAL);
  295. return -EINVAL;
  296. }
  297. df = fd_get(fd);
  298. if (!df)
  299. return -EINVAL;
  300. tfd = df->vnode->data;
  301. rt_atomic_store(&(tfd->ticks), 0);
  302. rt_atomic_store(&(tfd->timeout_num), 0);
  303. rt_mutex_take(&tfd->lock, RT_WAITING_FOREVER);
  304. tfd->isperiodic = INIT_PERIODIC;
  305. if (old)
  306. {
  307. old->it_interval.tv_nsec = tfd->ittimer.it_interval.tv_nsec;
  308. old->it_interval.tv_sec = tfd->ittimer.it_interval.tv_sec;
  309. old->it_value.tv_nsec = tfd->ittimer.it_value.tv_nsec;
  310. old->it_value.tv_sec = tfd->ittimer.it_value.tv_sec;
  311. }
  312. if (new)
  313. {
  314. if (tfd->timer != RT_NULL)
  315. {
  316. rt_timer_stop(tfd->timer);
  317. rt_timer_delete(tfd->timer);
  318. tfd->timer = RT_NULL;
  319. }
  320. if (new->it_value.tv_nsec == 0 && new->it_value.tv_sec == 0)
  321. {
  322. return 0;
  323. }
  324. value_msec = (new->it_value.tv_nsec / MSEC_TO_NSEC) + (new->it_value.tv_sec * SEC_TO_MSEC);
  325. interval_msec = (new->it_interval.tv_nsec / MSEC_TO_NSEC) + (new->it_interval.tv_sec * SEC_TO_MSEC);
  326. current_time.tv_nsec = 0;
  327. current_time.tv_sec = 0;
  328. if (flags == TFD_TIMER_ABSTIME)
  329. {
  330. ret = get_current_time(tfd, &current_time);
  331. if (ret < 0)
  332. return ret;
  333. cur_time = current_time.tv_sec * SEC_TO_MSEC + (current_time.tv_nsec / MSEC_TO_NSEC);
  334. value_msec = value_msec - cur_time;
  335. }
  336. tfd->ittimer.it_interval.tv_nsec = new->it_interval.tv_nsec;
  337. tfd->ittimer.it_interval.tv_sec = new->it_interval.tv_sec;
  338. tfd->ittimer.it_value.tv_sec = new->it_value.tv_sec - current_time.tv_sec;
  339. tfd->ittimer.it_value.tv_nsec = new->it_value.tv_nsec - current_time.tv_nsec;
  340. timerfd_time_operation(&tfd->ittimer.it_value.tv_sec, &tfd->ittimer.it_value.tv_nsec);
  341. if ((interval_msec > 0) && (interval_msec <= TIME_INT32_MAX))
  342. {
  343. tfd->tick_out = rt_tick_from_millisecond(interval_msec);
  344. if (tfd->tick_out < 0)
  345. return -EINVAL;
  346. tfd->isperiodic = OPEN_PERIODIC;
  347. }
  348. get_current_time(tfd, RT_NULL);
  349. if (value_msec > 0)
  350. {
  351. if (value_msec > TIME_INT32_MAX)
  352. return -EINVAL;
  353. tick_out = rt_tick_from_millisecond(value_msec);
  354. if (tick_out < 0)
  355. return -EINVAL;
  356. tfd->timer = rt_timer_create(TIMERFD_MUTEX_NAME, timerfd_timeout,
  357. tfd, tick_out,
  358. RT_TIMER_FLAG_ONE_SHOT | RT_TIMER_FLAG_SOFT_TIMER);
  359. rt_timer_start(tfd->timer);
  360. }
  361. else
  362. {
  363. timerfd_timeout(tfd);
  364. }
  365. }
  366. else
  367. {
  368. rt_set_errno(EINVAL);
  369. ret = -1;
  370. }
  371. rt_mutex_release(&tfd->lock);
  372. return ret;
  373. }
  374. static int timerfd_do_gettime(int fd, struct itimerspec *cur)
  375. {
  376. struct rt_timerfd *tfd;
  377. struct dfs_file *df = RT_NULL;
  378. struct timespec cur_time;
  379. rt_int64_t tv_sec = 0;
  380. rt_int64_t tv_nsec = 0;
  381. df = fd_get(fd);
  382. if (df == RT_NULL)
  383. {
  384. rt_set_errno(EINVAL);
  385. return -1;
  386. }
  387. tfd = df->vnode->data;
  388. get_current_time(tfd, &cur_time);
  389. rt_mutex_take(&tfd->lock, RT_WAITING_FOREVER);
  390. tv_sec = cur_time.tv_sec - tfd->pre_time.tv_sec;
  391. tv_nsec = cur_time.tv_nsec - tfd->pre_time.tv_nsec;
  392. timerfd_time_operation(&tv_sec, &tv_nsec);
  393. cur->it_interval.tv_nsec = tfd->ittimer.it_interval.tv_nsec;
  394. cur->it_interval.tv_sec = tfd->ittimer.it_interval.tv_sec;
  395. if (tfd->isperiodic == ENTER_PERIODIC)
  396. {
  397. cur->it_value.tv_nsec = tfd->ittimer.it_interval.tv_nsec - tv_nsec;
  398. cur->it_value.tv_sec = tfd->ittimer.it_interval.tv_sec - tv_sec;
  399. timerfd_time_operation(&cur->it_value.tv_sec, &cur->it_value.tv_nsec);
  400. }
  401. else
  402. {
  403. if (rt_atomic_load(&(tfd->timeout_num)) == 1)
  404. {
  405. cur->it_value.tv_nsec = 0;
  406. cur->it_value.tv_sec = 0;
  407. }
  408. else
  409. {
  410. cur->it_value.tv_nsec = tfd->ittimer.it_value.tv_nsec - tv_nsec;
  411. cur->it_value.tv_sec = tfd->ittimer.it_value.tv_sec - tv_sec;
  412. timerfd_time_operation(&cur->it_value.tv_sec, &cur->it_value.tv_nsec);
  413. }
  414. }
  415. rt_mutex_release(&tfd->lock);
  416. return 0;
  417. }
  418. int timerfd_create(int clockid, int flags)
  419. {
  420. return timerfd_do_create(clockid, flags);
  421. }
  422. int timerfd_settime(int fd, int flags, const struct itimerspec *new, struct itimerspec *old)
  423. {
  424. return timerfd_do_settime(fd, flags, new, old);
  425. }
  426. int timerfd_gettime(int fd, struct itimerspec *cur)
  427. {
  428. return timerfd_do_gettime(fd, cur);
  429. }