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 */ 32 RegistrarThread::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 { 39 fName[0] = 0; 40 status_t err = name && fManagerMessenger.IsValid() ? B_OK : B_BAD_VALUE; 41 if (!err) { 42 fId = spawn_thread(&RegistrarThread::EntryFunction, name, 43 priority, (void*)this); 44 err = fId >= 0 ? B_OK : fId; 45 } 46 if (!err) { 47 strcpy(fName, name); 48 } 49 fStatus = err; 50 } 51 52 // destructor 53 /*! \brief Destroys the RegistrarThread object 54 */ 55 RegistrarThread::~RegistrarThread() 56 { 57 } 58 59 // InitCheck() 60 /*! \brief Returns the initialization status of the object 61 */ 62 status_t 63 RegistrarThread::InitCheck() 64 { 65 return fStatus; 66 } 67 68 // Run 69 /*! \brief Begins executing the thread's ThreadFunction() 70 */ 71 status_t 72 RegistrarThread::Run() 73 { 74 status_t err = InitCheck(); 75 if (!err) 76 err = resume_thread(fId); 77 return err; 78 } 79 80 // Id 81 //! Returns the thread id 82 thread_id 83 RegistrarThread::Id() const 84 { 85 return fId; 86 } 87 88 // Name 89 //! Returns the name of the thread 90 const char* 91 RegistrarThread::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 */ 100 void 101 RegistrarThread::AskToExit() 102 { 103 fShouldExit = true; 104 } 105 106 // IsFinished 107 /*! \brief Returns \c true if the thread has finished executing 108 */ 109 bool 110 RegistrarThread::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 */ 120 int32 121 RegistrarThread::EntryFunction(void *data) 122 { 123 return ((RegistrarThread*)data)->ThreadFunction(); 124 } 125