Skip to content

Commit

Permalink
fs: use small lock to protect filelist
Browse files Browse the repository at this point in the history
Signed-off-by: hujun5 <[email protected]>
  • Loading branch information
hujun260 authored and xiaoxiang781216 committed Nov 29, 2024
1 parent fe2af95 commit 16f39fb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
19 changes: 10 additions & 9 deletions fs/inode/fs_files.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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,
Expand All @@ -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++)
{
Expand All @@ -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)
{
Expand Down Expand Up @@ -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);
}

/****************************************************************************
Expand Down Expand Up @@ -589,21 +590,21 @@ 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)
{
return ret;
}

flags = spin_lock_irqsave(NULL);
flags = spin_lock_irqsave(&list->fl_lock);
}

do
Expand Down Expand Up @@ -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)
{
Expand Down
2 changes: 2 additions & 0 deletions include/nuttx/fs/fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
#include <nuttx/spawn.h>
#include <nuttx/queue.h>
#include <nuttx/irq.h>
#include <nuttx/spinlock_type.h>

/****************************************************************************
* Pre-processor Definitions
Expand Down Expand Up @@ -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 */
Expand Down

0 comments on commit 16f39fb

Please sign in to comment.