From 9e3e321ade4688a336f497aa8d4cd68da6f19e9c Mon Sep 17 00:00:00 2001 From: maroy Date: Fri, 5 Nov 2004 13:29:59 +0000 Subject: [PATCH] refactored generic thread functionality into LiveSupport::Core::Thread --- .../eventScheduler/src/SchedulerThread.cxx | 50 +----------------- .../eventScheduler/src/SchedulerThread.h | 51 +++---------------- .../src/SchedulerThreadTest.cxx | 12 +++-- 3 files changed, 15 insertions(+), 98 deletions(-) diff --git a/livesupport/modules/eventScheduler/src/SchedulerThread.cxx b/livesupport/modules/eventScheduler/src/SchedulerThread.cxx index f406760a9..059bd3601 100644 --- a/livesupport/modules/eventScheduler/src/SchedulerThread.cxx +++ b/livesupport/modules/eventScheduler/src/SchedulerThread.cxx @@ -22,7 +22,7 @@ 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 $ ------------------------------------------------------------------------------*/ @@ -33,13 +33,6 @@ #include "configure.h" #endif -#ifdef HAVE_UNISTD_H -#include -#else -#error need unistd.h -#endif - - #include "LiveSupport/Core/TimeConversion.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 - } -} - diff --git a/livesupport/modules/eventScheduler/src/SchedulerThread.h b/livesupport/modules/eventScheduler/src/SchedulerThread.h index d50e7006f..bb54c90e2 100644 --- a/livesupport/modules/eventScheduler/src/SchedulerThread.h +++ b/livesupport/modules/eventScheduler/src/SchedulerThread.h @@ -22,7 +22,7 @@ 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 $ ------------------------------------------------------------------------------*/ @@ -40,14 +40,9 @@ #include "configure.h" #endif -#ifdef HAVE_PTHREAD_H -#include -#else -#error need pthread.h -#endif - #include +#include "LiveSupport/Core/RunnableInterface.h" #include "LiveSupport/EventScheduler/ScheduledEventInterface.h" #include "LiveSupport/EventScheduler/EventContainerInterface.h" @@ -72,9 +67,9 @@ using namespace LiveSupport::Core; * The main, executing thread of the scheduler. * * @author $Author: maroy $ - * @version $Revision: 1.1 $ + * @version $Revision: 1.2 $ */ -class SchedulerThread +class SchedulerThread : public virtual RunnableInterface { private: /** @@ -108,11 +103,6 @@ class SchedulerThread */ Ptr::Ref granularity; - /** - * The POSIX thread for this object. - */ - pthread_t thread; - /** * Flag indicating whether the thread should still run, or * 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. * @@ -169,14 +150,6 @@ class SchedulerThread nextStep(Ptr::Ref now) throw (); - protected: - /** - * The main execution loop for the thread. - */ - virtual void - run(void) throw (); - - public: /** * Constructor. @@ -199,15 +172,10 @@ class SchedulerThread } /** - * Start the execution of 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. + * The main execution loop for the thread. */ virtual void - start(void) throw (std::exception); + run(void) throw (); /** * Signal the thread to stop, gracefully. @@ -217,13 +185,6 @@ class SchedulerThread { shouldRun = false; } - - /** - * Join the thread. - * Wait for the thread to terminate and free up all its resources. - */ - virtual void - join(void) throw (); }; diff --git a/livesupport/modules/eventScheduler/src/SchedulerThreadTest.cxx b/livesupport/modules/eventScheduler/src/SchedulerThreadTest.cxx index e85d49f6e..8b8debe54 100644 --- a/livesupport/modules/eventScheduler/src/SchedulerThreadTest.cxx +++ b/livesupport/modules/eventScheduler/src/SchedulerThreadTest.cxx @@ -22,7 +22,7 @@ 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 $ ------------------------------------------------------------------------------*/ @@ -44,6 +44,8 @@ #include #include "LiveSupport/Core/TimeConversion.h" +#include "LiveSupport/Core/Thread.h" + #include "SchedulerThread.h" #include "TestScheduledEvent.h" #include "TestEventContainer.h" @@ -95,6 +97,7 @@ SchedulerThreadTest :: firstTest(void) { Ptr::Ref event; Ptr::Ref container; + Ptr::Ref thread; Ptr::Ref schedulerThread; Ptr::Ref now; Ptr::Ref when; @@ -119,8 +122,9 @@ SchedulerThreadTest :: firstTest(void) container.reset(new TestEventContainer(event)); schedulerThread.reset(new SchedulerThread(container, granularity)); + thread.reset(new Thread(schedulerThread)); - schedulerThread->start(); + thread->start(); CPPUNIT_ASSERT(event->getState() == TestScheduledEvent::created); state = event->getState(); @@ -177,7 +181,7 @@ SchedulerThreadTest :: firstTest(void) TimeConversion::sleep(granularity); } - schedulerThread->stop(); - schedulerThread->join(); + thread->stop(); + thread->join(); }