1 // <condition_variable> -*- C++ -*-
 
    3 // Copyright (C) 2008-2014 Free Software Foundation, Inc.
 
    5 // This file is part of the GNU ISO C++ Library.  This library is free
 
    6 // software; you can redistribute it and/or modify it under the
 
    7 // terms of the GNU General Public License as published by the
 
    8 // Free Software Foundation; either version 3, or (at your option)
 
   11 // This library is distributed in the hope that it will be useful,
 
   12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
 
   13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
   14 // GNU General Public License for more details.
 
   16 // Under Section 7 of GPL version 3, you are granted additional
 
   17 // permissions described in the GCC Runtime Library Exception, version
 
   18 // 3.1, as published by the Free Software Foundation.
 
   20 // You should have received a copy of the GNU General Public License and
 
   21 // a copy of the GCC Runtime Library Exception along with this program;
 
   22 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
 
   23 // <http://www.gnu.org/licenses/>.
 
   25 /** @file include/condition_variable
 
   26  *  This is a Standard C++ Library header.
 
   29 #ifndef _GLIBCXX_CONDITION_VARIABLE
 
   30 #define _GLIBCXX_CONDITION_VARIABLE 1
 
   32 #pragma GCC system_header
 
   34 #if __cplusplus < 201103L
 
   35 # include <bits/c++0x_warning.h>
 
   40 #include <ext/concurrence.h>
 
   41 #include <bits/alloc_traits.h>
 
   42 #include <bits/allocator.h>
 
   43 #include <bits/unique_ptr.h>
 
   44 #include <bits/shared_ptr.h>
 
   46 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)
 
   48 namespace std _GLIBCXX_VISIBILITY(default)
 
   50 _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
   53    * @defgroup condition_variables Condition Variables
 
   54    * @ingroup concurrency
 
   56    * Classes for condition_variable support.
 
   61   enum class cv_status { no_timeout, timeout };
 
   63   /// condition_variable
 
   64   class condition_variable
 
   66     typedef chrono::system_clock   __clock_t;
 
   67     typedef __gthread_cond_t       __native_type;
 
   69 #ifdef __GTHREAD_COND_INIT
 
   70     __native_type          _M_cond = __GTHREAD_COND_INIT;
 
   72     __native_type          _M_cond;
 
   76     typedef __native_type*         native_handle_type;
 
   78     condition_variable() noexcept;
 
   79     ~condition_variable() noexcept;
 
   81     condition_variable(const condition_variable&) = delete;
 
   82     condition_variable& operator=(const condition_variable&) = delete;
 
   85     notify_one() noexcept;
 
   88     notify_all() noexcept;
 
   91     wait(unique_lock<mutex>& __lock);
 
   93     template<typename _Predicate>
 
   95       wait(unique_lock<mutex>& __lock, _Predicate __p)
 
  101     template<typename _Duration>
 
  103       wait_until(unique_lock<mutex>& __lock,
 
  104         const chrono::time_point<__clock_t, _Duration>& __atime)
 
  105       { return __wait_until_impl(__lock, __atime); }
 
  107     template<typename _Clock, typename _Duration>
 
  109       wait_until(unique_lock<mutex>& __lock,
 
  110         const chrono::time_point<_Clock, _Duration>& __atime)
 
  112    // DR 887 - Sync unknown clock to known clock.
 
  113    const typename _Clock::time_point __c_entry = _Clock::now();
 
  114    const __clock_t::time_point __s_entry = __clock_t::now();
 
  115    const auto __delta = __atime - __c_entry;
 
  116    const auto __s_atime = __s_entry + __delta;
 
  118    return __wait_until_impl(__lock, __s_atime);
 
  121     template<typename _Clock, typename _Duration, typename _Predicate>
 
  123       wait_until(unique_lock<mutex>& __lock,
 
  124         const chrono::time_point<_Clock, _Duration>& __atime,
 
  128      if (wait_until(__lock, __atime) == cv_status::timeout)
 
  133     template<typename _Rep, typename _Period>
 
  135       wait_for(unique_lock<mutex>& __lock,
 
  136           const chrono::duration<_Rep, _Period>& __rtime)
 
  137       { return wait_until(__lock, __clock_t::now() + __rtime); }
 
  139     template<typename _Rep, typename _Period, typename _Predicate>
 
  141       wait_for(unique_lock<mutex>& __lock,
 
  142           const chrono::duration<_Rep, _Period>& __rtime,
 
  144       { return wait_until(__lock, __clock_t::now() + __rtime, std::move(__p)); }
 
  151     template<typename _Dur>
 
  153       __wait_until_impl(unique_lock<mutex>& __lock,
 
  154            const chrono::time_point<__clock_t, _Dur>& __atime)
 
  156    auto __s = chrono::time_point_cast<chrono::seconds>(__atime);
 
  157    auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
 
  159    __gthread_time_t __ts =
 
  161        static_cast<std::time_t>(__s.time_since_epoch().count()),
 
  162        static_cast<long>(__ns.count())
 
  165    __gthread_cond_timedwait(&_M_cond, __lock.mutex()->native_handle(),
 
  168    return (__clock_t::now() < __atime
 
  169        ? cv_status::no_timeout : cv_status::timeout);
 
  173   inline namespace _V2 {
 
  175   /// condition_variable_any
 
  176   // Like above, but mutex is not required to have try_lock.
 
  177   class condition_variable_any
 
  179     typedef chrono::system_clock   __clock_t;
 
  180     condition_variable         _M_cond;
 
  181     shared_ptr<mutex>          _M_mutex;
 
  183     // scoped unlock - unlocks in ctor, re-locks in dtor
 
  184     template<typename _Lock>
 
  187    explicit _Unlock(_Lock& __lk) : _M_lock(__lk) { __lk.unlock(); }
 
  189    ~_Unlock() noexcept(false)
 
  191      if (uncaught_exception())
 
  195          __catch(const __cxxabiv1::__forced_unwind&)
 
  196          { __throw_exception_again; }
 
  204    _Unlock(const _Unlock&) = delete;
 
  205    _Unlock& operator=(const _Unlock&) = delete;
 
  211     condition_variable_any() : _M_mutex(std::make_shared<mutex>()) { }
 
  212     ~condition_variable_any() = default;
 
  214     condition_variable_any(const condition_variable_any&) = delete;
 
  215     condition_variable_any& operator=(const condition_variable_any&) = delete;
 
  218     notify_one() noexcept
 
  220       lock_guard<mutex> __lock(*_M_mutex);
 
  221       _M_cond.notify_one();
 
  225     notify_all() noexcept
 
  227       lock_guard<mutex> __lock(*_M_mutex);
 
  228       _M_cond.notify_all();
 
  231     template<typename _Lock>
 
  235    shared_ptr<mutex> __mutex = _M_mutex;
 
  236    unique_lock<mutex> __my_lock(*__mutex);
 
  237    _Unlock<_Lock> __unlock(__lock);
 
  238    // *__mutex must be unlocked before re-locking __lock so move
 
  239    // ownership of *__mutex lock to an object with shorter lifetime.
 
  240    unique_lock<mutex> __my_lock2(std::move(__my_lock));
 
  241    _M_cond.wait(__my_lock2);
 
  245     template<typename _Lock, typename _Predicate>
 
  247       wait(_Lock& __lock, _Predicate __p)
 
  253     template<typename _Lock, typename _Clock, typename _Duration>
 
  255       wait_until(_Lock& __lock,
 
  256         const chrono::time_point<_Clock, _Duration>& __atime)
 
  258    shared_ptr<mutex> __mutex = _M_mutex;
 
  259    unique_lock<mutex> __my_lock(*__mutex);
 
  260    _Unlock<_Lock> __unlock(__lock);
 
  261    // *__mutex must be unlocked before re-locking __lock so move
 
  262    // ownership of *__mutex lock to an object with shorter lifetime.
 
  263    unique_lock<mutex> __my_lock2(std::move(__my_lock));
 
  264    return _M_cond.wait_until(__my_lock2, __atime);
 
  267     template<typename _Lock, typename _Clock,
 
  268         typename _Duration, typename _Predicate>
 
  270       wait_until(_Lock& __lock,
 
  271         const chrono::time_point<_Clock, _Duration>& __atime,
 
  275      if (wait_until(__lock, __atime) == cv_status::timeout)
 
  280     template<typename _Lock, typename _Rep, typename _Period>
 
  282       wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __rtime)
 
  283       { return wait_until(__lock, __clock_t::now() + __rtime); }
 
  285     template<typename _Lock, typename _Rep,
 
  286         typename _Period, typename _Predicate>
 
  288       wait_for(_Lock& __lock,
 
  289           const chrono::duration<_Rep, _Period>& __rtime, _Predicate __p)
 
  290       { return wait_until(__lock, __clock_t::now() + __rtime, std::move(__p)); }
 
  293   } // end inline namespace
 
  295   // @} group condition_variables
 
  296 _GLIBCXX_END_NAMESPACE_VERSION
 
  299 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
 
  303 #endif // _GLIBCXX_CONDITION_VARIABLE