[v3] More of condition_variable_any

Jonathan Wakely jwakely.gcc@gmail.com
Tue Feb 2 23:01:00 GMT 2010


On 2 February 2010 22:29, Paolo Carlini wrote:
> Great!
>
> I'm really curious about this kind of tweak:
>
> -      { return __wait_until_impl(__lock, __atime); }
> +      { return this->__wait_until_impl(__lock, __atime); }
>
> for sure, unless there are very good reasons (i.e, a template base
> class, etc), we are not consistent in the library. Is there a good
> technical reason, or it's just a kind of stylistic choice? Because if
> it's just matter of style, and there are no subtle lookup issues (I'm
> not aware of any for member functions) I would be in favor of not adding
> more qualifications... In fact, I think we briefly discussed that with
> Benjamin a few years ago and in that occasion we *removed* many...

I was thinking ADL could find another function, because __atime can
contain user-defined types from arbitrary namespaces, but I should
have thought about it properly -- there are no lookup issues.

Here's a new patch, without 'this' qualifiers.  Tested x86_64/Linux again.
-------------- next part --------------
Index: include/std/condition_variable
===================================================================
--- include/std/condition_variable	(revision 156433)
+++ include/std/condition_variable	(working copy)
@@ -162,14 +162,15 @@ namespace std
   };
 
   /// condition_variable_any
-  // Like above, only mutex may not have try_lock.
+  // Like above, but mutex is not required to have try_lock.
   class condition_variable_any
   {
-    typedef __gthread_cond_t		__native_type;
-    __native_type			_M_cond;
+    typedef chrono::system_clock	__clock_t;
+    condition_variable			_M_cond;
+    mutex				_M_mutex;
 
   public:
-    typedef __native_type* 		native_handle_type;
+    typedef condition_variable::native_handle_type	native_handle_type;
 
     condition_variable_any() throw ();
     ~condition_variable_any() throw ();
@@ -178,14 +179,29 @@ namespace std
     condition_variable_any& operator=(const condition_variable_any&) = delete;
 
     void
-    notify_one();
+    notify_one()
+    {
+      lock_guard<mutex> __lock(_M_mutex);
+      _M_cond.notify_one();
+    }
 
     void
-    notify_all();
+    notify_all()
+    {
+      lock_guard<mutex> __lock(_M_mutex);
+      _M_cond.notify_all();
+    }
 
     template<typename _Lock>
       void
-      wait(_Lock& __lock);
+      wait(_Lock& __lock)
+      {
+        unique_lock<mutex> __my_lock(_M_mutex);
+        __lock.unlock();
+        _M_cond.wait(__my_lock);
+        __lock.lock();
+      }
+      
 
     template<typename _Lock, typename _Predicate>
       void
@@ -198,7 +214,14 @@ namespace std
     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)
+      {
+        unique_lock<mutex> __my_lock(_M_mutex);
+        __lock.unlock();
+        cv_status __status = _M_cond.wait_until(__my_lock, __atime);
+        __lock.lock();
+        return __status;
+      }
 
     template<typename _Lock, typename _Clock,
 	     typename _Duration, typename _Predicate>
@@ -215,17 +238,19 @@ namespace std
 
     template<typename _Lock, typename _Rep, typename _Period>
       cv_status
-      wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __rtime);
+      wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __rtime)
+      { return wait_until(__lock, __clock_t::now() + __rtime); }
 
     template<typename _Lock, typename _Rep,
 	     typename _Period, typename _Predicate>
       bool
       wait_for(_Lock& __lock,
-	       const chrono::duration<_Rep, _Period>& __rtime, _Predicate __p);
+	       const chrono::duration<_Rep, _Period>& __rtime, _Predicate __p)
+      { return wait_until(__lock, __clock_t::now() + __rtime, std::move(__p)); }
 
     native_handle_type
     native_handle()
-    { return &_M_cond; }
+    { return _M_cond.native_handle(); }
   };
 
   // @} group condition_variables
Index: src/condition_variable.cc
===================================================================
--- src/condition_variable.cc	(revision 156433)
+++ src/condition_variable.cc	(working copy)
@@ -80,44 +80,10 @@ namespace std
   }
 
   condition_variable_any::condition_variable_any() throw ()
-  {
-#ifdef __GTHREAD_COND_INIT
-    __native_type __tmp = __GTHREAD_COND_INIT;
-    _M_cond = __tmp;
-#else
-    int __e = __gthread_cond_init(&_M_cond, NULL);
-
-    if (__e)
-      __throw_system_error(__e);
-#endif
-  }
+  { }
 
   condition_variable_any::~condition_variable_any() throw ()
-  {
-    __gthread_cond_destroy(&_M_cond);
-  }
-
-  void
-  condition_variable_any::notify_one()
-  {
-    int __e = __gthread_cond_signal(&_M_cond);
-
-    // XXX not in spec
-    // EINVAL
-    if (__e)
-      __throw_system_error(__e);
-  }
-
-  void
-  condition_variable_any::notify_all()
-  {
-    int __e = __gthread_cond_broadcast(&_M_cond);
-
-    // XXX not in spec
-    // EINVAL
-    if (__e)
-      __throw_system_error(__e);
-  }
+  { }
 }
 
 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
Index: config/abi/pre/gnu.ver
===================================================================
--- config/abi/pre/gnu.ver	(revision 156433)
+++ config/abi/pre/gnu.ver	(working copy)
@@ -1113,10 +1113,6 @@ GLIBCXX_3.4.14 {
     # std::time_get::_M_extract_wday_or_month
     _ZNKSt8time_getI[cw]St19istreambuf_iteratorI[cw]St11char_traitsI[cw]EEE24_M_extract_wday_or_month*;
 
-    # condition_variable_any::notify_*
-    _ZNSt22condition_variable_any10notify_allEv;
-    _ZNSt22condition_variable_any10notify_oneEv;    
-
 } GLIBCXX_3.4.13;
 
 # Symbols in the support library (libsupc++) have their own tag.
