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
|
2017-02-22 03:17:18 +01:00
|
|
|
** unit test \ref ThreadWrapper_test
|
2016-11-03 18:20:10 +01:00
|
|
|
*/
|
|
|
|
|
|
2009-01-02 08:07:55 +01:00
|
|
|
|
|
|
|
|
#include "lib/test/run.hpp"
|
2023-09-29 18:45:47 +02:00
|
|
|
#include "lib/test/test-helper.hpp"///////////TODO
|
2023-09-21 23:23:55 +02:00
|
|
|
#include "lib/thread.hpp"
|
2023-09-29 18:45:47 +02:00
|
|
|
#include "lib/iter-explorer.hpp"
|
2023-09-29 17:46:24 +02:00
|
|
|
#include "lib/scoped-collection.hpp"
|
2009-01-02 08:07:55 +01:00
|
|
|
|
2023-09-29 18:45:47 +02:00
|
|
|
#include <atomic>
|
2023-09-29 17:46:24 +02:00
|
|
|
#include <chrono>
|
2009-01-02 08:07:55 +01:00
|
|
|
|
|
|
|
|
using test::Test;
|
2023-09-29 18:45:47 +02:00
|
|
|
using lib::explore;
|
|
|
|
|
using std::atomic_uint;
|
|
|
|
|
using std::this_thread::yield;
|
2023-09-29 17:46:24 +02:00
|
|
|
using std::this_thread::sleep_for;
|
|
|
|
|
using std::chrono::microseconds;
|
2010-02-04 18:48:47 +01:00
|
|
|
|
2009-01-02 08:07:55 +01:00
|
|
|
|
2023-09-21 23:23:55 +02:00
|
|
|
namespace lib {
|
2009-02-01 01:01:44 +01:00
|
|
|
namespace test {
|
2009-01-02 08:07:55 +01:00
|
|
|
|
|
|
|
|
namespace { // private test classes and data...
|
|
|
|
|
|
2023-09-29 17:46:24 +02:00
|
|
|
const uint NUM_THREADS = 200;
|
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
|
|
|
{
|
2023-09-29 17:46:24 +02:00
|
|
|
using Thread::Thread;
|
2009-01-02 08:07:55 +01:00
|
|
|
|
2023-09-29 17:46:24 +02:00
|
|
|
uint local{0};
|
2010-01-20 18:45:48 +01:00
|
|
|
|
|
|
|
|
void
|
2023-09-29 17:46:24 +02:00
|
|
|
doIt (uint a, uint b) ///< the actual operation running in a separate thread
|
2009-01-02 08:07:55 +01:00
|
|
|
{
|
2023-09-29 17:46:24 +02:00
|
|
|
uint sum = a + b;
|
|
|
|
|
sleep_for (microseconds{sum});
|
|
|
|
|
local = sum;
|
2009-01-02 08:07:55 +01:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // (End) test classes and data....
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-24 23:06:36 +02:00
|
|
|
/**********************************************************************//**
|
2018-11-16 22:38:29 +01:00
|
|
|
* @test use the Lumiera Vault to create some new threads, utilising the
|
2009-01-02 08:07:55 +01:00
|
|
|
* lumiera::Thread wrapper for binding to an arbitrary operation
|
|
|
|
|
* and passing the appropriate context.
|
|
|
|
|
*
|
2018-11-15 23:59:23 +01:00
|
|
|
* @see vault::Thread
|
2009-01-02 08:07:55 +01:00
|
|
|
* @see threads.h
|
|
|
|
|
*/
|
|
|
|
|
class ThreadWrapper_test : public Test
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
virtual void
|
|
|
|
|
run (Arg)
|
2023-09-29 18:45:47 +02:00
|
|
|
{
|
|
|
|
|
demonstrateSimpleUsage();
|
|
|
|
|
verifyConcurrentExecution();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @test demonstrate simple usage of the thread-wrapper
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
demonstrateSimpleUsage()
|
|
|
|
|
{
|
|
|
|
|
lib::ScopedCollection<Thread> threads{NUM_THREADS};
|
|
|
|
|
|
|
|
|
|
atomic_uint invocations{0};
|
|
|
|
|
for (uint i=0; i<NUM_THREADS; ++i)
|
|
|
|
|
threads.emplace<Thread> ("counter"
|
|
|
|
|
,[&]{ ++invocations; });
|
|
|
|
|
|
|
|
|
|
while (explore(threads).has_any())
|
|
|
|
|
yield();
|
|
|
|
|
|
|
|
|
|
CHECK (invocations == NUM_THREADS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @test verify the thread function is actually performed concurrently
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
verifyConcurrentExecution()
|
2009-01-02 08:07:55 +01:00
|
|
|
{
|
2023-09-29 17:46:24 +02:00
|
|
|
lib::ScopedCollection<TestThread> threads{NUM_THREADS};
|
|
|
|
|
|
|
|
|
|
size_t globalSum = 0;
|
|
|
|
|
for (uint i=1; i<=NUM_THREADS; ++i)
|
|
|
|
|
{
|
|
|
|
|
uint x = rand() % 1000;
|
|
|
|
|
globalSum += (i + x);
|
|
|
|
|
threads.emplace<TestThread> (&TestThread::doIt, uint{i}, uint{x});
|
|
|
|
|
}
|
2009-01-02 08:07:55 +01:00
|
|
|
|
2023-09-29 18:45:47 +02:00
|
|
|
while (explore(threads).has_any())
|
|
|
|
|
yield();
|
2009-01-11 12:25:44 +01:00
|
|
|
|
2023-09-29 17:46:24 +02:00
|
|
|
size_t checkSum = 0;
|
|
|
|
|
for (auto& t : threads)
|
|
|
|
|
{
|
|
|
|
|
CHECK (not t);
|
|
|
|
|
CHECK (0 < t.local);
|
|
|
|
|
checkSum += t.local;
|
|
|
|
|
}
|
|
|
|
|
CHECK (checkSum == globalSum);
|
|
|
|
|
|
2009-01-02 08:07:55 +01:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Register this test class... */
|
|
|
|
|
LAUNCHER (ThreadWrapper_test, "function common");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-09-21 23:23:55 +02:00
|
|
|
}} // namespace lib::test
|