From e8842f59da791c0723376793a553f3f2b4d9f267 Mon Sep 17 00:00:00 2001 From: dongjiuzhu1 Date: Thu, 24 Aug 2023 11:43:57 +0800 Subject: [PATCH] libs/libc: remove unnecessary flockfile to improve performance modify lib_wrflush/lib_rdflush/lib_ffulsh to unlocked version Signed-off-by: dongjiuzhu1 --- libs/libc/libc.h | 9 ++--- libs/libc/stdio/CMakeLists.txt | 4 +-- libs/libc/stdio/Make.defs | 2 +- libs/libc/stdio/lib_freopen.c | 10 ++++-- libs/libc/stdio/lib_fseeko.c | 6 +++- libs/libc/stdio/lib_libfflush.c | 33 +++++++++---------- libs/libc/stdio/lib_libfwrite.c | 4 +-- libs/libc/stdio/lib_puts.c | 2 +- .../{lib_rdflush.c => lib_rdflush_unlocked.c} | 12 ++----- .../{lib_wrflush.c => lib_wrflush_unlocked.c} | 8 ++--- 10 files changed, 47 insertions(+), 43 deletions(-) rename libs/libc/stdio/{lib_rdflush.c => lib_rdflush_unlocked.c} (93%) rename libs/libc/stdio/{lib_wrflush.c => lib_wrflush_unlocked.c} (94%) diff --git a/libs/libc/libc.h b/libs/libc/libc.h index e377ed7d18..4330ebf6ef 100644 --- a/libs/libc/libc.h +++ b/libs/libc/libc.h @@ -235,14 +235,15 @@ FAR char *lib_fgets(FAR char *buf, size_t buflen, FILE *stream, /* Defined in lib_libfflush.c */ ssize_t lib_fflush(FAR FILE *stream, bool bforce); +ssize_t lib_fflush_unlocked(FAR FILE *stream, bool bforce); -/* Defined in lib_rdflush.c */ +/* Defined in lib_rdflush_unlocked.c */ -int lib_rdflush(FAR FILE *stream); +int lib_rdflush_unlocked(FAR FILE *stream); -/* Defined in lib_wrflush.c */ +/* Defined in lib_wrflush_unlocked.c */ -int lib_wrflush(FAR FILE *stream); +int lib_wrflush_unlocked(FAR FILE *stream); /* Defined in lib_libgetbase.c */ diff --git a/libs/libc/stdio/CMakeLists.txt b/libs/libc/stdio/CMakeLists.txt index 314ca36e22..6ed1c8b62f 100644 --- a/libs/libc/stdio/CMakeLists.txt +++ b/libs/libc/stdio/CMakeLists.txt @@ -79,8 +79,8 @@ if(CONFIG_FILE_STREAM) lib_fflush.c lib_libflushall.c lib_libfflush.c - lib_rdflush.c - lib_wrflush.c + lib_rdflush_unlocked.c + lib_wrflush_unlocked.c lib_putc.c lib_fputc.c lib_fputs.c diff --git a/libs/libc/stdio/Make.defs b/libs/libc/stdio/Make.defs index 90abc4b5fa..e6a8143a5f 100644 --- a/libs/libc/stdio/Make.defs +++ b/libs/libc/stdio/Make.defs @@ -42,7 +42,7 @@ CSRCS += lib_fseek.c lib_fseeko.c lib_ftell.c lib_ftello.c lib_fsetpos.c CSRCS += lib_getdelim.c lib_fgetpos.c lib_getc.c lib_fgetc.c CSRCS += lib_fgets.c lib_libfgets.c lib_fwrite.c CSRCS += lib_libfwrite.c lib_fflush.c lib_libflushall.c lib_libfflush.c -CSRCS += lib_rdflush.c lib_wrflush.c lib_putc.c lib_fputc.c +CSRCS += lib_rdflush_unlocked.c lib_wrflush_unlocked.c lib_putc.c lib_fputc.c CSRCS += lib_fputs.c lib_ungetc.c lib_fprintf.c lib_vfprintf.c CSRCS += lib_feof.c lib_ferror.c lib_rewind.c lib_clearerr.c CSRCS += lib_scanf.c lib_vscanf.c lib_fscanf.c lib_vfscanf.c lib_tmpfile.c diff --git a/libs/libc/stdio/lib_freopen.c b/libs/libc/stdio/lib_freopen.c index 2958dfdaac..7ca87f4c8a 100644 --- a/libs/libc/stdio/lib_freopen.c +++ b/libs/libc/stdio/lib_freopen.c @@ -100,14 +100,20 @@ FAR FILE *freopen(FAR const char *path, FAR const char *mode, return NULL; } + /* Make sure that we have exclusive access to the stream */ + + flockfile(stream); + /* Flush the stream and invalidate the read buffer. */ - fflush(stream); + lib_fflush_unlocked(stream, true); #ifndef CONFIG_STDIO_DISABLE_BUFFERING - lib_rdflush(stream); + lib_rdflush_unlocked(stream); #endif + funlockfile(stream); + /* Duplicate the new fd to the stream. */ ret = dup2(fd, fileno(stream)); diff --git a/libs/libc/stdio/lib_fseeko.c b/libs/libc/stdio/lib_fseeko.c index 69dc01e295..163a79de38 100644 --- a/libs/libc/stdio/lib_fseeko.c +++ b/libs/libc/stdio/lib_fseeko.c @@ -69,10 +69,14 @@ int fseeko(FAR FILE *stream, off_t offset, int whence) #ifndef CONFIG_STDIO_DISABLE_BUFFERING /* Flush any valid read/write data in the buffer (also verifies stream) */ - if (lib_rdflush(stream) < 0 || lib_wrflush(stream) < 0) + flockfile(stream); + if (lib_rdflush_unlocked(stream) < 0 || lib_wrflush_unlocked(stream) < 0) { + funlockfile(stream); return ERROR; } + + funlockfile(stream); #endif /* On success or failure, discard any characters saved by ungetc() */ diff --git a/libs/libc/stdio/lib_libfflush.c b/libs/libc/stdio/lib_libfflush.c index 60d33e4b26..bdc0250673 100644 --- a/libs/libc/stdio/lib_libfflush.c +++ b/libs/libc/stdio/lib_libfflush.c @@ -56,13 +56,12 @@ * ****************************************************************************/ -ssize_t lib_fflush(FAR FILE *stream, bool bforce) +ssize_t lib_fflush_unlocked(FAR FILE *stream, bool bforce) { #ifndef CONFIG_STDIO_DISABLE_BUFFERING FAR const unsigned char *src; ssize_t bytes_written; ssize_t nbuffer; - int ret; /* Return EBADF if the file is not opened for writing */ @@ -71,18 +70,13 @@ ssize_t lib_fflush(FAR FILE *stream, bool bforce) return -EBADF; } - /* Make sure that we have exclusive access to the stream */ - - flockfile(stream); - /* Check if there is an allocated I/O buffer */ if (stream->fs_bufstart == NULL) { /* No, then there can be nothing remaining in the buffer. */ - ret = 0; - goto errout_with_lock; + return 0; } /* Make sure that the buffer holds valid data */ @@ -99,8 +93,7 @@ ssize_t lib_fflush(FAR FILE *stream, bool bforce) * remaining in the buffer." */ - ret = 0; - goto errout_with_lock; + return 0; } /* How many bytes of write data are used in the buffer now */ @@ -122,8 +115,7 @@ ssize_t lib_fflush(FAR FILE *stream, bool bforce) */ stream->fs_flags |= __FS_FLAG_ERROR; - ret = _NX_GETERRVAL(bytes_written); - goto errout_with_lock; + return _NX_GETERRVAL(bytes_written); } /* Handle partial writes. fflush() must either return with @@ -156,16 +148,23 @@ ssize_t lib_fflush(FAR FILE *stream, bool bforce) * remaining in the buffer. */ - funlockfile(stream); return stream->fs_bufpos - stream->fs_bufstart; -errout_with_lock: - funlockfile(stream); - return ret; - #else /* Return no bytes remaining in the buffer */ return 0; #endif } + +ssize_t lib_fflush(FAR FILE *stream, bool bforce) +{ + ssize_t ret; + + /* Make sure that we have exclusive access to the stream */ + + flockfile(stream); + ret = lib_fflush_unlocked(stream, bforce); + funlockfile(stream); + return ret; +} diff --git a/libs/libc/stdio/lib_libfwrite.c b/libs/libc/stdio/lib_libfwrite.c index 37f9528202..a299378dbf 100644 --- a/libs/libc/stdio/lib_libfwrite.c +++ b/libs/libc/stdio/lib_libfwrite.c @@ -90,7 +90,7 @@ ssize_t lib_fwrite(FAR const void *ptr, size_t count, FAR FILE *stream) * buffered read/write access. */ - if (lib_rdflush(stream) < 0) + if (lib_rdflush_unlocked(stream) < 0) { goto errout_with_lock; } @@ -125,7 +125,7 @@ ssize_t lib_fwrite(FAR const void *ptr, size_t count, FAR FILE *stream) { /* Flush the buffered data to the IO stream */ - int bytes_buffered = lib_fflush(stream, false); + int bytes_buffered = lib_fflush_unlocked(stream, false); if (bytes_buffered < 0) { goto errout_with_lock; diff --git a/libs/libc/stdio/lib_puts.c b/libs/libc/stdio/lib_puts.c index 48bcbaab9f..86498dce5b 100644 --- a/libs/libc/stdio/lib_puts.c +++ b/libs/libc/stdio/lib_puts.c @@ -71,7 +71,7 @@ int puts(FAR const IPTR char *s) if ((stream->fs_flags & __FS_FLAG_LBF) != 0) { - ret = lib_fflush(stream, true); + ret = lib_fflush_unlocked(stream, true); if (ret < 0) { nput = EOF; diff --git a/libs/libc/stdio/lib_rdflush.c b/libs/libc/stdio/lib_rdflush_unlocked.c similarity index 93% rename from libs/libc/stdio/lib_rdflush.c rename to libs/libc/stdio/lib_rdflush_unlocked.c index edbfdeccfc..30d2c6a53b 100644 --- a/libs/libc/stdio/lib_rdflush.c +++ b/libs/libc/stdio/lib_rdflush_unlocked.c @@ -1,5 +1,5 @@ /**************************************************************************** - * libs/libc/stdio/lib_rdflush.c + * libs/libc/stdio/lib_rdflush_unlocked.c * * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with @@ -38,7 +38,7 @@ ****************************************************************************/ /**************************************************************************** - * Name: lib_rdflush + * Name: lib_rdflush_unlocked * * Description: * Flush read data from the I/O buffer and adjust the file pointer to @@ -46,7 +46,7 @@ * ****************************************************************************/ -int lib_rdflush(FAR FILE *stream) +int lib_rdflush_unlocked(FAR FILE *stream) { /* Sanity checking */ @@ -63,10 +63,6 @@ int lib_rdflush(FAR FILE *stream) return OK; } - /* Get exclusive access to the stream */ - - flockfile(stream); - /* If the buffer is currently being used for read access, then discard all * of the read-ahead data. We do not support concurrent buffered read/write * access. @@ -98,12 +94,10 @@ int lib_rdflush(FAR FILE *stream) if (fseek(stream, -rdoffset, SEEK_CUR) < 0) { - funlockfile(stream); return ERROR; } } - funlockfile(stream); return OK; } diff --git a/libs/libc/stdio/lib_wrflush.c b/libs/libc/stdio/lib_wrflush_unlocked.c similarity index 94% rename from libs/libc/stdio/lib_wrflush.c rename to libs/libc/stdio/lib_wrflush_unlocked.c index 99444d1196..7bb9e699ff 100644 --- a/libs/libc/stdio/lib_wrflush.c +++ b/libs/libc/stdio/lib_wrflush_unlocked.c @@ -1,5 +1,5 @@ /**************************************************************************** - * libs/libc/stdio/lib_wrflush.c + * libs/libc/stdio/lib_wrflush_unlocked.c * * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with @@ -35,7 +35,7 @@ ****************************************************************************/ /**************************************************************************** - * Name: lib_wrflush + * Name: lib_wrflush_unlocked * * Description: * This is simply a version of fflush that does not report an error if @@ -43,7 +43,7 @@ * ****************************************************************************/ -int lib_wrflush(FAR FILE *stream) +int lib_wrflush_unlocked(FAR FILE *stream) { #ifndef CONFIG_STDIO_DISABLE_BUFFERING /* Verify that we were passed a valid (i.e., non-NULL) stream */ @@ -82,7 +82,7 @@ int lib_wrflush(FAR FILE *stream) * buffered write data was successfully flushed by lib_fflush(). */ - return lib_fflush(stream, true); + return lib_fflush_unlocked(stream, true); #else /* Verify that we were passed a valid (i.e., non-NULL) stream */