diff options
author | Sebastian Huber <sebastian.huber@embedded-brains.de> | 2022-05-17 18:28:52 +0200 |
---|---|---|
committer | Sebastian Huber <sebastian.huber@embedded-brains.de> | 2022-06-10 20:13:52 +0200 |
commit | b0cb9f85ca3626e0e68fd451c3090d253ceb4300 (patch) | |
tree | db9ff5315fbc1557040d70bda51787ea2fb5a086 /newlib/libc/stdio/findfp.c | |
parent | c4d4439c4208b75454edef0b42d5585de8ac9f47 (diff) | |
download | newlib-b0cb9f85ca3626e0e68fd451c3090d253ceb4300.zip newlib-b0cb9f85ca3626e0e68fd451c3090d253ceb4300.tar.gz newlib-b0cb9f85ca3626e0e68fd451c3090d253ceb4300.tar.bz2 |
Use global stdio streams for all configurations
The _REENT_GLOBAL_STDIO_STREAMS was introduced by commit
668a4c8722090fffd10869dbb15b879651c1370d in 2017. Since then it was enabled by
default for RTEMS. Recently, the option was enabled for Cygwin which
previously used an alternative implementation to use global stdio streams.
In Newlib, the stdio streams are defined to thread-specific pointers
_reent::_stdin, _reent::_stdout and _reent::_stderr. If the option is disabled
(the default for most systems), then these pointers are initialized to
thread-specific FILE objects which use file descriptors 0, 1, and 2,
respectively. There are at least three problems with this:
(1) The thread-specific FILE objects are closed by _reclaim_reent(). This
leads to problems with language run-time libraries that provide wrappers to
the C/POSIX stdio streams (for example C++ and Ada), since they use the
thread-specific FILE objects of the initialization thread. In case the
initialization thread is deleted, then they use freed memory.
(2) Since thread-specific FILE objects are used with a common output device via
file descriptors 0, 1 and 2, the locking at FILE object level cannot ensure
atomicity of the output, e.g. a call to printf().
(3) There are resource managment issues, see:
https://sourceware.org/pipermail/newlib/2022/019558.html
https://bugs.linaro.org/show_bug.cgi?id=5841
This patch enables the _REENT_GLOBAL_STDIO_STREAMS behaviour for all Newlib
configurations and removes the option. This removes a couple of #ifdef blocks.
Diffstat (limited to 'newlib/libc/stdio/findfp.c')
-rw-r--r-- | newlib/libc/stdio/findfp.c | 53 |
1 files changed, 1 insertions, 52 deletions
diff --git a/newlib/libc/stdio/findfp.c b/newlib/libc/stdio/findfp.c index 858c09e..ee991ed 100644 --- a/newlib/libc/stdio/findfp.c +++ b/newlib/libc/stdio/findfp.c @@ -28,21 +28,9 @@ void (*__stdio_exit_handler) (void); -#if defined(_REENT_SMALL) && !defined(_REENT_GLOBAL_STDIO_STREAMS) -const struct __sFILE_fake __sf_fake_stdin = - {_NULL, 0, 0, 0, 0, {_NULL, 0}, 0, _NULL}; -const struct __sFILE_fake __sf_fake_stdout = - {_NULL, 0, 0, 0, 0, {_NULL, 0}, 0, _NULL}; -const struct __sFILE_fake __sf_fake_stderr = - {_NULL, 0, 0, 0, 0, {_NULL, 0}, 0, _NULL}; -#endif - -#ifdef _REENT_GLOBAL_STDIO_STREAMS __FILE __sf[3]; + struct _glue __sglue = {NULL, 3, &__sf[0]}; -#else -struct _glue __sglue = {NULL, 0, NULL}; -#endif #ifdef _STDIO_BSD_SEMANTICS /* BSD and Glibc systems only flush streams which have been written to @@ -161,11 +149,9 @@ global_stdio_init (void) { if (__stdio_exit_handler == NULL) { __stdio_exit_handler = stdio_exit_handler; -#ifdef _REENT_GLOBAL_STDIO_STREAMS stdin_init (&__sf[0]); stdout_init (&__sf[1]); stderr_init (&__sf[2]); -#endif } } @@ -232,16 +218,12 @@ found: static void cleanup_stdio (struct _reent *ptr) { -#ifdef _REENT_GLOBAL_STDIO_STREAMS if (ptr->_stdin != &__sf[0]) CLEANUP_FILE (ptr, ptr->_stdin); if (ptr->_stdout != &__sf[1]) CLEANUP_FILE (ptr, ptr->_stdout); if (ptr->_stderr != &__sf[2]) CLEANUP_FILE (ptr, ptr->_stderr); -#else - (void) _fwalk_sglue (ptr, CLEANUP_FILE, &ptr->__sglue); -#endif } /* @@ -262,22 +244,7 @@ __sinit (struct _reent *s) /* make sure we clean up on exit */ s->__cleanup = cleanup_stdio; /* conservative */ -#ifdef _REENT_SMALL -# ifndef _REENT_GLOBAL_STDIO_STREAMS - s->_stdin = __sfp(s); - s->_stdout = __sfp(s); - s->_stderr = __sfp(s); -# endif /* _REENT_GLOBAL_STDIO_STREAMS */ -#endif - global_stdio_init (); - -#ifndef _REENT_GLOBAL_STDIO_STREAMS - stdin_init (s->_stdin); - stdout_init (s->_stdout); - stderr_init (s->_stderr); -#endif /* _REENT_GLOBAL_STDIO_STREAMS */ - __sfp_lock_release (); } @@ -320,32 +287,14 @@ __fp_unlock (struct _reent * ptr __unused, FILE * fp) void __fp_lock_all (void) { -#ifndef _REENT_GLOBAL_STDIO_STREAMS - struct _reent *ptr; -#endif - __sfp_lock_acquire (); - -#ifndef _REENT_GLOBAL_STDIO_STREAMS - ptr = _REENT; - (void) _fwalk_sglue (ptr, __fp_lock, &ptr->__sglue); -#else (void) _fwalk_sglue (NULL, __fp_lock, &__sglue); -#endif } void __fp_unlock_all (void) { -#ifndef _REENT_GLOBAL_STDIO_STREAMS - struct _reent *ptr; - - ptr = _REENT; - (void) _fwalk_sglue (ptr, __fp_unlock, &ptr->__sglue); -#else (void) _fwalk_sglue (NULL, __fp_unlock, &__sglue); -#endif - __sfp_lock_release (); } #endif |