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