libstdc++
condition_variable
Go to the documentation of this file.
00001 // <condition_variable> -*- C++ -*-
00002 
00003 // Copyright (C) 2008-2018 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 <ext/concurrence.h>
00041 #include <bits/alloc_traits.h>
00042 #include <bits/allocator.h>
00043 #include <bits/unique_ptr.h>
00044 #include <bits/shared_ptr.h>
00045 #include <bits/cxxabi_forced.h>
00046 
00047 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)
00048 
00049 namespace std _GLIBCXX_VISIBILITY(default)
00050 {
00051 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00052 
00053   /**
00054    * @defgroup condition_variables Condition Variables
00055    * @ingroup concurrency
00056    *
00057    * Classes for condition_variable support.
00058    * @{
00059    */
00060 
00061   /// cv_status
00062   enum class cv_status { no_timeout, timeout };
00063 
00064   /// condition_variable
00065   class condition_variable
00066   {
00067     typedef chrono::system_clock        __clock_t;
00068     typedef __gthread_cond_t            __native_type;
00069 
00070 #ifdef __GTHREAD_COND_INIT
00071     __native_type                       _M_cond = __GTHREAD_COND_INIT;
00072 #else
00073     __native_type                       _M_cond;
00074 #endif
00075 
00076   public:
00077     typedef __native_type*              native_handle_type;
00078 
00079     condition_variable() noexcept;
00080     ~condition_variable() noexcept;
00081 
00082     condition_variable(const condition_variable&) = delete;
00083     condition_variable& operator=(const condition_variable&) = delete;
00084 
00085     void
00086     notify_one() noexcept;
00087 
00088     void
00089     notify_all() noexcept;
00090 
00091     void
00092     wait(unique_lock<mutex>& __lock) noexcept;
00093 
00094     template<typename _Predicate>
00095       void
00096       wait(unique_lock<mutex>& __lock, _Predicate __p)
00097       {
00098         while (!__p())
00099           wait(__lock);
00100       }
00101 
00102     template<typename _Duration>
00103       cv_status
00104       wait_until(unique_lock<mutex>& __lock,
00105                  const chrono::time_point<__clock_t, _Duration>& __atime)
00106       { return __wait_until_impl(__lock, __atime); }
00107 
00108     template<typename _Clock, typename _Duration>
00109       cv_status
00110       wait_until(unique_lock<mutex>& __lock,
00111                  const chrono::time_point<_Clock, _Duration>& __atime)
00112       {
00113         // DR 887 - Sync unknown clock to known clock.
00114         const typename _Clock::time_point __c_entry = _Clock::now();
00115         const __clock_t::time_point __s_entry = __clock_t::now();
00116         const auto __delta = __atime - __c_entry;
00117         const auto __s_atime = __s_entry + __delta;
00118 
00119         return __wait_until_impl(__lock, __s_atime);
00120       }
00121 
00122     template<typename _Clock, typename _Duration, typename _Predicate>
00123       bool
00124       wait_until(unique_lock<mutex>& __lock,
00125                  const chrono::time_point<_Clock, _Duration>& __atime,
00126                  _Predicate __p)
00127       {
00128         while (!__p())
00129           if (wait_until(__lock, __atime) == cv_status::timeout)
00130             return __p();
00131         return true;
00132       }
00133 
00134     template<typename _Rep, typename _Period>
00135       cv_status
00136       wait_for(unique_lock<mutex>& __lock,
00137                const chrono::duration<_Rep, _Period>& __rtime)
00138       {
00139         using __dur = typename __clock_t::duration;
00140         auto __reltime = chrono::duration_cast<__dur>(__rtime);
00141         if (__reltime < __rtime)
00142           ++__reltime;
00143         return wait_until(__lock, __clock_t::now() + __reltime);
00144       }
00145 
00146     template<typename _Rep, typename _Period, typename _Predicate>
00147       bool
00148       wait_for(unique_lock<mutex>& __lock,
00149                const chrono::duration<_Rep, _Period>& __rtime,
00150                _Predicate __p)
00151       {
00152         using __dur = typename __clock_t::duration;
00153         auto __reltime = chrono::duration_cast<__dur>(__rtime);
00154         if (__reltime < __rtime)
00155           ++__reltime;
00156         return wait_until(__lock, __clock_t::now() + __reltime, std::move(__p));
00157       }
00158 
00159     native_handle_type
00160     native_handle()
00161     { return &_M_cond; }
00162 
00163   private:
00164     template<typename _Dur>
00165       cv_status
00166       __wait_until_impl(unique_lock<mutex>& __lock,
00167                         const chrono::time_point<__clock_t, _Dur>& __atime)
00168       {
00169         auto __s = chrono::time_point_cast<chrono::seconds>(__atime);
00170         auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
00171 
00172         __gthread_time_t __ts =
00173           {
00174             static_cast<std::time_t>(__s.time_since_epoch().count()),
00175             static_cast<long>(__ns.count())
00176           };
00177 
00178         __gthread_cond_timedwait(&_M_cond, __lock.mutex()->native_handle(),
00179                                  &__ts);
00180 
00181         return (__clock_t::now() < __atime
00182                 ? cv_status::no_timeout : cv_status::timeout);
00183       }
00184   };
00185 
00186   void
00187   notify_all_at_thread_exit(condition_variable&, unique_lock<mutex>);
00188 
00189   struct __at_thread_exit_elt
00190   {
00191     __at_thread_exit_elt* _M_next;
00192     void (*_M_cb)(void*);
00193   };
00194 
00195   inline namespace _V2 {
00196 
00197   /// condition_variable_any
00198   // Like above, but mutex is not required to have try_lock.
00199   class condition_variable_any
00200   {
00201     typedef chrono::system_clock        __clock_t;
00202     condition_variable                  _M_cond;
00203     shared_ptr<mutex>                   _M_mutex;
00204 
00205     // scoped unlock - unlocks in ctor, re-locks in dtor
00206     template<typename _Lock>
00207       struct _Unlock
00208       {
00209         explicit _Unlock(_Lock& __lk) : _M_lock(__lk) { __lk.unlock(); }
00210 
00211         ~_Unlock() noexcept(false)
00212         {
00213           if (uncaught_exception())
00214             {
00215               __try
00216               { _M_lock.lock(); }
00217               __catch(const __cxxabiv1::__forced_unwind&)
00218               { __throw_exception_again; }
00219               __catch(...)
00220               { }
00221             }
00222           else
00223             _M_lock.lock();
00224         }
00225 
00226         _Unlock(const _Unlock&) = delete;
00227         _Unlock& operator=(const _Unlock&) = delete;
00228 
00229         _Lock& _M_lock;
00230       };
00231 
00232   public:
00233     condition_variable_any() : _M_mutex(std::make_shared<mutex>()) { }
00234     ~condition_variable_any() = default;
00235 
00236     condition_variable_any(const condition_variable_any&) = delete;
00237     condition_variable_any& operator=(const condition_variable_any&) = delete;
00238 
00239     void
00240     notify_one() noexcept
00241     {
00242       lock_guard<mutex> __lock(*_M_mutex);
00243       _M_cond.notify_one();
00244     }
00245 
00246     void
00247     notify_all() noexcept
00248     {
00249       lock_guard<mutex> __lock(*_M_mutex);
00250       _M_cond.notify_all();
00251     }
00252 
00253     template<typename _Lock>
00254       void
00255       wait(_Lock& __lock)
00256       {
00257         shared_ptr<mutex> __mutex = _M_mutex;
00258         unique_lock<mutex> __my_lock(*__mutex);
00259         _Unlock<_Lock> __unlock(__lock);
00260         // *__mutex must be unlocked before re-locking __lock so move
00261         // ownership of *__mutex lock to an object with shorter lifetime.
00262         unique_lock<mutex> __my_lock2(std::move(__my_lock));
00263         _M_cond.wait(__my_lock2);
00264       }
00265 
00266 
00267     template<typename _Lock, typename _Predicate>
00268       void
00269       wait(_Lock& __lock, _Predicate __p)
00270       {
00271         while (!__p())
00272           wait(__lock);
00273       }
00274 
00275     template<typename _Lock, typename _Clock, typename _Duration>
00276       cv_status
00277       wait_until(_Lock& __lock,
00278                  const chrono::time_point<_Clock, _Duration>& __atime)
00279       {
00280         shared_ptr<mutex> __mutex = _M_mutex;
00281         unique_lock<mutex> __my_lock(*__mutex);
00282         _Unlock<_Lock> __unlock(__lock);
00283         // *__mutex must be unlocked before re-locking __lock so move
00284         // ownership of *__mutex lock to an object with shorter lifetime.
00285         unique_lock<mutex> __my_lock2(std::move(__my_lock));
00286         return _M_cond.wait_until(__my_lock2, __atime);
00287       }
00288 
00289     template<typename _Lock, typename _Clock,
00290              typename _Duration, typename _Predicate>
00291       bool
00292       wait_until(_Lock& __lock,
00293                  const chrono::time_point<_Clock, _Duration>& __atime,
00294                  _Predicate __p)
00295       {
00296         while (!__p())
00297           if (wait_until(__lock, __atime) == cv_status::timeout)
00298             return __p();
00299         return true;
00300       }
00301 
00302     template<typename _Lock, typename _Rep, typename _Period>
00303       cv_status
00304       wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __rtime)
00305       { return wait_until(__lock, __clock_t::now() + __rtime); }
00306 
00307     template<typename _Lock, typename _Rep,
00308              typename _Period, typename _Predicate>
00309       bool
00310       wait_for(_Lock& __lock,
00311                const chrono::duration<_Rep, _Period>& __rtime, _Predicate __p)
00312       { return wait_until(__lock, __clock_t::now() + __rtime, std::move(__p)); }
00313   };
00314 
00315   } // end inline namespace
00316 
00317   // @} group condition_variables
00318 _GLIBCXX_END_NAMESPACE_VERSION
00319 } // namespace
00320 
00321 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
00322 
00323 #endif // C++11
00324 
00325 #endif // _GLIBCXX_CONDITION_VARIABLE