From 668a4c8722090fffd10869dbb15b879651c1370d Mon Sep 17 00:00:00 2001 From: Sebastian Huber Date: Thu, 22 Jun 2017 09:22:47 +0200 Subject: [PATCH] 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 --- newlib/libc/include/sys/reent.h | 21 +++++++++++++++------ newlib/libc/stdio/findfp.c | 24 ++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/newlib/libc/include/sys/reent.h b/newlib/libc/include/sys/reent.h index 8b67889ac..c045ca549 100644 --- a/newlib/libc/include/sys/reent.h +++ b/newlib/libc/include/sys/reent.h @@ -644,14 +644,23 @@ struct _reent of the above members (on the off chance that future binary compatibility would be broken otherwise). */ struct _glue __sglue; /* root of glue chain */ +# ifndef _REENT_GLOBAL_STDIO_STREAMS __FILE __sf[3]; /* first three file descriptors */ +# endif }; +#ifdef _REENT_GLOBAL_STDIO_STREAMS +extern __FILE __sf[3]; +#define _REENT_STDIO_STREAM(var, index) &__sf[index] +#else +#define _REENT_STDIO_STREAM(var, index) &(var)->__sf[index] +#endif + #define _REENT_INIT(var) \ { 0, \ - &(var).__sf[0], \ - &(var).__sf[1], \ - &(var).__sf[2], \ + _REENT_STDIO_STREAM(&(var), 0), \ + _REENT_STDIO_STREAM(&(var), 1), \ + _REENT_STDIO_STREAM(&(var), 2), \ 0, \ "", \ 0, \ @@ -696,9 +705,9 @@ struct _reent } #define _REENT_INIT_PTR_ZEROED(var) \ - { (var)->_stdin = &(var)->__sf[0]; \ - (var)->_stdout = &(var)->__sf[1]; \ - (var)->_stderr = &(var)->__sf[2]; \ + { (var)->_stdin = _REENT_STDIO_STREAM(var, 0); \ + (var)->_stdout = _REENT_STDIO_STREAM(var, 1); \ + (var)->_stderr = _REENT_STDIO_STREAM(var, 2); \ (var)->_new._reent._rand_next = 1; \ (var)->_new._reent._r48._seed[0] = _RAND48_SEED_0; \ (var)->_new._reent._r48._seed[1] = _RAND48_SEED_1; \ 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 @@ -217,6 +221,14 @@ _DEFUN(_cleanup_r, (ptr), #else 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; -- 2.43.5