summaryrefslogtreecommitdiff
path: root/components
diff options
context:
space:
mode:
authorShell <[email protected]>2024-11-15 11:54:38 +0800
committerRbb666 <[email protected]>2025-01-06 19:34:16 +0800
commitc0b0838892538d2ca13c844ea598db25ca0323e8 (patch)
tree77348fffc063d2ec54959f60f4abaa8781ba1ce1 /components
parent2946e4a316a949e80da6da3d4bda8646a2ae03b3 (diff)
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]>
Diffstat (limited to 'components')
-rw-r--r--components/lwp/lwp_pid.c29
-rw-r--r--components/lwp/lwp_pid.h4
2 files changed, 33 insertions, 0 deletions
diff --git a/components/lwp/lwp_pid.c b/components/lwp/lwp_pid.c
index af32818162..f127c591c0 100644
--- a/components/lwp/lwp_pid.c
+++ b/components/lwp/lwp_pid.c
@@ -93,6 +93,35 @@ void lwp_pid_lock_release(void)
RT_ASSERT(0);
}
+struct pid_foreach_param
+{
+ int (*cb)(pid_t pid, void *data);
+ void *data;
+};
+
+static int _before_cb(struct lwp_avl_struct *node, void *data)
+{
+ struct pid_foreach_param *param = data;
+ pid_t pid = node->avl_key;
+ return param->cb(pid, param->data);
+}
+
+int lwp_pid_for_each(int (*cb)(pid_t pid, void *data), void *data)
+{
+ int error;
+ struct pid_foreach_param buf =
+ {
+ .cb = cb,
+ .data = data,
+ };
+
+ lwp_pid_lock_take();
+ error = lwp_avl_traversal(lwp_pid_root, _before_cb, &buf);
+ lwp_pid_lock_release();
+
+ return error;
+}
+
struct lwp_avl_struct *lwp_get_pid_ary(void)
{
return lwp_pid_ary;
diff --git a/components/lwp/lwp_pid.h b/components/lwp/lwp_pid.h
index 5e170f652d..ae9d712422 100644
--- a/components/lwp/lwp_pid.h
+++ b/components/lwp/lwp_pid.h
@@ -15,6 +15,8 @@
extern "C" {
#endif
+#include <rtthread.h>
+
#define LWP_CREATE_FLAG_NONE 0x0000
#define LWP_CREATE_FLAG_ALLOC_PID 0x0001 /* allocate pid on lwp object create */
#define LWP_CREATE_FLAG_INIT_USPACE 0x0002 /* do user space initialization */
@@ -24,6 +26,8 @@ struct rt_lwp;
struct lwp_avl_struct *lwp_get_pid_ary(void);
int lwp_pid_init(void);
+
+int lwp_pid_for_each(int (*cb)(pid_t pid, void *data), void *data);
void lwp_pid_put(struct rt_lwp *lwp);
void lwp_pid_lock_take(void);
void lwp_pid_lock_release(void);