Index: testsuite/30_threads/condition_variable_any/cons/assign_neg.cc
===================================================================
--- testsuite/30_threads/condition_variable_any/cons/assign_neg.cc	(revision 156433)
+++ testsuite/30_threads/condition_variable_any/cons/assign_neg.cc	(working copy)
@@ -32,4 +32,4 @@ void test01()
 }
 
 // { dg-error "used here" "" { target *-*-* } 31 }
-// { dg-error "deleted function" "" { target *-*-* } 178 }
+// { dg-error "deleted function" "" { target *-*-* } 179 }
Index: testsuite/30_threads/condition_variable_any/cons/copy_neg.cc
===================================================================
--- testsuite/30_threads/condition_variable_any/cons/copy_neg.cc	(revision 156433)
+++ testsuite/30_threads/condition_variable_any/cons/copy_neg.cc	(working copy)
@@ -31,4 +31,4 @@ void test01()
 }
 
 // { dg-error "used here" "" { target *-*-* } 30 }
-// { dg-error "deleted function" "" { target *-*-* } 177 }
+// { dg-error "deleted function" "" { target *-*-* } 178 }
Index: testsuite/30_threads/condition_variable_any/members/1.cc
===================================================================
--- testsuite/30_threads/condition_variable_any/members/1.cc	(revision 0)
+++ testsuite/30_threads/condition_variable_any/members/1.cc	(revision 0)
@@ -0,0 +1,85 @@
+// { 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) 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>
+
+struct Mutex
+{
+  Mutex() : locked(false) { }
+
+  void lock()
+  {
+    if (locked)
+      throw locked;
+    mtx.lock();
+    locked = true;
+  }
+
+  void unlock()
+  {
+    if (!locked)
+      throw locked;
+    mtx.unlock();
+    locked = false;
+  }
+
+  std::mutex mtx;
+  bool locked;
+};
+
+void test01()
+{
+  bool test __attribute__((unused)) = true;
+
+  try 
+    {
+      std::chrono::microseconds ms(500);
+      std::condition_variable_any c1;
+      Mutex m;
+      m.lock();
+
+      auto then = std::chrono::system_clock::now();
+      std::cv_status result = c1.wait_for(m, ms);
+      VERIFY( result == std::cv_status::timeout );
+      VERIFY( (std::chrono::system_clock::now() - then) >= ms );
+      VERIFY( m.locked );
+    }
+  catch (const std::system_error& e)
+    {
+      VERIFY( false );
+    }
+  catch (...)
+    {
+      VERIFY( false );
+    }
+}
+
+int main()
+{
+  test01();
+  return 0;
+}
Index: testsuite/30_threads/condition_variable_any/members/2.cc
===================================================================
--- testsuite/30_threads/condition_variable_any/members/2.cc	(revision 0)
+++ testsuite/30_threads/condition_variable_any/members/2.cc	(revision 0)
@@ -0,0 +1,86 @@
+// { 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) 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>
+
+struct Mutex
+{
+  Mutex() : locked(false) { }
+
+  void lock()
+  {
+    if (locked)
+      throw locked;
+    mtx.lock();
+    locked = true;
+  }
+
+  void unlock()
+  {
+    if (!locked)
+      throw locked;
+    mtx.unlock();
+    locked = false;
+  }
+
+  std::mutex mtx;
+  bool locked;
+};
+
+
+void test01()
+{
+  bool test __attribute__((unused)) = true;
+
+  try 
+    {
+      std::chrono::microseconds ms(500);
+      std::condition_variable_any c1;
+      Mutex m;
+      m.lock();
+
+      auto then = std::chrono::monotonic_clock::now();
+      std::cv_status result = c1.wait_until(m, then + ms);
+      VERIFY( result == std::cv_status::timeout );
+      VERIFY( (std::chrono::monotonic_clock::now() - then) >= ms );
+      VERIFY( m.locked );
+    }
+  catch (const std::system_error& e)
+    {
+      VERIFY( false );
+    }
+  catch (...)
+    {
+      VERIFY( false );
+    }
+}
+
+int main()
+{
+  test01();
+  return 0;
+}
Index: testsuite/30_threads/condition_variable_any/native_handle/typesizes.cc
===================================================================
--- testsuite/30_threads/condition_variable_any/native_handle/typesizes.cc	(revision 156433)
+++ testsuite/30_threads/condition_variable_any/native_handle/typesizes.cc	(working copy)
@@ -1,33 +0,0 @@
-// { 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) 2009 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 <condition_variable>
-#include <thread/all.h>
-
-int main()
-{
-  typedef std::condition_variable_any test_type;
-  __gnu_test::compare_type_to_native_type<test_type>();
-  return 0;
-}
Index: testsuite/30_threads/condition_variable_any/requirements/standard_layout.cc
===================================================================
--- testsuite/30_threads/condition_variable_any/requirements/standard_layout.cc	(revision 156433)
+++ testsuite/30_threads/condition_variable_any/requirements/standard_layout.cc	(working copy)
@@ -1,30 +0,0 @@
-// { dg-do compile }
-// { dg-options "-std=gnu++0x" }
-// { dg-require-cstdint "" }
-// { dg-require-gthreads "" }
-
-// Copyright (C) 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 <condition_variable>
-#include <testsuite_common_types.h>
-
-void test01()
-{
-  __gnu_test::standard_layout test;
-  test.operator()<std::condition_variable_any>();
-}


More information about the Libstdc++ mailing list