2008-12-28 02:06:30 +01:00
|
|
|
/*
|
|
|
|
|
SyncTimedwait(Test) - check the monitor object based timed condition wait
|
2010-12-10 02:55:40 +01:00
|
|
|
|
2008-12-28 02:06:30 +01:00
|
|
|
Copyright (C) Lumiera.org
|
|
|
|
|
2008, Hermann Vosseler <Ichthyostega@web.de>
|
2010-12-10 02:55:40 +01:00
|
|
|
|
2008-12-28 02:06:30 +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.
|
2010-12-10 02:55:40 +01:00
|
|
|
|
2008-12-28 02:06:30 +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-10 02:55:40 +01:00
|
|
|
|
2008-12-28 02:06:30 +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-10 02:55:40 +01:00
|
|
|
|
2008-12-28 02:06:30 +01:00
|
|
|
* *****************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "lib/test/run.hpp"
|
|
|
|
|
#include "lib/error.hpp"
|
|
|
|
|
|
|
|
|
|
#include "lib/sync.hpp"
|
|
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
|
|
using std::cout;
|
|
|
|
|
using test::Test;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace lib {
|
2010-02-11 03:06:42 +01:00
|
|
|
namespace test{
|
2008-12-28 02:06:30 +01:00
|
|
|
|
2010-02-11 03:06:42 +01:00
|
|
|
namespace { // private test classes and data...
|
2008-12-28 02:06:30 +01:00
|
|
|
|
2010-02-11 03:06:42 +01:00
|
|
|
const uint WAIT_mSec = 200; ///< milliseconds to wait before timeout
|
2008-12-28 02:06:30 +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 timeout feature on condition wait as provided by pthread and accessible
|
|
|
|
|
* via the object monitor based locking/waiting mechanism. Without creating
|
|
|
|
|
* multiple threads, we engage into a blocking wait, which aborts due to
|
|
|
|
|
* setting a timeout. (Note it is discouraged to use the timed wait feature;
|
|
|
|
|
* when possible, you should prefer relying on the Lumiera scheduler)
|
|
|
|
|
*
|
|
|
|
|
* @see SyncWaiting_test
|
|
|
|
|
* @see sync::Timeout
|
|
|
|
|
* @see sync.hpp
|
|
|
|
|
*/
|
2010-12-10 02:55:40 +01:00
|
|
|
class SyncTimedwait_test
|
2010-02-11 03:06:42 +01:00
|
|
|
: public Test,
|
|
|
|
|
Sync<RecursiveLock_Waitable>
|
|
|
|
|
{
|
2008-12-28 02:06:30 +01:00
|
|
|
|
2010-02-11 03:06:42 +01:00
|
|
|
friend class Lock; // allows inheriting privately from Sync
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
virtual void
|
|
|
|
|
run (Arg)
|
|
|
|
|
{
|
|
|
|
|
checkTimeoutStruct();
|
|
|
|
|
|
|
|
|
|
Lock block(this, &SyncTimedwait_test::neverHappens);
|
|
|
|
|
|
|
|
|
|
cout << "back from LaLaLand, alive and thriving!\n";
|
2010-12-10 02:55:40 +01:00
|
|
|
CHECK (block.isTimedWait());
|
2010-02-11 03:06:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
neverHappens() ///< the "condition test" used for waiting....
|
|
|
|
|
{
|
|
|
|
|
Lock currentLock(this); // get the Lock recursively
|
|
|
|
|
if (!currentLock.isTimedWait()) // right from within the condition test:
|
|
|
|
|
currentLock.setTimeout(WAIT_mSec); // switch waiting mode to timed wait and set timeout
|
|
|
|
|
|
2010-12-10 02:55:40 +01:00
|
|
|
return false;
|
2010-02-11 03:06:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
checkTimeoutStruct()
|
|
|
|
|
{
|
|
|
|
|
sync::Timeout tout;
|
|
|
|
|
|
2010-12-10 02:55:40 +01:00
|
|
|
CHECK (!tout);
|
|
|
|
|
CHECK (0 == tout.tv_sec);
|
|
|
|
|
CHECK (0 == tout.tv_nsec);
|
2010-02-11 03:06:42 +01:00
|
|
|
|
|
|
|
|
tout.setOffset (0);
|
2010-12-10 02:55:40 +01:00
|
|
|
CHECK (!tout);
|
|
|
|
|
CHECK (0 == tout.tv_sec);
|
|
|
|
|
CHECK (0 == tout.tv_nsec);
|
2010-02-11 03:06:42 +01:00
|
|
|
|
|
|
|
|
timespec ref;
|
|
|
|
|
clock_gettime(CLOCK_REALTIME, &ref);
|
|
|
|
|
tout.setOffset (1);
|
2010-12-10 02:55:40 +01:00
|
|
|
CHECK (tout);
|
|
|
|
|
CHECK (0 < tout.tv_sec);
|
|
|
|
|
CHECK (ref.tv_sec <= tout.tv_sec);
|
|
|
|
|
CHECK (ref.tv_nsec <= 1000000 + tout.tv_nsec || ref.tv_nsec > 1000000000-100000);
|
2010-02-11 03:06:42 +01:00
|
|
|
|
|
|
|
|
clock_gettime(CLOCK_REALTIME, &ref);
|
|
|
|
|
tout.setOffset (1000);
|
2010-12-10 02:55:40 +01:00
|
|
|
CHECK (tout);
|
2010-02-11 03:06:42 +01:00
|
|
|
if (ref.tv_nsec!=0) // should have gotten an overflow to the seconds part
|
|
|
|
|
{
|
2010-12-10 02:55:40 +01:00
|
|
|
CHECK (ref.tv_sec <= 2 + tout.tv_sec );
|
|
|
|
|
CHECK ((ref.tv_nsec + 1000000 * 999) % 1000000000
|
2010-02-11 03:06:42 +01:00
|
|
|
<= tout.tv_nsec);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Register this test class... */
|
|
|
|
|
LAUNCHER (SyncTimedwait_test, "unit common");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}} // namespace lib::test
|