Skip to content

Commit 9ebabdd

Browse files
committed
[components/lwp]add doxygen comment for lwp.c with lwp.h.
1 parent 138c888 commit 9ebabdd

File tree

2 files changed

+291
-67
lines changed

2 files changed

+291
-67
lines changed

components/lwp/lwp.c

Lines changed: 204 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2006-2021, RT-Thread Development Team
2+
* Copyright (c) 2006-2025 RT-Thread Development Team
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*
@@ -60,6 +60,18 @@
6060
extern char working_directory[];
6161
#endif
6262

63+
/**
64+
* @brief Initializes the LWP (Light-Weight Process) component
65+
*
66+
* @return int Returns RT_EOK if all initializations succeed, otherwise returns
67+
* the error code from the first failed initialization
68+
*
69+
* @note This function performs initialization of various LWP subsystems in sequence:
70+
* 1. Thread ID (TID) initialization
71+
* 2. Process ID (PID) initialization
72+
* 3. Channel component initialization
73+
* 4. Futex (Fast Userspace Mutex) initialization
74+
*/
6375
static int lwp_component_init(void)
6476
{
6577
int rc;
@@ -83,6 +95,15 @@ static int lwp_component_init(void)
8395
}
8496
INIT_COMPONENT_EXPORT(lwp_component_init);
8597

98+
/**
99+
* @brief Sets the current working directory for the calling LWP or system
100+
*
101+
* @param[in] buf Pointer to the path string to set as working directory
102+
*
103+
* @note This function handles both LWP-specific and system-wide working directories:
104+
* - For LWPs, sets the working_directory in the LWP structure
105+
* - For non-LWP threads, sets the global working_directory variable
106+
*/
86107
void lwp_setcwd(char *buf)
87108
{
88109
struct rt_lwp *lwp = RT_NULL;
@@ -106,6 +127,15 @@ void lwp_setcwd(char *buf)
106127
return ;
107128
}
108129

130+
/**
131+
* @brief Get the current working directory for the light-weight process
132+
*
133+
* @return char* Pointer to the current working directory string
134+
*
135+
* @note The function returns either:
136+
* - LWP's working directory (if valid and absolute path)
137+
* - System default working directory (if no LWP or invalid path)
138+
*/
109139
char *lwp_getcwd(void)
110140
{
111141
char *dir_buf = RT_NULL;
@@ -135,13 +165,28 @@ char *lwp_getcwd(void)
135165
}
136166

137167
/**
138-
* RT-Thread light-weight process
168+
* @brief Set the kernel stack pointer for the current thread
169+
*
170+
* @param[in] sp Pointer to the new kernel stack location
171+
*
172+
* @note It's typically used during context switching or thread initialization.
139173
*/
140174
void lwp_set_kernel_sp(uint32_t *sp)
141175
{
142176
rt_thread_self()->kernel_sp = (rt_uint32_t *)sp;
143177
}
144178

179+
/**
180+
* @brief Get the kernel stack pointer for the current thread
181+
*
182+
* @return uint32_t* Pointer to the kernel stack
183+
*
184+
* @note Architecture-specific behavior:
185+
* 1. With MMU: Simply returns the current thread's stack pointer
186+
* 2. Without MMU: Checks interrupt context and returns either:
187+
* - Interrupted thread's kernel_sp (if in interrupt)
188+
* - Current thread's kernel_sp (if not in interrupt)
189+
*/
145190
uint32_t *lwp_get_kernel_sp(void)
146191
{
147192
#ifdef ARCH_MM_MMU
@@ -162,8 +207,15 @@ uint32_t *lwp_get_kernel_sp(void)
162207
#endif
163208
}
164209

165-
166-
/* lwp-thread clean up routine */
210+
/**
211+
* @brief Clean up resources associated with a light-weight process thread
212+
*
213+
* @param[in] tid Pointer to the thread control block to be cleaned up
214+
*
215+
* @note This function performs cleanup operations for a thread associated with a light-weight process (LWP).
216+
* It handles signal detachment and reference count decrement for the LWP structure.
217+
*
218+
*/
167219
void lwp_cleanup(struct rt_thread *tid)
168220
{
169221
struct rt_lwp *lwp;
@@ -191,6 +243,15 @@ void lwp_cleanup(struct rt_thread *tid)
191243
return;
192244
}
193245

