libstdc++
condition_variable
Go to the documentation of this file.
00001 // <condition_variable> -*- C++ -*-
00002 
00003 // Copyright (C) 2008-2019 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 3, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /** @file include/condition_variable
00026  *  This is a Standard C++ Library header.
00027  */
00028 
00029 #ifndef _GLIBCXX_CONDITION_VARIABLE
00030 #define _GLIBCXX_CONDITION_VARIABLE 1
00031 
00032 #pragma GCC system_header
00033 
00034 #if __cplusplus < 201103L
00035 # include <bits/c++0x_warning.h>
00036 #else
00037 
00038 #include <chrono>
00039 #include <bits/std_mutex.h>
00040 #include <bits/unique_lock.h>
00041 #include <ext/concurrence.h>
00042 #include <bits/alloc_traits.h>
00043 #include <bits/allocator.h>
00044 #include <bits/unique_ptr.h>
00045 #include <bits/shared_ptr.h>
00046 #include <bits/cxxabi_forced.h>
00047 
00048 #if defined(_GLIBCXX_HAS_GTHREADS)
00049 
00050 namespace std _GLIBCXX_VISIBILITY(default)
00051 {
00052 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00053 
00054   /**
00055    * @defgroup condition_variables Condition Variables
00056    * @ingroup concurrency
00057    *
00058    * Classes for condition_variable support.
00059    * @{
00060    */
00061 
00062   /// cv_status
00063   enum class cv_status { no_timeout, timeout };
00064 
00065   /// condition_variable
00066   class condition_variable
00067   {
00068     typedef chrono::system_clock        __clock_t;
00069     typedef chrono::steady_clock        __steady_clock_t;
00070     typedef __gthread_cond_t            __native_type;
00071 
00072 #ifdef __GTHREAD_COND_INIT
00073     __native_type                       _M_cond = __GTHREAD_COND_INIT;
00074 #else
00075     __native_type                       _M_cond;
00076 #endif
00077 
00078   public:
00079     typedef __native_type*              native_handle_type;
00080 
00081     condition_variable() noexcept;
00082     ~condition_variable() noexcept;
00083 
00084     condition_variable(const condition_variable&) = delete;
00085     condition_variable& operator=(const condition_variable&) = delete;
00086 
00087     void
00088     notify_one() noexcept;
00089 
00090     void
00091     notify_all() noexcept;
00092 
00093     void
00094     wait(unique_lock<mutex>& __lock) noexcept;
00095 
00096     template<typename _Predicate>
00097       void
00098       wait(unique_lock<mutex>& __lock, _Predicate __p)
00099       {
00100         while (!__p())
00101           wait(__lock);
00102       }
00103 
00104     template<typename _Duration>
00105       cv_status
00106       wait_until(unique_lock<mutex>& __lock,
00107                  const chrono::time_point<__clock_t, _Duration>& __atime)
00108       { return __wait_until_impl(__lock, __atime); }
00109 
00110     template<typename _Clock, typename _Duration>
00111       cv_status
00112       wait_until(unique_lock<mutex>& __lock,
00113                  const chrono::time_point<_Clock, _Duration>& __atime)
00114       {
00115         // DR 887 - Sync unknown clock to known clock.
00116         const typename _Clock::time_point __c_entry = _Clock::now();
00117         const __clock_t::time_point __s_entry = __clock_t::now();
00118         const auto __delta = __atime - __c_entry;
00119         const auto __s_atime = __s_entry + __delta;
00120 
00121         if (__wait_until_impl(__lock, __s_atime) == cv_status::no_timeout)
00122           return cv_status::no_timeout;
00123         // We got a timeout when measured against __clock_t but
00124         // we need to check against the caller-supplied clock
00125         // to tell whether we should return a timeout.
00126         if (_Clock::now() < __atime)
00127           return cv_status::no_timeout;
00128         return cv_status::timeout;
00129       }
00130 
00131     template<typename _Clock, typename _Duration, typename _Predicate>
00132       bool
00133       wait_until(unique_lock<mutex>& __lock,
00134                  const chrono::time_point<_Clock, _Duration>& __atime,
00135                  _Predicate __p)
00136       {
00137         while (!__p())
00138           if (wait_until(__lock, __atime) == cv_status::timeout)
00139             return __p();
00140         return true;
00141       }
00142 
00143     template<typename _Rep, typename _Period>
00144       cv_status
00145       wait_for(unique_lock<mutex>& __lock,
00146                const chrono::duration<_Rep, _Period>& __rtime)
00147       {
00148         using __dur = typename __steady_clock_t::duration;
00149         auto __reltime = chrono::duration_cast<__dur>(__rtime);
00150         if (__reltime < __rtime)
00151           ++__reltime;
00152         return wait_until(__lock, __steady_clock_t::now() + __reltime);
00153       }
00154 
00155     template<typename _Rep, typename _Period, typename _Predicate>
00156       bool
00157       wait_for(unique_lock<mutex>& __lock,
00158                const chrono::duration<_Rep, _Period>& __rtime,
00159                _Predicate __p)
00160       {
00161         using __dur = typename __steady_clock_t::duration;
00162         auto __reltime = chrono::duration_cast<__dur>(__rtime);
00163         if (__reltime < __rtime)
00164           ++__reltime;
00165         return wait_until(__lock, __steady_clock_t::now() + __reltime,
00166                           std::move(__p));
00167       }
00168 
00169     native_handle_type
00170     native_handle()
00171     { return &_M_cond; }
00172 
00173   private:
00174     template<typename _Dur>
00175       cv_status
00176       __wait_until_impl(unique_lock<mutex>& __lock,
00177                         const chrono::time_point<__clock_t, _Dur>& __atime)
00178       {
00179         auto __s = chrono::time_point_cast<chrono::seconds>(__atime);
00180         auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
00181 
00182         __gthread_time_t __ts =
00183           {
00184             static_cast<std::time_t>(__s.time_since_epoch().count()),
00185             static_cast<long>(__ns.count())
00186           };
00187 
00188         __gthread_cond_timedwait(&_M_cond, __lock.mutex()->native_handle(),
00189                                  &__ts);
00190 
00191         return (__clock_t::now() < __atime
00192                 ? cv_status::no_timeout : cv_status::timeout);
00193       }
00194   };
00195 
00196   void
00197   notify_all_at_thread_exit(condition_variable&, unique_lock<mutex>);
00198 
00199   struct __at_thread_exit_elt
00200   {
00201     __at_thread_exit_elt* _M_next;
00202     void (*_M_cb)(void*);
00203   };
00204 
00205   inline namespace _V2 {
00206 
00207   /// condition_variable_any
00208   // Like above, but mutex is not required to have try_lock.
00209   class condition_variable_any
00210   {
00211     typedef chrono::system_clock        __clock_t;
00212     condition_variable                  _M_cond;
00213     shared_ptr<mutex>                   _M_mutex;
00214 
00215     // scoped unlock - unlocks in ctor, re-locks in dtor
00216     template<typename _Lock>
00217       struct _Unlock
00218       {
00219         explicit _Unlock(_Lock& __lk) : _M_lock(__lk) { __lk.unlock(); }
00220 
00221         ~_Unlock() noexcept(false)
00222         {
00223           if (uncaught_exception())
00224             {
00225               __try
00226               { _M_lock.lock(); }
00227               __catch(const __cxxabiv1::__forced_unwind&)
00228               { __throw_exception_again; }
00229               __catch(...)
00230               { }
00231             }
00232           else
00233             _M_lock.lock();
00234         }
00235 
00236         _Unlock(const _Unlock&) = delete;
00237         _Unlock& operator=(const _Unlock&) = delete;
00238 
00239         _Lock& _M_lock;
00240       };
00241 
00242   public:
00243     condition_variable_any() : _M_mutex(std::make_shared<mutex>()) { }
00244     ~condition_variable_any() = default;
00245 
00246     condition_variable_any(const condition_variable_any&) = delete;
00247     condition_variable_any& operator=(const condition_variable_any&) = delete;
00248 
00249     void
00250     notify_one() noexcept
00251     {
00252       lock_guard<mutex> __lock(*_M_mutex);
00253       _M_cond.notify_one();
00254     }
00255 
00256     void
00257     notify_all() noexcept
00258     {
00259       lock_guard<mutex> __lock(*_M_mutex);
00260       _M_cond.notify_all();
00261     }
00262 
00263     template<typename _Lock>
00264       void
00265       wait(_Lock& __lock)
00266       {
00267         shared_ptr<mutex> __mutex = _M_mutex;
00268         unique_lock<mutex> __my_lock(*__mutex);
00269         _Unlock<_Lock> __unlock(__lock);
00270         // *__mutex must be unlocked before re-locking __lock so move
00271         // ownership of *__mutex lock to an object with shorter lifetime.
00272         unique_lock<mutex> __my_lock2(std::move(__my_lock));
00273         _M_cond.wait(__my_lock2);
00274       }
00275 
00276 
00277     template<typename _Lock, typename _Predicate>
00278       void
00279       wait(_Lock& __lock, _Predicate __p)
00280       {
00281         while (!__p())
00282           wait(__lock);
00283       }
00284 
00285     template<typename _Lock, typename _Clock, typename _Duration>
00286       cv_status
00287       wait_until(_Lock& __lock,
00288                  const chrono::time_point<_Clock, _Duration>& __atime)
00289       {
00290         shared_ptr<mutex> __mutex = _M_mutex;
00291         unique_lock<mutex> __my_lock(*__mutex);
00292         _Unlock<_Lock> __unlock(__lock);
00293         // *__mutex must be unlocked before re-locking __lock so move
00294         // ownership of *__mutex lock to an object with shorter lifetime.
00295         unique_lock<mutex> __my_lock2(std::move(__my_lock));
00296         return _M_cond.wait_until(__my_lock2, __atime);
00297       }
00298 
00299     template<typename _Lock, typename _Clock,
00300              typename _Duration, typename _Predicate>
00301       bool
00302       wait_until(_Lock& __lock,
00303                  const chrono::time_point<_Clock, _Duration>& __atime,
00304                  _Predicate __p)
00305       {
00306         while (!__p())
00307           if (wait_until(__lock, __atime) == cv_status::timeout)
00308             return __p();
00309         return true;
00310       }
00311 
00312     template<typename _Lock, typename _Rep, typename _Period>
00313       cv_status
00314       wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __rtime)
00315       { return wait_until(__lock, __clock_t::now() + __rtime); }
00316 
00317     template<typename _Lock, typename _Rep,
00318              typename _Period, typename _Predicate>
00319       bool
00320       wait_for(_Lock& __lock,
00321                const chrono::duration<_Rep, _Period>& __rtime, _Predicate __p)
00322       { return wait_until(__lock, __clock_t::now() + __rtime, std::move(__p)); }
00323   };
00324 
00325   } // end inline namespace
00326 
00327   // @} group condition_variables
00328 _GLIBCXX_END_NAMESPACE_VERSION
00329 } // namespace
00330 
00331 #endif // _GLIBCXX_HAS_GTHREADS
00332 #endif // C++11
00333 #endif // _GLIBCXX_CONDITION_VARIABLE