* @brief drop a thread from the suspend list of mutex * * @param mutex is a pointer to a mutex object. * @param thread is the thread should be dropped from mutex. */
| 1077 | * @param thread is the thread should be dropped from mutex. |
| 1078 | */ |
| 1079 | void rt_mutex_drop_thread(rt_mutex_t mutex, rt_thread_t thread) |
| 1080 | { |
| 1081 | rt_uint8_t priority; |
| 1082 | rt_bool_t need_update = RT_FALSE; |
| 1083 | rt_sched_lock_level_t slvl; |
| 1084 | |
| 1085 | /* parameter check */ |
| 1086 | RT_DEBUG_IN_THREAD_CONTEXT; |
| 1087 | RT_ASSERT(mutex != RT_NULL); |
| 1088 | RT_ASSERT(thread != RT_NULL); |
| 1089 | |
| 1090 | rt_spin_lock(&(mutex->spinlock)); |
| 1091 | |
| 1092 | RT_ASSERT(thread->pending_object == &mutex->parent.parent); |
| 1093 | |
| 1094 | rt_sched_lock(&slvl); |
| 1095 | |
| 1096 | /* detach from suspended list */ |
| 1097 | rt_list_remove(&RT_THREAD_LIST_NODE(thread)); |
| 1098 | |
| 1099 | /** |
| 1100 | * Should change the priority of mutex owner thread |
| 1101 | * Note: After current thread is detached from mutex pending list, there is |
| 1102 | * a chance that the mutex owner has been released the mutex. Which |
| 1103 | * means mutex->owner can be NULL at this point. If that happened, |
| 1104 | * it had already reset its priority. So it's okay to skip |
| 1105 | */ |
| 1106 | if (mutex->owner && rt_sched_thread_get_curr_prio(mutex->owner) == |
| 1107 | rt_sched_thread_get_curr_prio(thread)) |
| 1108 | { |
| 1109 | need_update = RT_TRUE; |
| 1110 | } |
| 1111 | |
| 1112 | /* update the priority of mutex */ |
| 1113 | if (!rt_list_isempty(&mutex->parent.suspend_thread)) |
| 1114 | { |
| 1115 | /* more thread suspended in the list */ |
| 1116 | struct rt_thread *th; |
| 1117 | |
| 1118 | th = RT_THREAD_LIST_NODE_ENTRY(mutex->parent.suspend_thread.next); |
| 1119 | /* update the priority of mutex */ |
| 1120 | mutex->priority = rt_sched_thread_get_curr_prio(th); |
| 1121 | } |
| 1122 | else |
| 1123 | { |
| 1124 | /* set mutex priority to maximal priority */ |
| 1125 | mutex->priority = 0xff; |
| 1126 | } |
| 1127 | |
| 1128 | /* try to change the priority of mutex owner thread */ |
| 1129 | if (need_update) |
| 1130 | { |
| 1131 | /* get the maximal priority of mutex in thread */ |
| 1132 | priority = _thread_get_mutex_priority(mutex->owner); |
| 1133 | if (priority != rt_sched_thread_get_curr_prio(mutex->owner)) |
| 1134 | { |
| 1135 | _thread_update_priority(mutex->owner, priority, RT_UNINTERRUPTIBLE); |
| 1136 | } |
no test coverage detected