From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 57688 invoked by alias); 7 Aug 2017 10:53:40 -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 57674 invoked by uid 89); 7 Aug 2017 10:53:39 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-18.0 required=5.0 tests=AWL,BAYES_05,FOREIGN_BODY,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,RCVD_IN_DNSWL_NONE,SPF_PASS,T_FILL_THIS_FORM_SHORT autolearn=ham version=3.3.2 spammy=diese, Nachricht, nachricht, mitteilung 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; Mon, 07 Aug 2017 10:53:37 +0000 Received: from [78.47.166.52] (helo=sslproxy04.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 1deffD-0006QC-HU for newlib@sourceware.org; Mon, 07 Aug 2017 12:53:35 +0200 Received: from [82.135.62.35] (helo=mail.embedded-brains.de) by sslproxy04.your-server.de with esmtpsa (TLSv1.2:DHE-RSA-AES256-GCM-SHA384:256) (Exim 4.84_2) (envelope-from ) id 1deffD-00024p-84 for newlib@sourceware.org; Mon, 07 Aug 2017 12:53:35 +0200 Received: from localhost (localhost.localhost [127.0.0.1]) by mail.embedded-brains.de (Postfix) with ESMTP id 0FDF32A004F for ; Mon, 7 Aug 2017 12:53:56 +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 l7QqpwtMB9vX for ; Mon, 7 Aug 2017 12:53:54 +0200 (CEST) Received: from localhost (localhost.localhost [127.0.0.1]) by mail.embedded-brains.de (Postfix) with ESMTP id 105752A1677 for ; Mon, 7 Aug 2017 12:53:54 +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 5xjOG6tv3F_S for ; Mon, 7 Aug 2017 12:53:53 +0200 (CEST) Received: from [192.168.96.129] (unknown [192.168.96.129]) by mail.embedded-brains.de (Postfix) with ESMTPSA id E0E9A2A004F for ; Mon, 7 Aug 2017 12:53:53 +0200 (CEST) Subject: Re: [PATCH] Proper locking for getchar() and putchar() To: newlib@sourceware.org References: <20170807063330.16281-1-sebastian.huber@embedded-brains.de> <20170807094957.GA18389@calimero.vinschen.de> From: Sebastian Huber Message-ID: <5b3ee17d-6e9f-4421-b1d2-5279a7de9a28@embedded-brains.de> Date: Mon, 07 Aug 2017 10:53:00 -0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1 MIME-Version: 1.0 In-Reply-To: <20170807094957.GA18389@calimero.vinschen.de> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: quoted-printable X-IsSubscribed: yes X-SW-Source: 2017/txt/msg00725.txt.bz2 On 07/08/17 11:49, Corinna Vinschen wrote: > Hi Sebastian, > > On Aug 7 08:33, Sebastian Huber wrote: >> Add internal inline functions _getchar_unlocked() and >> _putchar_unlocked() if __CUSTOM_FILE_IO__ is not defined. These >> functions get _REENT only once. Use them for getchar_unlocked() and >> putchar_unlocked(). Define getchar() and putchar() to these unlocked >> internal functions if __SINGLE_THREAD__ is defined, otherwise use the >> external functions to use proper locking of the FILE object. >> >> Assumes that __SINGLE_THREAD__ is not defined if __CYGWIN__ is defined. > Yeah :) But, even then, __SINGLE_THREAD__ is __SINGLE_THREAD__. > >> Signed-off-by: Sebastian Huber >> --- >> newlib/libc/include/stdio.h | 39 ++++++++++++++++++++++++++++++++-----= -- >> 1 file changed, 32 insertions(+), 7 deletions(-) >> >> diff --git a/newlib/libc/include/stdio.h b/newlib/libc/include/stdio.h >> index 1c32423d3..5d8cb1092 100644 >> --- a/newlib/libc/include/stdio.h >> +++ b/newlib/libc/include/stdio.h >> @@ -735,14 +735,37 @@ _ELIDABLE_INLINE int __sputc_r(struct _reent *_ptr= , int _c, FILE *_p) { >> #define fileno(p) __sfileno(p) >> #endif >>=20=20=20 >> -#ifndef __CYGWIN__ >> -#ifndef lint >> -#define getc(fp) __sgetc_r(_REENT, fp) >> -#define putc(x, fp) __sputc_r(_REENT, x, fp) >> -#endif /* lint */ >> -#endif /* __CYGWIN__ */ >> +static __inline int >> +_getchar_unlocked(void) >> +{ >> + struct _reent *_ptr; >> + >> + _ptr =3D _REENT; >> + return (__sgetc_r(_ptr, _stdin_r(_ptr))); >> +} >> + >> +static __inline int >> +_putchar_unlocked(int _c) >> +{ >> + struct _reent *_ptr; >> + >> + _ptr =3D _REENT; >> + return (__sputc_r(_ptr, _c, _stdout_r(_ptr))); >> +} >> + >> +#ifdef __SINGLE_THREAD__ >> +#define getc(_p) __sgetc_r(_REENT, _p) >> +#define putc(_c, _p) __sputc_r(_REENT, _c, _p) >> +#define getchar() _getchar_unlocked() >> +#define putchar(_c) _putchar_unlocked(_c) >> +#endif /* __SINGLE_THREAD__ */ >> #endif /* __cplusplus */ > That looks good, but I wonder, wouldn't it make sense to replace the > inline _getchar_unlocked/_putchar_unlocked with inline > _getc_unlocked/_puts_unlocked? > > I'm asking because that would allow to #define all four, > getc/putc/getchar/putchar, so that the parameters are only evaluated > once. > > What do you think? For getc()/putc() you have the FILE object already and only need _REENT. For getchar()/putchar() you have nothing and need the _REENT plus=20 stdin/stdout. I don't know how you would unify/simplify this further? --=20 Sebastian Huber, embedded brains GmbH Address : Dornierstr. 4, D-82178 Puchheim, Germany Phone : +49 89 189 47 41-16 Fax : +49 89 189 47 41-09 E-Mail : sebastian.huber@embedded-brains.de PGP : Public key available on request. Diese Nachricht ist keine gesch=C3=A4ftliche Mitteilung im Sinne des EHUG.