OSDN Git Service

2008-09-03 Chris Fairles <chris.fairles@gmail.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / std / condition_variable
index 1a7a7cd..f2035d6 100644 (file)
 # include <c++0x_warning.h>
 #else
 
+#include <chrono>
 #include <mutex> // unique_lock
 
+#if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)
+
 namespace std 
 {
-  namespace chrono
-  {
-    template<typename _Rep, typename _Period>
-      struct duration;
-
-    template<typename _Clock, typename _Duration>
-      struct time_point;
-  }
-
   /// condition_variable
   class condition_variable
   {
   public:
-
-#if __GTHREAD_HAS_COND
-    typedef __gthread_cond_t native_handle_type;
-#else
-    typedef int native_handle_type;
-#endif
+    typedef __gthread_cond_t* native_handle_type;
 
     condition_variable();
     ~condition_variable();
 
+    condition_variable(const condition_variable&) = delete;
+    condition_variable& operator=(const condition_variable&) = delete;
+
     void 
     notify_one();
 
@@ -87,7 +79,23 @@ namespace std
     template<typename _Clock, typename _Duration>
       bool 
       wait_until(unique_lock<mutex>& __lock, 
-                const chrono::time_point<_Clock, _Duration>& __atime);
+                const chrono::time_point<_Clock, _Duration>& __atime)
+      {
+       chrono::time_point<_Clock, 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_t::now() < __atime;
+      }
 
     template<typename _Clock, typename _Duration, typename _Predicate>
       bool
@@ -98,7 +106,8 @@ namespace std
     template<typename _Rep, typename _Period>
       bool
       wait_for(unique_lock<mutex>& __lock,
-              const chrono::duration<_Rep, _Period>& __rtime);
+              const chrono::duration<_Rep, _Period>& __rtime)
+      { return __wait_for_impl(__rtime); }
 
     template<typename _Rep, typename _Period, typename _Predicate>
       bool
@@ -107,12 +116,42 @@ namespace std
               _Predicate __p);
 
     native_handle_type 
-    native_handle() { return _M_cond; }
+    native_handle() 
+    { return &_M_cond; }
 
   private:
-    native_handle_type _M_cond;
-    condition_variable(const condition_variable&);
-    condition_variable& operator=(const condition_variable&);
+    __gthread_cond_t _M_cond;
+    mutex _M_internal_mutex;
+
+#ifdef _GLIBCXX_USE_CLOCK_MONOTONIC
+    typedef chrono::monotonic_clock __clock_t;
+#else
+    typedef chrono::high_resolution_clock __clock_t;
+#endif
+
+    template<typename _Rep, typename _Period>
+      typename enable_if<
+        ratio_less_equal<__clock_t::period, _Period>::value, bool>::type
+      __wait_for_impl(unique_lock<mutex>& __lock,
+                     const chrono::duration<_Rep, _Period>& __rtime)
+      { 
+       __clock_t::time_point __atime = __clock_t::now()
+         + chrono::duration_cast<__clock_t::duration>(__rtime);
+       
+       return wait_until(__lock, __atime);
+      }
+    
+    template<typename _Rep, typename _Period>
+      typename enable_if<
+        !ratio_less_equal<__clock_t::period, _Period>::value, bool>::type
+      __wait_for_impl(unique_lock<mutex>& __lock,
+                     const chrono::duration<_Rep, _Period>& __rtime)
+      { 
+       __clock_t::time_point __atime = __clock_t::now()
+         + ++chrono::duration_cast<__clock_t::duration>(__rtime);
+       
+       return wait_until(__lock, __atime);
+      }    
   };
 
   /// condition_variable_any
@@ -120,14 +159,13 @@ namespace std
   class condition_variable_any
   {
   public:
-#if __GTHREAD_HAS_COND
-    typedef __gthread_cond_t native_handle_type;
-#else
-    typedef int native_handle_type;
-#endif
+    typedef __gthread_cond_t* native_handle_type;
 
     condition_variable_any();
     ~condition_variable_any();
+    
+    condition_variable_any(const condition_variable_any&) = delete;
+    condition_variable_any& operator=(const condition_variable_any&) = delete;
 
     void 
     notify_one();
@@ -166,16 +204,16 @@ namespace std
               const chrono::duration<_Rep, _Period>& __rtime, _Predicate __p);
 
     native_handle_type 
-    native_handle() { return _M_cond; }
+    native_handle()
+    { return &_M_cond; }
 
   private:
-    native_handle_type _M_cond;
-    condition_variable_any(const condition_variable_any&);
-    condition_variable_any& operator=(const condition_variable_any&);
+    __gthread_cond_t _M_cond;
   };
-
 }
 
+#endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
+
 #endif // __GXX_EXPERIMENTAL_CXX0X__
 
 #endif // _GLIBCXX_CONDITION_VARIABLE