2008-12-14 04:34:40 +01:00
|
|
|
/*
|
2008-12-22 17:00:15 +01:00
|
|
|
SyncLocking(Test) - check the monitor object based locking
|
2010-12-17 23:28:49 +01:00
|
|
|
|
2008-12-14 04:34:40 +01:00
|
|
|
Copyright (C) Lumiera.org
|
|
|
|
|
2008, Hermann Vosseler <Ichthyostega@web.de>
|
2010-12-17 23:28:49 +01:00
|
|
|
|
2008-12-14 04:34:40 +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.
|
|
|
|
|
|
2008-12-14 04:34:40 +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
|
|
|
|
2008-12-14 04:34:40 +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
|
|
|
|
2008-12-14 04:34:40 +01:00
|
|
|
* *****************************************************/
|
|
|
|
|
|
2017-02-22 01:54:20 +01:00
|
|
|
/** @file sync-locking-test.cpp
|
2017-02-22 03:17:18 +01:00
|
|
|
** unit test \ref SyncLocking_test
|
2016-11-03 18:20:10 +01:00
|
|
|
*/
|
|
|
|
|
|
2008-12-14 04:34:40 +01:00
|
|
|
|
2008-12-20 06:33:36 +01:00
|
|
|
#include "lib/test/run.hpp"
|
2008-12-27 01:47:21 +01:00
|
|
|
#include "lib/error.hpp"
|
2008-12-14 04:34:40 +01:00
|
|
|
|
2008-12-22 17:00:15 +01:00
|
|
|
#include "lib/sync.hpp"
|
2023-09-21 23:23:55 +02:00
|
|
|
#include "lib/thread.hpp"
|
2008-12-14 04:34:40 +01:00
|
|
|
|
|
|
|
|
#include <iostream>
|
2014-04-03 22:42:48 +02:00
|
|
|
#include <functional>
|
2010-02-11 03:06:42 +01:00
|
|
|
|
2014-04-03 22:42:48 +02:00
|
|
|
using std::bind;
|
2008-12-14 04:34:40 +01:00
|
|
|
|
|
|
|
|
using std::cout;
|
|
|
|
|
using test::Test;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace lib {
|
2010-02-11 03:06:42 +01:00
|
|
|
namespace test{
|
2008-12-14 04:34:40 +01:00
|
|
|
|
2010-02-11 03:06:42 +01:00
|
|
|
namespace { // private test classes and data...
|
|
|
|
|
|
|
|
|
|
const uint NUM_COUNTERS = 20; ///< number of independent counters to increment in parallel
|
|
|
|
|
const uint NUM_THREADS = 10; ///< number of threads trying to increment these counters
|
|
|
|
|
const uint MAX_PAUSE = 10000; ///< maximum delay implemented as empty counting loop
|
|
|
|
|
const uint MAX_SUM = 1000; ///< trigger when to finish incrementing
|
|
|
|
|
const uint MAX_INC = 10; ///< maximum increment on each step
|
2008-12-14 04:34:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-02-11 03:06:42 +01:00
|
|
|
class Victim
|
|
|
|
|
: public Sync<RecursiveLock_NoWait>
|
2008-12-14 04:34:40 +01:00
|
|
|
{
|
2011-05-20 20:31:16 +02:00
|
|
|
volatile uint cnt_[NUM_COUNTERS];
|
2010-02-11 03:06:42 +01:00
|
|
|
volatile uint step_; ///< @note stored as instance variable
|
2008-12-14 04:34:40 +01:00
|
|
|
|
2010-02-11 03:06:42 +01:00
|
|
|
void
|
|
|
|
|
pause ()
|
2008-12-14 04:34:40 +01:00
|
|
|
{
|
2023-10-15 20:42:55 +02:00
|
|
|
Lock guard{this}; // note recursive lock
|
2008-12-14 04:34:40 +01:00
|
|
|
|
2010-02-11 03:06:42 +01:00
|
|
|
for ( uint i=0, lim=(rand() % MAX_PAUSE); i<lim; ++i)
|
|
|
|
|
;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
incrementAll ()
|
|
|
|
|
{
|
|
|
|
|
for (uint i=0; i<NUM_COUNTERS; ++i)
|
2008-12-14 04:34:40 +01:00
|
|
|
{
|
2010-02-11 03:06:42 +01:00
|
|
|
pause();
|
|
|
|
|
cnt_[i] += step_;
|
2008-12-14 04:34:40 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-11 03:06:42 +01:00
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
Victim ()
|
|
|
|
|
{
|
|
|
|
|
for (uint i=0; i<NUM_COUNTERS; ++i)
|
|
|
|
|
cnt_[i] = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
inc (uint newStep)
|
|
|
|
|
{
|
2023-10-15 20:42:55 +02:00
|
|
|
Lock guard{this};
|
2010-02-11 03:06:42 +01:00
|
|
|
step_ = newStep;
|
|
|
|
|
incrementAll();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
belowLimit ()
|
|
|
|
|
{
|
2023-10-15 20:42:55 +02:00
|
|
|
Lock guard{this};
|
2010-02-11 03:06:42 +01:00
|
|
|
return cnt_[0] < MAX_SUM;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
checkAllEqual ()
|
|
|
|
|
{
|
|
|
|
|
for (uint i=1; i<NUM_COUNTERS; ++i)
|
|
|
|
|
if (cnt_[i-1] != cnt_[i])
|
|
|
|
|
return false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
report ()
|
|
|
|
|
{
|
|
|
|
|
for (uint i=0; i<NUM_COUNTERS; ++i)
|
|
|
|
|
cout << "Counter-#" << i << " = " << cnt_[i] << "\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ourVictim;
|
2008-12-14 04:34:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-02-11 03:06:42 +01:00
|
|
|
/**
|
|
|
|
|
* A Thread trying to increment all victim counters in sync...
|
|
|
|
|
*/
|
|
|
|
|
class HavocThread
|
|
|
|
|
{
|
2023-09-28 01:09:07 +02:00
|
|
|
ThreadJoinable<> thread_;
|
2010-02-11 03:06:42 +01:00
|
|
|
|
|
|
|
|
void
|
|
|
|
|
doIt ()
|
|
|
|
|
{
|
|
|
|
|
while (ourVictim.belowLimit())
|
|
|
|
|
ourVictim.inc (rand() % MAX_INC);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
HavocThread ()
|
|
|
|
|
: thread_("HavocThread"
|
|
|
|
|
, bind (&HavocThread::doIt, this)
|
|
|
|
|
)
|
|
|
|
|
{
|
2023-09-25 16:27:38 +02:00
|
|
|
CHECK (thread_);
|
2010-02-11 03:06:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~HavocThread ()
|
|
|
|
|
{
|
2023-09-25 16:27:38 +02:00
|
|
|
if (thread_)
|
2010-02-11 03:06:42 +01:00
|
|
|
thread_.join();
|
|
|
|
|
}
|
|
|
|
|
};
|
2008-12-14 04:34:40 +01:00
|
|
|
|
2010-02-11 03:06:42 +01:00
|
|
|
} // (End) test classes and data....
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-24 23:06:36 +02:00
|
|
|
/******************************************************************//**
|
2010-02-11 03:06:42 +01:00
|
|
|
* @test create multiple threads, all concurrently trying to increment
|
|
|
|
|
* a number of counters with random steps and random pauses. Without
|
|
|
|
|
* locking, the likely result will be differing counters.
|
|
|
|
|
* But because the class Victim uses an object level monitor to
|
|
|
|
|
* guard the mutations, the state should remain consistent.
|
|
|
|
|
*
|
|
|
|
|
* @see SyncWaiting_test condition based wait/notify
|
|
|
|
|
* @see SyncClasslock_test locking a type, not an instance
|
|
|
|
|
* @see sync.hpp
|
|
|
|
|
*/
|
|
|
|
|
class SyncLocking_test : public Test
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
virtual void
|
|
|
|
|
run (Arg)
|
|
|
|
|
{
|
2010-12-10 03:00:17 +01:00
|
|
|
CHECK (ourVictim.checkAllEqual());
|
2010-02-11 03:06:42 +01:00
|
|
|
{
|
|
|
|
|
HavocThread threads[NUM_THREADS] SIDEEFFECT;
|
|
|
|
|
}
|
|
|
|
|
// all finished and joined here...
|
|
|
|
|
|
|
|
|
|
if (!ourVictim.checkAllEqual())
|
|
|
|
|
{
|
|
|
|
|
cout << "Thread locking is broken; internal state got messed up\n"
|
|
|
|
|
"NOTE: all counters should be equal and >=" << MAX_SUM << "\n";
|
|
|
|
|
ourVictim.report();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Register this test class... */
|
|
|
|
|
LAUNCHER (SyncLocking_test, "unit common");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}} // namespace lib::test
|