2009-01-02 08:07:55 +01:00
|
|
|
/*
|
|
|
|
|
ThreadWrapper(Test) - starting threads and passing context
|
2010-12-17 23:28:49 +01:00
|
|
|
|
2009-01-02 08:07:55 +01:00
|
|
|
Copyright (C) Lumiera.org
|
|
|
|
|
2008, Hermann Vosseler <Ichthyostega@web.de>
|
2010-12-17 23:28:49 +01:00
|
|
|
|
2009-01-02 08:07:55 +01:00
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the GNU General Public License as
|
2010-12-17 23:28:49 +01:00
|
|
|
published by the Free Software Foundation; either version 2 of
|
|
|
|
|
the License, or (at your option) any later version.
|
|
|
|
|
|
2009-01-02 08:07:55 +01:00
|
|
|
This program 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 General Public License for more details.
|
2010-12-17 23:28:49 +01:00
|
|
|
|
2009-01-02 08:07:55 +01:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
2010-12-17 23:28:49 +01:00
|
|
|
|
2009-01-02 08:07:55 +01:00
|
|
|
* *****************************************************/
|
|
|
|
|
|
2017-02-22 01:54:20 +01:00
|
|
|
/** @file thread-wrapper-test.cpp
|
|
|
|
|
** unit test §§TODO§§
|
2016-11-03 18:20:10 +01:00
|
|
|
*/
|
|
|
|
|
|
2009-01-02 08:07:55 +01:00
|
|
|
|
|
|
|
|
#include "lib/test/run.hpp"
|
|
|
|
|
|
2009-01-16 02:18:58 +01:00
|
|
|
#include "backend/thread-wrapper.hpp"
|
2009-01-02 08:07:55 +01:00
|
|
|
#include "lib/sync.hpp"
|
2010-02-04 18:48:47 +01:00
|
|
|
#include "lib/symbol.hpp"
|
2009-01-02 08:07:55 +01:00
|
|
|
|
2014-04-03 22:42:48 +02:00
|
|
|
#include <functional>
|
2009-01-02 08:07:55 +01:00
|
|
|
|
2014-04-03 22:42:48 +02:00
|
|
|
using std::bind;
|
2009-01-02 08:07:55 +01:00
|
|
|
using test::Test;
|
2010-02-04 18:48:47 +01:00
|
|
|
|
2009-01-02 08:07:55 +01:00
|
|
|
|
2009-02-01 01:01:44 +01:00
|
|
|
namespace backend {
|
|
|
|
|
namespace test {
|
2009-01-02 08:07:55 +01:00
|
|
|
|
|
|
|
|
namespace { // private test classes and data...
|
|
|
|
|
|
|
|
|
|
const uint NUM_THREADS = 20;
|
|
|
|
|
const uint MAX_RAND_SUMMAND = 100;
|
|
|
|
|
|
2010-02-04 18:48:47 +01:00
|
|
|
|
|
|
|
|
class Checker
|
|
|
|
|
: public lib::Sync<>
|
|
|
|
|
{
|
|
|
|
|
volatile ulong hot_sum_;
|
|
|
|
|
ulong control_sum_;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
Checker() : hot_sum_(0), control_sum_(0) { }
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
verify() ///< verify test values got handled and accounted
|
|
|
|
|
{
|
|
|
|
|
return 0 < hot_sum_
|
|
|
|
|
&& control_sum_ == hot_sum_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint
|
|
|
|
|
createVal() ///< generating test values, remembering the control sum
|
|
|
|
|
{
|
|
|
|
|
uint val(rand() % MAX_RAND_SUMMAND);
|
|
|
|
|
control_sum_ += val;
|
|
|
|
|
return val;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
addValues (uint a, uint b) ///< to be called concurrently
|
|
|
|
|
{
|
|
|
|
|
Lock guard(this);
|
|
|
|
|
|
|
|
|
|
hot_sum_ *= 2;
|
|
|
|
|
usleep (200); // force preemption
|
|
|
|
|
hot_sum_ += 2 * (a+b);
|
|
|
|
|
usleep (200);
|
|
|
|
|
hot_sum_ /= 2;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Checker checksum; ///< global variable used by multiple threads
|
|
|
|
|
|
|
|
|
|
|
2009-01-02 08:07:55 +01:00
|
|
|
|
|
|
|
|
|
2010-01-20 18:45:48 +01:00
|
|
|
struct TestThread
|
2010-02-04 18:48:47 +01:00
|
|
|
: Thread
|
2009-01-02 08:07:55 +01:00
|
|
|
{
|
|
|
|
|
TestThread()
|
|
|
|
|
: Thread("test Thread creation",
|
2010-02-04 18:48:47 +01:00
|
|
|
bind (&TestThread::theOperation, this, checksum.createVal(), checksum.createVal()))
|
2009-01-02 08:07:55 +01:00
|
|
|
{ } // note the binding (functor object) is passed as anonymous temporary
|
|
|
|
|
|
2010-01-20 18:45:48 +01:00
|
|
|
|
|
|
|
|
void
|
2009-01-02 08:07:55 +01:00
|
|
|
theOperation (uint a, uint b) ///< the actual operation running in a separate thread
|
|
|
|
|
{
|
2010-02-04 18:48:47 +01:00
|
|
|
checksum.addValues (a,b);
|
2009-01-02 08:07:55 +01:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // (End) test classes and data....
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-24 23:06:36 +02:00
|
|
|
/**********************************************************************//**
|
2009-01-02 08:07:55 +01:00
|
|
|
* @test use the Lumiera backend to create some new threads, utilising the
|
|
|
|
|
* lumiera::Thread wrapper for binding to an arbitrary operation
|
|
|
|
|
* and passing the appropriate context.
|
|
|
|
|
*
|
2009-02-01 01:01:44 +01:00
|
|
|
* @see backend::Thread
|
2009-01-02 08:07:55 +01:00
|
|
|
* @see threads.h
|
|
|
|
|
*/
|
|
|
|
|
class ThreadWrapper_test : public Test
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
virtual void
|
|
|
|
|
run (Arg)
|
|
|
|
|
{
|
|
|
|
|
TestThread instances[NUM_THREADS] SIDEEFFECT;
|
|
|
|
|
|
2010-01-20 18:45:48 +01:00
|
|
|
usleep (200000); // pause 200ms for the threads to terminate.....
|
2009-01-11 12:25:44 +01:00
|
|
|
|
2010-12-10 02:55:40 +01:00
|
|
|
CHECK (checksum.verify());
|
2009-01-02 08:07:55 +01:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Register this test class... */
|
|
|
|
|
LAUNCHER (ThreadWrapper_test, "function common");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-02-04 18:48:47 +01:00
|
|
|
}} // namespace backend::test
|