[v3] More of condition_variable_any

Jonathan Wakely jwakely.gcc@gmail.com
Tue Feb 2 21:57:00 GMT 2010


Here's another implementation of std::condition_variable_any.  Unless
anyone can see anything wrong I plan to commit it to trunk.

I've removed the notify_one and notify_all exports as they are not
exported in any released version, and can be defined line in the
header.  I'd prefer to also define the constructor and destructor
inline but that would change exports that are already part of
libstdc++.so.6.0.13 (unfortunate, because condition_variable_any was
unusable until now.)

The removed tests checked that condition_variable_any is a
standard-layout type and has the same size+alignment as
pthread_cond_t, but neither of those is required by the WP nor IMHO
necessary.

2010-02-02  Jonathan Wakely  <jwakely.gcc@gmail.com>

        * include/std/condition_variable (condition_variable): Prevent ADL.
        (condition_variable_any): Provide definitions for all members.
        * src/condition_variable.cc (condition_variable_any): Adjust
        definitions.
        * config/abi/pre/gnu.ver: Adjust exports for condition_variable_any.
        * 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_any/members/1.cc: New.
        * testsuite/30_threads/condition_variable_any/members/2.cc: New.
        * testsuite/30_threads/condition_variable_any/requirements/
        standard_layout.cc: Remove.
        * testsuite/30_threads/condition_variable_any/native_handle/
        typesizes.cc: Remove.

Tested x86_64/Linux
-------------- next part --------------
Index: include/std/condition_variable
===================================================================
--- include/std/condition_variable	(revision 156433)
+++ include/std/condition_variable	(working copy)
@@ -90,7 +90,7 @@ namespace std
       cv_status
       wait_until(unique_lock<mutex>& __lock,
 		 const chrono::time_point<__clock_t, _Duration>& __atime)
-      { return __wait_until_impl(__lock, __atime); }
+      { return this->__wait_until_impl(__lock, __atime); }
 
     template<typename _Clock, typename _Duration>
       cv_status
@@ -103,7 +103,7 @@ namespace std
 	const chrono::nanoseconds __delta = __atime - __c_entry;
 	const __clock_t::time_point __s_atime = __s_entry + __delta;
 
-	return __wait_until_impl(__lock, __s_atime);
+	return this->__wait_until_impl(__lock, __s_atime);
       }
 
     template<typename _Clock, typename _Duration, typename _Predicate>
@@ -113,7 +113,7 @@ namespace std
 		 _Predicate __p)
       {
 	while (!__p())
-	  if (wait_until(__lock, __atime) == cv_status::timeout)
+	  if (this->wait_until(__lock, __atime) == cv_status::timeout)
 	    return __p();
 	return true;
       }
@@ -122,14 +122,17 @@ namespace std
       cv_status
       wait_for(unique_lock<mutex>& __lock,
 	       const chrono::duration<_Rep, _Period>& __rtime)
-      { return wait_until(__lock, __clock_t::now() + __rtime); }
+      { return this->wait_until(__lock, __clock_t::now() + __rtime); }
 
     template<typename _Rep, typename _Period, typename _Predicate>
       bool
       wait_for(unique_lock<mutex>& __lock,
 	       const chrono::duration<_Rep, _Period>& __rtime,
 	       _Predicate __p)
-      { return wait_until(__lock, __clock_t::now() + __rtime, std::move(__p)); }
+      {
+        return this->wait_until(__lock, __clock_t::now() + __rtime,
+                                std::move(__p));
+      }
 
     native_handle_type
     native_handle()
@@ -162,14 +165,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,27 +182,49 @@ 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
       wait(_Lock& __lock, _Predicate __p)
       {
 	while (!__p())
-	  wait(__lock);
+	  this->wait(__lock);
       }
 
     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>
@@ -208,24 +234,29 @@ namespace std
 		 _Predicate __p)
       {
 	while (!__p())
-	  if (wait_until(__lock, __atime) == cv_status::timeout)
+	  if (this->wait_until(__lock, __atime) == cv_status::timeout)
 	    return __p();
 	return true;
       }
 
     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 this->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 this->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 *-*-* } 182 }
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 *-*-* } 181 }
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