From cd7adc0b064b163670c08768cf6432fed5d8765c Mon Sep 17 00:00:00 2001 From: Tyler Dauwalder Date: Sun, 29 Sep 2002 07:01:43 +0000 Subject: [PATCH] RegistrarThread class, which is the base class for threads spawned in the registrar. git-svn-id: file:///srv/svn/repos/haiku/trunk/current@1265 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- headers/private/app/RegistrarThread.h | 45 +++++++++++ src/kits/app/RegistrarThread.cpp | 111 ++++++++++++++++++++++++++ 2 files changed, 156 insertions(+) create mode 100644 headers/private/app/RegistrarThread.h create mode 100644 src/kits/app/RegistrarThread.cpp diff --git a/headers/private/app/RegistrarThread.h b/headers/private/app/RegistrarThread.h new file mode 100644 index 0000000000..7850a870f8 --- /dev/null +++ b/headers/private/app/RegistrarThread.h @@ -0,0 +1,45 @@ +//---------------------------------------------------------------------- +// This software is part of the OpenBeOS distribution and is covered +// by the OpenBeOS license. +//--------------------------------------------------------------------- +/*! + \file RegistrarThread.h + RegistrarThread interface declaration +*/ + +#ifndef REGISTRAR_THREAD_H +#define REGISTRAR_THREAD_H + +#include +#include + +class RegistrarThread { +public: + RegistrarThread(const char *name, int32 priority, BMessenger managerMessenger); + virtual ~RegistrarThread(); + + virtual status_t InitCheck(); + status_t Run(); + + thread_id Id() const; + const char* Name() const; + + void AskToExit(); + bool IsFinished() const; + +protected: + //! The function executed in the object's thread when Run() is called + virtual status_t ThreadFunction() = 0; + + BMessenger fManagerMessenger; + bool fShouldExit; // Initially false, may be set to true by AskToExit() + bool fIsFinished; // Initially false, set to true by the thread itself upon completion +private: + static int32 EntryFunction(void *data); + + status_t fStatus; + thread_id fId; // Initially -1, to be set prior to thread execution and neverafter changed + char fName[B_OS_NAME_LENGTH]; +}; + +#endif // REGISTRAR_THREAD_H diff --git a/src/kits/app/RegistrarThread.cpp b/src/kits/app/RegistrarThread.cpp new file mode 100644 index 0000000000..c1fa76896b --- /dev/null +++ b/src/kits/app/RegistrarThread.cpp @@ -0,0 +1,111 @@ +#include "RegistrarThread.h" + +/*! \class RegistrarThread + \brief Base thread class for threads spawned and managed by the registrar + +*/ + +// constructor +/*! \brief Creates a new RegistrarThread object, spawning the object's + thread. + + Call Run() to actually get the thread running. + + \param name The desired name of the new thread + \param priority The desired priority of the new thread + \param managerMessenger A BMessenger to the thread manager to which this + thread does or will belong. +*/ +RegistrarThread::RegistrarThread(const char *name, int32 priority, BMessenger managerMessenger) + : fManagerMessenger(managerMessenger) + , fShouldExit(false) + , fIsFinished(false) + , fStatus(B_NO_INIT) + , fId(-1) +{ + fName[0] = 0; + status_t err = name && fManagerMessenger.IsValid() ? B_OK : B_BAD_VALUE; + if (!err) { + fId = spawn_thread(&RegistrarThread::EntryFunction, name, + priority, (void*)this); + err = fId >= 0 ? B_OK : fId; + } + if (!err) { + strcpy(fName, name); + } + fStatus = err; +} + +// destructor +/*! \brief Destroys the RegistrarThread object +*/ +RegistrarThread::~RegistrarThread() +{ +} + +// InitCheck() +/*! \brief Returns the initialization status of the object +*/ +status_t +RegistrarThread::InitCheck() +{ + return fStatus; +} + +// Run +/*! \brief Begins executing the thread's ThreadFunction() +*/ +status_t +RegistrarThread::Run() +{ + status_t err = InitCheck(); + if (!err) + err = resume_thread(fId); + return err; +} + +// Id +//! Returns the thread id +thread_id +RegistrarThread::Id() const +{ + return fId; +} + +// Name +//! Returns the name of the thread +const char* +RegistrarThread::Name() const +{ + return fName; +} + +// AskToExit +/*! \brief Signals to thread that it needs to quit politely as soon + as possible. +*/ +void +RegistrarThread::AskToExit() +{ + fShouldExit = true; +} + +// IsFinished +/*! \brief Returns \c true if the thread has finished executing +*/ +bool +RegistrarThread::IsFinished() const +{ + return fIsFinished; +} + +// EntryFunction +/*! \brief This is the function supplied to spawn_thread. It simply calls + ThreadFunction() on the \a data parameter, which is assumed to be a pointer + to a RegistrarThread object. +*/ +int32 +RegistrarThread::EntryFunction(void *data) +{ + return ((RegistrarThread*)data)->ThreadFunction(); +}