diff --git a/src/ipc.c b/src/ipc.c index 25274deac4..3d760de2c5 100644 --- a/src/ipc.c +++ b/src/ipc.c @@ -956,8 +956,14 @@ void rt_mutex_drop_thread(rt_mutex_t mutex, rt_thread_t thread) rt_list_remove(&(thread->tlist)); - /* should change the priority of mutex owner thread */ - if (mutex->owner->current_priority == thread->current_priority) + /** + * Should change the priority of mutex owner thread + * Note: After current thread is detached from mutex pending list, there is + * a chance that the mutex owner has been released the mutex. Which + * means mutex->owner can be NULL at this point. If that happened, + * it had already reset its priority. So it's okay to skip + */ + if (mutex->owner && mutex->owner->current_priority == thread->current_priority) need_update = RT_TRUE; /* update the priority of mutex */ @@ -1304,8 +1310,14 @@ static rt_err_t _rt_mutex_take(rt_mutex_t mutex, rt_int32_t timeout, int suspend rt_bool_t need_update = RT_FALSE; - /* should change the priority of mutex owner thread */ - if (mutex->owner->current_priority == thread->current_priority) + /** + * Should change the priority of mutex owner thread + * Note: After current thread is detached from mutex pending list, there is + * a chance that the mutex owner has been released the mutex. Which + * means mutex->owner can be NULL at this point. If that happened, + * it had already reset its priority. So it's okay to skip + */ + if (mutex->owner && mutex->owner->current_priority == thread->current_priority) need_update = RT_TRUE; /* update the priority of mutex */ @@ -1340,8 +1352,12 @@ static rt_err_t _rt_mutex_take(rt_mutex_t mutex, rt_int32_t timeout, int suspend /* enable interrupt */ rt_hw_interrupt_enable(level); - /* return error */ - return thread->error; + /* clear pending object before exit */ + thread->pending_object = RT_NULL; + + /* fix thread error number to negative value and return */ + ret = thread->error; + return ret > 0 ? -ret : ret; } } }