Skip to content

Commit c0b0838

Browse files
polarvidRbb666
authored andcommitted
feat: support PID iteration
This change introduces the `lwp_pid_for_each` function, which provides a convenient and thread-safe method for iterating over PIDs with a user- defined callback. This addition is necessary to support cases where operations must be performed on each PID in the balanced tree, enhancing flexibility and modularity for PID management. Changes: - Added `lwp_pid_for_each` function in `lwp_pid.c` to allow iteration over PIDs using a callback function and optional data parameter. - Defined a new internal `pid_foreach_param` structure to encapsulate the callback and data for the iteration. - Added `_before_cb` helper function for executing the callback on each PID node during AVL traversal. - Ensured thread safety by acquiring and releasing the PID lock around the AVL traversal within `lwp_pid_for_each`. - Updated `lwp_pid.h` with the `lwp_pid_for_each` function prototype and included `rtthread.h` for necessary types. Signed-off-by: Shell <[email protected]>
1 parent 2946e4a commit c0b0838

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

components/lwp/lwp_pid.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,35 @@ void lwp_pid_lock_release(void)
9393
RT_ASSERT(0);
9494
}
9595

96+
struct pid_foreach_param
97+
{
98+
int (*cb)(pid_t pid, void *data);
99+
void *data;
100+
};
101+
102+
static int _before_cb(struct lwp_avl_struct *node, void *data)
103+
{
104+
struct pid_foreach_param *param = data;
105+
pid_t pid = node->avl_key;
106+
return param->cb(pid, param->data);
107+
}
108+
109+
int lwp_pid_for_each(int (*cb)(pid_t pid, void *data), void *data)
110+
{
111+
int error;
112+
struct pid_foreach_param buf =
113+
{
114+
.cb = cb,
115+
.data = data,
116+
};
117+
118+
lwp_pid_lock_take();
119+
error = lwp_avl_traversal(lwp_pid_root, _before_cb, &buf);
120+
lwp_pid_lock_release();
121+
122+
return error;
123+
}
124+
96125
struct lwp_avl_struct *lwp_get_pid_ary(void)
97126
{
98127
return lwp_pid_ary;

components/lwp/lwp_pid.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
extern "C" {
1616
#endif
1717

18+
#include <rtthread.h>
19+
1820
#define LWP_CREATE_FLAG_NONE 0x0000
1921
#define LWP_CREATE_FLAG_ALLOC_PID 0x0001 /* allocate pid on lwp object create */
2022
#define LWP_CREATE_FLAG_INIT_USPACE 0x0002 /* do user space initialization */
@@ -24,6 +26,8 @@ struct rt_lwp;
2426

2527
struct lwp_avl_struct *lwp_get_pid_ary(void);
2628
int lwp_pid_init(void);
29+
30+
int lwp_pid_for_each(int (*cb)(pid_t pid, void *data), void *data);
2731
void lwp_pid_put(struct rt_lwp *lwp);
2832
void lwp_pid_lock_take(void);
2933
void lwp_pid_lock_release(void);

0 commit comments

Comments
 (0)