addded Thread::yield()
replaced usleep() call to nanosleep() in TimeCoversion::sleep()
This commit is contained in:
parent
0a7ca4196c
commit
9ae4244b4b
|
@ -22,7 +22,7 @@
|
||||||
|
|
||||||
|
|
||||||
Author : $Author: maroy $
|
Author : $Author: maroy $
|
||||||
Version : $Revision: 1.2 $
|
Version : $Revision: 1.3 $
|
||||||
Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/include/LiveSupport/Core/Thread.h,v $
|
Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/include/LiveSupport/Core/Thread.h,v $
|
||||||
|
|
||||||
------------------------------------------------------------------------------*/
|
------------------------------------------------------------------------------*/
|
||||||
|
@ -62,7 +62,7 @@ namespace Core {
|
||||||
* A generic thread executor class.
|
* A generic thread executor class.
|
||||||
*
|
*
|
||||||
* @author $Author: maroy $
|
* @author $Author: maroy $
|
||||||
* @version $Revision: 1.2 $
|
* @version $Revision: 1.3 $
|
||||||
* @see RunnableInterface
|
* @see RunnableInterface
|
||||||
*/
|
*/
|
||||||
class Thread
|
class Thread
|
||||||
|
@ -136,6 +136,16 @@ class Thread
|
||||||
runnable->stop();
|
runnable->stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Force the current thread to relinquish use of its processor.
|
||||||
|
* So that other threads get a chance to run.
|
||||||
|
*/
|
||||||
|
static void
|
||||||
|
yield(void) throw ()
|
||||||
|
{
|
||||||
|
pthread_yield();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Join the thread.
|
* Join the thread.
|
||||||
* Wait for the thread to terminate and free up all its resources.
|
* Wait for the thread to terminate and free up all its resources.
|
||||||
|
|
|
@ -22,7 +22,7 @@
|
||||||
|
|
||||||
|
|
||||||
Author : $Author: maroy $
|
Author : $Author: maroy $
|
||||||
Version : $Revision: 1.1 $
|
Version : $Revision: 1.2 $
|
||||||
Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/src/TestRunnable.cxx,v $
|
Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/src/TestRunnable.cxx,v $
|
||||||
|
|
||||||
------------------------------------------------------------------------------*/
|
------------------------------------------------------------------------------*/
|
||||||
|
@ -54,10 +54,12 @@ using namespace LiveSupport::Core;
|
||||||
/*------------------------------------------------------------------------------
|
/*------------------------------------------------------------------------------
|
||||||
* Constructor.
|
* Constructor.
|
||||||
*----------------------------------------------------------------------------*/
|
*----------------------------------------------------------------------------*/
|
||||||
TestRunnable :: TestRunnable(void) throw ()
|
TestRunnable :: TestRunnable(Ptr<time_duration>::Ref loopTime)
|
||||||
|
throw ()
|
||||||
{
|
{
|
||||||
shouldRun = true;
|
shouldRun = true;
|
||||||
state = created;
|
state = created;
|
||||||
|
this->loopTime = loopTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -69,8 +71,6 @@ TestRunnable :: run(void) throw ()
|
||||||
{
|
{
|
||||||
state = running;
|
state = running;
|
||||||
|
|
||||||
Ptr<time_duration>::Ref loopTime(new time_duration(seconds(1)));
|
|
||||||
|
|
||||||
while (shouldRun) {
|
while (shouldRun) {
|
||||||
// don't do anything in the main loop
|
// don't do anything in the main loop
|
||||||
TimeConversion::sleep(loopTime);
|
TimeConversion::sleep(loopTime);
|
||||||
|
|
|
@ -22,7 +22,7 @@
|
||||||
|
|
||||||
|
|
||||||
Author : $Author: maroy $
|
Author : $Author: maroy $
|
||||||
Version : $Revision: 1.1 $
|
Version : $Revision: 1.2 $
|
||||||
Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/src/TestRunnable.h,v $
|
Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/src/TestRunnable.h,v $
|
||||||
|
|
||||||
------------------------------------------------------------------------------*/
|
------------------------------------------------------------------------------*/
|
||||||
|
@ -59,7 +59,7 @@ namespace Core {
|
||||||
* A sample Runnable object, for testing purposes.
|
* A sample Runnable object, for testing purposes.
|
||||||
*
|
*
|
||||||
* @author $Author: maroy $
|
* @author $Author: maroy $
|
||||||
* @version $Revision: 1.1 $
|
* @version $Revision: 1.2 $
|
||||||
*/
|
*/
|
||||||
class TestRunnable : public virtual RunnableInterface
|
class TestRunnable : public virtual RunnableInterface
|
||||||
{
|
{
|
||||||
|
@ -70,6 +70,12 @@ class TestRunnable : public virtual RunnableInterface
|
||||||
typedef enum { created, running, stopped } State;
|
typedef enum { created, running, stopped } State;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
/**
|
||||||
|
* The time interval the run() method will check if stop()
|
||||||
|
* has been called meanwhile.
|
||||||
|
*/
|
||||||
|
Ptr<time_duration>::Ref loopTime;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Flag that marks if the main execution body should be
|
* Flag that marks if the main execution body should be
|
||||||
* running.
|
* running.
|
||||||
|
@ -81,11 +87,22 @@ class TestRunnable : public virtual RunnableInterface
|
||||||
*/
|
*/
|
||||||
State state;
|
State state;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default constructor.
|
||||||
|
*/
|
||||||
|
TestRunnable(void) throw ()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructor.
|
||||||
|
*
|
||||||
|
* @param loopTime the time at which the run() method checks
|
||||||
|
* if it still should run.
|
||||||
*/
|
*/
|
||||||
TestRunnable(void) throw ();
|
TestRunnable(Ptr<time_duration>::Ref loopTime) throw ();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A virtual destructor, as this class has virtual functions.
|
* A virtual destructor, as this class has virtual functions.
|
||||||
|
|
|
@ -22,7 +22,7 @@
|
||||||
|
|
||||||
|
|
||||||
Author : $Author: maroy $
|
Author : $Author: maroy $
|
||||||
Version : $Revision: 1.2 $
|
Version : $Revision: 1.3 $
|
||||||
Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/src/Thread.cxx,v $
|
Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/src/Thread.cxx,v $
|
||||||
|
|
||||||
------------------------------------------------------------------------------*/
|
------------------------------------------------------------------------------*/
|
||||||
|
@ -75,7 +75,7 @@ Thread :: posixThreadFunction(void * thread) throw ()
|
||||||
|
|
||||||
pThread->runnable->run();
|
pThread->runnable->run();
|
||||||
|
|
||||||
return 0;
|
pthread_exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -85,8 +85,17 @@ Thread :: posixThreadFunction(void * thread) throw ()
|
||||||
void
|
void
|
||||||
Thread :: start(void) throw (std::exception)
|
Thread :: start(void) throw (std::exception)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
if ((ret = pthread_create(&thread, 0, posixThreadFunction, this))) {
|
pthread_attr_t attr;
|
||||||
|
|
||||||
|
pthread_attr_init(&attr);
|
||||||
|
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
|
||||||
|
ret = pthread_create(&thread, &attr, posixThreadFunction, this);
|
||||||
|
pthread_attr_destroy(&attr);
|
||||||
|
|
||||||
|
yield();
|
||||||
|
|
||||||
|
if (ret) {
|
||||||
// TODO: signal return code
|
// TODO: signal return code
|
||||||
throw std::exception();
|
throw std::exception();
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,7 +22,7 @@
|
||||||
|
|
||||||
|
|
||||||
Author : $Author: maroy $
|
Author : $Author: maroy $
|
||||||
Version : $Revision: 1.2 $
|
Version : $Revision: 1.3 $
|
||||||
Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/src/ThreadTest.cxx,v $
|
Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/src/ThreadTest.cxx,v $
|
||||||
|
|
||||||
------------------------------------------------------------------------------*/
|
------------------------------------------------------------------------------*/
|
||||||
|
@ -82,14 +82,14 @@ void
|
||||||
ThreadTest :: simpleTest(void)
|
ThreadTest :: simpleTest(void)
|
||||||
throw (CPPUNIT_NS::Exception)
|
throw (CPPUNIT_NS::Exception)
|
||||||
{
|
{
|
||||||
Ptr<TestRunnable>::Ref runnable(new TestRunnable());
|
Ptr<time_duration>::Ref cycle(new time_duration(seconds(1)));
|
||||||
|
Ptr<TestRunnable>::Ref runnable(new TestRunnable(cycle));
|
||||||
Ptr<Thread>::Ref thread(new Thread(runnable));
|
Ptr<Thread>::Ref thread(new Thread(runnable));
|
||||||
Ptr<time_duration>::Ref sleepTime(new time_duration(seconds(1)));
|
Ptr<time_duration>::Ref sleepTime(new time_duration(seconds(1)));
|
||||||
|
|
||||||
CPPUNIT_ASSERT(runnable->getState() == TestRunnable::created);
|
CPPUNIT_ASSERT(runnable->getState() == TestRunnable::created);
|
||||||
thread->start();
|
thread->start();
|
||||||
// sleep to yield the thread some time to actually start
|
Thread::yield();
|
||||||
TimeConversion::sleep(sleepTime);
|
|
||||||
CPPUNIT_ASSERT(runnable->getState() == TestRunnable::running);
|
CPPUNIT_ASSERT(runnable->getState() == TestRunnable::running);
|
||||||
TimeConversion::sleep(sleepTime);
|
TimeConversion::sleep(sleepTime);
|
||||||
CPPUNIT_ASSERT(runnable->getState() == TestRunnable::running);
|
CPPUNIT_ASSERT(runnable->getState() == TestRunnable::running);
|
||||||
|
@ -99,3 +99,29 @@ ThreadTest :: simpleTest(void)
|
||||||
thread->join();
|
thread->join();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*------------------------------------------------------------------------------
|
||||||
|
* A test to see if a thread respoding slowly for a stop()
|
||||||
|
* call is joined correctly.
|
||||||
|
*----------------------------------------------------------------------------*/
|
||||||
|
void
|
||||||
|
ThreadTest :: slowThreadTest(void)
|
||||||
|
throw (CPPUNIT_NS::Exception)
|
||||||
|
{
|
||||||
|
Ptr<time_duration>::Ref cycle(new time_duration(seconds(10)));
|
||||||
|
Ptr<TestRunnable>::Ref runnable(new TestRunnable(cycle));
|
||||||
|
Ptr<Thread>::Ref thread(new Thread(runnable));
|
||||||
|
Ptr<time_duration>::Ref sleepTime(new time_duration(seconds(1)));
|
||||||
|
|
||||||
|
CPPUNIT_ASSERT(runnable->getState() == TestRunnable::created);
|
||||||
|
thread->start();
|
||||||
|
Thread::yield();
|
||||||
|
CPPUNIT_ASSERT(runnable->getState() == TestRunnable::running);
|
||||||
|
TimeConversion::sleep(sleepTime);
|
||||||
|
CPPUNIT_ASSERT(runnable->getState() == TestRunnable::running);
|
||||||
|
thread->stop();
|
||||||
|
TimeConversion::sleep(sleepTime);
|
||||||
|
thread->join();
|
||||||
|
CPPUNIT_ASSERT(runnable->getState() == TestRunnable::stopped);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -22,7 +22,7 @@
|
||||||
|
|
||||||
|
|
||||||
Author : $Author: maroy $
|
Author : $Author: maroy $
|
||||||
Version : $Revision: 1.1 $
|
Version : $Revision: 1.2 $
|
||||||
Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/src/ThreadTest.h,v $
|
Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/src/ThreadTest.h,v $
|
||||||
|
|
||||||
------------------------------------------------------------------------------*/
|
------------------------------------------------------------------------------*/
|
||||||
|
@ -58,13 +58,14 @@ namespace Core {
|
||||||
* Unit test for the Thread class.
|
* Unit test for the Thread class.
|
||||||
*
|
*
|
||||||
* @author $Author: maroy $
|
* @author $Author: maroy $
|
||||||
* @version $Revision: 1.1 $
|
* @version $Revision: 1.2 $
|
||||||
* @see Thread
|
* @see Thread
|
||||||
*/
|
*/
|
||||||
class ThreadTest : public CPPUNIT_NS::TestFixture
|
class ThreadTest : public CPPUNIT_NS::TestFixture
|
||||||
{
|
{
|
||||||
CPPUNIT_TEST_SUITE(ThreadTest);
|
CPPUNIT_TEST_SUITE(ThreadTest);
|
||||||
CPPUNIT_TEST(simpleTest);
|
CPPUNIT_TEST(simpleTest);
|
||||||
|
CPPUNIT_TEST(slowThreadTest);
|
||||||
CPPUNIT_TEST_SUITE_END();
|
CPPUNIT_TEST_SUITE_END();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
@ -77,6 +78,15 @@ class ThreadTest : public CPPUNIT_NS::TestFixture
|
||||||
void
|
void
|
||||||
simpleTest(void) throw (CPPUNIT_NS::Exception);
|
simpleTest(void) throw (CPPUNIT_NS::Exception);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A test to see if a thread respoding slowly for a stop()
|
||||||
|
* call is joined correctly.
|
||||||
|
*
|
||||||
|
* @exception CPPUNIT_NS::Exception on test failures.
|
||||||
|
*/
|
||||||
|
void
|
||||||
|
slowThreadTest(void) throw (CPPUNIT_NS::Exception);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -22,7 +22,7 @@
|
||||||
|
|
||||||
|
|
||||||
Author : $Author: maroy $
|
Author : $Author: maroy $
|
||||||
Version : $Revision: 1.2 $
|
Version : $Revision: 1.3 $
|
||||||
Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/src/TimeConversion.cxx,v $
|
Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/src/TimeConversion.cxx,v $
|
||||||
|
|
||||||
------------------------------------------------------------------------------*/
|
------------------------------------------------------------------------------*/
|
||||||
|
@ -97,6 +97,19 @@ void
|
||||||
TimeConversion :: sleep(Ptr<time_duration>::Ref duration)
|
TimeConversion :: sleep(Ptr<time_duration>::Ref duration)
|
||||||
throw ()
|
throw ()
|
||||||
{
|
{
|
||||||
usleep(duration->total_microseconds());
|
int ret;
|
||||||
|
struct timespec tv;
|
||||||
|
|
||||||
|
tv.tv_sec = duration->total_seconds();
|
||||||
|
tv.tv_nsec = duration->fractional_seconds();
|
||||||
|
|
||||||
|
// if fractional digits is in microseconds, convert it to nanoseconds
|
||||||
|
if (time_duration::num_fractional_digits() == 6) {
|
||||||
|
tv.tv_nsec *= 1000L;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((ret = nanosleep(&tv, 0))) {
|
||||||
|
// TODO: signal error
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue