drivers/note: unify the spinlock operation in noteram

Signed-off-by: yinshengkai <yinshengkai@xiaomi.com>
This commit is contained in:
yinshengkai 2022-12-28 18:00:06 +08:00 committed by Xiang Xiao
parent 81448b5021
commit 1cee890aa7
1 changed files with 13 additions and 43 deletions

View File

@ -94,9 +94,7 @@ static const struct note_driver_ops_s g_noteram_ops =
noteram_add
};
#ifdef CONFIG_SMP
static volatile spinlock_t g_noteram_lock;
#endif
static spinlock_t g_noteram_lock;
/****************************************************************************
* Public Data
@ -127,10 +125,6 @@ struct note_driver_s g_noteram_driver =
static void noteram_buffer_clear(void)
{
irqstate_t flags;
flags = enter_critical_section();
g_noteram_info.ni_tail = g_noteram_info.ni_head;
g_noteram_info.ni_read = g_noteram_info.ni_head;
@ -138,8 +132,6 @@ static void noteram_buffer_clear(void)
{
g_noteram_info.ni_overwrite = NOTERAM_MODE_OVERWRITE_DISABLE;
}
leave_critical_section(flags);
}
/****************************************************************************
@ -289,22 +281,19 @@ static void noteram_remove(void)
static ssize_t noteram_get(FAR uint8_t *buffer, size_t buflen)
{
FAR struct note_common_s *note;
irqstate_t flags;
unsigned int remaining;
unsigned int read;
ssize_t notelen;
size_t circlen;
DEBUGASSERT(buffer != NULL);
flags = enter_critical_section();
/* Verify that the circular buffer is not empty */
circlen = noteram_unread_length();
if (circlen <= 0)
{
notelen = 0;
goto errout_with_csection;
return 0;
}
/* Get the read index of the circular buffer */
@ -328,8 +317,7 @@ static ssize_t noteram_get(FAR uint8_t *buffer, size_t buflen)
/* and return an error */
notelen = -EFBIG;
goto errout_with_csection;
return -EFBIG;
}
/* Loop until the note has been transferred to the user buffer */
@ -349,8 +337,6 @@ static ssize_t noteram_get(FAR uint8_t *buffer, size_t buflen)
g_noteram_info.ni_read = read;
errout_with_csection:
leave_critical_section(flags);
return notelen;
}
@ -373,20 +359,16 @@ errout_with_csection:
static ssize_t noteram_size(void)
{
FAR struct note_common_s *note;
irqstate_t flags;
unsigned int read;
ssize_t notelen;
size_t circlen;
flags = enter_critical_section();
/* Verify that the circular buffer is not empty */
circlen = noteram_unread_length();
if (circlen <= 0)
{
notelen = 0;
goto errout_with_csection;
return 0;
}
/* Get the read index of the circular buffer */
@ -400,8 +382,6 @@ static ssize_t noteram_size(void)
notelen = note->nc_length;
DEBUGASSERT(notelen <= circlen);
errout_with_csection:
leave_critical_section(flags);
return notelen;
}
@ -427,13 +407,14 @@ static ssize_t noteram_read(FAR struct file *filep,
{
ssize_t notelen;
ssize_t retlen ;
irqstate_t flags;
DEBUGASSERT(filep != 0 && buffer != NULL && buflen > 0);
/* Then loop, adding as many notes as possible to the user buffer. */
retlen = 0;
sched_lock();
flags = spin_lock_irqsave_wo_note(&g_noteram_lock);
do
{
/* Get the next note (removing it from the buffer) */
@ -474,7 +455,7 @@ static ssize_t noteram_read(FAR struct file *filep,
}
while (notelen > 0 && notelen <= buflen);
sched_unlock();
spin_unlock_irqrestore_wo_note(&g_noteram_lock, flags);
return retlen;
}
@ -485,6 +466,7 @@ static ssize_t noteram_read(FAR struct file *filep,
static int noteram_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
{
int ret = -ENOSYS;
irqstate_t flags = spin_lock_irqsave_wo_note(&g_noteram_lock);
/* Handle the ioctl commands */
@ -562,6 +544,7 @@ static int noteram_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
break;
}
spin_unlock_irqrestore_wo_note(&g_noteram_lock, flags);
return ret;
}
@ -591,17 +574,11 @@ static void noteram_add(FAR struct note_driver_s *drv,
unsigned int next;
irqstate_t flags;
flags = up_irq_save();
#ifdef CONFIG_SMP
spin_lock_wo_note(&g_noteram_lock);
#endif
flags = spin_lock_irqsave_wo_note(&g_noteram_lock);
if (g_noteram_info.ni_overwrite == NOTERAM_MODE_OVERWRITE_OVERFLOW)
{
#ifdef CONFIG_SMP
spin_unlock_wo_note(&g_noteram_lock);
#endif
up_irq_restore(flags);
spin_unlock_irqrestore_wo_note(&g_noteram_lock, flags);
return;
}
@ -626,11 +603,7 @@ static void noteram_add(FAR struct note_driver_s *drv,
/* Stop recording if not in overwrite mode */
g_noteram_info.ni_overwrite = NOTERAM_MODE_OVERWRITE_OVERFLOW;
#ifdef CONFIG_SMP
spin_unlock_wo_note(&g_noteram_lock);
#endif
up_irq_restore(flags);
spin_unlock_irqrestore_wo_note(&g_noteram_lock, flags);
return;
}
@ -649,10 +622,7 @@ static void noteram_add(FAR struct note_driver_s *drv,
g_noteram_info.ni_head = head;
#ifdef CONFIG_SMP
spin_unlock_wo_note(&g_noteram_lock);
#endif
up_irq_restore(flags);
spin_unlock_irqrestore_wo_note(&g_noteram_lock, flags);
}
/****************************************************************************