Skip to content

Commit dba77ff

Browse files
crafcat7xiaoxiang781216
authored andcommitted
fslock:Optimize the performance overhead caused by frequent close
Summary: Indicate whether the file is currently locked by adding a new field locked to filep. 0 - Unlocked 1 - Locked The status of the filep at close is used to determine whether to continue with the following procedure. Optimizing performance: Before Time taken to close the file: 33984 nsec After Time taken to close the file: 23744 nsec Improvement of about 10 msec Signed-off-by: chenrun1 <[email protected]>
1 parent c528244 commit dba77ff

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

fs/vfs/fs_lock.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <unistd.h>
3131
#include <sys/stat.h>
3232

33+
#include <nuttx/fs/fs.h>
3334
#include <nuttx/lib/lib.h>
3435
#include <nuttx/kmalloc.h>
3536
#include <nuttx/mutex.h>
@@ -715,6 +716,10 @@ int file_setlk(FAR struct file *filep, FAR struct flock *flock,
715716
goto out;
716717
}
717718

719+
/* Update filep lock state */
720+
721+
filep->locked = true;
722+
718723
/* When there is a lock change, we need to wake up the blocking lock */
719724

720725
if (bucket->nwaiter > 0)
@@ -751,6 +756,11 @@ void file_closelk(FAR struct file *filep)
751756
bool deleted = false;
752757
int ret;
753758

759+
if (filep->locked == false)
760+
{
761+
return;
762+
}
763+
754764
path = lib_get_pathbuffer();
755765
if (path == NULL)
756766
{
@@ -784,6 +794,7 @@ void file_closelk(FAR struct file *filep)
784794
{
785795
deleted = true;
786796
file_lock_delete(file_lock);
797+
filep->locked = false;
787798
}
788799
}
789800

include/nuttx/fs/fs.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,10 @@ struct file
480480
#if CONFIG_FS_BACKTRACE > 0
481481
FAR void *f_backtrace[CONFIG_FS_BACKTRACE]; /* Backtrace to while file opens */
482482
#endif
483+
484+
#if CONFIG_FS_LOCK_BUCKET_SIZE > 0
485+
bool locked; /* Filelock state: false - unlocked, true - locked */
486+
#endif
483487
};
484488

485489
/* This defines a two layer array of files indexed by the file descriptor.

0 commit comments

Comments
 (0)