From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 116243 invoked by alias); 7 Aug 2017 06:33:39 -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 116222 invoked by uid 89); 7 Aug 2017 06:33:37 -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=xp, 2123, lint, U*sebastian.huber 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 06:33:35 +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 1debbZ-00088D-Nw for newlib@sourceware.org; Mon, 07 Aug 2017 08:33:33 +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 1debbZ-0004UN-Gi for newlib@sourceware.org; Mon, 07 Aug 2017 08:33:33 +0200 Received: from localhost (localhost.localhost [127.0.0.1]) by mail.embedded-brains.de (Postfix) with ESMTP id 63BF22A004F for ; Mon, 7 Aug 2017 08:33: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 10032) with ESMTP id BVvX2-QtL5v5 for ; Mon, 7 Aug 2017 08:33:53 +0200 (CEST) Received: from localhost (localhost.localhost [127.0.0.1]) by mail.embedded-brains.de (Postfix) with ESMTP id 2F0232A1677 for ; Mon, 7 Aug 2017 08:33:53 +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 ujI0Ii8rkkOL for ; Mon, 7 Aug 2017 08:33:53 +0200 (CEST) Received: from linux-diu0.suse (unknown [192.168.96.129]) by mail.embedded-brains.de (Postfix) with ESMTP id 15A482A004F for ; Mon, 7 Aug 2017 08:33:53 +0200 (CEST) From: Sebastian Huber To: newlib@sourceware.org Subject: [PATCH] Proper locking for getchar() and putchar() Date: Mon, 07 Aug 2017 06:33:00 -0000 Message-Id: <20170807063330.16281-1-sebastian.huber@embedded-brains.de> X-IsSubscribed: yes X-SW-Source: 2017/txt/msg00722.txt.bz2 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. 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 -#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 = _REENT; + return (__sgetc_r(_ptr, _stdin_r(_ptr))); +} + +static __inline int +_putchar_unlocked(int _c) +{ + struct _reent *_ptr; + + _ptr = _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 */ +#if __MISC_VISIBLE || __POSIX_VISIBLE +#define getchar_unlocked() _getchar_unlocked() +#define putchar_unlocked(_c) _putchar_unlocked(_c) +#endif + #if __MISC_VISIBLE /* fast always-buffered version, true iff error */ #define fast_putc(x,p) (--(p)->_w < 0 ? \ @@ -756,7 +779,7 @@ _ELIDABLE_INLINE int __sputc_r(struct _reent *_ptr, int _c, FILE *_p) { #define L_ctermid 16 #endif -#endif /* !__CUSTOM_FILE_IO__ */ +#else /* __CUSTOM_FILE_IO__ */ #define getchar() getc(stdin) #define putchar(x) putc(x, stdout) @@ -766,6 +789,8 @@ _ELIDABLE_INLINE int __sputc_r(struct _reent *_ptr, int _c, FILE *_p) { #define putchar_unlocked(x) putc_unlocked(x, stdout) #endif +#endif /* !__CUSTOM_FILE_IO__ */ + _END_STD_C #endif /* _STDIO_H_ */ -- 2.12.3