1/* 2 * Copyright 2001-2005, Haiku. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Tyler Akidau 7 */ 8 9//! Base thread class for threads spawned and managed by the registrar 10 11 12#include "RegistrarThread.h" 13#include <string.h> 14 15 16/*! \class RegistrarThread 17 \brief Base thread class for threads spawned and managed by the registrar 18 19*/ 20 21// constructor 22/*! \brief Creates a new RegistrarThread object, spawning the object's 23 thread. 24 25 Call Run() to actually get the thread running. 26 27 \param name The desired name of the new thread 28 \param priority The desired priority of the new thread 29 \param managerMessenger A BMessenger to the thread manager to which this 30 thread does or will belong. 31*/ 32RegistrarThread::RegistrarThread(const char *name, int32 priority, BMessenger managerMessenger) 33 : fManagerMessenger(managerMessenger) 34 , fShouldExit(false) 35 , fIsFinished(false) 36 , fStatus(B_NO_INIT) 37 , fId(-1) 38 , fPriority(priority) 39{ 40 fName[0] = 0; 41 status_t err = name && fManagerMessenger.IsValid() ? B_OK : B_BAD_VALUE; 42 if (err == B_OK) 43 strcpy(fName, name); 44 fStatus = err; 45} 46 47// destructor 48/*! \brief Destroys the RegistrarThread object 49*/ 50RegistrarThread::~RegistrarThread() 51{ 52} 53 54// InitCheck() 55/*! \brief Returns the initialization status of the object 56*/ 57status_t 58RegistrarThread::InitCheck() 59{ 60 return fStatus; 61} 62 63// Run 64/*! \brief Begins executing the thread's ThreadFunction() 65*/ 66status_t 67RegistrarThread::Run() 68{ 69 status_t err = InitCheck(); 70 if (err == B_OK) { 71 fId = spawn_thread(&RegistrarThread::EntryFunction, fName, 72 fPriority, (void*)this); 73 err = fId >= 0 ? B_OK : fId; 74 if (err == B_OK) 75 err = resume_thread(fId); 76 } 77 return err; 78} 79 80// Id 81//! Returns the thread id 82thread_id 83RegistrarThread::Id() const 84{ 85 return fId; 86} 87 88// Name 89//! Returns the name of the thread 90const char* 91RegistrarThread::Name() const 92{ 93 return fName; 94} 95 96// AskToExit 97/*! \brief Signals to thread that it needs to quit politely as soon 98 as possible. 99*/ 100void 101RegistrarThread::AskToExit() 102{ 103 fShouldExit = true; 104} 105 106// IsFinished 107/*! \brief Returns \c true if the thread has finished executing 108*/ 109bool 110RegistrarThread::IsFinished() const 111{ 112 return fIsFinished; 113} 114 115// EntryFunction 116/*! \brief This is the function supplied to spawn_thread. It simply calls 117 ThreadFunction() on the \a data parameter, which is assumed to be a pointer 118 to a RegistrarThread object. 119*/ 120int32 121RegistrarThread::EntryFunction(void *data) 122{ 123 return ((RegistrarThread*)data)->ThreadFunction(); 124} 125