addded Thread::yield()

replaced usleep() call to nanosleep() in TimeCoversion::sleep()
This commit is contained in:
maroy 2004-11-24 13:05:27 +00:00
parent 0a7ca4196c
commit 9ae4244b4b
7 changed files with 108 additions and 23 deletions

View File

@ -22,7 +22,7 @@
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 $
------------------------------------------------------------------------------*/
@ -62,7 +62,7 @@ namespace Core {
* A generic thread executor class.
*
* @author $Author: maroy $
* @version $Revision: 1.2 $
* @version $Revision: 1.3 $
* @see RunnableInterface
*/
class Thread
@ -136,6 +136,16 @@ class Thread
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.
* Wait for the thread to terminate and free up all its resources.

View File

@ -22,7 +22,7 @@
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 $
------------------------------------------------------------------------------*/
@ -54,10 +54,12 @@ using namespace LiveSupport::Core;
/*------------------------------------------------------------------------------
* Constructor.
*----------------------------------------------------------------------------*/
TestRunnable :: TestRunnable(void) throw ()
TestRunnable :: TestRunnable(Ptr<time_duration>::Ref loopTime)
throw ()
{
shouldRun = true;
state = created;
shouldRun = true;
state = created;
this->loopTime = loopTime;
}
@ -69,8 +71,6 @@ TestRunnable :: run(void) throw ()
{
state = running;
Ptr<time_duration>::Ref loopTime(new time_duration(seconds(1)));
while (shouldRun) {
// don't do anything in the main loop
TimeConversion::sleep(loopTime);

View File

@ -22,7 +22,7 @@
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 $
------------------------------------------------------------------------------*/
@ -59,7 +59,7 @@ namespace Core {
* A sample Runnable object, for testing purposes.
*
* @author $Author: maroy $
* @version $Revision: 1.1 $
* @version $Revision: 1.2 $
*/
class TestRunnable : public virtual RunnableInterface
{
@ -70,6 +70,12 @@ class TestRunnable : public virtual RunnableInterface
typedef enum { created, running, stopped } State;
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
* running.
@ -81,11 +87,22 @@ class TestRunnable : public virtual RunnableInterface
*/
State state;
/**
* Default constructor.
*/
TestRunnable(void) throw ()
{
}
public:
/**
* 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.

View File

@ -22,7 +22,7 @@
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 $
------------------------------------------------------------------------------*/
@ -75,7 +75,7 @@ Thread :: posixThreadFunction(void * thread) throw ()
pThread->runnable->run();
return 0;
pthread_exit(0);
}
@ -85,8 +85,17 @@ Thread :: posixThreadFunction(void * thread) throw ()
void
Thread :: start(void) throw (std::exception)
{
int ret;
if ((ret = pthread_create(&thread, 0, posixThreadFunction, this))) {
int ret;
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
throw std::exception();
}

View File

@ -22,7 +22,7 @@
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 $
------------------------------------------------------------------------------*/
@ -82,14 +82,14 @@ void
ThreadTest :: simpleTest(void)
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<time_duration>::Ref sleepTime(new time_duration(seconds(1)));
CPPUNIT_ASSERT(runnable->getState() == TestRunnable::created);
thread->start();
// sleep to yield the thread some time to actually start
TimeConversion::sleep(sleepTime);
Thread::yield();
CPPUNIT_ASSERT(runnable->getState() == TestRunnable::running);
TimeConversion::sleep(sleepTime);
CPPUNIT_ASSERT(runnable->getState() == TestRunnable::running);
@ -99,3 +99,29 @@ ThreadTest :: simpleTest(void)
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);
}

View File

@ -22,7 +22,7 @@
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 $
------------------------------------------------------------------------------*/
@ -58,13 +58,14 @@ namespace Core {
* Unit test for the Thread class.
*
* @author $Author: maroy $
* @version $Revision: 1.1 $
* @version $Revision: 1.2 $
* @see Thread
*/
class ThreadTest : public CPPUNIT_NS::TestFixture
{
CPPUNIT_TEST_SUITE(ThreadTest);
CPPUNIT_TEST(simpleTest);
CPPUNIT_TEST(slowThreadTest);
CPPUNIT_TEST_SUITE_END();
protected:
@ -77,6 +78,15 @@ class ThreadTest : public CPPUNIT_NS::TestFixture
void
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:
/**

View File

@ -22,7 +22,7 @@
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 $
------------------------------------------------------------------------------*/
@ -97,6 +97,19 @@ void
TimeConversion :: sleep(Ptr<time_duration>::Ref duration)
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
}
}