xref: /haiku/headers/private/shared/Thread.h (revision cbe0a0c436162d78cc3f92a305b64918c839d079)
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 #ifndef __THREAD__
35 #define __THREAD__
36 
37 
38 #include <Debug.h>
39 #include <Looper.h>
40 #include <OS.h>
41 
42 #include "ObjectList.h"
43 #include "FunctionObject.h"
44 
45 
46 namespace BPrivate {
47 
48 class SimpleThread {
49 	// this should only be used as a base class,
50 	// subclass needs to add proper locking mechanism
51 public:
52 	SimpleThread(int32 priority = B_LOW_PRIORITY, const char* name = 0);
53 	virtual ~SimpleThread();
54 
55 	void Go();
56 
57 private:
58 	static status_t RunBinder(void*);
59 	virtual void Run() = 0;
60 
61 protected:
62 	thread_id fScanThread;
63 	int32 fPriority;
64 	const char* fName;
65 };
66 
67 
68 class Thread : private SimpleThread {
69 public:
70 	static void Launch(FunctionObject* functor,
71 		int32 priority = B_LOW_PRIORITY, const char* name = 0);
72 
73 private:
74 	Thread(FunctionObject*, int32 priority, const char* name);
75 	~Thread();
76 	virtual void Run();
77 
78 	FunctionObject* fFunctor;
79 };
80 
81 
82 class ThreadSequence : private SimpleThread {
83 public:
84 	static void Launch(BObjectList<FunctionObject>*, bool async = true,
85 		int32 priority = B_LOW_PRIORITY);
86 
87 private:
88 	ThreadSequence(BObjectList<FunctionObject>*, int32 priority);
89 	~ThreadSequence();
90 
91 	virtual void Run();
92 	static void Run(BObjectList<FunctionObject>*list);
93 
94 	BObjectList<FunctionObject>* fFunctorList;
95 };
96 
97 
98 // would use SingleParamFunctionObjectWithResult, except mwcc won't handle this
99 template <class Param1>
100 class SingleParamFunctionObjectWorkaround : public
101 	FunctionObjectWithResult<status_t> {
102 public:
103 	SingleParamFunctionObjectWorkaround(
104 		status_t (*function)(Param1), Param1 param1)
105 		:	fFunction(function),
106 			fParam1(param1)
107 		{
108 		}
109 
110 	virtual void operator()()
111 		{ (fFunction)(fParam1); }
112 
113 	virtual ulong Size() const { return sizeof(*this); }
114 
115 private:
116 	status_t (*fFunction)(Param1);
117 	Param1 fParam1;
118 };
119 
120 
121 template <class T>
122 class SimpleMemberFunctionObjectWorkaround : public
123 	FunctionObjectWithResult<status_t> {
124 public:
125 	SimpleMemberFunctionObjectWorkaround(status_t (T::*function)(), T* onThis)
126 		:	fFunction(function),
127 			fOnThis(onThis)
128 		{
129 		}
130 
131 	virtual void operator()()
132 		{ (fOnThis->*fFunction)(); }
133 
134 	virtual ulong Size() const { return sizeof(*this); }
135 
136 private:
137 	status_t (T::*fFunction)();
138 	T fOnThis;
139 };
140 
141 
142 template <class Param1, class Param2>
143 class TwoParamFunctionObjectWorkaround : public
144 	FunctionObjectWithResult<status_t>  {
145 public:
146 	TwoParamFunctionObjectWorkaround(status_t (*callThis)(Param1, Param2),
147 		Param1 param1, Param2 param2)
148 		:	function(callThis),
149 			fParam1(param1),
150 			fParam2(param2)
151 		{
152 		}
153 
154 	virtual void operator()()
155 		{ (function)(fParam1, fParam2); }
156 
157 	virtual uint32 Size() const { return sizeof(*this); }
158 
159 private:
160 	status_t (*function)(Param1, Param2);
161 	Param1 fParam1;
162 	Param2 fParam2;
163 };
164 
165 
166 template <class Param1, class Param2, class Param3>
167 class ThreeParamFunctionObjectWorkaround : public
168 	FunctionObjectWithResult<status_t>  {
169 public:
170 	ThreeParamFunctionObjectWorkaround(
171 		status_t (*callThis)(Param1, Param2, Param3),
172 		Param1 param1, Param2 param2, Param3 param3)
173 		:	function(callThis),
174 			fParam1(param1),
175 			fParam2(param2),
176 			fParam3(param3)
177 		{
178 		}
179 
180 	virtual void operator()()
181 		{ (function)(fParam1, fParam2, fParam3); }
182 
183 	virtual uint32 Size() const { return sizeof(*this); }
184 
185 private:
186 	status_t (*function)(Param1, Param2, Param3);
187 	Param1 fParam1;
188 	Param2 fParam2;
189 	Param3 fParam3;
190 };
191 
192 
193 template <class Param1, class Param2, class Param3, class Param4>
194 class FourParamFunctionObjectWorkaround : public
195 	FunctionObjectWithResult<status_t>  {
196 public:
197 	FourParamFunctionObjectWorkaround(
198 		status_t (*callThis)(Param1, Param2, Param3, Param4),
199 		Param1 param1, Param2 param2, Param3 param3, Param4 param4)
200 		:	function(callThis),
201 			fParam1(param1),
202 			fParam2(param2),
203 			fParam3(param3),
204 			fParam4(param4)
205 		{
206 		}
207 
208 	virtual void operator()()
209 		{ (function)(fParam1, fParam2, fParam3, fParam4); }
210 
211 	virtual uint32 Size() const { return sizeof(*this); }
212 
213 private:
214 	status_t (*function)(Param1, Param2, Param3, Param4);
215 	Param1 fParam1;
216 	Param2 fParam2;
217 	Param3 fParam3;
218 	Param4 fParam4;
219 };
220 
221 
222 template<class Param1>
223 void
224 LaunchInNewThread(const char* name, int32 priority, status_t (*func)(Param1),
225 	Param1 p1)
226 {
227 	Thread::Launch(new SingleParamFunctionObjectWorkaround<Param1>(func, p1),
228 		priority, name);
229 }
230 
231 
232 template<class T>
233 void
234 LaunchInNewThread(const char* name, int32 priority, status_t (T::*function)(),
235 	T* onThis)
236 {
237 	Thread::Launch(new SimpleMemberFunctionObjectWorkaround<T>(function,
238 		onThis), priority, name);
239 }
240 
241 
242 template<class Param1, class Param2>
243 void
244 LaunchInNewThread(const char* name, int32 priority,
245 	status_t (*func)(Param1, Param2),
246 	Param1 p1, Param2 p2)
247 {
248 	Thread::Launch(new
249 		TwoParamFunctionObjectWorkaround<Param1, Param2>(func, p1, p2),
250 			priority, name);
251 }
252 
253 
254 template<class Param1, class Param2, class Param3>
255 void
256 LaunchInNewThread(const char* name, int32 priority,
257 	status_t (*func)(Param1, Param2, Param3),
258 	Param1 p1, Param2 p2, Param3 p3)
259 {
260 	Thread::Launch(new ThreeParamFunctionObjectWorkaround<Param1, Param2,
261 		Param3>(func, p1, p2, p3), priority, name);
262 }
263 
264 
265 template<class Param1, class Param2, class Param3, class Param4>
266 void
267 LaunchInNewThread(const char* name, int32 priority,
268 	status_t (*func)(Param1, Param2, Param3, Param4),
269 	Param1 p1, Param2 p2, Param3 p3, Param4 p4)
270 {
271 	Thread::Launch(new FourParamFunctionObjectWorkaround<Param1, Param2,
272 		Param3, Param4>(func, p1, p2, p3, p4), priority, name);
273 }
274 
275 
276 } // namespace BPrivate
277 
278 using namespace BPrivate;
279 
280 #endif	// __THREAD__
281