lib_pathbuffer.c:Use atomic instead of locks
Summary: Use atomic_cmpxchg to ensure that in multithreaded situations, if someone releases the buffer, it can be applied for in time. And use atomic_ulong to save free_bitmap Signed-off-by: chenrun1 <chenrun1@xiaomi.com>
This commit is contained in:
parent
4c379ecc98
commit
9138e8af01
1 changed files with 21 additions and 26 deletions
|
@ -25,7 +25,7 @@
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
#include <nuttx/config.h>
|
#include <nuttx/config.h>
|
||||||
#include <nuttx/spinlock.h>
|
#include <nuttx/atomic.h>
|
||||||
#include <nuttx/lib/lib.h>
|
#include <nuttx/lib/lib.h>
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
@ -41,8 +41,7 @@
|
||||||
|
|
||||||
struct pathbuffer_s
|
struct pathbuffer_s
|
||||||
{
|
{
|
||||||
spinlock_t lock; /* Lock for the buffer */
|
atomic_t free_bitmap; /* Bitmap of free buffer */
|
||||||
unsigned long free_bitmap; /* Bitmap of free buffer */
|
|
||||||
char buffer[CONFIG_LIBC_PATHBUFFER_MAX][PATH_MAX];
|
char buffer[CONFIG_LIBC_PATHBUFFER_MAX][PATH_MAX];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -52,7 +51,6 @@ struct pathbuffer_s
|
||||||
|
|
||||||
static struct pathbuffer_s g_pathbuffer =
|
static struct pathbuffer_s g_pathbuffer =
|
||||||
{
|
{
|
||||||
SP_UNLOCKED,
|
|
||||||
(1u << CONFIG_LIBC_PATHBUFFER_MAX) - 1,
|
(1u << CONFIG_LIBC_PATHBUFFER_MAX) - 1,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -82,21 +80,23 @@ static struct pathbuffer_s g_pathbuffer =
|
||||||
|
|
||||||
FAR char *lib_get_pathbuffer(void)
|
FAR char *lib_get_pathbuffer(void)
|
||||||
{
|
{
|
||||||
irqstate_t flags;
|
for (; ; )
|
||||||
int index;
|
|
||||||
|
|
||||||
/* Try to find a free buffer */
|
|
||||||
|
|
||||||
flags = spin_lock_irqsave(&g_pathbuffer.lock);
|
|
||||||
index = ffsl(g_pathbuffer.free_bitmap) - 1;
|
|
||||||
if (index >= 0 && index < CONFIG_LIBC_PATHBUFFER_MAX)
|
|
||||||
{
|
{
|
||||||
g_pathbuffer.free_bitmap &= ~(1u << index);
|
int32_t update;
|
||||||
spin_unlock_irqrestore(&g_pathbuffer.lock, flags);
|
int32_t free_bitmap = atomic_read(&g_pathbuffer.free_bitmap);
|
||||||
return g_pathbuffer.buffer[index];
|
int index = ffsl(free_bitmap) - 1;
|
||||||
}
|
if (index < 0 || index >= CONFIG_LIBC_PATHBUFFER_MAX)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
spin_unlock_irqrestore(&g_pathbuffer.lock, flags);
|
update = free_bitmap & ~(1u << index);
|
||||||
|
if (atomic_cmpxchg(&g_pathbuffer.free_bitmap, &free_bitmap,
|
||||||
|
update))
|
||||||
|
{
|
||||||
|
return g_pathbuffer.buffer[index];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* If no free buffer is found, allocate a new one if
|
/* If no free buffer is found, allocate a new one if
|
||||||
* CONFIG_LIBC_PATHBUFFER_MALLOC is enabled
|
* CONFIG_LIBC_PATHBUFFER_MALLOC is enabled
|
||||||
|
@ -125,17 +125,12 @@ FAR char *lib_get_pathbuffer(void)
|
||||||
|
|
||||||
void lib_put_pathbuffer(FAR char *buffer)
|
void lib_put_pathbuffer(FAR char *buffer)
|
||||||
{
|
{
|
||||||
irqstate_t flags;
|
int index = (buffer - &g_pathbuffer.buffer[0][0]) / PATH_MAX;
|
||||||
int index;
|
|
||||||
|
|
||||||
index = (buffer - &g_pathbuffer.buffer[0][0]) / PATH_MAX;
|
|
||||||
if (index >= 0 && index < CONFIG_LIBC_PATHBUFFER_MAX)
|
if (index >= 0 && index < CONFIG_LIBC_PATHBUFFER_MAX)
|
||||||
{
|
{
|
||||||
/* Mark the corresponding bit as free */
|
DEBUGASSERT((atomic_read(&g_pathbuffer.free_bitmap) &
|
||||||
|
(1u << index)) == 0);
|
||||||
flags = spin_lock_irqsave(&g_pathbuffer.lock);
|
atomic_fetch_or_acquire(&g_pathbuffer.free_bitmap, 1u << index);
|
||||||
g_pathbuffer.free_bitmap |= 1u << index;
|
|
||||||
spin_unlock_irqrestore(&g_pathbuffer.lock, flags);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue