Skip to content

Commit 529864e

Browse files
committed
[smart] update sched_setaffinity() to use thread(task) ID
This change was necessary to correctly set CPU affinity using thread IDs (TIDs) instead of process IDs (PIDs). The previous implementation used PIDs, which caused issues since affinity settings need to be applied at the thread level. Changes were made by updating the function signatures and logic in `lwp.h`, `lwp_pid.c`, and `lwp_syscall.c` to accept TIDs. Specifically, the `lwp_setaffinity` function and related internal functions now operate using thread IDs and adjust thread affinity settings accordingly Signed-off-by: Shell <smokewood@qq.com>
1 parent 3172d37 commit 529864e

File tree

3 files changed

+15
-19
lines changed

3 files changed

+15
-19
lines changed

components/lwp/lwp.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ void lwp_user_setting_restore(rt_thread_t thread);
249249
void lwp_uthread_ctx_save(void *ctx);
250250
void lwp_uthread_ctx_restore(void);
251251

252-
int lwp_setaffinity(pid_t pid, int cpu);
252+
int lwp_setaffinity(int tid, int cpu);
253253

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

components/lwp/lwp_pid.c

+8-18
Original file line numberDiff line numberDiff line change
@@ -1600,35 +1600,25 @@ static void _resr_cleanup(struct rt_lwp *lwp)
16001600
}
16011601
}
16021602

1603-
static int _lwp_setaffinity(pid_t pid, int cpu)
1603+
static int _lwp_setaffinity(int tid, int cpu)
16041604
{
1605-
struct rt_lwp *lwp;
1605+
rt_thread_t thread;
16061606
int ret = -1;
16071607

1608-
lwp_pid_lock_take();
1609-
lwp = lwp_from_pid_locked(pid);
1608+
thread = lwp_tid_get_thread_and_inc_ref(tid);
16101609

1611-
if (lwp)
1610+
if (thread)
16121611
{
16131612
#ifdef RT_USING_SMP
1614-
rt_list_t *list;
1615-
1616-
lwp->bind_cpu = cpu;
1617-
for (list = lwp->t_grp.next; list != &lwp->t_grp; list = list->next)
1618-
{
1619-
rt_thread_t thread;
1620-
1621-
thread = rt_list_entry(list, struct rt_thread, sibling);
1622-
rt_thread_control(thread, RT_THREAD_CTRL_BIND_CPU, (void *)(rt_size_t)cpu);
1623-
}
1613+
rt_thread_control(thread, RT_THREAD_CTRL_BIND_CPU, (void *)(rt_ubase_t)cpu);
16241614
#endif
16251615
ret = 0;
16261616
}
1627-
lwp_pid_lock_release();
1617+
lwp_tid_dec_ref(thread);
16281618
return ret;
16291619
}
16301620

1631-
int lwp_setaffinity(pid_t pid, int cpu)
1621+
int lwp_setaffinity(int tid, int cpu)
16321622
{
16331623
int ret;
16341624

@@ -1638,7 +1628,7 @@ int lwp_setaffinity(pid_t pid, int cpu)
16381628
cpu = RT_CPUS_NR;
16391629
}
16401630
#endif
1641-
ret = _lwp_setaffinity(pid, cpu);
1631+
ret = _lwp_setaffinity(tid, cpu);
16421632
return ret;
16431633
}
16441634

components/lwp/lwp_syscall.c

+6
Original file line numberDiff line numberDiff line change
@@ -5426,6 +5426,12 @@ sysret_t sys_sched_setaffinity(pid_t pid, size_t size, void *set)
54265426
if (CPU_ISSET_S(i, size, kset))
54275427
{
54285428
kmem_put(kset);
5429+
5430+
/**
5431+
* yes it's tricky.
5432+
* But when we talk about 'pid' from GNU libc, it's the 'task-id'
5433+
* aka 'thread->tid' known in kernel.
5434+
*/
54295435
return lwp_setaffinity(pid, i);
54305436
}
54315437
}

0 commit comments

Comments
 (0)