diff --git a/include/nuttx/fs/fs.h b/include/nuttx/fs/fs.h index 3816e1501cf..067686dc600 100644 --- a/include/nuttx/fs/fs.h +++ b/include/nuttx/fs/fs.h @@ -40,6 +40,7 @@ #include #include #include +#include /**************************************************************************** * Pre-processor Definitions @@ -538,7 +539,7 @@ struct filelist #ifdef CONFIG_FILE_STREAM 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 */ cookie_io_functions_t fs_iofunc; /* Callbacks to user / system functions */ FAR void *fs_cookie; /* Pointer to file descriptor / cookie struct */ @@ -563,8 +564,7 @@ struct streamlist { mutex_t sl_lock; /* For thread safety */ struct file_struct sl_std[3]; - FAR struct file_struct *sl_head; - FAR struct file_struct *sl_tail; + sq_queue_t sl_queue; }; #endif /* CONFIG_FILE_STREAM */ diff --git a/libs/libc/stdio/lib_fclose.c b/libs/libc/stdio/lib_fclose.c index 174238c45f6..98b28ed417b 100644 --- a/libs/libc/stdio/lib_fclose.c +++ b/libs/libc/stdio/lib_fclose.c @@ -59,8 +59,6 @@ int fclose(FAR FILE *stream) { FAR struct streamlist *slist; - FAR FILE *prev = NULL; - FAR FILE *next; int errcode = EINVAL; int ret = ERROR; int status; @@ -91,27 +89,7 @@ int fclose(FAR FILE *stream) slist = lib_get_streams(); nxmutex_lock(&slist->sl_lock); - for (next = slist->sl_head; next; prev = next, next = next->fs_next) - { - 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; - } - } + sq_rem(&stream->fs_entry, &slist->sl_queue); nxmutex_unlock(&slist->sl_lock); diff --git a/libs/libc/stdio/lib_fopen.c b/libs/libc/stdio/lib_fopen.c index 50293282dbe..c0283feb56f 100644 --- a/libs/libc/stdio/lib_fopen.c +++ b/libs/libc/stdio/lib_fopen.c @@ -98,16 +98,7 @@ FAR FILE *fdopen(int fd, FAR const char *mode) goto errout; } - if (list->sl_tail) - { - list->sl_tail->fs_next = filep; - list->sl_tail = filep; - } - else - { - list->sl_head = filep; - list->sl_tail = filep; - } + sq_addlast(&filep->fs_entry, &list->sl_queue); nxmutex_unlock(&list->sl_lock); diff --git a/libs/libc/stdio/lib_libflushall.c b/libs/libc/stdio/lib_libflushall.c index 2c4f37c592c..f8b60b27c75 100644 --- a/libs/libc/stdio/lib_libflushall.c +++ b/libs/libc/stdio/lib_libflushall.c @@ -28,6 +28,7 @@ #include #include +#include #include #include "libc.h" @@ -47,6 +48,7 @@ int lib_flushall_unlocked(FAR struct streamlist *list) { + FAR sq_entry_t *entry; int lasterrno = OK; int ret; @@ -64,8 +66,10 @@ int lib_flushall_unlocked(FAR struct streamlist *list) 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 * 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) { + FAR sq_entry_t *entry; int lasterrno = OK; int ret; @@ -114,8 +119,10 @@ int lib_flushall(FAR struct streamlist *list) 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 * the pending write data in the stream. */ diff --git a/sched/tls/task_initinfo.c b/sched/tls/task_initinfo.c index adbe84c185d..7818e8f0390 100644 --- a/sched/tls/task_initinfo.c +++ b/sched/tls/task_initinfo.c @@ -52,8 +52,7 @@ static void task_init_stream(FAR struct streamlist *list) /* Initialize the list access mutex */ nxmutex_init(&list->sl_lock); - list->sl_head = NULL; - list->sl_tail = NULL; + sq_init(&list->sl_queue); /* Initialize stdin, stdout and stderr stream */ diff --git a/sched/tls/task_uninitinfo.c b/sched/tls/task_uninitinfo.c index 4bd35b047cd..f2e56c537a5 100644 --- a/sched/tls/task_uninitinfo.c +++ b/sched/tls/task_uninitinfo.c @@ -22,6 +22,7 @@ * Included Files ****************************************************************************/ +#include #include #include #include @@ -47,6 +48,8 @@ static void task_uninit_stream(FAR struct task_group_s *group) { FAR struct streamlist *list; FAR struct file_struct *stream; + FAR sq_entry_t *curr; + FAR sq_entry_t *next; DEBUGASSERT(group && group->tg_info); 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 */ - list->sl_tail = NULL; - while (list->sl_head != NULL) + sq_for_every_safe(&list->sl_queue, curr, next) { - stream = list->sl_head; - list->sl_head = stream->fs_next; + stream = container_of(curr, struct file_struct, fs_entry); #ifndef CONFIG_STDIO_DISABLE_BUFFERING /* Destroy the mutex that protects the IO buffer */