@@ -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+ rt_bool_t _rt_mutex_release (rt_mutex_t mutex )
15901590{
15911591 rt_sched_lock_level_t slvl ;
15921592 struct rt_thread * thread ;
@@ -1601,25 +1601,14 @@ rt_err_t rt_mutex_release(rt_mutex_t mutex)
16011601 /* only thread could release mutex because we need test the ownership */
16021602 RT_DEBUG_IN_THREAD_CONTEXT ;
16031603
1604- /* get current thread */
1605- thread = rt_thread_self ();
1606-
1607- rt_spin_lock (& (mutex -> spinlock ));
1604+ /* get the current owner thread of the mutex */
1605+ thread = mutex -> owner ;
16081606
1609- LOG_D ("mutex_release:current thread %s, hold: %d" ,
1610- thread -> parent .name , mutex -> hold );
1607+ LOG_D ("mutex_release: mutex owner thread %s, hold count : %d" ,
1608+ thread -> parent .name , mutex -> hold );
16111609
16121610 RT_OBJECT_HOOK_CALL (rt_object_put_hook , (& (mutex -> parent .parent )));
16131611
1614- /* mutex only can be released by owner */
1615- if (thread != mutex -> owner )
1616- {
1617- thread -> error = - RT_ERROR ;
1618- rt_spin_unlock (& (mutex -> spinlock ));
1619-
1620- return - RT_ERROR ;
1621- }
1622-
16231612 /* decrease hold */
16241613 mutex -> hold -- ;
16251614 /* if no hold */
@@ -1705,17 +1694,68 @@ rt_err_t rt_mutex_release(rt_mutex_t mutex)
17051694 }
17061695 }
17071696
1697+ return need_schedule ;
1698+ }
1699+
1700+
1701+ rt_bool_t _rt_mutex_release_detach (rt_mutex_t mutex )
1702+ {
1703+ _rt_mutex_release (mutex );
1704+ }
1705+
1706+
1707+ /**
1708+ * @brief Release a mutex owned by current thread.
1709+ *
1710+ * @note Resumes first suspended thread if any (requires scheduling).
1711+ * Increases mutex->value if no threads waiting.
1712+ * Must be called by mutex owner with spinlock held.
1713+ *
1714+ * @param mutex Pointer to the mutex object.
1715+ *
1716+ * @return RT_EOK on success, -RT_ERROR if not owner.
1717+ */
1718+ rt_err_t rt_mutex_release (rt_mutex_t mutex )
1719+ {
1720+ struct rt_thread * thread ;
1721+ rt_bool_t need_schedule ;
1722+
1723+ /* parameter check */
1724+ RT_ASSERT (mutex != RT_NULL );
1725+ RT_ASSERT (rt_object_get_type (& mutex -> parent .parent ) == RT_Object_Class_Mutex );
1726+
1727+ need_schedule = RT_FALSE ;
1728+
1729+ /* only thread could release mutex because we need test the ownership */
1730+ RT_DEBUG_IN_THREAD_CONTEXT ;
1731+
1732+ /* get current thread */
1733+ thread = rt_thread_self ();
1734+
1735+ rt_spin_lock (& (mutex -> spinlock ));
1736+
1737+ if (thread != mutex -> owner )
1738+ {
1739+ thread -> error = - RT_ERROR ;
1740+ rt_spin_unlock (& (mutex -> spinlock ));
1741+
1742+ return - RT_ERROR ;
1743+ }
1744+
1745+ need_schedule = _rt_mutex_release (mutex );
1746+
17081747 rt_spin_unlock (& (mutex -> spinlock ));
17091748
17101749 /* perform a schedule */
17111750 if (need_schedule == RT_TRUE )
1751+ {
17121752 rt_schedule ();
1753+ }
17131754
17141755 return RT_EOK ;
17151756}
17161757RTM_EXPORT (rt_mutex_release );
17171758
1718-
17191759/**
17201760 * @brief This function will set some extra attributions of a mutex object.
17211761 *
0 commit comments