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#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
46namespace BPrivate {
47
48class SimpleThread {
49	// this should only be used as a base class,
50	// subclass needs to add proper locking mechanism
51public:
52	SimpleThread(int32 priority = B_LOW_PRIORITY, const char* name = 0);
53	virtual ~SimpleThread();
54
55	void Go();
56
57private:
58	static status_t RunBinder(void*);
59	virtual void Run() = 0;
60
61protected:
62	thread_id fScanThread;
63	int32 fPriority;
64	const char* fName;
65};
66
67
68class Thread : private SimpleThread {
69public:
70	static void Launch(FunctionObject* functor,
71		int32 priority = B_LOW_PRIORITY, const char* name = 0);
72
73private:
74	Thread(FunctionObject*, int32 priority, const char* name);
75	~Thread();
76	virtual void Run();
77
78	FunctionObject* fFunctor;
79};
80
81
82class ThreadSequence : private SimpleThread {
83public:
84	static void Launch(BObjectList<FunctionObject>*, bool async = true,
85		int32 priority = B_LOW_PRIORITY);
86
87private:
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
99template <class Param1>
100class SingleParamFunctionObjectWorkaround : public
101	FunctionObjectWithResult<status_t> {
102public:
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
115private:
116	status_t (*fFunction)(Param1);
117	Param1 fParam1;
118};
119
120
121template <class T>
122class SimpleMemberFunctionObjectWorkaround : public
123	FunctionObjectWithResult<status_t> {
124public:
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
136private:
137	status_t (T::*fFunction)();
138	T fOnThis;
139};
140
141
142template <class Param1, class Param2>
143class TwoParamFunctionObjectWorkaround : public
144	FunctionObjectWithResult<status_t>  {
145public:
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
159private:
160	status_t (*function)(Param1, Param2);
161	Param1 fParam1;
162	Param2 fParam2;
163};
164
165
166template <class Param1, class Param2, class Param3>
167class ThreeParamFunctionObjectWorkaround : public
168	FunctionObjectWithResult<status_t>  {
169public:
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
185private:
186	status_t (*function)(Param1, Param2, Param3);
187	Param1 fParam1;
188	Param2 fParam2;
189	Param3 fParam3;
190};
191
192
193template <class Param1, class Param2, class Param3, class Param4>
194class FourParamFunctionObjectWorkaround : public
195	FunctionObjectWithResult<status_t>  {
196public:
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
213private:
214	status_t (*function)(Param1, Param2, Param3, Param4);
215	Param1 fParam1;
216	Param2 fParam2;
217	Param3 fParam3;
218	Param4 fParam4;
219};
220
221
222template<class Param1>
223void
224LaunchInNewThread(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
232template<class T>
233void
234LaunchInNewThread(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
242template<class Param1, class Param2>
243void
244LaunchInNewThread(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
254template<class Param1, class Param2, class Param3>
255void
256LaunchInNewThread(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
265template<class Param1, class Param2, class Param3, class Param4>
266void
267LaunchInNewThread(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
278using namespace BPrivate;
279
280#endif	// __THREAD__
281