Commit d9a3c34c authored by Sebastian Eichelbaum's avatar Sebastian Eichelbaum
Browse files

[ADD] - new type of condition added

parent b347ecaa
......@@ -44,3 +44,13 @@ void WCondition::notify()
m_condition.notify_all();
}
boost::signals2::connection WCondition::subscribeSignal( t_ConditionNotifierType notifier )
{
return signal_ConditionFired.connect( notifier );
}
void WCondition::unsubscribeSignal( t_ConditionNotifierType notifier )
{
signal_ConditionFired.disconnect( &notifier );
}
......@@ -25,6 +25,8 @@
#ifndef WCONDITION_H
#define WCONDITION_H
#include <boost/function.hpp>
#include <boost/signals2/signal.hpp>
#include <boost/thread.hpp>
/**
......@@ -51,13 +53,44 @@ public:
*/
virtual void wait() const;
/**
/**
* Notifies all waiting threads.
*/
virtual void notify();
/**
* Type used for signalling condition changes.
*/
typedef boost::function<void ( boost::shared_ptr< WCondition > )> t_ConditionNotifierType;
/**
* Subscribes a specified function to be notified on condition change.
*
* \param notifier the notifier function
*
* \return the connection.
*/
boost::signals2::connection subscribeSignal( t_ConditionNotifierType notifier );
/**
* Disconnects a specified function from the signal.
*
* \param notifier the notifier function
*/
void unsubscribeSignal( t_ConditionNotifierType notifier );
protected:
/**
* Type used for condition notification.
*/
typedef boost::signals2::signal<void ( boost::shared_ptr< WCondition > )> t_ConditionSignalType;
/**
* Signal getting fired whenever the condition fires.
*/
t_ConditionSignalType signal_ConditionFired;
/**
* The condition.
*/
......
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#include "WConditionSet.h"
WConditionSet::WConditionSet():
WCondition()
{
}
WConditionSet::~WConditionSet()
{
// clean conditions list
m_conditionSet.clear();
}
void WConditionSet::add( boost::shared_ptr< WCondition > condition )
{
// get write lock
boost::unique_lock<boost::shared_mutex> lock = boost::unique_lock<boost::shared_mutex>( m_conditionSetLock );
m_conditionSet.insert( condition );
lock.unlock();
condition->subscribeSignal( boost::bind( &WConditionSet::conditionFired, this, _1 ) );
}
void WConditionSet::remove( boost::shared_ptr< WCondition > condition )
{
condition->unsubscribeSignal( boost::bind( &WConditionSet::conditionFired, this, _1 ) );
// get write lock
boost::unique_lock<boost::shared_mutex> lock = boost::unique_lock<boost::shared_mutex>( m_conditionSetLock );
m_conditionSet.erase( condition );
lock.unlock();
}
void WConditionSet::conditionFired( boost::shared_ptr< WCondition > /* con */ )
{
notify();
}
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------
#ifndef WCONDITIONSET_H
#define WCONDITIONSET_H
#include <set>
#include <boost/shared_ptr.hpp>
#include <boost/thread.hpp>
#include "WCondition.h"
/**
* Class allowing multiple conditions to be used for one waiting cycle. Since wait() can not be used for waiting on multiple
* conditions, this class can encapsulate multiple conditions and offer a wait() command to wait for one of them to change its
* state. Please not that this class can also be used as condition.
*/
class WConditionSet: public WCondition
{
public:
/**
* Default constructor.
*/
WConditionSet();
/**
* Destructor.
*/
virtual ~WConditionSet();
/**
* Adds another condition to the set of conditions to wait for. Note that, whenever someone is waiting for this WConditionSet,
* the newly added one is also directly included into the wait() call.
*
* \param condition the condition to add.
*/
virtual void add( boost::shared_ptr< WCondition > condition );
/**
* Removes the specified condition. As add() this immediately takes effect on running wait() calls.
*
* \param condition the condition to remove
*/
virtual void remove( boost::shared_ptr< WCondition > condition );
protected:
/**
* Set of conditions to be waited for.
*/
std::set< boost::shared_ptr< WCondition > > m_conditionSet;
/**
* Lock used for thread-safe writing to the condition set.
*/
boost::shared_mutex m_conditionSetLock;
/**
* Notifier function getting notified whenever a condition got fired.
*
* \param con the connection that actually fires.
*/
virtual void conditionFired( boost::shared_ptr< WCondition > con );
private:
};
#endif // WCONDITIONSET_H
......@@ -36,9 +36,9 @@ class WFlag
{
public:
/**
/**
* Constructor. Uses a given condition to realize the wait/notify functionality.
*
*
* \param condition the condition to use. NOTE: can also be a WConditionOneShot.
* \param initial the initial value of this flag
*/
......@@ -49,47 +49,47 @@ public:
*/
virtual ~WFlag();
/**
/**
* Operator returns value of the flag.
*
*
* \return the value.
*/
virtual const T get() const;
/**
/**
* Operator returns value of the flag.
*
*
* \return the value.
*/
virtual const T operator()() const;
/**
/**
* Wait for the flag to change its value.
*/
virtual void wait() const;
/**
/**
* Sets the new value for this flag. Also notifies waiting threads.
*
*
* \param value the new value
*/
virtual void set( T value );
/**
/**
* Sets the new value for this flag. Also notifies waiting threads.
*
*
* \param value the new value
*/
virtual void operator()( T value );
protected:
/**
/**
* The condition to be used for waiting/notifying. Please note, that it gets deleted during destruction.
*/
WCondition* m_condition;
/**
/**
* The flag value.
*/
T m_flag;
......@@ -97,7 +97,7 @@ protected:
private:
};
/**
/**
* Alias for easy usage of WFLag< bool >.
*/
typedef WFlag< bool > WBoolFlag;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment