diff --git a/components/lwp/lwp.h b/components/lwp/lwp.h index c504ef60ad1..060e9ff34cf 100644 --- a/components/lwp/lwp.h +++ b/components/lwp/lwp.h @@ -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); diff --git a/components/lwp/lwp_pid.c b/components/lwp/lwp_pid.c index 63a635d2f55..8b149dd5427 100644 --- a/components/lwp/lwp_pid.c +++ b/components/lwp/lwp_pid.c @@ -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; @@ -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; } diff --git a/components/lwp/lwp_syscall.c b/components/lwp/lwp_syscall.c index f7fe959eab3..27285e34c5c 100644 --- a/components/lwp/lwp_syscall.c +++ b/components/lwp/lwp_syscall.c @@ -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); } }