[Compbench-devel] CompBenchmarks++/libcompbenchmarks/Base Thread.cpp, NONE, 1.1 Thread.h, NONE, 1.1
Brought to you by:
xfred
From: Frederic T. <xf...@us...> - 2007-05-16 09:44:26
|
Update of /cvsroot/compbench/CompBenchmarks++/libcompbenchmarks/Base In directory sc8-pr-cvs4.sourceforge.net:/tmp/cvs-serv28929 Added Files: Thread.cpp Thread.h Log Message: First import. --- NEW FILE: Thread.h --- /* ---------------------------------------------------------------------------- $Id: Thread.h,v 1.1 2007/05/16 09:44:19 xfred Exp $ This is free software. For details, see the GNU Public License in the COPYING file, or Look http://www.fsf.org ------------------------------------------------------------------------- */ #ifndef H_CBMTHREAD #define H_CBMTHREAD 1 #include <pthread.h> namespace CBM { /** \brief Class for storing and managing threads */ class Mutex { private: pthread_mutex_t mutex; public: Mutex(); virtual void Lock(void); virtual void Unlock(void); virtual ~Mutex(); }; class Thread { private: pthread_t tid; int isTerminated; Mutex mutex; protected: virtual void P(void); virtual void R(void); virtual void join(void); public: Thread(); virtual void routine(void) = 0; virtual void terminated(void); virtual int Terminated(void); virtual float Progress(void); virtual void Start(void); /** Destructor */ virtual ~Thread(); }; class ThreadAction : public Thread { private: protected: ThreadAction(); public: virtual float Progress(void) = 0; ~ThreadAction(); }; class ThreadActionTest : public ThreadAction { private: int slp; Mutex m; protected: virtual void routine(void); public: ThreadActionTest(); virtual float Progress(void); ~ThreadActionTest(); }; } #endif --- NEW FILE: Thread.cpp --- /* ---------------------------------------------------------------------------- $Id: Thread.cpp,v 1.1 2007/05/16 09:44:19 xfred Exp $ This is free software. For details, see the GNU Public License in the COPYING file, or Look http://www.fsf.org ------------------------------------------------------------------------- */ #include <Base/Thread.h> #include <vector> using namespace CBM; void *thread_main_routine(void *tobj) { CBM::Thread *thread = ((CBM::Thread*)tobj); thread->routine(); thread->terminated(); pthread_exit(NULL); } Mutex::Mutex() { pthread_mutex_init(&mutex, NULL); } void Mutex::Lock(void) { pthread_mutex_lock(&mutex); } void Mutex::Unlock(void) { pthread_mutex_unlock(&mutex); } Mutex::~Mutex() { pthread_mutex_destroy(&mutex); } Thread::Thread() { isTerminated=0; } void Thread::P(void) { mutex.Lock(); } void Thread::R(void) { mutex.Unlock(); } void Thread::join(void) { pthread_join(tid, NULL); } void Thread::terminated(void) { P(); isTerminated=1; R(); } int Thread::Terminated(void) { int term; P(); term=isTerminated; R(); if (term) join(); return(term); } float Thread::Progress(void) { return(0.0); } void Thread::Start(void) { pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); isTerminated=0; pthread_create(&tid, &attr, thread_main_routine, (void*) this); } Thread::~Thread() { } ThreadAction::ThreadAction() : Thread() { } ThreadAction::~ThreadAction() { } ThreadActionTest::ThreadActionTest() : ThreadAction() { slp=5; } void ThreadActionTest::routine(void) { int loc = slp; while (loc--) { m.Lock(); slp=loc; m.Unlock(); sleep(1); } } float ThreadActionTest::Progress(void) { int loc; m.Lock(); loc=slp; m.Unlock(); return((5-loc)/.05); } ThreadActionTest::~ThreadActionTest(void) { } |