MCPcopy Create free account
hub / github.com/RT-Thread/rt-thread / sys_sched_setaffinity

Function sys_sched_setaffinity

components/lwp/lwp_syscall.c:8560–8601  ·  view source on GitHub ↗

* @brief Set the CPU affinity mask of a process. * * This function sets the CPU affinity mask for a specified process. The affinity mask determines which CPUs the * process is allowed to execute on. The process will be restricted to the CPUs specified in the mask. * * @param[in] pid The process ID of the process whose CPU affinity is to be set. If the `pid` is `0`, the *

Source from the content-addressed store, hash-verified

8558 * @see sys_sched_getaffinity(), sys_setcpu()
8559 */
8560sysret_t sys_sched_setaffinity(pid_t pid, size_t size, void *set)
8561{
8562 void *kset = RT_NULL;
8563
8564 if (size <= 0 || size > sizeof(cpu_set_t))
8565 {
8566 return -EINVAL;
8567 }
8568 if (!lwp_user_accessable((void *)set, size))
8569 return -EFAULT;
8570
8571 kset = kmem_get(size);
8572 if (kset == RT_NULL)
8573 {
8574 return -ENOMEM;
8575 }
8576
8577 if (lwp_get_from_user(kset, set, size) != size)
8578 {
8579 kmem_put(kset);
8580 return -EINVAL;
8581 }
8582
8583 for (int i = 0;i < size * 8; i++)
8584 {
8585 if (CPU_ISSET_S(i, size, kset))
8586 {
8587 kmem_put(kset);
8588
8589 /**
8590 * yes it's tricky.
8591 * But when we talk about 'pid' from GNU libc, it's the 'task-id'
8592 * aka 'thread->tid' known in kernel.
8593 */
8594 return lwp_setaffinity(pid, i);
8595 }
8596 }
8597
8598 kmem_put(kset);
8599
8600 return -1;
8601}
8602
8603/**
8604 * @brief Get the CPU affinity mask of a process.

Callers

nothing calls this directly

Calls 5

lwp_user_accessableFunction · 0.85
kmem_getFunction · 0.85
lwp_get_from_userFunction · 0.85
kmem_putFunction · 0.85
lwp_setaffinityFunction · 0.85

Tested by

no test coverage detected