poll.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2016-12-28 Bernard first version
  9. * 2018-03-09 Bernard Add protection for pt->triggered.
  10. * 2023-12-04 Shell Fix return code and error verification
  11. * 2023-12-14 Shell When poll goes to sleep before the waitqueue has added a
  12. * record and finished enumerating all the fd's, it may be
  13. * incorrectly woken up. This is basically because the poll
  14. * mechanism wakeup algorithm does not correctly distinguish
  15. * the current wait state.
  16. * 2024-03-29 TroyMitchelle Add all function comments and comments to structure members
  17. */
  18. #include <stdint.h>
  19. #include <rthw.h>
  20. #include <rtthread.h>
  21. #include <dfs_file.h>
  22. #include "poll.h"
  23. enum rt_poll_status
  24. {
  25. RT_POLL_STAT_INIT, /**< Poll operation initialization status. */
  26. RT_POLL_STAT_TRIG, /**< Poll operation triggered status. */
  27. RT_POLL_STAT_WAITING /**< Poll operation waiting status. */
  28. };
  29. struct rt_poll_table
  30. {
  31. rt_pollreq_t req; /**< Poll request. */
  32. enum rt_poll_status status; /**< Status of the poll operation. */
  33. rt_thread_t polling_thread; /**< Polling thread associated with the table. */
  34. struct rt_poll_node *nodes; /**< Linked list of poll nodes. */
  35. };
  36. struct rt_poll_node
  37. {
  38. struct rt_wqueue_node wqn; /**< Wait queue node for the poll node. */
  39. struct rt_poll_table *pt; /**< Pointer to the parent poll table. */
  40. struct rt_poll_node *next; /**< Pointer to the next poll node. */
  41. };
  42. static RT_DEFINE_SPINLOCK(_spinlock);
  43. /**
  44. * @brief Wake-up function for the wait queue.
  45. *
  46. * This function is invoked when a node in the wait queue needs to be woken up.
  47. *
  48. * @param wait Pointer to the wait queue node.
  49. * @param key Key associated with the wake-up operation.
  50. * @return Upon successful wake-up, returns 0; otherwise, -1 is returned.
  51. */
  52. static int __wqueue_pollwake(struct rt_wqueue_node *wait, void *key)
  53. {
  54. rt_ubase_t level;
  55. struct rt_poll_node *pn;
  56. int is_waiting;
  57. if (key && !((rt_ubase_t)key & wait->key))
  58. return -1;
  59. pn = rt_container_of(wait, struct rt_poll_node, wqn);
  60. level = rt_spin_lock_irqsave(&_spinlock);
  61. is_waiting = (pn->pt->status == RT_POLL_STAT_WAITING);
  62. pn->pt->status = RT_POLL_STAT_TRIG;
  63. rt_spin_unlock_irqrestore(&_spinlock, level);
  64. if (is_waiting)
  65. return __wqueue_default_wake(wait, key);
  66. return -1;
  67. }
  68. /**
  69. * @brief Adds a poll request to the wait queue.
  70. *
  71. * This function adds a poll request to the wait queue associated with the specified
  72. * wait queue and poll request.
  73. *
  74. * @param wq Pointer to the wait queue.
  75. * @param req Pointer to the poll request.
  76. */
  77. static void _poll_add(rt_wqueue_t *wq, rt_pollreq_t *req)
  78. {
  79. struct rt_poll_table *pt;
  80. struct rt_poll_node *node;
  81. node = (struct rt_poll_node *)rt_malloc(sizeof(struct rt_poll_node));
  82. if (node == RT_NULL)
  83. return;
  84. pt = rt_container_of(req, struct rt_poll_table, req);
  85. node->wqn.key = req->_key;
  86. rt_list_init(&(node->wqn.list));
  87. node->wqn.polling_thread = pt->polling_thread;
  88. node->wqn.wakeup = __wqueue_pollwake;
  89. node->next = pt->nodes;
  90. node->pt = pt;
  91. pt->nodes = node;
  92. rt_wqueue_add(wq, &node->wqn);
  93. }
  94. /**
  95. * @brief Initializes a poll table.
  96. *
  97. * This function initializes a poll table with the provided poll request, status,
  98. * and polling thread.
  99. *
  100. * @param pt Pointer to the poll table to be initialized.
  101. */
  102. static void poll_table_init(struct rt_poll_table *pt)
  103. {
  104. pt->req._proc = _poll_add;
  105. pt->status = RT_POLL_STAT_INIT;
  106. pt->nodes = RT_NULL;
  107. pt->polling_thread = rt_thread_self();
  108. }
  109. /**
  110. * @brief Waits for events on the poll table with a specified timeout.
  111. *
  112. * This function waits for events on the poll table with the specified timeout
  113. * in milliseconds.
  114. *
  115. * @param pt Pointer to the poll table.
  116. * @param msec Timeout value in milliseconds.
  117. * @return Upon successful completion, returns 0. If the timeout expires, -RT_ETIMEOUT
  118. * is returned. If the operation is interrupted by a signal, -RT_EINTR is
  119. * returned.
  120. */
  121. static int poll_wait_timeout(struct rt_poll_table *pt, int msec)
  122. {
  123. rt_int32_t timeout;
  124. int ret = 0;
  125. struct rt_thread *thread;
  126. rt_base_t level;
  127. thread = pt->polling_thread;
  128. timeout = rt_tick_from_millisecond(msec);
  129. level = rt_spin_lock_irqsave(&_spinlock);
  130. if (timeout != 0 && pt->status != RT_POLL_STAT_TRIG)
  131. {
  132. if (rt_thread_suspend_with_flag(thread, RT_INTERRUPTIBLE) == RT_EOK)
  133. {
  134. if (timeout > 0)
  135. {
  136. rt_timer_control(&(thread->thread_timer),
  137. RT_TIMER_CTRL_SET_TIME,
  138. &timeout);
  139. rt_timer_start(&(thread->thread_timer));
  140. rt_set_errno(RT_ETIMEOUT);
  141. }
  142. else
  143. {
  144. rt_set_errno(0);
  145. }
  146. pt->status = RT_POLL_STAT_WAITING;
  147. rt_spin_unlock_irqrestore(&_spinlock, level);
  148. rt_schedule();
  149. level = rt_spin_lock_irqsave(&_spinlock);
  150. if (pt->status == RT_POLL_STAT_WAITING)
  151. pt->status = RT_POLL_STAT_INIT;
  152. }
  153. }
  154. ret = rt_get_errno();
  155. if (ret == RT_EINTR)
  156. ret = -RT_EINTR;
  157. else if (pt->status == RT_POLL_STAT_TRIG)
  158. ret = RT_EOK;
  159. else
  160. ret = -RT_ETIMEOUT;
  161. rt_spin_unlock_irqrestore(&_spinlock, level);
  162. return ret;
  163. }
  164. /**
  165. * @brief Performs poll operation for a single file descriptor.
  166. *
  167. * This function performs a poll operation for a single file descriptor and updates
  168. * the revents field of the pollfd structure accordingly.
  169. *
  170. * @param pollfd Pointer to the pollfd structure.
  171. * @param req Pointer to the poll request.
  172. * @return Upon successful completion, returns the bitmask of events that occurred.
  173. * If an error occurs, -1 is returned.
  174. */
  175. static int do_pollfd(struct pollfd *pollfd, rt_pollreq_t *req)
  176. {
  177. int mask = 0;
  178. int fd;
  179. fd = pollfd->fd;
  180. if (fd >= 0)
  181. {
  182. struct dfs_file *f = fd_get(fd);
  183. mask = POLLNVAL;
  184. if (f)
  185. {
  186. mask = POLLMASK_DEFAULT;
  187. if (f->vnode->fops->poll)
  188. {
  189. req->_key = pollfd->events | POLLERR | POLLHUP;
  190. mask = f->vnode->fops->poll(f, req);
  191. /* dealwith the device return error -1*/
  192. if (mask < 0)
  193. {
  194. pollfd->revents = 0;
  195. return mask;
  196. }
  197. }
  198. /* Mask out unneeded events. */
  199. mask &= pollfd->events | POLLERR | POLLHUP;
  200. }
  201. }
  202. pollfd->revents = mask;
  203. return mask;
  204. }
  205. /**
  206. * @brief Performs the poll operation on an array of file descriptors.
  207. *
  208. * This function performs the poll operation on an array of file descriptors and
  209. * waits for events with the specified timeout.
  210. *
  211. * @param fds Pointer to the array of pollfd structures.
  212. * @param nfds Number of file descriptors in the array.
  213. * @param pt Pointer to the poll table.
  214. * @param msec Timeout value in milliseconds.
  215. * @return Upon successful completion, returns the number of file descriptors
  216. * for which events were received. If the timeout expires, -RT_ETIMEOUT
  217. * is returned. If the operation is interrupted by a signal, -RT_EINTR is
  218. * returned.
  219. */
  220. static int poll_do(struct pollfd *fds, nfds_t nfds, struct rt_poll_table *pt, int msec)
  221. {
  222. int num;
  223. int istimeout = 0;
  224. nfds_t n;
  225. struct pollfd *pf;
  226. int ret = 0;
  227. if (msec == 0)
  228. {
  229. pt->req._proc = RT_NULL;
  230. istimeout = 1;
  231. }
  232. while (1)
  233. {
  234. pf = fds;
  235. num = 0;
  236. pt->status = RT_POLL_STAT_INIT;
  237. for (n = 0; n < nfds; n ++)
  238. {
  239. ret = do_pollfd(pf, &pt->req);
  240. if(ret < 0)
  241. {
  242. /*dealwith the device return error -1 */
  243. pt->req._proc = RT_NULL;
  244. return ret;
  245. }
  246. else if(ret > 0)
  247. {
  248. num ++;
  249. pt->req._proc = RT_NULL;
  250. }
  251. pf ++;
  252. }
  253. pt->req._proc = RT_NULL;
  254. if (num || istimeout)
  255. break;
  256. ret = poll_wait_timeout(pt, msec);
  257. if (ret == -RT_EINTR)
  258. return -EINTR;
  259. else if (ret == -RT_ETIMEOUT)
  260. istimeout = 1;
  261. else
  262. istimeout = 0;
  263. }
  264. return num;
  265. }
  266. /**
  267. * @brief Tears down the poll table.
  268. *
  269. * This function tears down the poll table by removing all poll nodes associated
  270. * with it.
  271. *
  272. * @param pt Pointer to the poll table.
  273. */
  274. static void poll_teardown(struct rt_poll_table *pt)
  275. {
  276. struct rt_poll_node *node, *next;
  277. next = pt->nodes;
  278. while (next)
  279. {
  280. node = next;
  281. rt_wqueue_remove(&node->wqn);
  282. next = node->next;
  283. rt_free(node);
  284. }
  285. }
  286. /**
  287. * @brief Performs the poll operation on a set of file descriptors.
  288. *
  289. * This function performs the poll operation on a set of file descriptors and
  290. * waits for events with the specified timeout.
  291. *
  292. * @param fds Pointer to the array of pollfd structures.
  293. * @param nfds Number of file descriptors in the array.
  294. * @param timeout Timeout value in milliseconds.
  295. * @return Upon successful completion, returns the number of file descriptors
  296. * for which events were received. If the timeout expires, 0 is returned.
  297. * If an error occurs, -1 is returned.
  298. */
  299. int poll(struct pollfd *fds, nfds_t nfds, int timeout)
  300. {
  301. int num;
  302. struct rt_poll_table table;
  303. poll_table_init(&table);
  304. num = poll_do(fds, nfds, &table, timeout);
  305. poll_teardown(&table);
  306. return num;
  307. }