From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 129691 invoked by alias); 29 Jun 2017 12:25:49 -0000 Mailing-List: contact newlib-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: newlib-owner@sourceware.org Received: (qmail 129638 invoked by uid 89); 29 Jun 2017 12:25:48 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-24.6 required=5.0 tests=AWL,BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,RCVD_IN_DNSWL_NONE,SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: dedi548.your-server.de Received: from dedi548.your-server.de (HELO dedi548.your-server.de) (85.10.215.148) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 29 Jun 2017 12:25:46 +0000 Received: from [88.198.220.130] (helo=sslproxy01.your-server.de) by dedi548.your-server.de with esmtpsa (TLSv1.2:DHE-RSA-AES256-GCM-SHA384:256) (Exim 4.85_2) (envelope-from ) id 1dQYQX-0008Jw-HK for newlib@sourceware.org; Thu, 29 Jun 2017 14:20:05 +0200 Received: from [82.135.62.35] (helo=mail.embedded-brains.de) by sslproxy01.your-server.de with esmtpsa (TLSv1.2:DHE-RSA-AES256-GCM-SHA384:256) (Exim 4.84_2) (envelope-from ) id 1dQYQW-0002Iv-QK for newlib@sourceware.org; Thu, 29 Jun 2017 14:20:05 +0200 Received: from localhost (localhost.localhost [127.0.0.1]) by mail.embedded-brains.de (Postfix) with ESMTP id C51372A1677 for ; Thu, 29 Jun 2017 14:20:05 +0200 (CEST) Received: from mail.embedded-brains.de ([127.0.0.1]) by localhost (zimbra.eb.localhost [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id v4F6Ep-Ly27B for ; Thu, 29 Jun 2017 14:20:05 +0200 (CEST) Received: from localhost (localhost.localhost [127.0.0.1]) by mail.embedded-brains.de (Postfix) with ESMTP id 5A7252A160A for ; Thu, 29 Jun 2017 14:20:05 +0200 (CEST) Received: from mail.embedded-brains.de ([127.0.0.1]) by localhost (zimbra.eb.localhost [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id P0JrH8PD3w8F for ; Thu, 29 Jun 2017 14:20:05 +0200 (CEST) Received: from huber-linux.eb.localhost (unknown [192.168.96.129]) by mail.embedded-brains.de (Postfix) with ESMTP id 3BC672A1677 for ; Thu, 29 Jun 2017 14:20:05 +0200 (CEST) From: Sebastian Huber To: newlib@sourceware.org Subject: [PATCH v3 3/5] Introduce _REENT_GLOBAL_STDIO_STREAMS Date: Thu, 29 Jun 2017 12:25:00 -0000 Message-Id: <20170629122002.25614-3-sebastian.huber@embedded-brains.de> In-Reply-To: <20170629122002.25614-1-sebastian.huber@embedded-brains.de> References: <20170629122002.25614-1-sebastian.huber@embedded-brains.de> X-IsSubscribed: yes X-SW-Source: 2017/txt/msg00516.txt.bz2 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 @@ -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; -- 2.12.3