1/* 2Open Tracker License 3 4Terms and Conditions 5 6Copyright (c) 1991-2000, Be Incorporated. All rights reserved. 7 8Permission is hereby granted, free of charge, to any person obtaining a copy of 9this software and associated documentation files (the "Software"), to deal in 10the Software without restriction, including without limitation the rights to 11use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 12of the Software, and to permit persons to whom the Software is furnished to do 13so, subject to the following conditions: 14 15The above copyright notice and this permission notice applies to all licensees 16and shall be included in all copies or substantial portions of the Software. 17 18THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY, 20FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 21BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 22AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION 23WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 25Except as contained in this notice, the name of Be Incorporated shall not be 26used in advertising or otherwise to promote the sale, use or other dealings in 27this Software without prior written authorization from Be Incorporated. 28 29Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered trademarks 30of Be Incorporated in the United States and other countries. Other brand product 31names are registered trademarks or trademarks of their respective holders. 32All rights reserved. 33*/ 34 35 36#include "Thread.h" 37#include "FunctionObject.h" 38 39 40SimpleThread::SimpleThread(int32 priority, const char* name) 41 : fScanThread(-1), 42 fPriority(priority), 43 fName(name) 44{ 45} 46 47 48SimpleThread::~SimpleThread() 49{ 50 if (fScanThread > 0 && fScanThread != find_thread(NULL)) { 51 // kill the thread if it is not the one we are running in 52 kill_thread(fScanThread); 53 } 54} 55 56 57void 58SimpleThread::Go() 59{ 60 fScanThread = spawn_thread(SimpleThread::RunBinder, 61 fName ? fName : "TrackerTaskLoop", fPriority, this); 62 resume_thread(fScanThread); 63} 64 65 66status_t 67SimpleThread::RunBinder(void* castToThis) 68{ 69 SimpleThread* self = static_cast<SimpleThread*>(castToThis); 70 self->Run(); 71 return B_OK; 72} 73 74 75void 76Thread::Launch(FunctionObject* functor, int32 priority, const char* name) 77{ 78 new Thread(functor, priority, name); 79} 80 81 82Thread::Thread(FunctionObject* functor, int32 priority, const char* name) 83 : SimpleThread(priority, name), 84 fFunctor(functor) 85{ 86 Go(); 87} 88 89 90Thread::~Thread() 91{ 92 delete fFunctor; 93} 94 95 96void 97Thread::Run() 98{ 99 (*fFunctor)(); 100 delete this; 101 // commit suicide 102} 103 104 105void 106ThreadSequence::Launch(BObjectList<FunctionObject>* list, bool async, 107 int32 priority) 108{ 109 if (!async) { 110 // if not async, don't even create a thread, just do it right away 111 Run(list); 112 } else 113 new ThreadSequence(list, priority); 114} 115 116 117ThreadSequence::ThreadSequence(BObjectList<FunctionObject>* list, 118 int32 priority) 119 : SimpleThread(priority), 120 fFunctorList(list) 121{ 122 Go(); 123} 124 125 126ThreadSequence::~ThreadSequence() 127{ 128 delete fFunctorList; 129} 130 131 132void 133ThreadSequence::Run(BObjectList<FunctionObject>* list) 134{ 135 int32 count = list->CountItems(); 136 for (int32 index = 0; index < count; index++) 137 (*list->ItemAt(index))(); 138} 139 140 141void 142ThreadSequence::Run() 143{ 144 Run(fFunctorList); 145 delete this; 146 // commit suicide 147} 148