From patchwork Tue Nov 1 11:33:01 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: niXman X-Patchwork-Id: 123060 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]) by ozlabs.org (Postfix) with SMTP id 7B78CB6F7B for ; Tue, 1 Nov 2011 22:33:30 +1100 (EST) Received: (qmail 26873 invoked by alias); 1 Nov 2011 11:33:23 -0000 Received: (qmail 26857 invoked by uid 22791); 1 Nov 2011 11:33:21 -0000 X-SWARE-Spam-Status: No, hits=-0.9 required=5.0 tests=AWL, BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_LOW X-Spam-Check-By: sourceware.org Received: from mail-ey0-f175.google.com (HELO mail-ey0-f175.google.com) (209.85.215.175) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Tue, 01 Nov 2011 11:33:02 +0000 Received: by eyd9 with SMTP id 9so6990148eyd.20 for ; Tue, 01 Nov 2011 04:33:01 -0700 (PDT) MIME-Version: 1.0 Received: by 10.213.27.135 with SMTP id i7mr302097ebc.107.1320147181311; Tue, 01 Nov 2011 04:33:01 -0700 (PDT) Received: by 10.213.27.148 with HTTP; Tue, 1 Nov 2011 04:33:01 -0700 (PDT) In-Reply-To: <0223B132-51E6-4B54-8C2D-0EC690A5E9E0@oracle.com> References: <0223B132-51E6-4B54-8C2D-0EC690A5E9E0@oracle.com> Date: Tue, 1 Nov 2011 15:33:01 +0400 Message-ID: Subject: Re: implementation of std::thread::hardware_concurrency() From: niXman To: gcc-patches@gcc.gnu.org, libstdc++@gcc.gnu.org Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Rechecked. 2011/11/1 Paolo Carlini : > Hi, > >> This is patch is implement the std::thread::hardware_concurrency(). >> Tested on pthreads-win32/winpthreads on windows OS, and on Linux/FreeBSD. > > Please send library patches to the library mailing list too. Also, always parch mainline first: actually in the latter the function is alread implemented, maybe something is missing for win32, please check, rediff, and resend. > > Thanks > Paolo diff --git a/libstdc++-v3/src/thread.cc b/libstdc++-v3/src/thread.cc index 09e7fc5..6feda4d 100644 --- a/libstdc++-v3/src/thread.cc +++ b/libstdc++-v3/src/thread.cc @@ -112,10 +112,20 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION unsigned int thread::hardware_concurrency() noexcept { - int __n = _GLIBCXX_NPROCS; - if (__n < 0) - __n = 0; - return __n; + int count=0; +#if defined(PTW32_VERSION) || \ + (defined(__MINGW64_VERSION_MAJOR) && defined(_POSIX_THREADS)) || \ + defined(__hpux) + count=pthread_num_processors_np(); +#elif defined(__APPLE__) || defined(__FreeBSD__) + size_t size=sizeof(count); + sysctlbyname("hw.ncpu", &count, &size, NULL, 0); +#elif defined(_SC_NPROCESSORS_ONLN) + count=sysconf(_SC_NPROCESSORS_ONLN); +#elif defined(_GLIBCXX_USE_GET_NPROCS) + count=_GLIBCXX_NPROCS; +#endif + return (count>0)?count:0; } _GLIBCXX_END_NAMESPACE_VERSION