added Thread class

This commit is contained in:
maroy 2004-11-05 13:29:16 +00:00
parent 5a0544bedf
commit b1cc568fae
9 changed files with 800 additions and 5 deletions

View File

@ -21,7 +21,7 @@
#
#
# Author : $Author: maroy $
# Version : $Revision: 1.10 $
# Version : $Revision: 1.11 $
# Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/etc/Makefile.in,v $
#
# @configure_input@
@ -89,13 +89,16 @@ CORE_LIB_OBJS = ${TMP_DIR}/UniqueId.o \
${TMP_DIR}/FadeInfo.o \
${TMP_DIR}/PlaylistElement.o \
${TMP_DIR}/Playlist.o \
${TMP_DIR}/TimeConversion.o
${TMP_DIR}/TimeConversion.o \
${TMP_DIR}/Thread.o
TEST_RUNNER_OBJS = ${TMP_DIR}/AudioClipTest.o \
${TMP_DIR}/FadeInfoTest.o \
${TMP_DIR}/PlaylistElementTest.o \
${TMP_DIR}/PlaylistTest.o \
${TMP_DIR}/TimeConversionTest.o \
${TMP_DIR}/TestRunnable.o \
${TMP_DIR}/ThreadTest.o \
${TMP_DIR}/TestRunner.o
TEST_RUNNER_LIBS = -l${CORE_LIB} -lxml++-1.0 -lboost_date_time-gcc \

View File

@ -21,7 +21,7 @@ dnl Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
dnl
dnl
dnl Author : $Author: maroy $
dnl Version : $Revision: 1.4 $
dnl Version : $Revision: 1.5 $
dnl Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/etc/configure.ac,v $
dnl-----------------------------------------------------------------------------
@ -35,14 +35,14 @@ dnl-----------------------------------------------------------------------------
AC_INIT(Core, 1.0, bugs@campware.org)
AC_PREREQ(2.59)
AC_COPYRIGHT([Copyright (c) 2004 Media Development Loan Fund under the GNU GPL])
AC_REVISION($Revision: 1.4 $)
AC_REVISION($Revision: 1.5 $)
AC_CONFIG_SRCDIR(../src/UniqueId.cxx)
AC_CONFIG_HEADERS(configure.h)
AC_PROG_CXX()
AC_CHECK_HEADERS(getopt.h sys/time.h)
AC_CHECK_HEADERS(getopt.h sys/time.h pthread.h)
dnl-----------------------------------------------------------------------------

View File

@ -0,0 +1,100 @@
/*------------------------------------------------------------------------------
Copyright (c) 2004 Media Development Loan Fund
This file is part of the LiveSupport project.
http://livesupport.campware.org/
To report bugs, send an e-mail to bugs@campware.org
LiveSupport is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
LiveSupport 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.
You should have received a copy of the GNU General Public License
along with LiveSupport; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Author : $Author: maroy $
Version : $Revision: 1.1 $
Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/include/LiveSupport/Core/RunnableInterface.h,v $
------------------------------------------------------------------------------*/
#ifndef LiveSupport_Core_RunnableInterface_h
#define LiveSupport_Core_RunnableInterface_h
#ifndef __cplusplus
#error This is a C++ include file
#endif
/* ============================================================ include files */
#ifdef HAVE_CONFIG_H
#include "configure.h"
#endif
namespace LiveSupport {
namespace Core {
/* ================================================================ constants */
/* =================================================================== macros */
/* =============================================================== data types */
/**
* A Runnable object, that can form the main execution body of a thread.
*
* @author $Author: maroy $
* @version $Revision: 1.1 $
* @see Thread
*/
class RunnableInterface
{
public:
/**
* A virtual destructor, as this class has virtual functions.
*/
virtual
~RunnableInterface(void) throw ()
{
}
/**
* The main execution loop for the thread.
*/
virtual void
run(void) throw () = 0;
/**
* Signal the thread to stop, gracefully.
* This is just a call to signal the execution to stop, eventually.
*/
virtual void
stop(void) throw () = 0;
};
/* ================================================= external data structures */
/* ====================================================== function prototypes */
} // namespace Core
} // namespace LiveSupport
#endif // LiveSupport_Core_RunnableInterface_h

