* @brief Update thread priority and adjust scheduling attributes * * @param thread The thread to update priority for * @param priority New priority value to set * @param update_init_prio Flag to determine if initial priority should also be updated * @return rt_err_t Always returns RT_EOK on success * * @details This function: * - Requires scheduler lock to be held (RT_SCHED_DEBUG_IS_LOCK
| 344 | * Thread status must be valid before calling |
| 345 | */ |
| 346 | static rt_err_t _rt_sched_update_priority(struct rt_thread *thread, rt_uint8_t priority, rt_bool_t update_init_prio) |
| 347 | { |
| 348 | RT_ASSERT(priority < RT_THREAD_PRIORITY_MAX); |
| 349 | RT_SCHED_DEBUG_IS_LOCKED; |
| 350 | |
| 351 | /* for ready thread, change queue; otherwise simply update the priority */ |
| 352 | if ((RT_SCHED_CTX(thread).stat & RT_THREAD_STAT_MASK) == RT_THREAD_READY) |
| 353 | { |
| 354 | /* remove thread from schedule queue first */ |
| 355 | rt_sched_remove_thread(thread); |
| 356 | |
| 357 | /* change thread priority */ |
| 358 | if (update_init_prio) |
| 359 | { |
| 360 | RT_SCHED_PRIV(thread).init_priority = priority; |
| 361 | } |
| 362 | RT_SCHED_PRIV(thread).current_priority = priority; |
| 363 | |
| 364 | /* recalculate priority attribute */ |
| 365 | #if RT_THREAD_PRIORITY_MAX > 32 |
| 366 | RT_SCHED_PRIV(thread).number = RT_SCHED_PRIV(thread).current_priority >> 3; /* 5bit */ |
| 367 | RT_SCHED_PRIV(thread).number_mask = 1 << RT_SCHED_PRIV(thread).number; |
| 368 | RT_SCHED_PRIV(thread).high_mask = 1 << (RT_SCHED_PRIV(thread).current_priority & 0x07); /* 3bit */ |
| 369 | #else |
| 370 | RT_SCHED_PRIV(thread).number_mask = 1 << RT_SCHED_PRIV(thread).current_priority; |
| 371 | #endif /* RT_THREAD_PRIORITY_MAX > 32 */ |
| 372 | RT_SCHED_CTX(thread).stat = RT_THREAD_INIT; |
| 373 | |
| 374 | /* insert thread to schedule queue again */ |
| 375 | rt_sched_insert_thread(thread); |
| 376 | } |
| 377 | else |
| 378 | { |
| 379 | if (update_init_prio) |
| 380 | { |
| 381 | RT_SCHED_PRIV(thread).init_priority = priority; |
| 382 | } |
| 383 | RT_SCHED_PRIV(thread).current_priority = priority; |
| 384 | |
| 385 | /* recalculate priority attribute */ |
| 386 | #if RT_THREAD_PRIORITY_MAX > 32 |
| 387 | RT_SCHED_PRIV(thread).number = RT_SCHED_PRIV(thread).current_priority >> 3; /* 5bit */ |
| 388 | RT_SCHED_PRIV(thread).number_mask = 1 << RT_SCHED_PRIV(thread).number; |
| 389 | RT_SCHED_PRIV(thread).high_mask = 1 << (RT_SCHED_PRIV(thread).current_priority & 0x07); /* 3bit */ |
| 390 | #else |
| 391 | RT_SCHED_PRIV(thread).number_mask = 1 << RT_SCHED_PRIV(thread).current_priority; |
| 392 | #endif /* RT_THREAD_PRIORITY_MAX > 32 */ |
| 393 | } |
| 394 | |
| 395 | return RT_EOK; |
| 396 | } |
| 397 | |
| 398 | /** |
| 399 | * @brief Update priority of the target thread |
no test coverage detected