44 * SPDX-License-Identifier: Apache-2.0
55 *
66 * 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.
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
1617 */
1718
1819#include <stdint.h>
2122#include <dfs_file.h>
2223#include "poll.h"
2324
24- struct rt_poll_node ;
25- enum rt_poll_status {
26- RT_POLL_STAT_INIT ,
27- RT_POLL_STAT_TRIG ,
28- RT_POLL_STAT_WAITING ,
25+
26+ enum rt_poll_status
27+ {
28+ RT_POLL_STAT_INIT , /**< Poll operation initialization status. */
29+ RT_POLL_STAT_TRIG , /**< Poll operation triggered status. */
30+ RT_POLL_STAT_WAITING /**< Poll operation waiting status. */
2931};
3032
31- struct rt_poll_table
33+
34+ struct rt_poll_table
3235{
33- rt_pollreq_t req ;
34- enum rt_poll_status status ; /* the waited thread whether triggered */
35- rt_thread_t polling_thread ;
36- struct rt_poll_node * nodes ;
36+ rt_pollreq_t req ; /**< Poll request. */
37+ enum rt_poll_status status ; /**< Status of the poll operation. */
38+ rt_thread_t polling_thread ; /**< Polling thread associated with the table. */
39+ struct rt_poll_node * nodes ; /**< Linked list of poll nodes. */
3740};
3841
39- struct rt_poll_node
42+
43+ struct rt_poll_node
4044{
41- struct rt_wqueue_node wqn ;
42- struct rt_poll_table * pt ;
43- struct rt_poll_node * next ;
45+ struct rt_wqueue_node wqn ; /**< Wait queue node for the poll node. */
46+ struct rt_poll_table * pt ; /**< Pointer to the parent poll table. */
47+ struct rt_poll_node * next ; /**< Pointer to the next poll node. */
4448};
4549
4650static RT_DEFINE_SPINLOCK (_spinlock );
4751
52+ /**
53+ * @brief Wake-up function for the wait queue.
54+ *
55+ * This function is invoked when a node in the wait queue needs to be woken up.
56+ *
57+ * @param wait Pointer to the wait queue node.
58+ * @param key Key associated with the wake-up operation.
59+ * @return Upon successful wake-up, returns 0; otherwise, -1 is returned.
60+ */
4861static int __wqueue_pollwake (struct rt_wqueue_node * wait , void * key )
4962{
5063 rt_ubase_t level ;
@@ -68,6 +81,15 @@ static int __wqueue_pollwake(struct rt_wqueue_node *wait, void *key)
6881 return -1 ;
6982}
7083
84+ /**
85+ * @brief Adds a poll request to the wait queue.
86+ *
87+ * This function adds a poll request to the wait queue associated with the specified
88+ * wait queue and poll request.
89+ *
90+ * @param wq Pointer to the wait queue.
91+ * @param req Pointer to the poll request.
92+ */
7193static void _poll_add (rt_wqueue_t * wq , rt_pollreq_t * req )
7294{
7395 struct rt_poll_table * pt ;
@@ -89,6 +111,14 @@ static void _poll_add(rt_wqueue_t *wq, rt_pollreq_t *req)
89111 rt_wqueue_add (wq , & node -> wqn );
90112}
91113
114+ /**
115+ * @brief Initializes a poll table.
116+ *
117+ * This function initializes a poll table with the provided poll request, status,
118+ * and polling thread.
119+ *
120+ * @param pt Pointer to the poll table to be initialized.
121+ */
92122static void poll_table_init (struct rt_poll_table * pt )
93123{
94124 pt -> req ._proc = _poll_add ;
@@ -97,6 +127,18 @@ static void poll_table_init(struct rt_poll_table *pt)
97127 pt -> polling_thread = rt_thread_self ();
98128}
99129
130+ /**
131+ * @brief Waits for events on the poll table with a specified timeout.
132+ *
133+ * This function waits for events on the poll table with the specified timeout
134+ * in milliseconds.
135+ *
136+ * @param pt Pointer to the poll table.
137+ * @param msec Timeout value in milliseconds.
138+ * @return Upon successful completion, returns 0. If the timeout expires, -RT_ETIMEOUT
139+ * is returned. If the operation is interrupted by a signal, -RT_EINTR is
140+ * returned.
141+ */
100142static int poll_wait_timeout (struct rt_poll_table * pt , int msec )
101143{
102144 rt_int32_t timeout ;
@@ -150,6 +192,17 @@ static int poll_wait_timeout(struct rt_poll_table *pt, int msec)
150192 return ret ;
151193}
152194
195+ /**
196+ * @brief Performs poll operation for a single file descriptor.
197+ *
198+ * This function performs a poll operation for a single file descriptor and updates
199+ * the revents field of the pollfd structure accordingly.
200+ *
201+ * @param pollfd Pointer to the pollfd structure.
202+ * @param req Pointer to the poll request.
203+ * @return Upon successful completion, returns the bitmask of events that occurred.
204+ * If an error occurs, -1 is returned.
205+ */
153206static int do_pollfd (struct pollfd * pollfd , rt_pollreq_t * req )
154207{
155208 int mask = 0 ;
@@ -187,6 +240,21 @@ static int do_pollfd(struct pollfd *pollfd, rt_pollreq_t *req)
187240 return mask ;
188241}
189242
243+ /**
244+ * @brief Performs the poll operation on an array of file descriptors.
245+ *
246+ * This function performs the poll operation on an array of file descriptors and
247+ * waits for events with the specified timeout.
248+ *
249+ * @param fds Pointer to the array of pollfd structures.
250+ * @param nfds Number of file descriptors in the array.
251+ * @param pt Pointer to the poll table.
252+ * @param msec Timeout value in milliseconds.
253+ * @return Upon successful completion, returns the number of file descriptors
254+ * for which events were received. If the timeout expires, -RT_ETIMEOUT
255+ * is returned. If the operation is interrupted by a signal, -RT_EINTR is
256+ * returned.
257+ */
190258static int poll_do (struct pollfd * fds , nfds_t nfds , struct rt_poll_table * pt , int msec )
191259{
192260 int num ;
@@ -241,6 +309,14 @@ static int poll_do(struct pollfd *fds, nfds_t nfds, struct rt_poll_table *pt, in
241309 return num ;
242310}
243311
312+ /**
313+ * @brief Tears down the poll table.
314+ *
315+ * This function tears down the poll table by removing all poll nodes associated
316+ * with it.
317+ *
318+ * @param pt Pointer to the poll table.
319+ */
244320static void poll_teardown (struct rt_poll_table * pt )
245321{
246322 struct rt_poll_node * node , * next ;
@@ -255,6 +331,19 @@ static void poll_teardown(struct rt_poll_table *pt)
255331 }
256332}
257333
334+ /**
335+ * @brief Performs the poll operation on a set of file descriptors.
336+ *
337+ * This function performs the poll operation on a set of file descriptors and
338+ * waits for events with the specified timeout.
339+ *
340+ * @param fds Pointer to the array of pollfd structures.
341+ * @param nfds Number of file descriptors in the array.
342+ * @param timeout Timeout value in milliseconds.
343+ * @return Upon successful completion, returns the number of file descriptors
344+ * for which events were received. If the timeout expires, 0 is returned.
345+ * If an error occurs, -1 is returned.
346+ */
258347int poll (struct pollfd * fds , nfds_t nfds , int timeout )
259348{
260349 int num ;
0 commit comments