OpenWalnut  1.4.0
WCondition.h
00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #ifndef WCONDITION_H
00026 #define WCONDITION_H
00027 
00028 #include <boost/shared_ptr.hpp>
00029 #include <boost/function.hpp>
00030 #include <boost/signals2/signal.hpp>
00031 #include <boost/thread.hpp>
00032 
00033 
00034 
00035 /**
00036  * Class to encapsulate boost::condition_variable_any. You may use it to efficiently wait for events (a condition comes true). It
00037  * is a very simple implementation. It might be extended easily. Timed wait functions and so on.
00038  */
00039 class WCondition // NOLINT
00040 {
00041     friend class WCondition_test;
00042 public:
00043     /**
00044      * Shared pointer type for WCondition.
00045      */
00046     typedef boost::shared_ptr< WCondition > SPtr;
00047 
00048     /**
00049      * Const shared pointer type for WCondition.
00050      */
00051     typedef boost::shared_ptr< const WCondition > ConstSPtr;
00052 
00053     /**
00054      * Default constructor.
00055      */
00056     WCondition();
00057 
00058     /**
00059      * Destructor.
00060      */
00061     virtual ~WCondition();
00062 
00063     /**
00064      * Wait for the condition. Sets the calling thread asleep.
00065      */
00066     virtual void wait() const;
00067 
00068     /**
00069      * Notifies all waiting threads.
00070      */
00071     virtual void notify();
00072 
00073     /**
00074      * Type used for signalling condition changes.
00075      */
00076     typedef boost::function0< void > t_ConditionNotifierType;
00077 
00078     /**
00079      * Subscribes a specified function to be notified on condition change.
00080      *
00081      * \param notifier the notifier function
00082      *
00083      * \return the connection.
00084      */
00085     boost::signals2::connection subscribeSignal( t_ConditionNotifierType notifier ) const;
00086 
00087 protected:
00088     /**
00089      * Type used for condition notification.
00090      */
00091     typedef boost::signals2::signal<void ( void )>  t_ConditionSignalType;
00092 
00093     /**
00094      * Signal getting fired whenever the condition fires.
00095      */
00096     mutable t_ConditionSignalType signal_ConditionFired;
00097 
00098     /**
00099      * The condition.
00100      */
00101     mutable boost::condition_variable_any m_condition;
00102 
00103     /**
00104      * The mutex used for the condition.
00105      */
00106     mutable boost::shared_mutex m_mutex;
00107 
00108 private:
00109 };
00110 
00111 #endif  // WCONDITION_H
00112