[PATCH] Introduce _REENT_GLOBAL_STDIO_STREAMS
Sebastian Huber
sebastian.huber at embedded-brains.de
Thu Jun 22 07:48:39 UTC 2017
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. Use this option for RTEMS.
Signed-off-by: Sebastian Huber <sebastian.huber at embedded-brains.de>
---
newlib/libc/include/sys/config.h | 1 +
newlib/libc/include/sys/reent.h | 21 +++++++++++++++------
newlib/libc/stdio/findfp.c | 6 ++++++
3 files changed, 22 insertions(+), 6 deletions(-)
diff --git a/newlib/libc/include/sys/config.h b/newlib/libc/include/sys/config.h
index 555239f8b..ae8caff7b 100644
--- a/newlib/libc/include/sys/config.h
+++ b/newlib/libc/include/sys/config.h
@@ -238,6 +238,7 @@
#define _READ_WRITE_RETURN_TYPE _ssize_t
#define __DYNAMIC_REENT__
#define _REENT_GLOBAL_ATEXIT
+#define _REENT_GLOBAL_STDIO_STREAMS
#endif
#ifndef __EXPORT
diff --git a/newlib/libc/include/sys/reent.h b/newlib/libc/include/sys/reent.h
index 8b67889ac..2a79ccc71 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 83d3dc558..886ca7cc6 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
@@ -221,8 +225,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;
--
2.12.3
More information about the devel
mailing list