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

Function rt_sched_thread_bind_cpu

src/scheduler_mp.c:1501–1582  ·  view source on GitHub ↗

* @brief Bind a thread to a specific CPU core * * @param thread The thread to be bound * @param cpu The target CPU core index (RT_CPUS_NR for no binding) * * @return rt_err_t * - RT_EOK: Operation successful * * @details This function handles thread-CPU binding with the following logic: * - If thread is READY: * * Removes from current ready queue * * Updates bind CPU informa

Source from the content-addressed store, hash-verified

1499 * nothing and always returns -RT_EINVAL.
1500 */
1501rt_err_t rt_sched_thread_bind_cpu(struct rt_thread *thread, int cpu)
1502{
1503 rt_sched_lock_level_t slvl;
1504 rt_uint8_t thread_stat;
1505
1506 RT_SCHED_DEBUG_IS_UNLOCKED;
1507
1508 if (cpu >= RT_CPUS_NR)
1509 {
1510 cpu = RT_CPUS_NR;
1511 }
1512
1513 rt_sched_lock(&slvl);
1514
1515 thread_stat = rt_sched_thread_get_stat(thread);
1516
1517 if (thread_stat == RT_THREAD_READY)
1518 {
1519 /* unbind */
1520 /* remove from old ready queue */
1521 rt_sched_remove_thread(thread);
1522 /* change thread bind cpu */
1523 RT_SCHED_CTX(thread).bind_cpu = cpu;
1524 /* add to new ready queue */
1525 rt_sched_insert_thread(thread);
1526
1527 if (rt_thread_self() != RT_NULL)
1528 {
1529 rt_sched_unlock_n_resched(slvl);
1530 }
1531 else
1532 {
1533 rt_sched_unlock(slvl);
1534 }
1535 }
1536 else
1537 {
1538 RT_SCHED_CTX(thread).bind_cpu = cpu;
1539 if (thread_stat == RT_THREAD_RUNNING)
1540 {
1541 /* thread is running on a cpu */
1542 int current_cpu = rt_hw_cpu_id();
1543
1544 if (cpu != RT_CPUS_NR)
1545 {
1546 if (RT_SCHED_CTX(thread).oncpu == current_cpu)
1547 {
1548 /* current thread on current cpu */
1549 if (cpu != current_cpu)
1550 {
1551 /* bind to other cpu */
1552 rt_hw_ipi_send(RT_SCHEDULE_IPI, 1U << cpu);
1553 /* self cpu need reschedule */
1554 rt_sched_unlock_n_resched(slvl);
1555 }
1556 else
1557 {
1558 /* else do nothing */

Callers

nothing calls this directly

Calls 9

rt_sched_thread_get_statFunction · 0.85
rt_thread_selfFunction · 0.85
rt_sched_lockFunction · 0.70
rt_sched_remove_threadFunction · 0.70
rt_sched_insert_threadFunction · 0.70
rt_sched_unlockFunction · 0.70
rt_hw_cpu_idFunction · 0.50
rt_hw_ipi_sendFunction · 0.50

Tested by

no test coverage detected