diff options
author | Sebastian Huber <sebastian.huber@embedded-brains.de> | 2017-06-22 10:22:47 +0300 |
---|---|---|
committer | Sebastian Huber <sebastian.huber@embedded-brains.de> | 2017-06-30 08:45:16 +0300 |
commit | 668a4c8722090fffd10869dbb15b879651c1370d (patch) | |
tree | a8a2bc692d02981e0708c69bccfd4e1b828bc9bb /newlib/libc/stdio | |
parent | 79cc9cb8f30522a4bdf73362803aff5831b73512 (diff) |
Introduce _REENT_GLOBAL_STDIO_STREAMS
In Newlib, the stdio streams are defined to thread-specific pointers
_reent::_stdin, _reent::_stdout and _reent::_stderr. In case
_REENT_SMALL is not defined, then these pointers are initialized via
_REENT_INIT_PTR() or _REENT_INIT_PTR_ZEROED() to thread-specific FILE
objects provided via _reent::__sf[3]. There are two problems with this
(at least in case of RTEMS).
(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 (e.g. 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().
Introduce a new Newlib configuration option _REENT_GLOBAL_STDIO_STREAMS
to enable the use of global stdio FILE objects.
As a side-effect this reduces the size of struct _reent by more than
50%.
The _REENT_GLOBAL_STDIO_STREAMS should not be used without
_STDIO_CLOSE_PER_REENT_STD_STREAMS.
Signed-off-by: Sebastian Huber <sebastian.huber@embedded-brains.de>
Diffstat (limited to 'newlib/libc/stdio')
-rw-r--r-- | newlib/libc/stdio/findfp.c | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/newlib/libc/stdio/findfp.c b/newlib/libc/stdio/findfp.c index b40aa9240..737bde102 100644 --- a/newlib/libc/stdio/findfp.c +++ b/newlib/libc/stdio/findfp.c @@ -35,6 +35,10 @@ 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]; +#endif + #if (defined (__OPTIMIZE_SIZE__) || defined (PREFER_SIZE_OVER_SPEED)) _NOINLINE_STATIC _VOID #else @@ -218,6 +222,14 @@ _DEFUN(_cleanup_r, (ptr), cleanup_func = _fclose_r; #endif #endif +#ifdef _REENT_GLOBAL_STDIO_STREAMS + if (ptr->_stdin != &__sf[0]) + (*cleanup_func) (ptr, ptr->_stdin); + if (ptr->_stdout != &__sf[1]) + (*cleanup_func) (ptr, ptr->_stdout); + if (ptr->_stderr != &__sf[2]) + (*cleanup_func) (ptr, ptr->_stderr); +#endif _CAST_VOID _fwalk_reent (ptr, cleanup_func); } @@ -250,8 +262,10 @@ _DEFUN(__sinit, (s), s->__sglue._next = NULL; #ifndef _REENT_SMALL +# ifndef _REENT_GLOBAL_STDIO_STREAMS s->__sglue._niobs = 3; s->__sglue._iobs = &s->__sf[0]; +# endif #else s->__sglue._niobs = 0; s->__sglue._iobs = NULL; @@ -265,9 +279,19 @@ _DEFUN(__sinit, (s), s->_stderr = __sfp(s); #endif +#ifdef _REENT_GLOBAL_STDIO_STREAMS + if (__sf[0]._cookie == NULL) { + _GLOBAL_REENT->__sglue._niobs = 3; + _GLOBAL_REENT->__sglue._iobs = &__sf[0]; + stdin_init (&__sf[0]); + stdout_init (&__sf[1]); + stderr_init (&__sf[2]); + } +#else stdin_init (s->_stdin); stdout_init (s->_stdout); stderr_init (s->_stderr); +#endif s->__sdidinit = 1; |