246+
/**
247+
* @brief Set up standard I/O for a light-weight process
248+
*
249+
* @param[in] lwp Pointer to the light-weight process structure
250+
*
251+
* @note This function initializes the standard input, output, and error streams
252+
* for a light-weight process by opening the console device and associating
253+
* it with file descriptors 0, 1, and 2.
254+
*/
194255
static void lwp_execve_setup_stdio(struct rt_lwp *lwp)
195256
{
196257
struct dfs_fdtable *lwp_fdt;
@@ -223,6 +284,14 @@ static void lwp_execve_setup_stdio(struct rt_lwp *lwp)
223284
return;
224285
}
225286

287+
/**
288+
* @brief Entry point for light-weight process threads
289+
*
290+
* @param[in] parameter Thread parameter (unused)
291+
*
292+
* @note This function is the main entry point for threads created within a light-weight process.
293+
* It handles thread initialization, debug mode setup, and transitions to user mode.
294+
*/
226295
static void _lwp_thread_entry(void *parameter)
227296
{
228297
rt_thread_t tid;
@@ -262,6 +331,15 @@ static void _lwp_thread_entry(void *parameter)
262331
#endif /* ARCH_MM_MMU */
263332
}
264333

334+
/**
335+
* @brief Get the current light-weight process
336+
*
337+
* @return Pointer to the current light-weight process structure
338+
* RT_NULL if no process is associated with current thread
339+
*
340+
* @note This function retrieves the light-weight process associated with the
341+
* currently running thread.
342+
*/
265343
struct rt_lwp *lwp_self(void)
266344
{
267345
rt_thread_t tid;
@@ -275,6 +353,17 @@ struct rt_lwp *lwp_self(void)
275353
return RT_NULL;
276354
}
277355

356+
/**
357+
* @brief Register a child process with its parent
358+
*
359+
* @param[in] parent Pointer to the parent process structure
360+
* @param[in] child Pointer to the child process structure to register
361+
*
362+
* @return RT_EOK on success
363+
*
364+
* @note This function adds a child process to its parent's children list and
365+
* increases reference counts for both processes.
366+
*/
278367
rt_err_t lwp_children_register(struct rt_lwp *parent, struct rt_lwp *child)
279368
{
280369
/* lwp add to children link */
@@ -293,6 +382,17 @@ rt_err_t lwp_children_register(struct rt_lwp *parent, struct rt_lwp *child)
293382
return 0;
294383
}
295384

385+
/**
386+
* @brief Unregister a child process from its parent
387+
*
388+
* @param[in] parent Pointer to the parent process structure
389+
* @param[in] child Pointer to the child process structure to unregister
390+
*
391+
* @return RT_EOK on success
392+
*
393+
* @note This function removes a child process from its parent's children list and
394+
* decreases reference counts for both processes.
395+
*/
296396
rt_err_t lwp_children_unregister(struct rt_lwp *parent, struct rt_lwp *child)
297397
{
298398
struct rt_lwp **lwp_node;
@@ -316,6 +416,23 @@ rt_err_t lwp_children_unregister(struct rt_lwp *parent, struct rt_lwp *child)
316416
return 0;
317417
}
318418

