[smart] split out part of lwp_new() to lwp_create()

Prev implementation of lwp_new() including the create of lwp object
and the pid allocation. But not every lwp object need a pid.
So this patch split out the business of lwp_new() to improve the
maintainability.

Signed-off-by: Shell <smokewood@qq.com>
This commit is contained in:
Shell 2023-09-25 16:12:49 +08:00 committed by guo
parent 261f5bee67
commit a300cef2a8
7 changed files with 49 additions and 56 deletions

View File

@ -1152,7 +1152,7 @@ pid_t lwp_execve(char *filename, int debug, int argc, char **argv, char **envp)
return -EACCES;
}
lwp = lwp_new();
lwp = lwp_create(LWP_CREATE_FLAG_ALLOC_PID);
if (lwp == RT_NULL)
{

View File

@ -145,6 +145,7 @@ struct rt_lwp
unsigned int asid;
#endif
};
typedef struct rt_lwp *rt_lwp_t;
struct rt_lwp *lwp_self(void);

View File

@ -314,53 +314,46 @@ void lwp_user_object_dup(struct rt_lwp *dst_lwp, struct rt_lwp *src_lwp)
lwp_user_object_unlock(src_lwp);
}
struct rt_lwp* lwp_new(void)
rt_lwp_t lwp_create(rt_base_t flags)
{
pid_t pid;
rt_base_t level;
struct rt_lwp* lwp = RT_NULL;
rt_lwp_t new_lwp = rt_calloc(1, sizeof(struct rt_lwp));
lwp = (struct rt_lwp *)rt_malloc(sizeof(struct rt_lwp));
if (lwp == RT_NULL)
if (new_lwp)
{
return lwp;
/* minimal setup of lwp object */
new_lwp->session = -1;
new_lwp->ref = 1;
rt_list_init(&new_lwp->wait_list);
rt_list_init(&new_lwp->t_grp);
rt_list_init(&new_lwp->timer);
lwp_user_object_lock_init(new_lwp);
rt_wqueue_init(&new_lwp->wait_queue);
lwp_signal_init(&new_lwp->signal);
/* lwp with pid */
if (flags & LWP_CREATE_FLAG_ALLOC_PID)
{
level = rt_hw_interrupt_disable();
pid = lwp_pid_get();
if (pid == 0)
{
lwp_user_object_lock_destroy(new_lwp);
rt_free(new_lwp);
new_lwp = RT_NULL;
LOG_E("pid slot fulled!\n");
}
else
{
new_lwp->pid = pid;
lwp_pid_set_lwp(pid, new_lwp);
}
rt_hw_interrupt_enable(level);
}
}
memset(lwp, 0, sizeof(*lwp));
//lwp->tgroup_leader = RT_NULL;
rt_list_init(&lwp->wait_list);
lwp->leader = 0;
lwp->session = -1;
lwp->tty = RT_NULL;
rt_list_init(&lwp->t_grp);
rt_list_init(&lwp->timer);
lwp_user_object_lock_init(lwp);
lwp->address_search_head = RT_NULL;
rt_wqueue_init(&lwp->wait_queue);
lwp->ref = 1;
lwp_signal_init(&lwp->signal);
level = rt_hw_interrupt_disable();
pid = lwp_pid_get();
if (pid == 0)
{
lwp_user_object_lock_destroy(lwp);
rt_free(lwp);
lwp = RT_NULL;
LOG_E("pid slot fulled!\n");
goto out;
}
lwp->pid = pid;
lwp_pid_set_lwp(pid, lwp);
#ifdef LWP_ENABLE_ASID
lwp->generation = 0;
lwp->asid = 0;
#endif
out:
rt_hw_interrupt_enable(level);
return lwp;
return new_lwp;
}
void lwp_free(struct rt_lwp* lwp)

View File

@ -17,11 +17,16 @@
extern "C" {
#endif
#define LWP_CREATE_FLAG_NONE 0x0000
#define LWP_CREATE_FLAG_ALLOC_PID 0x0001 /* allocate pid on lwp object create */
struct rt_lwp;
struct lwp_avl_struct *lwp_get_pid_ary(void);
struct rt_lwp* lwp_new(void);
/* create a lwp object */
struct rt_lwp* lwp_create(rt_base_t flags);
void lwp_free(struct rt_lwp* lwp);
int lwp_ref_inc(struct rt_lwp *lwp);

View File

@ -2083,7 +2083,7 @@ sysret_t _sys_fork(void)
void *user_stack = RT_NULL;
/* new lwp */
lwp = lwp_new();
lwp = lwp_create(LWP_CREATE_FLAG_ALLOC_PID);
if (!lwp)
{
SET_ERRNO(ENOMEM);
@ -2685,15 +2685,13 @@ sysret_t sys_execve(const char *path, char *const argv[], char *const envp[])
}
/* alloc new lwp to operation */
new_lwp = (struct rt_lwp *)rt_malloc(sizeof(struct rt_lwp));
new_lwp = lwp_create(LWP_CREATE_FLAG_NONE);
if (!new_lwp)
{
SET_ERRNO(ENOMEM);
goto quit;
}
rt_memset(new_lwp, 0, sizeof(struct rt_lwp));
new_lwp->ref = 1;
lwp_user_object_lock_init(new_lwp);
ret = lwp_user_space_init(new_lwp, 0);
if (ret != 0)
{
@ -2788,10 +2786,6 @@ sysret_t sys_execve(const char *path, char *const argv[], char *const envp[])
rt_hw_interrupt_enable(level);
/* setup the signal, timer_list for the dummy lwp, so that is can be smoothly recycled */
lwp_signal_init(&new_lwp->signal);
rt_list_init(&new_lwp->timer);
lwp_ref_dec(new_lwp);
arch_start_umode(lwp->args,
lwp->text_entry,

View File

@ -50,7 +50,7 @@ static void test_user_map_varea(void)
const size_t buf_sz = ARCH_PAGE_SIZE * 4;
struct rt_lwp *lwp;
rt_varea_t varea;
lwp = lwp_new();
lwp = lwp_create(LWP_CREATE_FLAG_NONE);
/* prepare environment */
uassert_true(!!lwp);
@ -74,7 +74,7 @@ static void test_user_map_varea_ext(void)
const size_t buf_sz = ARCH_PAGE_SIZE * 4;
struct rt_lwp *lwp;
rt_varea_t varea;
lwp = lwp_new();
lwp = lwp_create(LWP_CREATE_FLAG_NONE);
uassert_true(!!lwp);
uassert_true(!lwp_user_space_init(lwp, 1));
@ -104,7 +104,7 @@ static void test_user_accessible(void)
/* Prepare Environment */
char *test_address = (char *)(USER_STACK_VEND);
struct rt_lwp *lwp;
lwp = lwp_new();
lwp = lwp_create(LWP_CREATE_FLAG_NONE);
uassert_true(!!lwp);
uassert_true(!lwp_user_space_init(lwp, 0));

View File

@ -31,7 +31,7 @@ void test_bst_adpt(void)
rt_mem_obj_t mem_obj;
/* create aspace by lwp */
lwp = lwp_new();
lwp = lwp_create(LWP_CREATE_FLAG_NONE);
uassert_true(!!lwp);
uassert_true(!lwp_user_space_init(lwp, 0));
aspace = lwp->aspace;