stdio/file: unify group file list to common single queue

unify group file list to common single queue

Signed-off-by: chao an <anchao@lixiang.com>
This commit is contained in:
chao an 2024-03-15 15:34:30 +08:00 committed by Xiang Xiao
parent 8e0bf9a0b4
commit d4d49e9645
6 changed files with 20 additions and 44 deletions

View File

@ -40,6 +40,7 @@
#include <nuttx/spinlock.h> #include <nuttx/spinlock.h>
#include <nuttx/mm/map.h> #include <nuttx/mm/map.h>
#include <nuttx/spawn.h> #include <nuttx/spawn.h>
#include <nuttx/queue.h>
/**************************************************************************** /****************************************************************************
* Pre-processor Definitions * Pre-processor Definitions
@ -538,7 +539,7 @@ struct filelist
#ifdef CONFIG_FILE_STREAM #ifdef CONFIG_FILE_STREAM
struct file_struct struct file_struct
{ {
FAR struct file_struct *fs_next; /* Pointer to next file stream */ sq_entry_t fs_entry; /* Entry of file stream */
rmutex_t fs_lock; /* Recursive lock */ rmutex_t fs_lock; /* Recursive lock */
cookie_io_functions_t fs_iofunc; /* Callbacks to user / system functions */ cookie_io_functions_t fs_iofunc; /* Callbacks to user / system functions */
FAR void *fs_cookie; /* Pointer to file descriptor / cookie struct */ FAR void *fs_cookie; /* Pointer to file descriptor / cookie struct */
@ -563,8 +564,7 @@ struct streamlist
{ {
mutex_t sl_lock; /* For thread safety */ mutex_t sl_lock; /* For thread safety */
struct file_struct sl_std[3]; struct file_struct sl_std[3];
FAR struct file_struct *sl_head; sq_queue_t sl_queue;
FAR struct file_struct *sl_tail;
}; };
#endif /* CONFIG_FILE_STREAM */ #endif /* CONFIG_FILE_STREAM */

View File

@ -59,8 +59,6 @@
int fclose(FAR FILE *stream) int fclose(FAR FILE *stream)
{ {
FAR struct streamlist *slist; FAR struct streamlist *slist;
FAR FILE *prev = NULL;
FAR FILE *next;
int errcode = EINVAL; int errcode = EINVAL;
int ret = ERROR; int ret = ERROR;
int status; int status;
@ -91,27 +89,7 @@ int fclose(FAR FILE *stream)
slist = lib_get_streams(); slist = lib_get_streams();
nxmutex_lock(&slist->sl_lock); nxmutex_lock(&slist->sl_lock);
for (next = slist->sl_head; next; prev = next, next = next->fs_next) sq_rem(&stream->fs_entry, &slist->sl_queue);
{
if (next == stream)
{
if (next == slist->sl_head)
{
slist->sl_head = next->fs_next;
}
else
{
prev->fs_next = next->fs_next;
}
if (next == slist->sl_tail)
{
slist->sl_tail = prev;
}
break;
}
}
nxmutex_unlock(&slist->sl_lock); nxmutex_unlock(&slist->sl_lock);

View File

@ -98,16 +98,7 @@ FAR FILE *fdopen(int fd, FAR const char *mode)
goto errout; goto errout;
} }
if (list->sl_tail) sq_addlast(&filep->fs_entry, &list->sl_queue);
{
list->sl_tail->fs_next = filep;
list->sl_tail = filep;
}
else
{
list->sl_head = filep;
list->sl_tail = filep;
}
nxmutex_unlock(&list->sl_lock); nxmutex_unlock(&list->sl_lock);

View File

@ -28,6 +28,7 @@
#include <fcntl.h> #include <fcntl.h>
#include <errno.h> #include <errno.h>
#include <nuttx/nuttx.h>
#include <nuttx/fs/fs.h> #include <nuttx/fs/fs.h>
#include "libc.h" #include "libc.h"
@ -47,6 +48,7 @@
int lib_flushall_unlocked(FAR struct streamlist *list) int lib_flushall_unlocked(FAR struct streamlist *list)
{ {
FAR sq_entry_t *entry;
int lasterrno = OK; int lasterrno = OK;
int ret; int ret;
@ -64,8 +66,10 @@ int lib_flushall_unlocked(FAR struct streamlist *list)
lib_fflush_unlocked(&list->sl_std[i]); lib_fflush_unlocked(&list->sl_std[i]);
} }
for (stream = list->sl_head; stream != NULL; stream = stream->fs_next) sq_for_every(&list->sl_queue, entry)
{ {
stream = container_of(entry, struct file_struct, fs_entry);
/* If the stream is opened for writing, then flush all of /* If the stream is opened for writing, then flush all of
* the pending write data in the stream. * the pending write data in the stream.
*/ */
@ -95,6 +99,7 @@ int lib_flushall_unlocked(FAR struct streamlist *list)
int lib_flushall(FAR struct streamlist *list) int lib_flushall(FAR struct streamlist *list)
{ {
FAR sq_entry_t *entry;
int lasterrno = OK; int lasterrno = OK;
int ret; int ret;
@ -114,8 +119,10 @@ int lib_flushall(FAR struct streamlist *list)
lib_fflush(&list->sl_std[i]); lib_fflush(&list->sl_std[i]);
} }
for (stream = list->sl_head; stream != NULL; stream = stream->fs_next) sq_for_every(&list->sl_queue, entry)
{ {
stream = container_of(entry, struct file_struct, fs_entry);
/* If the stream is opened for writing, then flush all of /* If the stream is opened for writing, then flush all of
* the pending write data in the stream. * the pending write data in the stream.
*/ */

View File

@ -52,8 +52,7 @@ static void task_init_stream(FAR struct streamlist *list)
/* Initialize the list access mutex */ /* Initialize the list access mutex */
nxmutex_init(&list->sl_lock); nxmutex_init(&list->sl_lock);
list->sl_head = NULL; sq_init(&list->sl_queue);
list->sl_tail = NULL;
/* Initialize stdin, stdout and stderr stream */ /* Initialize stdin, stdout and stderr stream */

View File

@ -22,6 +22,7 @@
* Included Files * Included Files
****************************************************************************/ ****************************************************************************/
#include <nuttx/nuttx.h>
#include <nuttx/kmalloc.h> #include <nuttx/kmalloc.h>
#include <nuttx/mutex.h> #include <nuttx/mutex.h>
#include <nuttx/list.h> #include <nuttx/list.h>
@ -47,6 +48,8 @@ static void task_uninit_stream(FAR struct task_group_s *group)
{ {
FAR struct streamlist *list; FAR struct streamlist *list;
FAR struct file_struct *stream; FAR struct file_struct *stream;
FAR sq_entry_t *curr;
FAR sq_entry_t *next;
DEBUGASSERT(group && group->tg_info); DEBUGASSERT(group && group->tg_info);
list = &group->tg_info->ta_streamlist; list = &group->tg_info->ta_streamlist;
@ -66,11 +69,9 @@ static void task_uninit_stream(FAR struct task_group_s *group)
/* Release each stream in the list */ /* Release each stream in the list */
list->sl_tail = NULL; sq_for_every_safe(&list->sl_queue, curr, next)
while (list->sl_head != NULL)
{ {
stream = list->sl_head; stream = container_of(curr, struct file_struct, fs_entry);
list->sl_head = stream->fs_next;
#ifndef CONFIG_STDIO_DISABLE_BUFFERING #ifndef CONFIG_STDIO_DISABLE_BUFFERING
/* Destroy the mutex that protects the IO buffer */ /* Destroy the mutex that protects the IO buffer */