libs/libc: remove unnecessary flockfile to improve performance

modify lib_wrflush/lib_rdflush/lib_ffulsh to unlocked version

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
This commit is contained in:
dongjiuzhu1 2023-08-24 11:43:57 +08:00 committed by Petro Karashchenko
parent aa9f15d635
commit e8842f59da
10 changed files with 47 additions and 43 deletions

View File

@ -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 */

View File

@ -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

View File

@ -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

View File

@ -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));

View File

@ -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() */

View File

@ -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;
}

View File

@ -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;

View File

@ -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;

View File

@ -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;
}

View File

@ -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 */