From patchwork Mon Mar 16 14:29:58 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 450594 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id D5CC714007F for ; Tue, 17 Mar 2015 01:30:16 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass reason="1024-bit key; unprotected key" header.d=sourceware.org header.i=@sourceware.org header.b=ytHw08WC; dkim-adsp=none (unprotected policy); dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:message-id:date:from:mime-version:to:subject :references:in-reply-to:content-type; q=dns; s=default; b=A4ixGM MBc6Ch9AaqVxwUb/vE/hQoApp0gBBgNpytXXw1uEU3P8JJaAgh4kHfUqUlatTDDB ow9PYWsl/xYmiE/+vSrXYhCiPbOTtpsNvFC5RE5ZfltKhEmp5fggHA83lq7uGJeF TCtUKldIFSF5HIn2vv2509Gx+i+z1dv8Izivc= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:message-id:date:from:mime-version:to:subject :references:in-reply-to:content-type; s=default; bh=I+ijADomwqOb OcSa4EahswyblOw=; b=ytHw08WCTxkJgtocJps3X1YEgQj65AuDfEmhTWtcOOuT Z6FDb80wYwSwVyVyV7nkQJsjayUic+YIdDI90efeBgrLltIrQ8S7OvTAVtRGrEUh +iTx5NELfimoUmbsUyTRXHTNpA23A4q9dXs/wkiSGcuxDze3VPXasFTRK7CvSFg= Received: (qmail 113513 invoked by alias); 16 Mar 2015 14:30:08 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 113439 invoked by uid 89); 16 Mar 2015 14:30:07 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=AWL, BAYES_00, SPF_HELO_PASS, SPF_PASS, T_RP_MATCHES_RCVD, UNSUBSCRIBE_BODY autolearn=no version=3.3.2 X-HELO: mx1.redhat.com Message-ID: <5506E8E6.1000906@redhat.com> Date: Mon, 16 Mar 2015 15:29:58 +0100 From: Florian Weimer User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.5.0 MIME-Version: 1.0 To: libc-alpha@sourceware.org Subject: Re: [PATCH 02/25] pthread_setaffinity (Linux variant): Rewrite to use VLA instead of alloca References: <080236bd5f23d9be4c47794ad23e96d52d380f5d.1425285061.git.fweimer@redhat.com> <20150302195844.GA24238@vapier> In-Reply-To: <20150302195844.GA24238@vapier> On 03/02/2015 08:58 PM, Mike Frysinger wrote: > On 01 Mar 2015 15:05, Florian Weimer wrote: >> extend_alloca was used to emulate VLA deallocation. > > general note: i found the old code easier to read. i think GNU style requires > newlines after decls and before the return statement. that would certainly help > readability here. What about this version? I think it makes the control flow more explicit, and it prompted me to deal differently with the res == 0 case (which was swept under the carpet before). 2015-03-16 Florian Weimer * sysdeps/unix/sysv/linux/pthread_setaffinity.c (__determine_cpumask_size): Replace extend_alloca with a variable-length array. Always return zero if res == 0. From 5dddb59c0c8ea06afebfee70026b854fbaf487f1 Mon Sep 17 00:00:00 2001 From: Florian Weimer Date: Sun, 1 Mar 2015 15:05:09 +0100 Subject: [PATCH] pthread_setaffinity (Linux variant): Rewrite to use VLA instead of alloca extend_alloca was used to emulate VLA deallocation. The new version also handles the res == 0 corner case more explicitly, by returning 0 instead of the (potentially undefined, but usually zero) system call error. --- sysdeps/unix/sysv/linux/pthread_setaffinity.c | 32 ++++++++++++++------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/sysdeps/unix/sysv/linux/pthread_setaffinity.c b/sysdeps/unix/sysv/linux/pthread_setaffinity.c index 37997e9..9137e56 100644 --- a/sysdeps/unix/sysv/linux/pthread_setaffinity.c +++ b/sysdeps/unix/sysv/linux/pthread_setaffinity.c @@ -16,7 +16,6 @@ License along with the GNU C Library; if not, see . */ -#include #include #include #include @@ -27,26 +26,29 @@ size_t __kernel_cpumask_size attribute_hidden; -/* Determine the current affinity. As a side affect we learn - about the size of the cpumask_t in the kernel. */ +/* Determine the size of cpumask_t in the kernel. */ int __determine_cpumask_size (pid_t tid) { - INTERNAL_SYSCALL_DECL (err); - int res; - size_t psize = 128; - void *p = alloca (psize); - - while (res = INTERNAL_SYSCALL (sched_getaffinity, err, 3, tid, psize, p), - INTERNAL_SYSCALL_ERROR_P (res, err) - && INTERNAL_SYSCALL_ERRNO (res, err) == EINVAL) - p = extend_alloca (p, psize, 2 * psize); + int res; - if (res == 0 || INTERNAL_SYSCALL_ERROR_P (res, err)) - return INTERNAL_SYSCALL_ERRNO (res, err); + for (psize = 128; ; psize *= 2) + { + char buf[psize]; + INTERNAL_SYSCALL_DECL (err); + res = INTERNAL_SYSCALL (sched_getaffinity, err, 3, tid, psize, buf); + if (INTERNAL_SYSCALL_ERROR_P (res, err)) + { + if (INTERNAL_SYSCALL_ERRNO (res, err) != EINVAL) + return INTERNAL_SYSCALL_ERRNO (res, err); + } + else + break; + } - __kernel_cpumask_size = res; + if (res != 0) + __kernel_cpumask_size = res; return 0; } -- 2.1.0