Commit 7721fab4 authored by Sebastian Eichelbaum's avatar Sebastian Eichelbaum
Browse files

[ADD] - Condition classes encapsulating boost::condition_variable

parent 3b3068e1
//---------------------------------------------------------------------------
//
// 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 "WCondition.h"
WCondition::WCondition()
{
// initialize members
}
WCondition::~WCondition()
{
// cleanup
}
void WCondition::wait()
{
m_condition.wait( m_mutex );
}
void WCondition::notify()
{
m_condition.notify_all();
}
//---------------------------------------------------------------------------
//
// 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 WCONDITION_H
#define WCONDITION_H
#include <boost/thread.hpp>
/**
* Class to encapsulate boost::condition_variable_any. You may use it to efficiently wait for events (a condition comes true). It
* is a very simple implementation. It might be extended easily. Timed wait functions and so on.
*/
class WCondition
{
friend class WCondition_test;
public:
/**
* Default constructor.
*/
WCondition();
/**
* Destructor.
*/
virtual ~WCondition();
/**
* Wait for the condition. Sets the calling thread asleep.
*/
virtual void wait();
/**
* Notifies all waiting threads.
*/
virtual void notify();
protected:
/**
* The condition.
*/
boost::condition_variable_any m_condition;
/**
* The mutex used for the condition.
*/
boost::shared_mutex m_mutex;
private:
};
#endif // WCONDITION_H
//---------------------------------------------------------------------------
//
// 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 WCONDITION_TEST_H
#define WCONDITION_TEST_H
#include <iostream>
#include <boost/thread.hpp>
#include <cxxtest/TestSuite.h>
#include "../WCondition.h"
/**
* Helper class.
*/
class Callable
{
public:
bool flag;
WCondition* c;
void threadMain()
{
flag = true;
boost::this_thread::sleep( boost::posix_time::seconds( 1 ) );
c->notify();
};
};
/**
* Test WCondition
*/
class WConditionTest : public CxxTest::TestSuite
{
public:
/**
* An instantiation should never throw an exception, as well as tear down.
*/
void testInstantiation( void )
{
WCondition* c;
TS_ASSERT_THROWS_NOTHING( c = new WCondition() );
TS_ASSERT_THROWS_NOTHING( delete c );
}
/**
* Test whether notification is working.
*/
void testWaitNotify()
{
WCondition c;
Callable t;
t.flag = false;
t.c = &c;
// start a thread
boost::thread thread = boost::thread( boost::bind( &Callable::threadMain, &t ) );
// wait for condition
c.wait();
TS_ASSERT( t.flag );
}
};
#endif // WCONDITION_TEST_H
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