Skip to content

[smart] update sched_setaffinity() to use thread(task) ID #9004

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion components/lwp/lwp.h
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ void lwp_user_setting_restore(rt_thread_t thread);
void lwp_uthread_ctx_save(void *ctx);
void lwp_uthread_ctx_restore(void);

int lwp_setaffinity(pid_t pid, int cpu);
int lwp_setaffinity(int tid, int cpu);

pid_t exec(char *filename, int debug, int argc, char **argv);

Expand Down
26 changes: 8 additions & 18 deletions components/lwp/lwp_pid.c
Original file line number Diff line number Diff line change
Expand Up @@ -1600,35 +1600,25 @@ static void _resr_cleanup(struct rt_lwp *lwp)
}
}

static int _lwp_setaffinity(pid_t pid, int cpu)
static int _lwp_setaffinity(int tid, int cpu)
{
struct rt_lwp *lwp;
rt_thread_t thread;
int ret = -1;

lwp_pid_lock_take();
lwp = lwp_from_pid_locked(pid);
thread = lwp_tid_get_thread_and_inc_ref(tid);

if (lwp)
if (thread)
{
#ifdef RT_USING_SMP
rt_list_t *list;

lwp->bind_cpu = cpu;
for (list = lwp->t_grp.next; list != &lwp->t_grp; list = list->next)
{
rt_thread_t thread;

thread = rt_list_entry(list, struct rt_thread, sibling);
rt_thread_control(thread, RT_THREAD_CTRL_BIND_CPU, (void *)(rt_size_t)cpu);
}
rt_thread_control(thread, RT_THREAD_CTRL_BIND_CPU, (void *)(rt_ubase_t)cpu);
#endif
ret = 0;
}
lwp_pid_lock_release();
lwp_tid_dec_ref(thread);
return ret;
}

int lwp_setaffinity(pid_t pid, int cpu)
int lwp_setaffinity(int tid, int cpu)
{
int ret;

Expand All @@ -1638,7 +1628,7 @@ int lwp_setaffinity(pid_t pid, int cpu)
cpu = RT_CPUS_NR;
}
#endif
ret = _lwp_setaffinity(pid, cpu);
ret = _lwp_setaffinity(tid, cpu);
return ret;
}

Expand Down
6 changes: 6 additions & 0 deletions components/lwp/lwp_syscall.c
Original file line number Diff line number Diff line change
Expand Up @@ -5426,6 +5426,12 @@ sysret_t sys_sched_setaffinity(pid_t pid, size_t size, void *set)
if (CPU_ISSET_S(i, size, kset))
{
kmem_put(kset);

/**
* yes it's tricky.
* But when we talk about 'pid' from GNU libc, it's the 'task-id'
* aka 'thread->tid' known in kernel.
*/
return lwp_setaffinity(pid, i);
}
}
Expand Down
Loading