From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 58880 invoked by alias); 31 Jan 2017 17:10:28 -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 35772 invoked by uid 89); 31 Jan 2017 17:10:20 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-0.7 required=5.0 tests=BAYES_00,KAM_LAZY_DOMAIN_SECURITY,KAM_LOTSOFHASH,RP_MATCHES_RCVD autolearn=no version=3.3.2 spammy=814, 928, 203,7, 88 X-HELO: foss.arm.com Received: from foss.arm.com (HELO foss.arm.com) (217.140.101.70) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 31 Jan 2017 17:10:18 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 0E96814F6; Tue, 31 Jan 2017 09:10:17 -0800 (PST) Received: from [10.2.206.52] (usa-sjc-imap-foss1.foss.arm.com [10.72.51.249]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id B4CDA3F24D for ; Tue, 31 Jan 2017 09:10:16 -0800 (PST) To: newlib@sourceware.org From: Thomas Preudhomme Subject: [PATCH 2/3, newlib] Only define static locks in multithreaded mode Message-ID: <7a943313-48e7-2c8f-7250-ce47aeecda13@foss.arm.com> Date: Tue, 31 Jan 2017 17:10:00 -0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.5.1 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="------------E6CDCA657228232AEAA8FDD5" X-IsSubscribed: yes X-SW-Source: 2017/txt/msg00110.txt.bz2 This is a multi-part message in MIME format. --------------E6CDCA657228232AEAA8FDD5 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Content-length: 557 Hi, Newlib build system defines __SINGLE_THREAD__ to allow concurrency code to be only compiled when newlib is configured for multithread. One such example are locks which become useless in single thread mode. Although most static locks are indeed guarded by !defined(__SINGLE_THREAD__), some are not. This commit adds these missing guards to __dd_hash_mutex, __atexit_recursive_mutex, __at_quick_exit_mutex and __arc4random_mutex. It also makes sure locking macros in lock.h are noop in single thread mode. Is this ok for master? Best regards, Thomas --------------E6CDCA657228232AEAA8FDD5 Content-Type: text/x-patch; name="guard_static_lock_single_thread.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="guard_static_lock_single_thread.patch" Content-length: 5314 diff --git a/newlib/libc/include/sys/lock.h b/newlib/libc/include/sys/lock.h index 9075e35c9179968031010432515e3a845ff6ca8d..a406ec7fd71f10b81bdfb0ca2476fa5ea50e0c0b 100644 --- a/newlib/libc/include/sys/lock.h +++ b/newlib/libc/include/sys/lock.h @@ -8,8 +8,14 @@ typedef int _LOCK_RECURSIVE_T; #include <_ansi.h> +#ifdef __SINGLE_THREAD__ +#define __LOCK_INIT(class,lock) ; +#define __LOCK_INIT_RECURSIVE(class,lock) ; +#else #define __LOCK_INIT(class,lock) static int lock = 0; #define __LOCK_INIT_RECURSIVE(class,lock) static int lock = 0; +#endif + #define __lock_init(lock) (_CAST_VOID 0) #define __lock_init_recursive(lock) (_CAST_VOID 0) #define __lock_close(lock) (_CAST_VOID 0) diff --git a/newlib/libc/posix/telldir.c b/newlib/libc/posix/telldir.c index 0a8eac024b4955cd70b192307938358bb1cb2236..959e3b7bb78860f775e0de2f9d52ff2eb6b67411 100644 --- a/newlib/libc/posix/telldir.c +++ b/newlib/libc/posix/telldir.c @@ -70,7 +70,7 @@ struct ddloc { static long dd_loccnt = 1; /* Index of entry for sequential readdir's */ static struct ddloc *dd_hash[NDIRHASH]; /* Hash list heads for ddlocs */ -#ifdef HAVE_DD_LOCK +#if !defined(__SINGLE_THREAD__) && defined(HAVE_DD_LOCK) __LOCK_INIT(static, __dd_hash_mutex); #endif @@ -92,8 +92,10 @@ _DEFUN(telldir, (dirp), #ifdef HAVE_DD_LOCK __lock_acquire_recursive(dirp->dd_lock); +#ifndef __SINGLE_THREAD__ __lock_acquire(__dd_hash_mutex); #endif +#endif index = dd_loccnt++; lp->loc_index = index; lp->loc_seek = dirp->dd_seek; @@ -102,7 +104,9 @@ _DEFUN(telldir, (dirp), lp->loc_next = dd_hash[LOCHASH(index)]; dd_hash[LOCHASH(index)] = lp; #ifdef HAVE_DD_LOCK +#ifndef __SINGLE_THREAD__ __lock_release(__dd_hash_mutex); +#endif __lock_release_recursive(dirp->dd_lock); #endif return (index); @@ -123,7 +127,7 @@ _DEFUN(_seekdir, (dirp, loc), register struct ddloc **prevlp; struct dirent *dp; -#ifdef HAVE_DD_LOCK +#if !defined(__SINGLE_THREAD__) && defined(HAVE_DD_LOCK) __lock_acquire(__dd_hash_mutex); #endif if (loc != 0) { @@ -136,7 +140,7 @@ _DEFUN(_seekdir, (dirp, loc), lp = lp->loc_next; } if (lp == NULL) { -#ifdef HAVE_DD_LOCK +#if !defined(__SINGLE_THREAD__) && defined(HAVE_DD_LOCK) __lock_release(__dd_hash_mutex); #endif return; @@ -162,7 +166,7 @@ found: dirp->dd_seek = 0; dirp->dd_loc = 0; } -#ifdef HAVE_DD_LOCK +#if !defined(__SINGLE_THREAD__) && defined(HAVE_DD_LOCK) __lock_release(__dd_hash_mutex); #endif } @@ -174,7 +178,7 @@ _DEFUN(_cleanupdir, (dirp), { int i; -#ifdef HAVE_DD_LOCK +#if !defined(__SINGLE_THREAD__) && defined(HAVE_DD_LOCK) __lock_acquire(__dd_hash_mutex); #endif for (i = 0; i < NDIRHASH; ++i) { @@ -199,7 +203,7 @@ _DEFUN(_cleanupdir, (dirp), } dd_hash[i] = head.loc_next; } -#ifdef HAVE_DD_LOCK +#if !defined(__SINGLE_THREAD__) && defined(HAVE_DD_LOCK) __lock_release(__dd_hash_mutex); #endif diff --git a/newlib/libc/stdlib/__call_atexit.c b/newlib/libc/stdlib/__call_atexit.c index 7d5e0d0464b4b9ff74fcb003d965a3c2da020699..6a809cc4d5bd09905d010fdc21708a537166971d 100644 --- a/newlib/libc/stdlib/__call_atexit.c +++ b/newlib/libc/stdlib/__call_atexit.c @@ -11,7 +11,9 @@ /* Make this a weak reference to avoid pulling in free. */ void free(void *) _ATTRIBUTE((__weak__)); +#ifndef __SINGLE_THREAD__ __LOCK_INIT_RECURSIVE(, __atexit_recursive_mutex); +#endif #ifdef _REENT_GLOBAL_ATEXIT struct _atexit *_global_atexit = _NULL; diff --git a/newlib/libc/stdlib/arc4random.h b/newlib/libc/stdlib/arc4random.h index 3c5fe235338270b1af48190cefa9278ea3af917f..4b9855305f5cbce00dd96ee58ed32d1ad48aa5cf 100644 --- a/newlib/libc/stdlib/arc4random.h +++ b/newlib/libc/stdlib/arc4random.h @@ -47,7 +47,9 @@ #endif /* _ARC4_LOCK_INIT */ +#ifndef __SINGLE_THREAD__ _ARC4_LOCK_INIT +#endif #ifdef _ARC4RANDOM_DATA _ARC4RANDOM_DATA diff --git a/newlib/libc/stdlib/arc4random.c b/newlib/libc/stdlib/arc4random.c index 75cdff3bc4b5d4245cce4130df64aebc956c853f..3cccc3ed46a5e9e756d4e216cb7686589eb60993 100644 --- a/newlib/libc/stdlib/arc4random.c +++ b/newlib/libc/stdlib/arc4random.c @@ -180,16 +180,24 @@ arc4random(void) { uint32_t val; +#ifndef __SINGLE_THREAD__ _ARC4_LOCK(); +#endif _rs_random_u32(&val); +#ifndef __SINGLE_THREAD__ _ARC4_UNLOCK(); +#endif return val; } void arc4random_buf(void *buf, size_t n) { +#ifndef __SINGLE_THREAD__ _ARC4_LOCK(); +#endif _rs_random_buf(buf, n); +#ifndef __SINGLE_THREAD__ _ARC4_UNLOCK(); +#endif } diff --git a/newlib/libc/stdlib/quick_exit.c b/newlib/libc/stdlib/quick_exit.c index aaa5f9f7f1df12dace7d87271291aa87cd2fb3b1..5ab2609bf04d508a228049a52aea5e6ec7216d54 100644 --- a/newlib/libc/stdlib/quick_exit.c +++ b/newlib/libc/stdlib/quick_exit.c @@ -44,7 +44,9 @@ struct quick_exit_handler { /** * Lock protecting the handlers list. */ +#ifndef __SINGLE_THREAD__ __LOCK_INIT(static, __at_quick_exit_mutex); +#endif /** * Stack of cleanup handlers. These will be invoked in reverse order when */ @@ -60,10 +62,14 @@ at_quick_exit(void (*func)(void)) if (NULL == h) return (1); h->cleanup = func; +#ifndef __SINGLE_THREAD__ __lock_acquire(__at_quick_exit_mutex); +#endif h->next = handlers; handlers = h; +#ifndef __SINGLE_THREAD__ __lock_release(__at_quick_exit_mutex); +#endif return (0); } --------------E6CDCA657228232AEAA8FDD5--