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