From patchwork Thu Oct 11 16:37:51 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 982612 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-487369-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="HrDNEtM2"; dkim-atps=neutral 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 42WGqP0gBKz9s1x for ; Fri, 12 Oct 2018 03:38:07 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; q=dns; s= default; b=cKHwpbiwOydopWHgEdI8udv7dZJ9DMqq9SzaiiB1ZiEmUbi5/e8sF awYUxm+dXs1Drbbf75yhdcHAKI3iIxRepx9TRiAUgfSS2Ha9aAR8P+GV5Fe+nLxi vy82MSnhlNHSs6ZNKfuzxoLmyFEkut2apI8pzEr5Eq+aIvGv8wee3I= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; s= default; bh=Fhz4qiqIP8HsMP20uip9Rr9uNNY=; b=HrDNEtM2cBq/XUQQx5tw xMF2B2f20c5DaBqbVju8w9hlXtyHXyRTpPEvgaAYQ/D+zuT4XiNcP4/xPGf5xNYh 1+Oo0ySdMA83v7Z4rtdY+kQRCazGy4Zi36szq4e/0CwCHzFOlcSPqRnhUgG6nWOT QKhQV/PWdKamEd2dmVBBexY= Received: (qmail 37043 invoked by alias); 11 Oct 2018 16:37:58 -0000 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 Received: (qmail 36991 invoked by uid 89); 11 Oct 2018 16:37:55 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-25.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_LAZY_DOMAIN_SECURITY, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=interruptions, interruption X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 11 Oct 2018 16:37:53 +0000 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 89CB25F793; Thu, 11 Oct 2018 16:37:52 +0000 (UTC) Received: from localhost (unknown [10.33.36.15]) by smtp.corp.redhat.com (Postfix) with ESMTP id 34C2A8573D; Thu, 11 Oct 2018 16:37:52 +0000 (UTC) Date: Thu, 11 Oct 2018 17:37:51 +0100 From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH] PR libstdc++/80538 Only call sleep for non-zero values Message-ID: <20181011163751.GA8203@redhat.com> MIME-Version: 1.0 Content-Disposition: inline X-Clacks-Overhead: GNU Terry Pratchett User-Agent: Mutt/1.9.2 (2017-12-15) Avoid a system call when no sleep is required. Sleep in a loop (actually two loops) to handle interruption by signals. PR libstdc++/80538 * src/c++11/thread.cc (this_thread::__sleep_for) [_GLIBCXX_HAVE_SLEEP]: Only call sleep for non-zero values. Loop while sleep call is interrupted and until steady_clock shows requested duration has elapsed. (!_GLIBCXX_HAVE_USLEEP]: Use the _GLIBCXX_HAVE_SLEEP code path, but avoiding the usleep call. * testsuite/30_threads/this_thread/60421.cc: Test repeated signal interruptions. Tested x86_64-linux (by manually fudging the configure macros to test the !_GLIBCXX_USE_NANOSLEEP paths). Committed to trunk. commit 7cad0b3cbb85a78dce40535f897ba27886469da9 Author: Jonathan Wakely Date: Fri Apr 28 17:43:25 2017 +0100 PR libstdc++/80538 Only call sleep for non-zero values Avoid a system call when no sleep is required. Sleep in a loop (actually two loops) to handle interruption by signals. PR libstdc++/80538 * src/c++11/thread.cc (this_thread::__sleep_for) [_GLIBCXX_HAVE_SLEEP]: Only call sleep for non-zero values. Loop while sleep call is interrupted and until steady_clock shows requested duration has elapsed. (!_GLIBCXX_HAVE_USLEEP]: Use the _GLIBCXX_HAVE_SLEEP code path, but avoiding the usleep call. * testsuite/30_threads/this_thread/60421.cc: Test repeated signal interruptions. diff --git a/libstdc++-v3/src/c++11/thread.cc b/libstdc++-v3/src/c++11/thread.cc index c62cb71bf99..564eae6f166 100644 --- a/libstdc++-v3/src/c++11/thread.cc +++ b/libstdc++-v3/src/c++11/thread.cc @@ -194,18 +194,35 @@ namespace this_thread while (::nanosleep(&__ts, &__ts) == -1 && errno == EINTR) { } #elif defined(_GLIBCXX_HAVE_SLEEP) -# ifdef _GLIBCXX_HAVE_USLEEP - ::sleep(__s.count()); - if (__ns.count() > 0) + const auto target = chrono::steady_clock::now() + __s + __ns; + while (true) { - long __us = __ns.count() / 1000; - if (__us == 0) - __us = 1; - ::usleep(__us); - } + unsigned secs = __s.count(); + if (__ns.count() > 0) + { +# ifdef _GLIBCXX_HAVE_USLEEP + long us = __ns.count() / 1000; + if (us == 0) + us = 1; + ::usleep(us); # else - ::sleep(__s.count() + (__ns.count() >= 1000000)); + if (__ns.count() > 1000000 || secs == 0) + ++secs; // No sub-second sleep function, so round up. # endif + } + + if (secs > 0) + { + // Sleep in a loop to handle interruption by signals: + while ((secs = ::sleep(secs))) + { } + } + const auto now = chrono::steady_clock::now(); + if (now >= target) + break; + __s = chrono::duration_cast(target - now); + __ns = chrono::duration_cast(target - (now + __s)); + } #elif defined(_GLIBCXX_HAVE_WIN32_SLEEP) unsigned long ms = __ns.count() / 1000000; if (__ns.count() > 0 && ms == 0) diff --git a/libstdc++-v3/testsuite/30_threads/this_thread/60421.cc b/libstdc++-v3/testsuite/30_threads/this_thread/60421.cc index e7d69a49fb6..cd8d2fdd6f3 100644 --- a/libstdc++-v3/testsuite/30_threads/this_thread/60421.cc +++ b/libstdc++-v3/testsuite/30_threads/this_thread/60421.cc @@ -53,10 +53,19 @@ test02() sleeping = true; std::this_thread::sleep_for(time); result = std::chrono::system_clock::now() >= (start + time); + sleeping = false; }); - while (!sleeping) { } - std::this_thread::sleep_for(std::chrono::milliseconds(500)); - pthread_kill(t.native_handle(), SIGUSR1); + while (!sleeping) + { + // Wait for the thread to start sleeping. + } + while (sleeping) + { + // The sleeping thread should finish eventually, + // even if continually interrupted after less than a second: + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + pthread_kill(t.native_handle(), SIGUSR1); + } t.join(); VERIFY( result ); }