From patchwork Mon Oct 7 23:22:39 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 281290 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 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id ABCB22C00A9 for ; Tue, 8 Oct 2013 10:23:01 +1100 (EST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :mime-version:in-reply-to:references:date:message-id:subject :from:to:content-type; q=dns; s=default; b=UzTReeUdu4y5UVncXkgjW VjMcPNPlftGpY49+AVGcxB1ab65q0gccSEkHztUpkiI1NLrkwIs+kixOcp1zE3Jz zKy9WjOeLosIwBOLq4eaYwdoU+xCCorxOuuf9EZptJSwnBOZbg9aESkZ4Vm8LCca /h87rVxdeMLLBJHJl7UTS4= 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 :mime-version:in-reply-to:references:date:message-id:subject :from:to:content-type; s=default; bh=RE4zPgqaR8D4TZspmm4SlSb01AE =; b=b2vQRQPNBUavj/ZcJKl/Mrl9/X8NcBsoIyHK8OYFEIpHZXD+v0R7GFEbPDs bnPlQ0WKAwew+hH8D+tm74lMhdKRgvtLRln37tHRnwn6O5ktvCqLxyXxF1jWcoTJ /lNY3J8bJtmKmk2tW+y20VmduGtZJ4lxfmbGVSccfcruWrTc= Received: (qmail 7481 invoked by alias); 7 Oct 2013 23:22:45 -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 7462 invoked by uid 89); 7 Oct 2013 23:22:45 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-2.8 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.2 X-Spam-User: qpsmtpd, 2 recipients X-HELO: mail-la0-f47.google.com Received: from mail-la0-f47.google.com (HELO mail-la0-f47.google.com) (209.85.215.47) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Mon, 07 Oct 2013 23:22:43 +0000 Received: by mail-la0-f47.google.com with SMTP id eo20so6192358lab.34 for ; Mon, 07 Oct 2013 16:22:39 -0700 (PDT) MIME-Version: 1.0 X-Received: by 10.112.73.37 with SMTP id i5mr21855lbv.88.1381188159514; Mon, 07 Oct 2013 16:22:39 -0700 (PDT) Received: by 10.112.101.7 with HTTP; Mon, 7 Oct 2013 16:22:39 -0700 (PDT) In-Reply-To: References: Date: Tue, 8 Oct 2013 00:22:39 +0100 Message-ID: Subject: Re: [patch] fix libstdc++/57641 From: Jonathan Wakely To: "libstdc++" , gcc-patches On 18 June 2013 23:55, Jonathan Wakely wrote: > Instead of fixing the bug three times I refactored the try_lock_xxx > functions into a mixin template and used that in the various timed > mutexes. > > PR libstdc++/57641 > * include/std/mutex (timed_mutex, recursive_timed_mutex): Move common > functionality to new __timed_mutex_impl mixin. Overload try_lock_until > to handle conversion between different clocks. Replace constrained > __try_lock_for_impl overloads with conditional increment. > * include/std/shared_mutex (shared_mutex::_Mutex): Use the new mixin. > * testsuite/30_threads/timed_mutex/try_lock_until/57641.cc: New. > > Tested x86_64-linux, committed to trunk. I've committed a less invasive version of this patch (without the refactoring to add a new base class) to the 4.8 branch. Tested x86_64-linux. PR libstdc++/57641 * include/std/mutex (timed_mutex, recursive_timed_mutex): Add overloaded _M_try_lock_until to handle conversion between different clocks. Replace constrained __try_lock_for_impl overloads with conditional increment. * testsuite/30_threads/timed_mutex/try_lock_until/57641.cc: New. commit b28b4820d7a76e58dc764a9bfe978c657386991b Author: Jonathan Wakely Date: Mon Oct 7 10:13:00 2013 +0100 PR libstdc++/57641 * include/std/mutex (timed_mutex, recursive_timed_mutex): Add overloaded _M_try_lock_until to handle conversion between different clocks. Replace constrained __try_lock_for_impl overloads with conditional increment. * testsuite/30_threads/timed_mutex/try_lock_until/57641.cc: New. diff --git a/libstdc++-v3/include/std/mutex b/libstdc++-v3/include/std/mutex index 67f3418..3093d9a 100644 --- a/libstdc++-v3/include/std/mutex +++ b/libstdc++-v3/include/std/mutex @@ -237,25 +237,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template bool try_lock_for(const chrono::duration<_Rep, _Period>& __rtime) - { return __try_lock_for_impl(__rtime); } + { return _M_try_lock_for(__rtime); } template bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __atime) - { - chrono::time_point<_Clock, chrono::seconds> __s = - chrono::time_point_cast(__atime); - - chrono::nanoseconds __ns = - chrono::duration_cast(__atime - __s); - - __gthread_time_t __ts = { - static_cast(__s.time_since_epoch().count()), - static_cast(__ns.count()) - }; - - return !__gthread_mutex_timedlock(&_M_mutex, &__ts); - } + { return _M_try_lock_until(__atime); } void unlock() @@ -270,26 +257,39 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION private: template - typename enable_if< - ratio_less_equal<__clock_t::period, _Period>::value, bool>::type - __try_lock_for_impl(const chrono::duration<_Rep, _Period>& __rtime) + bool + _M_try_lock_for(const chrono::duration<_Rep, _Period>& __rtime) { - __clock_t::time_point __atime = __clock_t::now() - + chrono::duration_cast<__clock_t::duration>(__rtime); + auto __rt = chrono::duration_cast<__clock_t::duration>(__rtime); + if (ratio_greater<__clock_t::period, _Period>()) + ++__rt; - return try_lock_until(__atime); + return _M_try_lock_until(__clock_t::now() + __rt); } - template - typename enable_if< - !ratio_less_equal<__clock_t::period, _Period>::value, bool>::type - __try_lock_for_impl(const chrono::duration<_Rep, _Period>& __rtime) + template + bool + _M_try_lock_until(const chrono::time_point<__clock_t, + _Duration>& __atime) { - __clock_t::time_point __atime = __clock_t::now() - + ++chrono::duration_cast<__clock_t::duration>(__rtime); + chrono::time_point<__clock_t, chrono::seconds> __s = + chrono::time_point_cast(__atime); + + chrono::nanoseconds __ns = + chrono::duration_cast(__atime - __s); + + __gthread_time_t __ts = { + static_cast(__s.time_since_epoch().count()), + static_cast(__ns.count()) + }; - return try_lock_until(__atime); + return !__gthread_mutex_timedlock(native_handle(), &__ts); } + + template + bool + _M_try_lock_until(const chrono::time_point<_Clock, _Duration>& __atime) + { return _M_try_lock_for(__atime - _Clock::now()); } }; /// recursive_timed_mutex @@ -330,25 +330,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template bool try_lock_for(const chrono::duration<_Rep, _Period>& __rtime) - { return __try_lock_for_impl(__rtime); } + { return _M_try_lock_for(__rtime); } template bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __atime) - { - chrono::time_point<_Clock, chrono::seconds> __s = - chrono::time_point_cast(__atime); - - chrono::nanoseconds __ns = - chrono::duration_cast(__atime - __s); - - __gthread_time_t __ts = { - static_cast(__s.time_since_epoch().count()), - static_cast(__ns.count()) - }; - - return !__gthread_recursive_mutex_timedlock(&_M_mutex, &__ts); - } + { return _M_try_lock_until(__atime); } void unlock() @@ -363,26 +350,39 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION private: template - typename enable_if< - ratio_less_equal<__clock_t::period, _Period>::value, bool>::type - __try_lock_for_impl(const chrono::duration<_Rep, _Period>& __rtime) + bool + _M_try_lock_for(const chrono::duration<_Rep, _Period>& __rtime) { - __clock_t::time_point __atime = __clock_t::now() - + chrono::duration_cast<__clock_t::duration>(__rtime); + auto __rt = chrono::duration_cast<__clock_t::duration>(__rtime); + if (ratio_greater<__clock_t::period, _Period>()) + ++__rt; - return try_lock_until(__atime); + return _M_try_lock_until(__clock_t::now() + __rt); } - template - typename enable_if< - !ratio_less_equal<__clock_t::period, _Period>::value, bool>::type - __try_lock_for_impl(const chrono::duration<_Rep, _Period>& __rtime) + template + bool + _M_try_lock_until(const chrono::time_point<__clock_t, + _Duration>& __atime) { - __clock_t::time_point __atime = __clock_t::now() - + ++chrono::duration_cast<__clock_t::duration>(__rtime); + chrono::time_point<__clock_t, chrono::seconds> __s = + chrono::time_point_cast(__atime); + + chrono::nanoseconds __ns = + chrono::duration_cast(__atime - __s); + + __gthread_time_t __ts = { + static_cast(__s.time_since_epoch().count()), + static_cast(__ns.count()) + }; - return try_lock_until(__atime); + return !__gthread_mutex_timedlock(native_handle(), &__ts); } + + template + bool + _M_try_lock_until(const chrono::time_point<_Clock, _Duration>& __atime) + { return _M_try_lock_for(__atime - _Clock::now()); } }; #endif #endif // _GLIBCXX_HAS_GTHREADS diff --git a/libstdc++-v3/testsuite/30_threads/timed_mutex/try_lock_until/57641.cc b/libstdc++-v3/testsuite/30_threads/timed_mutex/try_lock_until/57641.cc new file mode 100644 index 0000000..94fe5b3 --- /dev/null +++ b/libstdc++-v3/testsuite/30_threads/timed_mutex/try_lock_until/57641.cc @@ -0,0 +1,69 @@ +// { dg-do run { target *-*-freebsd* *-*-netbsd* *-*-linux* *-*-gnu* *-*-solaris* *-*-cygwin *-*-darwin* powerpc-ibm-aix* } } +// { dg-options " -std=gnu++0x -pthread" { target *-*-freebsd* *-*-netbsd* *-*-linux* *-*-gnu* powerpc-ibm-aix* } } +// { dg-options " -std=gnu++0x -pthreads" { target *-*-solaris* } } +// { dg-options " -std=gnu++0x " { target *-*-cygwin *-*-darwin* } } +// { dg-require-cstdint "" } +// { dg-require-gthreads-timed "" } + +// Copyright (C) 2013 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +#include +#include +#include +#include + +// PR libstdc++/57641 + +namespace C = std::chrono; + +// custom clock with epoch 10s before system_clock's +struct clock +{ + typedef C::system_clock::rep rep; + typedef C::system_clock::period period; + typedef C::system_clock::duration duration; + typedef C::time_point time_point; + static constexpr bool is_steady = C::system_clock::is_steady; + + static time_point + now() + { + auto sys_time = C::system_clock::now().time_since_epoch(); + return time_point(sys_time + C::seconds(10)); + } +}; + +std::timed_mutex mx; +bool test = false; + +void f() +{ + test = mx.try_lock_until(clock::now() + C::milliseconds(1)); +} + +int main() +{ + bool test = false; + std::lock_guard l(mx); + auto start = C::system_clock::now(); + std::thread t(f); + t.join(); + auto stop = C::system_clock::now(); + VERIFY( (stop - start) < C::seconds(9) ); + VERIFY( !test ); +}