refactored generic thread functionality into LiveSupport::Core::Thread

This commit is contained in:
maroy 2004-11-05 13:29:59 +00:00
parent b1cc568fae
commit 9e3e321ade
3 changed files with 15 additions and 98 deletions

View File

@ -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/eventScheduler/src/SchedulerThread.cxx,v $ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/eventScheduler/src/SchedulerThread.cxx,v $
------------------------------------------------------------------------------*/ ------------------------------------------------------------------------------*/
@ -33,13 +33,6 @@
#include "configure.h" #include "configure.h"
#endif #endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#else
#error need unistd.h
#endif
#include "LiveSupport/Core/TimeConversion.h" #include "LiveSupport/Core/TimeConversion.h"
#include "SchedulerThread.h" #include "SchedulerThread.h"
@ -142,44 +135,3 @@ SchedulerThread :: run(void) throw ()
} }
} }
/*------------------------------------------------------------------------------
* The POSIX thread function for this thread.
*----------------------------------------------------------------------------*/
void *
SchedulerThread :: posixThreadFunction(void * schedulerThread) throw ()
{
SchedulerThread * sThread = (SchedulerThread *) schedulerThread;
sThread->run();
pthread_exit(0);
}
/*------------------------------------------------------------------------------
* Start the thread.
*----------------------------------------------------------------------------*/
void
SchedulerThread :: start(void) throw (std::exception)
{
int ret;
if ((ret = pthread_create(&thread, 0, posixThreadFunction, this))) {
// TODO: signal return code
throw std::exception();
}
}
/*------------------------------------------------------------------------------
* Join the thread.
*----------------------------------------------------------------------------*/
void
SchedulerThread :: join(void) throw ()
{
int ret;
if ((ret = pthread_join(thread, 0))) {
// TODO: signal return code
}
}

View File

@ -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/eventScheduler/src/SchedulerThread.h,v $ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/eventScheduler/src/SchedulerThread.h,v $
------------------------------------------------------------------------------*/ ------------------------------------------------------------------------------*/
@ -40,14 +40,9 @@
#include "configure.h" #include "configure.h"
#endif #endif
#ifdef HAVE_PTHREAD_H
#include <pthread.h>
#else
#error need pthread.h
#endif
#include <boost/date_time/posix_time/posix_time.hpp> #include <boost/date_time/posix_time/posix_time.hpp>
#include "LiveSupport/Core/RunnableInterface.h"
#include "LiveSupport/EventScheduler/ScheduledEventInterface.h" #include "LiveSupport/EventScheduler/ScheduledEventInterface.h"
#include "LiveSupport/EventScheduler/EventContainerInterface.h" #include "LiveSupport/EventScheduler/EventContainerInterface.h"
@ -72,9 +67,9 @@ using namespace LiveSupport::Core;
* The main, executing thread of the scheduler. * The main, executing thread of the scheduler.
* *
* @author $Author: maroy $ * @author $Author: maroy $
* @version $Revision: 1.1 $ * @version $Revision: 1.2 $
*/ */
class SchedulerThread class SchedulerThread : public virtual RunnableInterface
{ {
private: private:
/** /**
@ -108,11 +103,6 @@ class SchedulerThread
*/ */
Ptr<time_duration>::Ref granularity; Ptr<time_duration>::Ref granularity;
/**
* The POSIX thread for this object.
*/
pthread_t thread;
/** /**
* Flag indicating whether the thread should still run, or * Flag indicating whether the thread should still run, or
* actually terminate. * actually terminate.
@ -126,15 +116,6 @@ class SchedulerThread
{ {
} }
/**
* The thread function for the POSIX thread interface.
*
* @param schedulerThread pointer to this SchedulerThread instance.
* @return always 0
*/
static void *
posixThreadFunction(void * schedulerThread) throw ();
/** /**
* Get the next event. * Get the next event.
* *
@ -169,14 +150,6 @@ class SchedulerThread
nextStep(Ptr<ptime>::Ref now) throw (); nextStep(Ptr<ptime>::Ref now) throw ();
protected:
/**
* The main execution loop for the thread.
*/
virtual void
run(void) throw ();
public: public:
/** /**
* Constructor. * Constructor.
@ -199,15 +172,10 @@ class SchedulerThread
} }
/** /**
* Start the execution of the thread. * The main execution loop for the thread.
* This funcion will create a new thread and starts executing
* it by the run() function of this object. Start will
* return immediately.
*
* @exception std::exception if the thread could not be started.
*/ */
virtual void virtual void
start(void) throw (std::exception); run(void) throw ();
/** /**
* Signal the thread to stop, gracefully. * Signal the thread to stop, gracefully.
@ -217,13 +185,6 @@ class SchedulerThread
{ {
shouldRun = false; shouldRun = false;
} }
/**
* Join the thread.
* Wait for the thread to terminate and free up all its resources.
*/
virtual void
join(void) throw ();
}; };

View File

@ -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/eventScheduler/src/SchedulerThreadTest.cxx,v $ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/eventScheduler/src/SchedulerThreadTest.cxx,v $
------------------------------------------------------------------------------*/ ------------------------------------------------------------------------------*/
@ -44,6 +44,8 @@
#include <iostream> #include <iostream>
#include "LiveSupport/Core/TimeConversion.h" #include "LiveSupport/Core/TimeConversion.h"
#include "LiveSupport/Core/Thread.h"
#include "SchedulerThread.h" #include "SchedulerThread.h"
#include "TestScheduledEvent.h" #include "TestScheduledEvent.h"
#include "TestEventContainer.h" #include "TestEventContainer.h"
@ -95,6 +97,7 @@ SchedulerThreadTest :: firstTest(void)
{ {
Ptr<TestScheduledEvent>::Ref event; Ptr<TestScheduledEvent>::Ref event;
Ptr<TestEventContainer>::Ref container; Ptr<TestEventContainer>::Ref container;
Ptr<Thread>::Ref thread;
Ptr<SchedulerThread>::Ref schedulerThread; Ptr<SchedulerThread>::Ref schedulerThread;
Ptr<ptime>::Ref now; Ptr<ptime>::Ref now;
Ptr<ptime>::Ref when; Ptr<ptime>::Ref when;
@ -119,8 +122,9 @@ SchedulerThreadTest :: firstTest(void)
container.reset(new TestEventContainer(event)); container.reset(new TestEventContainer(event));
schedulerThread.reset(new SchedulerThread(container, granularity)); schedulerThread.reset(new SchedulerThread(container, granularity));
thread.reset(new Thread(schedulerThread));
schedulerThread->start(); thread->start();
CPPUNIT_ASSERT(event->getState() == TestScheduledEvent::created); CPPUNIT_ASSERT(event->getState() == TestScheduledEvent::created);
state = event->getState(); state = event->getState();
@ -177,7 +181,7 @@ SchedulerThreadTest :: firstTest(void)
TimeConversion::sleep(granularity); TimeConversion::sleep(granularity);
} }
schedulerThread->stop(); thread->stop();
schedulerThread->join(); thread->join();
} }