@@ -1578,15 +1578,15 @@ RTM_EXPORT(rt_mutex_trytake);
15781578 * @brief This function will release a mutex. If there is thread suspended on the mutex, the thread will be resumed.
15791579 *
15801580 * @note If there are threads suspended on this mutex, the first thread in the list of this mutex object
1581- * will be resumed, and a thread scheduling (rt_schedule) will be executed .
1581+ * will be resumed.
15821582 * If no threads are suspended on this mutex, the count value mutex->value of this mutex will increase by 1.
1583+ * This operation requires mutex->spinlock to be held.
15831584 *
15841585 * @param mutex is a pointer to a mutex object.
15851586 *
1586- * @return Return the operation status. When the return value is RT_EOK, the operation is successful.
1587- * If the return value is any other values, it means that the mutex release failed.
1587+ * @return Return RT_TRUE if scheduling is needed, RT_FALSE otherwise.
15881588 */
1589- rt_err_t rt_mutex_release (rt_mutex_t mutex )
1589+ static rt_err_t _rt_mutex_release (rt_mutex_t mutex , rt_bool_t is_force )
15901590{
15911591 rt_sched_lock_level_t slvl ;
15921592 struct rt_thread * thread ;
@@ -1612,11 +1612,15 @@ rt_err_t rt_mutex_release(rt_mutex_t mutex)
16121612 RT_OBJECT_HOOK_CALL (rt_object_put_hook , (& (mutex -> parent .parent )));
16131613
16141614 /* mutex only can be released by owner */
1615- if (thread != mutex -> owner )
1615+ if (is_force )
1616+ {
1617+ /* Force release by setting hold count to 1 */
1618+ mutex -> hold = 1 ;
1619+ }
1620+ else if (thread != mutex -> owner )
16161621 {
16171622 thread -> error = - RT_ERROR ;
16181623 rt_spin_unlock (& (mutex -> spinlock ));
1619-
16201624 return - RT_ERROR ;
16211625 }
16221626
@@ -1713,9 +1717,31 @@ rt_err_t rt_mutex_release(rt_mutex_t mutex)
17131717
17141718 return RT_EOK ;
17151719}
1716- RTM_EXPORT (rt_mutex_release );
17171720
17181721
1722+ rt_err_t rt_mutex_force_release (rt_mutex_t mutex )
1723+ {
1724+ return _rt_mutex_release (mutex , RT_TRUE );
1725+ }
1726+
1727+
1728+ /**
1729+ * @brief Release a mutex owned by current thread.
1730+ *
1731+ * @note Resumes first suspended thread if any (requires scheduling).
1732+ * Increases mutex->value if no threads waiting.
1733+ * Must be called by mutex owner with spinlock held.
1734+ *
1735+ * @param mutex Pointer to the mutex object.
1736+ *
1737+ * @return RT_EOK on success, -RT_ERROR if not owner.
1738+ */
1739+ rt_err_t rt_mutex_release (rt_mutex_t mutex )
1740+ {
1741+ return _rt_mutex_release (mutex , RT_FALSE );
1742+ }
1743+ RTM_EXPORT (rt_mutex_release );
1744+
17191745/**
17201746 * @brief This function will set some extra attributions of a mutex object.
17211747 *
0 commit comments