View File

@ -0,0 +1,163 @@
/*------------------------------------------------------------------------------
Copyright (c) 2004 Media Development Loan Fund
This file is part of the LiveSupport project.
http://livesupport.campware.org/
To report bugs, send an e-mail to bugs@campware.org
LiveSupport is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
LiveSupport 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.
You should have received a copy of the GNU General Public License
along with LiveSupport; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Author : $Author: maroy $
Version : $Revision: 1.1 $
Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/include/LiveSupport/Core/Thread.h,v $
------------------------------------------------------------------------------*/
#ifndef LiveSupport_Core_Thread_h
#define LiveSupport_Core_Thread_h
#ifndef __cplusplus
#error This is a C++ include file
#endif
/* ============================================================ include files */
#ifdef HAVE_CONFIG_H
#include "configure.h"
#endif
#ifdef HAVE_PTHREAD_H
#include <pthread.h>
#else
#error need pthread.h
#endif
#include "LiveSupport/Core/Ptr.h"
#include "LiveSupport/Core/RunnableInterface.h"
namespace LiveSupport {
namespace Core {
/* ================================================================ constants */
/* =================================================================== macros */
/* =============================================================== data types */
/**
* A generic thread executor class.
*
* @author $Author: maroy $
* @version $Revision: 1.1 $
* @see RunnableInterface
*/
class Thread
{
private:
/**
* The POSIX thread for this object.
*/
pthread_t thread;
/**
* The Runnable object, that constitutes the main running body
* of the thread.
*/
Ptr<RunnableInterface>::Ref runnable;
/**
* Default constructor.
*/
Thread(void) throw ()
{
}
/**
* The thread function for the POSIX thread interface.
*
* @param thread pointer to this thread instance.
* @return always 0
*/
static void *
posixThreadFunction(void * thread) throw ();
public:
/**
* Constructor.
*
* @param runnable the Runnable object making up the execution
* part of the thread.
*/
Thread(Ptr<RunnableInterface>::Ref runnable) throw ();
/**
* A virtual destructor, as this class has virtual functions.
*/
virtual
~Thread(void) throw ()
{
}
/**
* Start the execution of the thread.
* This funcion will create a new thread and starts executing
* it by the run() function of the Runnable object. Start will
* return immediately.
*
* @exception std::exception if the thread could not be started.
*/
virtual void
start(void) throw (std::exception);
/**
* Signal the thread to stop, gracefully.
* This is just a call to signal the execution to stop, eventually.
* The thread still has to be joined after calling stop().
*
* @see #join
*/
virtual void
stop(void) throw ()
{
runnable->stop();
}
/**
* Join the thread.
* Wait for the thread to terminate and free up all its resources.
*/
virtual void
join(void) throw ();
};
/* ================================================= external data structures */
/* ====================================================== function prototypes */
} // namespace Core
} // namespace LiveSupport
#endif // LiveSupport_Core_Thread_h

View File

@ -0,0 +1,81 @@
/*------------------------------------------------------------------------------
Copyright (c) 2004 Media Development Loan Fund
This file is part of the LiveSupport project.
http://livesupport.campware.org/
To report bugs, send an e-mail to bugs@campware.org
LiveSupport is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
LiveSupport 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.
You should have received a copy of the GNU General Public License
along with LiveSupport; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Author : $Author: maroy $
Version : $Revision: 1.1 $
Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/src/TestRunnable.cxx,v $
------------------------------------------------------------------------------*/
/* ============================================================ include files */
#ifdef HAVE_CONFIG_H
#include "configure.h"
#endif
#include "LiveSupport/Core/TimeConversion.h"
#include "TestRunnable.h"
using namespace LiveSupport::Core;
/* =================================================== local data structures */
/* ================================================ local constants & macros */
/* =============================================== local function prototypes */
/* ============================================================= module code */
/*------------------------------------------------------------------------------
* Constructor.
*----------------------------------------------------------------------------*/
TestRunnable :: TestRunnable(void) throw ()
{
shouldRun = true;
state = created;
}
/*------------------------------------------------------------------------------
* The main execution body of the thread.
*----------------------------------------------------------------------------*/
void
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);
}
state = stopped;
}

