diff options
author | Paolo Carlini <paolo.carlini@oracle.com> | 2010-01-29 22:46:31 +0000 |
---|---|---|
committer | Paolo Carlini <paolo@gcc.gnu.org> | 2010-01-29 22:46:31 +0000 |
commit | 023cee968b768ec0dfcbd35373d6195332d5dd76 (patch) | |
tree | 25adc7b9103aa9b46f13d1c6466e073100f0fe0a | |
parent | 55eb4dab2793417e583d02c2ceaa20955442818b (diff) | |
download | gcc-023cee968b768ec0dfcbd35373d6195332d5dd76.zip gcc-023cee968b768ec0dfcbd35373d6195332d5dd76.tar.gz gcc-023cee968b768ec0dfcbd35373d6195332d5dd76.tar.bz2 |
condition_variable (condition_variable_any:: __wait_until_impl): Add.
2010-01-29 Paolo Carlini <paolo.carlini@oracle.com>
* include/std/condition_variable (condition_variable_any::
__wait_until_impl): Add.
(condition_variable_any::wait_until): Provide definitions.
* testsuite/30_threads/condition_variable_any/members/2.cc: New.
* testsuite/30_threads/condition_variable_any/cons/assign_neg.cc:
Adjust dg-error line number.
* testsuite/30_threads/condition_variable_any/cons/copy_neg.cc:
Likewise.
* testsuite/30_threads/condition_variable/members/1.cc: Minor
stylistic changes.
* testsuite/30_threads/condition_variable/members/1.cc: Likewise.
From-SVN: r156367
7 files changed, 136 insertions, 13 deletions
diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 33d7790..5dddc09 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,5 +1,20 @@ 2010-01-29 Paolo Carlini <paolo.carlini@oracle.com> + * include/std/condition_variable (condition_variable_any:: + __wait_until_impl): Add. + (condition_variable_any::wait_until): Provide definitions. + * testsuite/30_threads/condition_variable_any/members/2.cc: New. + * testsuite/30_threads/condition_variable_any/cons/assign_neg.cc: + Adjust dg-error line number. + * testsuite/30_threads/condition_variable_any/cons/copy_neg.cc: + Likewise. + + * testsuite/30_threads/condition_variable/members/1.cc: Minor + stylistic changes. + * testsuite/30_threads/condition_variable/members/1.cc: Likewise. + +2010-01-29 Paolo Carlini <paolo.carlini@oracle.com> + * include/std/condition_variable (enum class cv_status): Add and use it throughout, per N3000. (condition_variable_any::wait<>(_Lock&), wait<>(_Lock&, _Predicate), diff --git a/libstdc++-v3/include/std/condition_variable b/libstdc++-v3/include/std/condition_variable index ea4570e..329b972 100644 --- a/libstdc++-v3/include/std/condition_variable +++ b/libstdc++-v3/include/std/condition_variable @@ -98,10 +98,10 @@ namespace std const chrono::time_point<_Clock, _Duration>& __atime) { // DR 887 - Sync unknown clock to known clock. - typename _Clock::time_point __c_entry = _Clock::now(); - __clock_t::time_point __s_entry = __clock_t::now(); - chrono::nanoseconds __delta = __atime - __c_entry; - __clock_t::time_point __s_atime = __s_entry + __delta; + const typename _Clock::time_point __c_entry = _Clock::now(); + const __clock_t::time_point __s_entry = __clock_t::now(); + const chrono::nanoseconds __delta = __atime - __c_entry; + const __clock_t::time_point __s_atime = __s_entry + __delta; return __wait_until_impl(__lock, __s_atime); } @@ -165,6 +165,7 @@ namespace std // Like above, only mutex may not have try_lock. class condition_variable_any { + typedef chrono::system_clock __clock_t; typedef __gthread_cond_t __native_type; __native_type _M_cond; @@ -201,10 +202,25 @@ namespace std wait(__lock); } + template<typename _Lock, typename _Duration> + cv_status + wait_until(_Lock& __lock, + const chrono::time_point<__clock_t, _Duration>& __atime) + { return __wait_until_impl(__lock, __atime); } + template<typename _Lock, typename _Clock, typename _Duration> cv_status wait_until(_Lock& __lock, - const chrono::time_point<_Clock, _Duration>& __atime); + const chrono::time_point<_Clock, _Duration>& __atime) + { + // DR 887 - Sync unknown clock to known clock. + const typename _Clock::time_point __c_entry = _Clock::now(); + const __clock_t::time_point __s_entry = __clock_t::now(); + const chrono::nanoseconds __delta = __atime - __c_entry; + const __clock_t::time_point __s_atime = __s_entry + __delta; + + return __wait_until_impl(__lock, __s_atime); + } template<typename _Lock, typename _Clock, typename _Duration, typename _Predicate> @@ -232,6 +248,31 @@ namespace std native_handle_type native_handle() { return &_M_cond; } + + private: + template<typename _Lock, typename _Clock, typename _Duration> + cv_status + __wait_until_impl(_Lock& __lock, + const chrono::time_point<_Clock, _Duration>& __atime) + { + chrono::time_point<__clock_t, chrono::seconds> __s = + chrono::time_point_cast<chrono::seconds>(__atime); + + chrono::nanoseconds __ns = + chrono::duration_cast<chrono::nanoseconds>(__atime - __s); + + __gthread_time_t __ts = + { + static_cast<std::time_t>(__s.time_since_epoch().count()), + static_cast<long>(__ns.count()) + }; + + __gthread_cond_timedwait(&_M_cond, __lock.mutex()->native_handle(), + &__ts); + + return (_Clock::now() < __atime + ? cv_status::no_timeout : cv_status::timeout); + } }; // @} group condition_variables diff --git a/libstdc++-v3/testsuite/30_threads/condition_variable/members/1.cc b/libstdc++-v3/testsuite/30_threads/condition_variable/members/1.cc index 127960a..3b94c23 100644 --- a/libstdc++-v3/testsuite/30_threads/condition_variable/members/1.cc +++ b/libstdc++-v3/testsuite/30_threads/condition_variable/members/1.cc @@ -5,7 +5,7 @@ // { dg-require-cstdint "" } // { dg-require-gthreads "" } -// Copyright (C) 2008, 2009 Free Software Foundation, Inc. +// Copyright (C) 2008, 2009, 2010 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 @@ -22,13 +22,12 @@ // with this library; see the file COPYING3. If not see // <http://www.gnu.org/licenses/>. - #include <chrono> #include <condition_variable> #include <system_error> #include <testsuite_hooks.h> -int main() +void test01() { bool test __attribute__((unused)) = true; @@ -53,6 +52,10 @@ int main() { VERIFY( false ); } +} +int main() +{ + test01(); return 0; } diff --git a/libstdc++-v3/testsuite/30_threads/condition_variable/members/2.cc b/libstdc++-v3/testsuite/30_threads/condition_variable/members/2.cc index ab2e877..f4904ad 100644 --- a/libstdc++-v3/testsuite/30_threads/condition_variable/members/2.cc +++ b/libstdc++-v3/testsuite/30_threads/condition_variable/members/2.cc @@ -5,7 +5,7 @@ // { dg-require-cstdint "" } // { dg-require-gthreads "" } -// Copyright (C) 2008, 2009 Free Software Foundation, Inc. +// Copyright (C) 2008, 2009, 2010 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 @@ -22,13 +22,12 @@ // with this library; see the file COPYING3. If not see // <http://www.gnu.org/licenses/>. - #include <chrono> #include <condition_variable> #include <system_error> #include <testsuite_hooks.h> -int main() +void test01() { bool test __attribute__((unused)) = true; @@ -53,6 +52,10 @@ int main() { VERIFY( false ); } +} +int main() +{ + test01(); return 0; } diff --git a/libstdc++-v3/testsuite/30_threads/condition_variable_any/cons/assign_neg.cc b/libstdc++-v3/testsuite/30_threads/condition_variable_any/cons/assign_neg.cc index 5e3e302..8fa79a2 100644 --- a/libstdc++-v3/testsuite/30_threads/condition_variable_any/cons/assign_neg.cc +++ b/libstdc++-v3/testsuite/30_threads/condition_variable_any/cons/assign_neg.cc @@ -32,4 +32,4 @@ void test01() } // { dg-error "used here" "" { target *-*-* } 31 } -// { dg-error "deleted function" "" { target *-*-* } 178 } +// { dg-error "deleted function" "" { target *-*-* } 179 } diff --git a/libstdc++-v3/testsuite/30_threads/condition_variable_any/cons/copy_neg.cc b/libstdc++-v3/testsuite/30_threads/condition_variable_any/cons/copy_neg.cc index 2b61c1b..344f90c 100644 --- a/libstdc++-v3/testsuite/30_threads/condition_variable_any/cons/copy_neg.cc +++ b/libstdc++-v3/testsuite/30_threads/condition_variable_any/cons/copy_neg.cc @@ -31,4 +31,4 @@ void test01() } // { dg-error "used here" "" { target *-*-* } 30 } -// { dg-error "deleted function" "" { target *-*-* } 177 } +// { dg-error "deleted function" "" { target *-*-* } 178 } diff --git a/libstdc++-v3/testsuite/30_threads/condition_variable_any/members/2.cc b/libstdc++-v3/testsuite/30_threads/condition_variable_any/members/2.cc new file mode 100644 index 0000000..2cb21ed --- /dev/null +++ b/libstdc++-v3/testsuite/30_threads/condition_variable_any/members/2.cc @@ -0,0 +1,61 @@ +// { dg-do run { target *-*-freebsd* *-*-netbsd* *-*-linux* *-*-solaris* *-*-cygwin *-*-darwin* alpha*-*-osf* mips-sgi-irix6* } } +// { dg-options " -std=gnu++0x -pthread" { target *-*-freebsd* *-*-netbsd* *-*-linux* alpha*-*-osf* mips-sgi-irix6* } } +// { dg-options " -std=gnu++0x -pthreads" { target *-*-solaris* } } +// { dg-options " -std=gnu++0x " { target *-*-cygwin *-*-darwin* } } +// { dg-require-cstdint "" } +// { dg-require-gthreads "" } + +// Copyright (C) 2008, 2009, 2010 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 +// <http://www.gnu.org/licenses/>. + +#include <chrono> +#include <condition_variable> +#include <system_error> +#include <testsuite_hooks.h> + +void test01() +{ + bool test __attribute__((unused)) = true; + + try + { + std::chrono::microseconds ms(500); + std::condition_variable_any c1; + std::mutex m; + std::unique_lock<std::mutex> l(m); + + auto then = std::chrono::monotonic_clock::now(); + std::cv_status result = c1.wait_until(l, then + ms); + VERIFY( result == std::cv_status::timeout ); + VERIFY( (std::chrono::monotonic_clock::now() - then) >= ms ); + VERIFY( l.owns_lock() ); + } + catch (const std::system_error& e) + { + VERIFY( false ); + } + catch (...) + { + VERIFY( false ); + } +} + +int main() +{ + test01(); + return 0; +} |