419+
/**
420+
* @brief Copy process arguments and environment variables from kernel space to user space.
421+
*
422+
* @param[in] lwp Pointer to the light-weight process structure
423+
* @param[in] argc Argument count
424+
* @param[in] argv Argument vector
425+
* @param[in] envp Environment variables
426+
*
427+
* @return Pointer to the process auxiliary structure on success
428+
* RT_NULL if memory allocation fails or arguments initialization fails
429+
*
430+
* @note This function performs the following operations:
431+
* 1. Initializes argument information structure
432+
* 2. Copies command line arguments to user space
433+
* 3. Copies environment variables to user space
434+
* 4. Returns the auxiliary structure containing copied data
435+
*/
319436
struct process_aux *argscopy(struct rt_lwp *lwp, int argc, char **argv, char **envp)
320437
{
321438
struct lwp_args_info ai;
@@ -344,6 +461,30 @@ struct process_aux *argscopy(struct rt_lwp *lwp, int argc, char **argv, char **e
344461
return ua;
345462
}
346463

464+
/**
465+
* @brief Creates and starts a new LWP by loading and executing the specified executable file.
466+
*
467+
* @param[in] filename Path to the executable file
468+
* @param[in] debug Debug flag (non-zero to enable debugging)
469+
* @param[in] argc Argument count
470+
* @param[in] argv Argument vector
471+
* @param[in] envp Environment variables
472+
*
473+
* @return Process ID (PID) of the new LWP on success
474+
* -EINVAL if filename is NULL
475+
* -EACCES if file is not executable
476+
* -ENOMEM if memory allocation fails
477+
* -RT_ERROR on other failures
478+
*
479+
* @note This function performs the following operations:
480+
* 1. Validates input parameters
481+
* 2. Creates new LWP structure
482+
* 3. Initializes user space (for MMU systems)
483+
* 4. Copies arguments and environment
484+
* 5. Loads the executable
485+
* 6. Sets up standard I/O
486+
* 7. Creates and starts the main thread
487+
*/
347488
pid_t lwp_execve(char *filename, int debug, int argc, char **argv, char **envp)
348489
{
349490
int result;
@@ -499,13 +640,38 @@ extern char **__environ;
499640
char **__environ = 0;
500641
#endif
501642

643+
/**
644+
* @brief Execute a new program in the current process context
645+
*
646+
* @param[in] filename Path to the executable file
647+
* @param[in] debug Debug flag (non-zero enables debug mode)
648+
* @param[in] argc Number of command line arguments
649+
* @param[in] argv Array of command line argument strings
650+
*
651+
* @return Process ID (PID) of the new process on success
652+
* Negative error code on failure
653+
*
654+
* @note This is a wrapper function for lwp_execve.
655+
*
656+
* @see lwp_execve()
657+
*/
502658
pid_t exec(char *filename, int debug, int argc, char **argv)
503659
{
504660
setenv("OS", "RT-Thread", 1);
505661
return lwp_execve(filename, debug, argc, argv, __environ);
506662
}
507663

508664
#ifdef ARCH_MM_MMU
665+
/**
666+
* @brief Saves thread-specific user settings (TID register)
667+
*
668+
* @param[in,out] thread Pointer to the thread control block
669+
*
670+
* @note This function stores the architecture-specific TID register
671+
* into the specified thread's control block.This is typically used
672+
* when switching between threads to preserve thread-specific settings
673+
*/
674+
509675
void lwp_user_setting_save(rt_thread_t thread)
510676
{
511677
if (thread)
@@ -514,6 +680,14 @@ void lwp_user_setting_save(rt_thread_t thread)
514680
}
515681
}
516682

683+
/**
684+
* @brief Restores thread-specific user settings (TID register and debug state)
685+
*
686+
* @param[in] thread Pointer to the thread control block
687+
*
688+
* @note This function restores architecture-specific Thread ID Register (TIDR) value
689+
* and debug-related settings for the specified thread.
690+
*/
517691
void lwp_user_setting_restore(rt_thread_t thread)
518692
{
519693
if (!thread)
@@ -556,20 +730,46 @@ void lwp_user_setting_restore(rt_thread_t thread)
556730
}
557731
#endif /* ARCH_MM_MMU */
558732

733+
/**
734+
* @brief Saves user thread context pointer
735+
*
736+
* @param[in] ctx Pointer to user thread context structure to be saved
737+
*
738+
* @note This function stores a pointer to user thread context in the current thread's
739+
* control block for later restoration. The context pointer is typically used
740+
* during thread context switching.
741+
*/
559742
void lwp_uthread_ctx_save(void *ctx)
560743
{
561744
rt_thread_t thread;
562745
thread = rt_thread_self();
563746
thread->user_ctx.ctx = ctx;
564747
}
565748

749+
/**
750+
* @brief Restores the user thread context by clearing the context pointer
751+
*
752+
* @note Typically called during thread context switching to clean up any
753+
* previously saved user context.
754+
*/
566755
void lwp_uthread_ctx_restore(void)
567756
{
568757
rt_thread_t thread;
569758
thread = rt_thread_self();
570759
thread->user_ctx.ctx = RT_NULL;
571760
}
572761

762+
/**
763+
* @brief Prints a backtrace of the current thread's call stack
764+
*
765+
* @param[in] uthread The thread to backtrace (must be associated with an LWP)
766+
* @param[in] frame Pointer to the initial stack frame
767+
*
768+
* @return RT_EOK on success, -RT_ERROR on failure
769+
*
770+
* @note This function prints a backtrace of the call stack for the specified user thread,
771+
* providing addresses that can be used with addr2line to get file and line information.
772+
*/
573773
rt_err_t lwp_backtrace_frame(rt_thread_t uthread, struct rt_hw_backtrace_frame *frame)
574774
{
575775
rt_err_t rc = -RT_ERROR;

0 commit comments

Comments
 (0)