fs: use small lock to protect filelist

Signed-off-by: hujun5 <hujun5@xiaomi.com>
This commit is contained in:
hujun5 2024-11-29 21:50:04 +08:00 committed by Xiang Xiao
parent fe2af95222
commit 16f39fbb56
2 changed files with 12 additions and 9 deletions

View file

@ -72,7 +72,7 @@ static FAR struct file *files_fget_by_index(FAR struct filelist *list,
FAR struct file *filep;
irqstate_t flags;
flags = spin_lock_irqsave(NULL);
flags = spin_lock_irqsave(&list->fl_lock);
filep = &list->fl_files[l1][l2];
#ifdef CONFIG_FS_REFCOUNT
@ -111,7 +111,7 @@ static FAR struct file *files_fget_by_index(FAR struct filelist *list,
}
#endif
spin_unlock_irqrestore(NULL, flags);
spin_unlock_irqrestore(&list->fl_lock, flags);
return filep;
}
@ -165,7 +165,7 @@ static int files_extend(FAR struct filelist *list, size_t row)
}
while (++i < row);
flags = spin_lock_irqsave(NULL);
flags = spin_lock_irqsave(&list->fl_lock);
/* To avoid race condition, if the file list is updated by other threads
* and list rows is greater or equal than temp list,
@ -174,7 +174,7 @@ static int files_extend(FAR struct filelist *list, size_t row)
if (orig_rows != list->fl_rows && list->fl_rows >= row)
{
spin_unlock_irqrestore(NULL, flags);
spin_unlock_irqrestore(&list->fl_lock, flags);
for (j = orig_rows; j < i; j++)
{
@ -196,7 +196,7 @@ static int files_extend(FAR struct filelist *list, size_t row)
list->fl_files = files;
list->fl_rows = row;
spin_unlock_irqrestore(NULL, flags);
spin_unlock_irqrestore(&list->fl_lock, flags);
if (tmp != NULL && tmp != &list->fl_prefile)
{
@ -371,6 +371,7 @@ void files_initlist(FAR struct filelist *list)
list->fl_crefs = 1;
list->fl_files = &list->fl_prefile;
list->fl_prefile = list->fl_prefiles;
spin_lock_init(&list->fl_lock);
}
/****************************************************************************
@ -589,13 +590,13 @@ int file_allocate_from_tcb(FAR struct tcb_s *tcb, FAR struct inode *inode,
/* Find free file */
flags = spin_lock_irqsave(NULL);
flags = spin_lock_irqsave(&list->fl_lock);
for (; ; i++, j = 0)
{
if (i >= list->fl_rows)
{
spin_unlock_irqrestore(NULL, flags);
spin_unlock_irqrestore(&list->fl_lock, flags);
ret = files_extend(list, i + 1);
if (ret < 0)
@ -603,7 +604,7 @@ int file_allocate_from_tcb(FAR struct tcb_s *tcb, FAR struct inode *inode,
return ret;
}
flags = spin_lock_irqsave(NULL);
flags = spin_lock_irqsave(&list->fl_lock);
}
do
@ -632,7 +633,7 @@ int file_allocate_from_tcb(FAR struct tcb_s *tcb, FAR struct inode *inode,
}
found:
spin_unlock_irqrestore(NULL, flags);
spin_unlock_irqrestore(&list->fl_lock, flags);
if (addref)
{

View file

@ -49,6 +49,7 @@
#include <nuttx/spawn.h>
#include <nuttx/queue.h>
#include <nuttx/irq.h>
#include <nuttx/spinlock_type.h>
/****************************************************************************
* Pre-processor Definitions
@ -491,6 +492,7 @@ struct file
struct filelist
{
spinlock_t fl_lock; /* Manage access to the file list */
uint8_t fl_rows; /* The number of rows of fl_files array */
uint8_t fl_crefs; /* The references to filelist */
FAR struct file **fl_files; /* The pointer of two layer file descriptors array */