View File

@ -0,0 +1,136 @@
/*------------------------------------------------------------------------------
Copyright (c) 2004 Media Development Loan Fund
This file is part of the LiveSupport project.
http://livesupport.campware.org/
To report bugs, send an e-mail to bugs@campware.org
LiveSupport is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
LiveSupport 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.
You should have received a copy of the GNU General Public License
along with LiveSupport; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Author : $Author: maroy $
Version : $Revision: 1.1 $
Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/src/TestRunnable.h,v $
------------------------------------------------------------------------------*/
#ifndef TestRunnable_h
#define TestRunnable_h
#ifndef __cplusplus
#error This is a C++ include file
#endif
/* ============================================================ include files */
#ifdef HAVE_CONFIG_H
#include "configure.h"
#endif
#include "LiveSupport/Core/RunnableInterface.h"
namespace LiveSupport {
namespace Core {
/* ================================================================ constants */
/* =================================================================== macros */
/* =============================================================== data types */
/**
* A sample Runnable object, for testing purposes.
*
* @author $Author: maroy $
* @version $Revision: 1.1 $
*/
class TestRunnable : public virtual RunnableInterface
{
public:
/**
* An enum signaling the states of the Runnable object.
*/
typedef enum { created, running, stopped } State;
private:
/**
* Flag that marks if the main execution body should be
* running.
*/
bool shouldRun;
/**
* The state of the object.
*/
State state;
public:
/**
* Constructor.
*/
TestRunnable(void) throw ();
/**
* A virtual destructor, as this class has virtual functions.
*/
virtual
~TestRunnable(void) throw ()
{
}
/**
* The main execution loop for the thread.
*/
virtual void
run(void) throw ();
/**
* Signal the thread to stop, gracefully.
* This is just a call to signal the execution to stop, eventually.
*/
virtual void
stop(void) throw ()
{
shouldRun = false;
}
/**
* Get the state of the object.
*/
virtual State
getState(void) const throw ()
{
return state;
}
};
/* ================================================= external data structures */
/* ====================================================== function prototypes */
} // namespace Core
} // namespace LiveSupport
#endif // TestRunnable_h

View File

@ -0,0 +1,107 @@
/*------------------------------------------------------------------------------
Copyright (c) 2004 Media Development Loan Fund
This file is part of the LiveSupport project.
http://livesupport.campware.org/
To report bugs, send an e-mail to bugs@campware.org
LiveSupport is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
LiveSupport 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.
You should have received a copy of the GNU General Public License
along with LiveSupport; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Author : $Author: maroy $
Version : $Revision: 1.1 $
Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/src/Thread.cxx,v $
------------------------------------------------------------------------------*/
/* ============================================================ include files */
#ifdef HAVE_CONFIG_H
#include "configure.h"
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#else
#error need unistd.h
#endif
#include "LiveSupport/Core/Thread.h"
using namespace LiveSupport::Core;
/* =================================================== local data structures */
/* ================================================ local constants & macros */
/* =============================================== local function prototypes */
/* ============================================================= module code */
/*------------------------------------------------------------------------------
* Constructor.
*----------------------------------------------------------------------------*/
Thread :: Thread(Ptr<RunnableInterface>::Ref runnable) throw ()
{
this->runnable = runnable;
}
/*------------------------------------------------------------------------------
* The POSIX thread function for this thread.
*----------------------------------------------------------------------------*/
void *
Thread :: posixThreadFunction(void * thread) throw ()
{
Thread * pThread = (Thread *) thread;
pThread->runnable->run();
pthread_exit(0);
}
/*------------------------------------------------------------------------------
* Start the thread.
*----------------------------------------------------------------------------*/
void
Thread :: 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
Thread :: join(void) throw ()
{
int ret;
if ((ret = pthread_join(thread, 0))) {
// TODO: signal return code
}
}

