OpenWalnut  1.4.0
WConditionSet.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 WCONDITIONSET_H
00026 #define WCONDITIONSET_H
00027 
00028 #include <map>
00029 #include <utility>
00030 
00031 #include <boost/shared_ptr.hpp>
00032 #include <boost/thread.hpp>
00033 
00034 #include "WCondition.h"
00035 
00036 
00037 /**
00038  * Class allowing multiple conditions to be used for one waiting cycle. Since wait() can not be used for waiting on multiple
00039  * conditions, this class can encapsulate multiple conditions and offer a wait() command to wait for one of them to change its
00040  * state. Please not that this class can also be used as condition.
00041  */
00042 class WConditionSet: public WCondition
00043 {
00044 friend class WConditionSetTest;
00045 public:
00046     /**
00047      * Shared pointer to instance of this class.
00048      */
00049     typedef boost::shared_ptr< WConditionSet > SPtr;
00050 
00051     /**
00052      * Shared pointer to const instance of this class.
00053      */
00054     typedef boost::shared_ptr< const WConditionSet > ConstSPtr;
00055 
00056     /**
00057      * Default constructor.
00058      */
00059     WConditionSet();
00060 
00061     /**
00062      * Destructor.
00063      */
00064     virtual ~WConditionSet();
00065 
00066     /**
00067      * Adds another condition to the set of conditions to wait for. Note that, whenever someone is waiting for this WConditionSet,
00068      * the newly added one is also directly included into the wait() call.
00069      *
00070      * \param condition the condition to add.
00071      */
00072     virtual void add( boost::shared_ptr< WCondition > condition );
00073 
00074     /**
00075      * Removes the specified condition. As add() this immediately takes effect on running wait() calls.
00076      *
00077      * \param condition the condition to remove
00078      */
00079     virtual void remove( boost::shared_ptr< WCondition > condition );
00080 
00081     /**
00082      * Wait for the condition. Sets the calling thread asleep. If the condition set is resetable, this will return immediately
00083      * when a condition in the set fired in the past and there has been no reset() call until now.
00084      */
00085     virtual void wait() const;
00086 
00087     /**
00088      * Resets the internal fire state. This does nothing if !isResetable().
00089      */
00090     virtual void reset() const;
00091 
00092     /**
00093      * Sets the resetable flag. This causes the condition set to act like a WConditionOneShot. There are several implications to
00094      * this you should consider when using the condition set as a resetable. If one condition in the condition set fires, a
00095      * subsequent call to wait() will immediately return until a reset() call has been done. If you share one condition set among
00096      * several threads, you should consider, that one thread can reset the condition set before the other thread had a chance to
00097      * call wait() which causes the other thread to wait until the next condition in the set fires.
00098      *
00099      * \param resetable true if the fire state should be delayed and can be reseted.
00100      * \param autoReset true if the state should be reset whenever a wait call is called and continues.This is especially useful if a
00101      * condition set is used only by one thread, so there is no need to call reset() explicitly.
00102      */
00103     void setResetable( bool resetable = true, bool autoReset = true );
00104 
00105     /**
00106      * Returns whether the condition set acts like a one shot condition.
00107      *
00108      * \return true if the fire state is delayed and can be reseted.
00109      */
00110     bool isResetable();
00111 
00112 protected:
00113     /**
00114      * Flag denoting whether the condition set should act like a one shot condition.
00115      */
00116     bool m_resetable;
00117 
00118     /**
00119      * Flag which shows whether the wait() call should reset the state m_fired when it returns.
00120      */
00121     bool m_autoReset;
00122 
00123     /**
00124      * We need to keep track of the connections a condition has made since boost::function objects do not provide a == operator and can therefore
00125      * not easily be removed from a signals by signal.desconnect( functor ).
00126      */
00127     typedef std::map< boost::shared_ptr< WCondition >, boost::signals2::connection > ConditionConnectionMap;
00128 
00129     /**
00130      * Set of conditions to be waited for.
00131      */
00132     ConditionConnectionMap m_conditionSet;
00133 
00134     /**
00135      * Each condition has a connection.
00136      */
00137     typedef std::pair< boost::shared_ptr< WCondition >, boost::signals2::connection > ConditionConnectionPair;
00138 
00139     /**
00140      * Lock used for thread-safe writing to the condition set.
00141      */
00142     boost::shared_mutex m_conditionSetLock;
00143 
00144     /**
00145      * Notifier function getting notified whenever a condition got fired.
00146      */
00147     virtual void conditionFired();
00148 
00149     /**
00150      * Flag denoting whether one condition fired in the past. Just useful when m_resetable is true.
00151      */
00152     mutable bool m_fired;
00153 
00154     /**
00155      * The notifier which gets called by all conditions if they fire
00156      */
00157     WCondition::t_ConditionNotifierType m_notifier;
00158 
00159 private:
00160 };
00161 
00162 #endif  // WCONDITIONSET_H
00163