From 5e60007904a2a7589ee0f391a276dbc3d3ca112e Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Sun, 3 Aug 2014 17:12:56 +0200 Subject: [PATCH] Remove dcloseall() and fcloseall(). --- libc/Makefile | 3 +- libc/dirent/dregister.cpp | 11 +++--- libc/dirent/dunregister.cpp | 9 ++--- libc/include/DIR.h | 7 ++++ libc/include/dirent.h | 9 +++-- libc/include/stdio.h | 1 - libc/stdio/fcloseall.cpp | 35 ------------------- libc/stdio/fregister.cpp | 31 ++-------------- .../dcloseall.cpp => stdio/funregister.cpp} | 30 ++++++++-------- libc/stdlib/exit.cpp | 19 ++++++++-- 10 files changed, 54 insertions(+), 101 deletions(-) delete mode 100644 libc/stdio/fcloseall.cpp rename libc/{dirent/dcloseall.cpp => stdio/funregister.cpp} (64%) diff --git a/libc/Makefile b/libc/Makefile index 88b8a49c..02c04522 100644 --- a/libc/Makefile +++ b/libc/Makefile @@ -37,7 +37,6 @@ dirent/alphasort.o \ dirent/alphasort_r.o \ dirent/closedir.o \ dirent/dclearerr.o \ -dirent/dcloseall.o \ dirent/deof.o \ dirent/derror.o \ dirent/dirfd.o \ @@ -129,6 +128,7 @@ stdio/ftello.o \ stdio/ftello_unlocked.o \ stdio/ftrylockfile.o \ stdio/funlockfile.o \ +stdio/funregister.o \ stdio/fwritable.o \ stdio/fwritable_unlocked.o \ stdio/fwrite.o \ @@ -412,7 +412,6 @@ signal/signal.o \ signal/sigpending.o \ signal/sigprocmask.o \ signal/sigsuspend.o \ -stdio/fcloseall.o \ stdio/fdio_install_fd.o \ stdio/fdio_install_path.o \ stdio/fdio.o \ diff --git a/libc/dirent/dregister.cpp b/libc/dirent/dregister.cpp index 1b4516cf..a16d8195 100644 --- a/libc/dirent/dregister.cpp +++ b/libc/dirent/dregister.cpp @@ -23,17 +23,14 @@ *******************************************************************************/ #include -#include #include -extern "C" pthread_mutex_t __dirname_lock; - extern "C" void dregister(DIR* dir) { - pthread_mutex_lock(&__dirname_lock); + pthread_mutex_lock(&__first_dir_lock); dir->flags |= _DIR_REGISTERED; - if ( (dir->next = __firstdir) ) + if ( (dir->next = __first_dir) ) dir->next->prev = dir; - __firstdir = dir; - pthread_mutex_unlock(&__dirname_lock); + __first_dir = dir; + pthread_mutex_unlock(&__first_dir_lock); } diff --git a/libc/dirent/dunregister.cpp b/libc/dirent/dunregister.cpp index 6a3a7e22..1f037f31 100644 --- a/libc/dirent/dunregister.cpp +++ b/libc/dirent/dunregister.cpp @@ -23,22 +23,19 @@ *******************************************************************************/ #include -#include #include -extern "C" { pthread_mutex_t __dirname_lock = PTHREAD_MUTEX_INITIALIZER; } - extern "C" void dunregister(DIR* dir) { if ( !(dir->flags & _DIR_REGISTERED) ) return; - pthread_mutex_lock(&__dirname_lock); + pthread_mutex_lock(&__first_dir_lock); if ( !dir->prev ) - __firstdir = dir->next; + __first_dir = dir->next; if ( dir->prev ) dir->prev->next = dir->next; if ( dir->next ) dir->next->prev = dir->prev; dir->flags &= ~_DIR_REGISTERED; - pthread_mutex_unlock(&__dirname_lock); + pthread_mutex_unlock(&__first_dir_lock); } diff --git a/libc/include/DIR.h b/libc/include/DIR.h index 0d265f11..065afa85 100644 --- a/libc/include/DIR.h +++ b/libc/include/DIR.h @@ -29,6 +29,8 @@ #include +#include + __BEGIN_DECLS #ifndef __size_t_defined @@ -64,6 +66,11 @@ struct DIR int flags; }; +#if defined(__is_sortix_libc) +extern DIR* __first_dir; +extern __pthread_mutex_t __first_dir_lock; +#endif + __END_DECLS #endif diff --git a/libc/include/dirent.h b/libc/include/dirent.h index 0ab7d654..6c74d067 100644 --- a/libc/include/dirent.h +++ b/libc/include/dirent.h @@ -31,6 +31,10 @@ #include +#if defined(__is_sortix_libc) +#include +#endif + __BEGIN_DECLS #ifndef __dev_t_defined @@ -111,16 +115,11 @@ int versionsort_r(const struct dirent**, const struct dirent**, void*); void dregister(DIR* dir); void dunregister(DIR* dir); DIR* dnewdir(void); -int dcloseall(void); void dclearerr(DIR* dir); int derror(DIR* dir); int deof(DIR* dif); #endif -#if defined(__is_sortix_libc) -extern DIR* __firstdir; -#endif - __END_DECLS #endif diff --git a/libc/include/stdio.h b/libc/include/stdio.h index 75e9bf15..5de67bf1 100644 --- a/libc/include/stdio.h +++ b/libc/include/stdio.h @@ -247,7 +247,6 @@ int vdprintf(int fildes, const char* __restrict format, __gnuc_va_list ap) int asprintf(char** __restrict, const char* __restrict, ...) __attribute__((__format__ (printf, 2, 3))); void clearerr_unlocked(FILE* stream); -int fcloseall(void); int feof_unlocked(FILE* stream); int ferror_unlocked(FILE* stream); int fflush_unlocked(FILE* stream); diff --git a/libc/stdio/fcloseall.cpp b/libc/stdio/fcloseall.cpp deleted file mode 100644 index 8b987b42..00000000 --- a/libc/stdio/fcloseall.cpp +++ /dev/null @@ -1,35 +0,0 @@ -/******************************************************************************* - - Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013. - - This file is part of the Sortix C Library. - - The Sortix C Library is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or (at your - option) any later version. - - The Sortix C Library is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public - License for more details. - - You should have received a copy of the GNU Lesser General Public License - along with the Sortix C Library. If not, see . - - stdio/fcloseall.cpp - Closes and flushes all open registered files. - -*******************************************************************************/ - -#include - -extern "C" int fcloseall(void) -{ - int result = 0; - // We do not lock __first_file_lock here because this function is called on - // process termination and only one thread can call exit(3). - while ( __first_file ) - result |= fclose(__first_file); - return result ? EOF : 0; -} diff --git a/libc/stdio/fregister.cpp b/libc/stdio/fregister.cpp index aed41038..7489bc1d 100644 --- a/libc/stdio/fregister.cpp +++ b/libc/stdio/fregister.cpp @@ -1,6 +1,6 @@ /******************************************************************************* - Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013. + Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013, 2014. This file is part of the Sortix C Library. @@ -18,44 +18,19 @@ along with the Sortix C Library. If not, see . stdio/fregister.cpp - Registers a FILE in the global list of open FILES, which allows the standard - library to close and flush all open files upon program exit. + Registers a FILE in the global list of open FILEs. *******************************************************************************/ #include #include -extern "C" -{ - FILE* __first_file = NULL; - pthread_mutex_t __first_file_lock = PTHREAD_MUTEX_INITIALIZER; -} - extern "C" void fregister(FILE* fp) { pthread_mutex_lock(&__first_file_lock); fp->flags |= _FILE_REGISTERED; - if ( __first_file ) - { - fp->next = __first_file; + if ( (fp->next = __first_file) ) __first_file->prev = fp; - } __first_file = fp; pthread_mutex_unlock(&__first_file_lock); } - -extern "C" void funregister(FILE* fp) -{ - if ( !(fp->flags & _FILE_REGISTERED) ) - return; - pthread_mutex_lock(&__first_file_lock); - if ( !fp->prev ) - __first_file = fp->next; - if ( fp->prev ) - fp->prev->next = fp->next; - if ( fp->next ) - fp->next->prev = fp->prev; - fp->flags &= ~_FILE_REGISTERED; - pthread_mutex_unlock(&__first_file_lock); -} diff --git a/libc/dirent/dcloseall.cpp b/libc/stdio/funregister.cpp similarity index 64% rename from libc/dirent/dcloseall.cpp rename to libc/stdio/funregister.cpp index 0afeeeae..5613d910 100644 --- a/libc/dirent/dcloseall.cpp +++ b/libc/stdio/funregister.cpp @@ -1,6 +1,6 @@ /******************************************************************************* - Copyright(C) Jonas 'Sortie' Termansen 2011, 2014. + Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013, 2014. This file is part of the Sortix C Library. @@ -17,23 +17,25 @@ You should have received a copy of the GNU Lesser General Public License along with the Sortix C Library. If not, see . - dirent/dcloseall.cpp - Closes all registered directory streams. + stdio/funregister.cpp + Unregisters a FILE in the global list of open FILEs. *******************************************************************************/ -#include -#include +#include #include -extern "C" { DIR* __firstdir = NULL; } - -extern "C" int dcloseall(void) +extern "C" void funregister(FILE* fp) { - int result = 0; - // We do not lock __firstdir_lock here because this function is called on - // process termination and only one thread can call exit(3). - while ( __firstdir ) - result |= closedir(__firstdir); - return result ? EOF : 0; + if ( !(fp->flags & _FILE_REGISTERED) ) + return; + pthread_mutex_lock(&__first_file_lock); + if ( !fp->prev ) + __first_file = fp->next; + if ( fp->prev ) + fp->prev->next = fp->next; + if ( fp->next ) + fp->next->prev = fp->prev; + fp->flags &= ~_FILE_REGISTERED; + pthread_mutex_unlock(&__first_file_lock); } diff --git a/libc/stdlib/exit.cpp b/libc/stdlib/exit.cpp index a72a3780..161af10f 100644 --- a/libc/stdlib/exit.cpp +++ b/libc/stdlib/exit.cpp @@ -1,6 +1,6 @@ /******************************************************************************* - Copyright(C) Jonas 'Sortie' Termansen 2011, 2012. + Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2014. This file is part of the Sortix C Library. @@ -23,6 +23,8 @@ *******************************************************************************/ #include +#include +#include #include #include #include @@ -33,6 +35,11 @@ extern "C" { struct exit_handler* __exit_handler_stack = NULL; } static pthread_mutex_t exit_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; static bool currently_exiting = false; +extern "C" { DIR* __first_dir = NULL; } +extern "C" { pthread_mutex_t __first_dir_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; } +extern "C" { FILE* __first_file = NULL; } +extern "C" { pthread_mutex_t __first_file_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; } + extern "C" void exit(int status) { // It's undefined behavior to call this function more than once: If more @@ -51,7 +58,13 @@ extern "C" void exit(int status) __exit_handler_stack = __exit_handler_stack->next; } - dcloseall(); - fcloseall(); + pthread_mutex_lock(&__first_dir_lock); + pthread_mutex_lock(&__first_file_lock); + + while ( __first_dir ) + closedir(__first_dir); + while ( __first_file ) + fclose(__first_file); + _Exit(status); }