View File

@ -0,0 +1,99 @@
/*------------------------------------------------------------------------------
Copyright (c) 2004 Media Development Loan Fund
This file is part of the LiveSupport project.
http://livesupport.campware.org/
To report bugs, send an e-mail to bugs@campware.org
LiveSupport is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
LiveSupport 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.
You should have received a copy of the GNU General Public License
along with LiveSupport; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Author : $Author: maroy $
Version : $Revision: 1.1 $
Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/src/ThreadTest.cxx,v $
------------------------------------------------------------------------------*/
/* ============================================================ include files */
#ifdef HAVE_CONFIG_H
#include "configure.h"
#endif
#include <string>
#include <iostream>
#include "LiveSupport/Core/TimeConversion.h"
#include "LiveSupport/Core/Thread.h"
#include "TestRunnable.h"
#include "ThreadTest.h"
using namespace LiveSupport::Core;
/* =================================================== local data structures */
/* ================================================ local constants & macros */
CPPUNIT_TEST_SUITE_REGISTRATION(ThreadTest);
/* =============================================== local function prototypes */
/* ============================================================= module code */
/*------------------------------------------------------------------------------
* Set up the test environment
*----------------------------------------------------------------------------*/
void
ThreadTest :: setUp(void) throw ()
{
}
/*------------------------------------------------------------------------------
* Clean up the test environment
*----------------------------------------------------------------------------*/
void
ThreadTest :: tearDown(void) throw ()
{
}
/*------------------------------------------------------------------------------
* A simple thread test.
*----------------------------------------------------------------------------*/
void
ThreadTest :: simpleTest(void)
throw (CPPUNIT_NS::Exception)
{
Ptr<TestRunnable>::Ref runnable(new TestRunnable());
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();
CPPUNIT_ASSERT(runnable->getState() == TestRunnable::running);
TimeConversion::sleep(sleepTime);
CPPUNIT_ASSERT(runnable->getState() == TestRunnable::running);
thread->stop();
TimeConversion::sleep(sleepTime);
CPPUNIT_ASSERT(runnable->getState() == TestRunnable::stopped);
thread->join();
}

View File

@ -0,0 +1,106 @@
/*------------------------------------------------------------------------------
Copyright (c) 2004 Media Development Loan Fund
This file is part of the LiveSupport project.
http://livesupport.campware.org/
To report bugs, send an e-mail to bugs@campware.org
LiveSupport is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
LiveSupport 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.
You should have received a copy of the GNU General Public License
along with LiveSupport; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Author : $Author: maroy $
Version : $Revision: 1.1 $
Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/core/src/ThreadTest.h,v $
------------------------------------------------------------------------------*/
#ifndef ThreadTest_h
#define ThreadTest_H
#ifndef __cplusplus
#error This is a C++ include file
#endif
/* ============================================================ include files */
#ifdef HAVE_CONFIG_H
#include "configure.h"
#endif
#include <cppunit/extensions/HelperMacros.h>
namespace LiveSupport {
namespace Core {
/* ================================================================ constants */
/* =================================================================== macros */
/* =============================================================== data types */
/**
* Unit test for the Thread class.
*
* @author $Author: maroy $
* @version $Revision: 1.1 $
* @see Thread
*/
class ThreadTest : public CPPUNIT_NS::TestFixture
{
CPPUNIT_TEST_SUITE(ThreadTest);
CPPUNIT_TEST(simpleTest);
CPPUNIT_TEST_SUITE_END();
protected:
/**
* A simple thread test.
*
* @exception CPPUNIT_NS::Exception on test failures.
*/
void
simpleTest(void) throw (CPPUNIT_NS::Exception);
public:
/**
* Set up the environment for the test case.
*/
void
setUp(void) throw ();
/**
* Clean up the environment after the test case.
*/
void
tearDown(void) throw ();
};
/* ================================================= external data structures */
/* ====================================================== function prototypes */
} // namespace Core
} // namespace LiveSupport
#endif // ThreadTest_h