arch/xtensa: replace critical_section with spinlock.

The benefits of doing this are:

1. It makes the code logic clearer, with different resources protected by different locks.

2. It improves system responsiveness and avoids contention issues caused by acquiring the same large lock.

Signed-off-by: wangzhi16 <wangzhi16@xiaomi.com>
This commit is contained in:
wangzhi16
2025-06-12 21:19:31 +08:00
committed by Xiang Xiao
parent 4de188cbed
commit a2e8a5464e
2 changed files with 18 additions and 10 deletions
+11 -5
View File
@@ -26,7 +26,7 @@
#include <nuttx/config.h>
#include <nuttx/irq.h>
#include <nuttx/spinlock.h>
#include <nuttx/sched.h>
#include <arch/xtensa/xtensa_coproc.h>
@@ -36,6 +36,12 @@
#if XCHAL_CP_NUM > 0
/****************************************************************************
* Private Data
****************************************************************************/
static spinlock_t g_lock = SP_UNLOCKED;
/****************************************************************************
* Public Functions
****************************************************************************/
@@ -63,7 +69,7 @@ void xtensa_coproc_enable(int cpset)
/* These operations must be atomic */
flags = enter_critical_section();
flags = spin_lock_irqsave(&g_lock);
if (cpset != 0)
{
@@ -74,7 +80,7 @@ void xtensa_coproc_enable(int cpset)
xtensa_set_cpenable(cpenable);
}
leave_critical_section(flags);
spin_unlock_irqrestore(&g_lock, flags);
}
/****************************************************************************
@@ -100,7 +106,7 @@ void xtensa_coproc_disable(int cpset)
/* These operations must be atomic */
flags = enter_critical_section();
flags = spin_lock_irqsave(&g_lock);
if (cpset != 0)
{
@@ -111,7 +117,7 @@ void xtensa_coproc_disable(int cpset)
xtensa_set_cpenable(cpenable);
}
leave_critical_section(flags);
spin_unlock_irqrestore(&g_lock, flags);
}
#endif /* XCHAL_CP_NUM */
+7 -5
View File
@@ -27,7 +27,7 @@
#include <nuttx/config.h>
#include <assert.h>
#include <nuttx/irq.h>
#include <nuttx/spinlock.h>
#include <nuttx/kmalloc.h>
#include <nuttx/timers/oneshot.h>
@@ -48,6 +48,7 @@ struct xoneshot_lowerhalf_s
struct oneshot_lowerhalf_s lh; /* Lower half operations */
uint32_t freq; /* Timer working clock frequency(Hz) */
uint32_t irq;
spinlock_t lock; /* Lock to protect oneshot state */
};
/****************************************************************************
@@ -99,7 +100,7 @@ static int xtensa_oneshot_start(struct oneshot_lowerhalf_s *lower_,
uint32_t count;
irqstate_t flags;
flags = enter_critical_section();
flags = spin_lock_irqsave(&lower->lock);
count = sec_to_count((uint64_t)ts->tv_sec, lower->freq) +
nsec_to_count((uint64_t)ts->tv_nsec, lower->freq);
@@ -109,7 +110,7 @@ static int xtensa_oneshot_start(struct oneshot_lowerhalf_s *lower_,
up_enable_irq(lower->irq);
leave_critical_section(flags);
spin_unlock_irqrestore(&lower->lock, flags);
return 0;
}
@@ -121,11 +122,11 @@ static int xtensa_oneshot_cancel(struct oneshot_lowerhalf_s *lower_,
(struct xoneshot_lowerhalf_s *)lower_;
irqstate_t flags;
flags = enter_critical_section();
flags = spin_lock_irqsave(&lower->lock);
up_disable_irq(lower->irq);
leave_critical_section(flags);
spin_unlock_irqrestore(&lower->lock, flags);
return 0;
}
@@ -173,6 +174,7 @@ xtensa_oneshot_initialize(uint32_t irq, uint32_t freq)
lower->freq = freq;
lower->irq = irq;
spin_lock_init(&lower->lock);
irq_attach(irq, xtensa_oneshot_interrupt, lower);
return (struct oneshot_lowerhalf_s *)lower;