From d02ac985dc2fc0c50eadca5f1f15977032eb754c Mon Sep 17 00:00:00 2001 From: zhanghongyu Date: Mon, 23 Jun 2025 17:51:58 +0800 Subject: [PATCH] wireless/ieee802154/: remove the use of critical_section so as to better support multi-core scenarios Signed-off-by: zhanghongyu --- wireless/ieee802154/ieee802154_primitive.c | 39 +++++++++++----------- wireless/ieee802154/mac802154_device.c | 22 ++++++------ wireless/ieee802154/mac802154_netdev.c | 7 +--- 3 files changed, 31 insertions(+), 37 deletions(-) diff --git a/wireless/ieee802154/ieee802154_primitive.c b/wireless/ieee802154/ieee802154_primitive.c index f718928aea9..42d9957c460 100644 --- a/wireless/ieee802154/ieee802154_primitive.c +++ b/wireless/ieee802154/ieee802154_primitive.c @@ -31,7 +31,7 @@ #include #include -#include +#include #include #include @@ -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 diff --git a/wireless/ieee802154/mac802154_device.c b/wireless/ieee802154/mac802154_device.c index a85e361fa98..5658023e9c5 100644 --- a/wireless/ieee802154/mac802154_device.c +++ b/wireless/ieee802154/mac802154_device.c @@ -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; diff --git a/wireless/ieee802154/mac802154_netdev.c b/wireless/ieee802154/mac802154_netdev.c index 8510e6b8ffb..beb2a6dd334 100644 --- a/wireless/ieee802154/mac802154_netdev.c +++ b/wireless/ieee802154/mac802154_netdev.c @@ -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; }