* @brief Set CPU affinity for a thread * * @param[in] tid The thread ID to set affinity for * @param[in] cpu The target CPU core number * * @return 0 on success, -1 on failure (invalid thread ID) * * @note This function binds a thread to a specific CPU core in SMP systems. * It handles thread reference counting and returns operation status. */
| 2240 | * It handles thread reference counting and returns operation status. |
| 2241 | */ |
| 2242 | static int _lwp_setaffinity(int tid, int cpu) |
| 2243 | { |
| 2244 | rt_thread_t thread; |
| 2245 | int ret = -1; |
| 2246 | |
| 2247 | thread = lwp_tid_get_thread_and_inc_ref(tid); |
| 2248 | |
| 2249 | if (thread) |
| 2250 | { |
| 2251 | #ifdef RT_USING_SMP |
| 2252 | rt_thread_control(thread, RT_THREAD_CTRL_BIND_CPU, (void *)(rt_ubase_t)cpu); |
| 2253 | #endif |
| 2254 | ret = 0; |
| 2255 | } |
| 2256 | lwp_tid_dec_ref(thread); |
| 2257 | return ret; |
| 2258 | } |
| 2259 | |
| 2260 | /** |
| 2261 | * @brief Sets CPU affinity for a thread |
no test coverage detected