Skip to content

Commit 0655742

Browse files
TroyMitchell911Rbb666
authored andcommitted
[libc][posix/io] add comments
1 parent 1720811 commit 0655742

File tree

5 files changed

+200
-87
lines changed

5 files changed

+200
-87
lines changed

components/libc/posix/io/epoll/epoll.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Change Logs:
77
* Date Author Notes
88
* 2023-07-29 zmq810150896 first version
9-
* 2024/03/26 TroyMitchelle Add comments for all functions, members within structure members and fix incorrect naming of triggered
9+
* 2024-03-26 TroyMitchelle Add comments for all functions, members within structure members and fix incorrect naming of triggered
1010
* 2023-12-14 Shell When poll goes to sleep before the waitqueue has added a
1111
* record and finished enumerating all the fd's, it may be
1212
* incorrectly woken up. This is basically because the poll

components/libc/posix/io/mman/mman.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*
66
* Change Logs:
7-
* Date Author Notes
8-
* 2017/11/30 Bernard The first version.
7+
* Date Author Notes
8+
* 2017/11/30 Bernard The first version.
9+
* 2024/03/29 TroyMitchelle Add all function comments
910
*/
1011

1112
#include <stdint.h>
@@ -20,6 +21,16 @@
2021

2122
#include "sys/mman.h"
2223

24+
/**
25+
* @brief Maps a region of memory into the calling process's address space.
26+
* @param addr Desired starting address of the mapping.
27+
* @param length Length of the mapping.
28+
* @param prot Protection of the mapped memory region.
29+
* @param flags Type of the mapped memory region.
30+
* @param fd File descriptor of the file to be mapped.
31+
* @param offset Offset within the file to start the mapping.
32+
* @return Upon success, returns a pointer to the mapped region; otherwise, MAP_FAILED is returned.
33+
*/
2334
void *mmap(void *addr, size_t length, int prot, int flags,
2435
int fd, off_t offset)
2536
{
@@ -59,6 +70,12 @@ void *mmap(void *addr, size_t length, int prot, int flags,
5970
return MAP_FAILED;
6071
}
6172

73+
/**
74+
* @brief Unmaps a mapped region of memory.
75+
* @param addr Starting address of the mapping to be unmapped.
76+
* @param length Length of the mapping.
77+
* @return Upon success, returns 0; otherwise, -1 is returned.
78+
*/
6279
int munmap(void *addr, size_t length)
6380
{
6481
if (addr)

components/libc/posix/io/mman/sys/mman.h

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*
66
* Change Logs:
7-
* Date Author Notes
8-
* 2017/11/30 Bernard The first version.
7+
* Date Author Notes
8+
* 2017/11/30 Bernard The first version.
9+
* 2024/03/29 TroyMitchelle Add comments for all macros
910
*/
1011

1112
#ifndef __SYS_MMAN_H__
@@ -19,37 +20,42 @@ extern "C" {
1920

2021
#define MAP_FAILED ((void *) -1)
2122

22-
#define MAP_SHARED 0x01
23-
#define MAP_PRIVATE 0x02
24-
#define MAP_TYPE 0x0f
25-
#define MAP_FIXED 0x10
26-
#define MAP_ANON 0x20
27-
#define MAP_ANONYMOUS MAP_ANON
28-
#define MAP_NORESERVE 0x4000
29-
#define MAP_GROWSDOWN 0x0100
30-
#define MAP_DENYWRITE 0x0800
31-
#define MAP_EXECUTABLE 0x1000
32-
#define MAP_LOCKED 0x2000
33-
#define MAP_POPULATE 0x8000
34-
#define MAP_NONBLOCK 0x10000
35-
#define MAP_STACK 0x20000
36-
#define MAP_HUGETLB 0x40000
37-
#define MAP_FILE 0
38-
39-
#define PROT_NONE 0
40-
#define PROT_READ 1
41-
#define PROT_WRITE 2
42-
#define PROT_EXEC 4
43-
#define PROT_GROWSDOWN 0x01000000
44-
#define PROT_GROWSUP 0x02000000
45-
46-
#define MS_ASYNC 1
47-
#define MS_INVALIDATE 2
48-
#define MS_SYNC 4
49-
50-
#define MCL_CURRENT 1
51-
#define MCL_FUTURE 2
52-
#define MCL_ONFAULT 4
23+
/* mmap flags */
24+
#define MAP_SHARED 0x01 /**< Share the mapping with other processes. */
25+
#define MAP_PRIVATE 0x02 /**< Create a private copy-on-write mapping. */
26+
#define MAP_TYPE 0x0f /**< Mask for type of mapping. */
27+
#define MAP_FIXED 0x10 /**< Interpret addr exactly. */
28+
#define MAP_ANON 0x20 /**< Anonymous mapping. */
29+
#define MAP_ANONYMOUS MAP_ANON /**< Synonym for MAP_ANON. */
30+
#define MAP_NORESERVE 0x4000 /**< Don't reserve swap space for this mapping. */
31+
#define MAP_GROWSDOWN 0x0100 /**< Stack-like segment. */
32+
#define MAP_DENYWRITE 0x0800 /**< ETXTBSY. */
33+
#define MAP_EXECUTABLE 0x1000 /**< Mark it as an executable. */
34+
#define MAP_LOCKED 0x2000 /**< Lock the mapping's pages. */
35+
#define MAP_POPULATE 0x8000 /**< Populate (prefault) pagetables. */
36+
#define MAP_NONBLOCK 0x10000 /**< Do not block on IO. */
37+
#define MAP_STACK 0x20000 /**< Allocation is a stack segment. */
38+
#define MAP_HUGETLB 0x40000 /**< Create a huge page mapping. */
39+
#define MAP_FILE 0 /**< Compatibility */
40+
41+
/* mmap protections */
42+
#define PROT_NONE 0 /**< No access. */
43+
#define PROT_READ 1 /**< Page can be read. */
44+
#define PROT_WRITE 2 /**< Page can be written. */
45+
#define PROT_EXEC 4 /**< Page can be executed. */
46+
#define PROT_GROWSDOWN 0x01000000/**< Extend change to start of growsdown vma (mprotect only). */
47+
#define PROT_GROWSUP 0x02000000/**< Extend change to start of growsup vma (mprotect only). */
48+
49+
/* msync flags */
50+
#define MS_ASYNC 1 /**< Perform asynchronous writes. */
51+
#define MS_INVALIDATE 2 /**< Invalidate mappings after writing. */
52+
#define MS_SYNC 4 /**< Perform synchronous writes. */
53+
54+
/* mlockall flags */
55+
#define MCL_CURRENT 1 /**< Lock all pages which are currently mapped into the address space of the process. */
56+
#define MCL_FUTURE 2 /**< Lock all pages which will become mapped into the address space of the process in the future. */
57+
#define MCL_ONFAULT 4 /**< Lock all pages which are currently mapped into the address space of the process on access. */
58+
5359

5460
void *mmap (void *start, size_t len, int prot, int flags, int fd, off_t off);
5561
int munmap (void *start, size_t len);

components/libc/posix/io/poll/poll.c

Lines changed: 112 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@
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>
@@ -21,30 +22,42 @@
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

4650
static 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+
*/
4861
static 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+
*/
7193
static 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+
*/
92122
static 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+
*/
100142
static 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+
*/
153206
static 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+
*/
190258
static 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+
*/
244320
static 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+
*/
258347
int poll(struct pollfd *fds, nfds_t nfds, int timeout)
259348
{
260349
int num;

0 commit comments

Comments
 (0)