timerfd.c 11 KB

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