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