wireless/ieee802154/: remove the use of critical_section

so as to better support multi-core scenarios

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
This commit is contained in:
zhanghongyu 2025-06-23 17:51:58 +08:00 committed by archer
parent 9dab103fa0
commit d02ac985dc
3 changed files with 31 additions and 37 deletions

View file

@ -31,7 +31,7 @@
#include <assert.h>
#include <debug.h>
#include <nuttx/irq.h>
#include <nuttx/spinlock.h>
#include <nuttx/kmalloc.h>
#include <nuttx/wireless/ieee802154/ieee802154_mac.h>
@ -114,6 +114,7 @@ static struct ieee802154_priv_primitive_s
#endif /* CONFIG_IEEE802154_PRIMITIVE_PREALLOC > 0 */
static bool g_poolinit = false;
static spinlock_t g_primfree_lock = SP_UNLOCKED;
/****************************************************************************
* Public Functions
@ -238,7 +239,7 @@ FAR struct ieee802154_primitive_s *ieee802154_primitive_allocate(void)
* then try the list of messages reserved for interrupt handlers
*/
flags = enter_critical_section(); /* Always necessary in SMP mode */
flags = spin_lock_irqsave(&g_primfree_lock); /* Always necessary in SMP mode */
if (up_interrupt_context())
{
#if CONFIG_IEEE802154_PRIMITIVE_PREALLOC > CONFIG_IEEE802154_PRIMITIVE_IRQRESERVE
@ -246,11 +247,11 @@ FAR struct ieee802154_primitive_s *ieee802154_primitive_allocate(void)
if (g_primfree != NULL)
{
prim = g_primfree;
g_primfree = prim->flink;
prim = g_primfree;
g_primfree = prim->flink;
leave_critical_section(flags);
pool = POOL_PRIMITIVE_GENERAL;
spin_unlock_irqrestore(&g_primfree_lock, flags);
pool = POOL_PRIMITIVE_GENERAL;
}
else
#endif
@ -262,13 +263,13 @@ FAR struct ieee802154_primitive_s *ieee802154_primitive_allocate(void)
prim = g_primfree_irq;
g_primfree_irq = prim->flink;
leave_critical_section(flags);
pool = POOL_PRIMITIVE_IRQ;
spin_unlock_irqrestore(&g_primfree_lock, flags);
pool = POOL_PRIMITIVE_IRQ;
}
else
#endif
{
leave_critical_section(flags);
spin_unlock_irqrestore(&g_primfree_lock, flags);
return NULL;
}
}
@ -282,11 +283,11 @@ FAR struct ieee802154_primitive_s *ieee802154_primitive_allocate(void)
if (g_primfree != NULL)
{
prim = g_primfree;
g_primfree = prim->flink;
prim = g_primfree;
g_primfree = prim->flink;
leave_critical_section(flags);
pool = POOL_PRIMITIVE_GENERAL;
spin_unlock_irqrestore(&g_primfree_lock, flags);
pool = POOL_PRIMITIVE_GENERAL;
}
else
#endif
@ -295,7 +296,7 @@ FAR struct ieee802154_primitive_s *ieee802154_primitive_allocate(void)
* will have to allocate one from the kernel memory pool.
*/
leave_critical_section(flags);
spin_unlock_irqrestore(&g_primfree_lock, flags);
prim = (FAR struct ieee802154_priv_primitive_s *)
kmm_malloc((sizeof (struct ieee802154_priv_primitive_s)));
@ -372,10 +373,10 @@ void ieee802154_primitive_free(FAR struct ieee802154_primitive_s *prim)
* list from interrupt handlers.
*/
flags = enter_critical_section();
flags = spin_lock_irqsave(&g_primfree_lock);
priv->flink = g_primfree;
g_primfree = priv;
leave_critical_section(flags);
spin_unlock_irqrestore(&g_primfree_lock, flags);
}
else
#endif
@ -391,10 +392,10 @@ void ieee802154_primitive_free(FAR struct ieee802154_primitive_s *prim)
* list from interrupt handlers.
*/
flags = enter_critical_section();
flags = spin_lock_irqsave(&g_primfree_lock);
priv->flink = g_primfree_irq;
g_primfree_irq = priv;
leave_critical_section(flags);
g_primfree_irq = priv;
spin_unlock_irqrestore(&g_primfree_lock, flags);
}
else
#endif

View file

@ -223,7 +223,6 @@ static int mac802154dev_close(FAR struct file *filep)
FAR struct mac802154dev_open_s *opriv;
FAR struct mac802154dev_open_s *curr;
FAR struct mac802154dev_open_s *prev;
irqstate_t flags;
bool closing;
int ret;
@ -233,6 +232,15 @@ static int mac802154dev_close(FAR struct file *filep)
DEBUGASSERT(inode->i_private);
dev = inode->i_private;
/* Get exclusive access to the driver structure */
ret = nxmutex_lock(&dev->md_lock);
if (ret < 0)
{
wlerr("ERROR: nxsem_wait failed: %d\n", ret);
return ret;
}
/* Handle an improbable race conditions with the following atomic test
* and set.
*
@ -243,27 +251,17 @@ static int mac802154dev_close(FAR struct file *filep)
* detection anyway.
*/
flags = enter_critical_section();
closing = opriv->md_closing;
opriv->md_closing = true;
leave_critical_section(flags);
if (closing)
{
/* Another thread is doing the close */
nxmutex_unlock(&dev->md_lock);
return OK;
}
/* Get exclusive access to the driver structure */
ret = nxmutex_lock(&dev->md_lock);
if (ret < 0)
{
wlerr("ERROR: nxsem_wait failed: %d\n", ret);
return ret;
}
/* Find the open structure in the list of open structures for the device */
for (prev = NULL, curr = dev->md_open;

View file

@ -709,11 +709,6 @@ static int macnet_ifdown(FAR struct net_driver_s *dev)
{
FAR struct macnet_driver_s *priv = (FAR struct macnet_driver_s *)
dev->d_private;
irqstate_t flags;
/* Disable interruption */
flags = enter_critical_section();
/* Put the EMAC in its reset, non-operational state. This should be
* a known configuration that will guarantee the macnet_ifup() always
@ -723,7 +718,7 @@ static int macnet_ifdown(FAR struct net_driver_s *dev)
/* Mark the device "down" */
priv->md_bifup = false;
leave_critical_section(flags);
return OK;
}