poll.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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_tick_t timeout_tick = timeout;
  137. rt_timer_control(&(thread->thread_timer),
  138. RT_TIMER_CTRL_SET_TIME,
  139. &timeout_tick);
  140. rt_timer_start(&(thread->thread_timer));
  141. rt_set_errno(RT_ETIMEOUT);
  142. }
  143. else
  144. {
  145. rt_set_errno(0);
  146. }
  147. pt->status = RT_POLL_STAT_WAITING;
  148. rt_spin_unlock_irqrestore(&_spinlock, level);
  149. rt_schedule();
  150. level = rt_spin_lock_irqsave(&_spinlock);
  151. if (pt->status == RT_POLL_STAT_WAITING)
  152. pt->status = RT_POLL_STAT_INIT;
  153. }
  154. }
  155. ret = rt_get_errno();
  156. if (ret == RT_EINTR)
  157. ret = -RT_EINTR;
  158. else if (pt->status == RT_POLL_STAT_TRIG)
  159. ret = RT_EOK;
  160. else
  161. ret = -RT_ETIMEOUT;
  162. rt_spin_unlock_irqrestore(&_spinlock, level);
  163. return ret;
  164. }
  165. /**
  166. * @brief Performs poll operation for a single file descriptor.
  167. *
  168. * This function performs a poll operation for a single file descriptor and updates
  169. * the revents field of the pollfd structure accordingly.
  170. *
  171. * @param pollfd Pointer to the pollfd structure.
  172. * @param req Pointer to the poll request.
  173. * @return Upon successful completion, returns the bitmask of events that occurred.
  174. * If an error occurs, -1 is returned.
  175. */
  176. static int do_pollfd(struct pollfd *pollfd, rt_pollreq_t *req)
  177. {
  178. int mask = 0;
  179. int fd;
  180. fd = pollfd->fd;
  181. if (fd >= 0)
  182. {
  183. struct dfs_file *f = fd_get(fd);
  184. mask = POLLNVAL;
  185. if (f)
  186. {
  187. mask = POLLMASK_DEFAULT;
  188. if (f->vnode->fops->poll)
  189. {
  190. req->_key = pollfd->events | POLLERR | POLLHUP;
  191. mask = f->vnode->fops->poll(f, req);
  192. /* dealwith the device return error -1*/
  193. if (mask < 0)
  194. {
  195. pollfd->revents = 0;
  196. return mask;
  197. }
  198. }
  199. /* Mask out unneeded events. */
  200. mask &= pollfd->events | POLLERR | POLLHUP;
  201. }
  202. }
  203. pollfd->revents = mask;
  204. return mask;
  205. }
  206. /**
  207. * @brief Performs the poll operation on an array of file descriptors.
  208. *
  209. * This function performs the poll operation on an array of file descriptors and
  210. * waits for events with the specified timeout.
  211. *
  212. * @param fds Pointer to the array of pollfd structures.
  213. * @param nfds Number of file descriptors in the array.
  214. * @param pt Pointer to the poll table.
  215. * @param msec Timeout value in milliseconds.
  216. * @return Upon successful completion, returns the number of file descriptors
  217. * for which events were received. If the timeout expires, -RT_ETIMEOUT
  218. * is returned. If the operation is interrupted by a signal, -RT_EINTR is
  219. * returned.
  220. */
  221. static int poll_do(struct pollfd *fds, nfds_t nfds, struct rt_poll_table *pt, int msec)
  222. {
  223. int num;
  224. int istimeout = 0;
  225. nfds_t n;
  226. struct pollfd *pf;
  227. int ret = 0;
  228. if (msec == 0)
  229. {
  230. pt->req._proc = RT_NULL;
  231. istimeout = 1;
  232. }
  233. while (1)
  234. {
  235. pf = fds;
  236. num = 0;
  237. pt->status = RT_POLL_STAT_INIT;
  238. for (n = 0; n < nfds; n ++)
  239. {
  240. ret = do_pollfd(pf, &pt->req);
  241. if(ret < 0)
  242. {
  243. /*dealwith the device return error -1 */
  244. pt->req._proc = RT_NULL;
  245. return ret;
  246. }
  247. else if(ret > 0)
  248. {
  249. num ++;
  250. pt->req._proc = RT_NULL;
  251. }
  252. pf ++;
  253. }
  254. pt->req._proc = RT_NULL;
  255. if (num || istimeout)
  256. break;
  257. ret = poll_wait_timeout(pt, msec);
  258. if (ret == -RT_EINTR)
  259. return -EINTR;
  260. else if (ret == -RT_ETIMEOUT)
  261. istimeout = 1;
  262. else
  263. istimeout = 0;
  264. }
  265. return num;
  266. }
  267. /**
  268. * @brief Tears down the poll table.
  269. *
  270. * This function tears down the poll table by removing all poll nodes associated
  271. * with it.
  272. *
  273. * @param pt Pointer to the poll table.
  274. */
  275. static void poll_teardown(struct rt_poll_table *pt)
  276. {
  277. struct rt_poll_node *node, *next;
  278. next = pt->nodes;
  279. while (next)
  280. {
  281. node = next;
  282. rt_wqueue_remove(&node->wqn);
  283. next = node->next;
  284. rt_free(node);
  285. }
  286. }
  287. /**
  288. * @brief Performs the poll operation on a set of file descriptors.
  289. *
  290. * This function performs the poll operation on a set of file descriptors and
  291. * waits for events with the specified timeout.
  292. *
  293. * @param fds Pointer to the array of pollfd structures.
  294. * @param nfds Number of file descriptors in the array.
  295. * @param timeout Timeout value in milliseconds.
  296. * @return Upon successful completion, returns the number of file descriptors
  297. * for which events were received. If the timeout expires, 0 is returned.
  298. * If an error occurs, -1 is returned.
  299. */
  300. int poll(struct pollfd *fds, nfds_t nfds, int timeout)
  301. {
  302. int num;
  303. struct rt_poll_table table;
  304. poll_table_init(&table);
  305. num = poll_do(fds, nfds, &table, timeout);
  306. poll_teardown(&table);
  307. return num;
  308. }