OpenWalnut 1.2.5

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