2005-05-23 04:58:42 +04:00
|
|
|
/*
|
|
|
|
Open Tracker License
|
|
|
|
|
|
|
|
Terms and Conditions
|
|
|
|
|
|
|
|
Copyright (c) 1991-2000, Be Incorporated. All rights reserved.
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
this software and associated documentation files (the "Software"), to deal in
|
|
|
|
the Software without restriction, including without limitation the rights to
|
|
|
|
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
|
|
of the Software, and to permit persons to whom the Software is furnished to do
|
|
|
|
so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice applies to all licensees
|
|
|
|
and shall be included in all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
|
|
|
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION
|
|
|
|
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
|
|
|
Except as contained in this notice, the name of Be Incorporated shall not be
|
|
|
|
used in advertising or otherwise to promote the sale, use or other dealings in
|
|
|
|
this Software without prior written authorization from Be Incorporated.
|
|
|
|
|
|
|
|
Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered trademarks
|
|
|
|
of Be Incorporated in the United States and other countries. Other brand product
|
|
|
|
names are registered trademarks or trademarks of their respective holders.
|
|
|
|
All rights reserved.
|
|
|
|
*/
|
|
|
|
#ifndef __THREAD__
|
|
|
|
#define __THREAD__
|
|
|
|
|
2012-07-28 03:07:09 +04:00
|
|
|
|
2009-04-12 01:24:32 +04:00
|
|
|
#include <Debug.h>
|
2013-10-09 03:46:21 +04:00
|
|
|
#include <Looper.h>
|
2005-05-23 04:58:42 +04:00
|
|
|
#include <OS.h>
|
2009-04-12 01:24:32 +04:00
|
|
|
|
2005-05-23 04:58:42 +04:00
|
|
|
#include "ObjectList.h"
|
|
|
|
#include "FunctionObject.h"
|
|
|
|
|
2012-07-28 03:07:09 +04:00
|
|
|
|
2005-05-23 04:58:42 +04:00
|
|
|
namespace BPrivate {
|
|
|
|
|
|
|
|
class SimpleThread {
|
|
|
|
// this should only be used as a base class,
|
|
|
|
// subclass needs to add proper locking mechanism
|
|
|
|
public:
|
2012-07-28 03:07:09 +04:00
|
|
|
SimpleThread(int32 priority = B_LOW_PRIORITY, const char* name = 0);
|
2005-05-23 04:58:42 +04:00
|
|
|
virtual ~SimpleThread();
|
2009-04-12 01:24:32 +04:00
|
|
|
|
2005-05-23 04:58:42 +04:00
|
|
|
void Go();
|
|
|
|
|
|
|
|
private:
|
2012-07-28 03:07:09 +04:00
|
|
|
static status_t RunBinder(void*);
|
2005-05-23 04:58:42 +04:00
|
|
|
virtual void Run() = 0;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
thread_id fScanThread;
|
|
|
|
int32 fPriority;
|
2012-07-28 03:07:09 +04:00
|
|
|
const char* fName;
|
2005-05-23 04:58:42 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class Thread : private SimpleThread {
|
|
|
|
public:
|
2012-07-28 03:07:09 +04:00
|
|
|
static void Launch(FunctionObject* functor,
|
|
|
|
int32 priority = B_LOW_PRIORITY, const char* name = 0);
|
2005-05-23 04:58:42 +04:00
|
|
|
|
|
|
|
private:
|
2012-07-28 03:07:09 +04:00
|
|
|
Thread(FunctionObject*, int32 priority, const char* name);
|
2005-05-23 04:58:42 +04:00
|
|
|
~Thread();
|
|
|
|
virtual void Run();
|
|
|
|
|
2012-07-28 03:07:09 +04:00
|
|
|
FunctionObject* fFunctor;
|
2005-05-23 04:58:42 +04:00
|
|
|
};
|
|
|
|
|
2013-10-09 03:46:21 +04:00
|
|
|
|
2005-05-23 04:58:42 +04:00
|
|
|
class ThreadSequence : private SimpleThread {
|
|
|
|
public:
|
2012-07-28 03:07:09 +04:00
|
|
|
static void Launch(BObjectList<FunctionObject>*, bool async = true,
|
2005-05-23 04:58:42 +04:00
|
|
|
int32 priority = B_LOW_PRIORITY);
|
|
|
|
|
|
|
|
private:
|
2012-07-28 03:07:09 +04:00
|
|
|
ThreadSequence(BObjectList<FunctionObject>*, int32 priority);
|
2005-05-23 04:58:42 +04:00
|
|
|
~ThreadSequence();
|
|
|
|
|
|
|
|
virtual void Run();
|
2012-07-28 03:07:09 +04:00
|
|
|
static void Run(BObjectList<FunctionObject>*list);
|
2005-05-23 04:58:42 +04:00
|
|
|
|
2012-07-28 03:07:09 +04:00
|
|
|
BObjectList<FunctionObject>* fFunctorList;
|
2005-05-23 04:58:42 +04:00
|
|
|
};
|
|
|
|
|
2013-10-09 03:46:21 +04:00
|
|
|
|
2005-05-23 04:58:42 +04:00
|
|
|
// would use SingleParamFunctionObjectWithResult, except mwcc won't handle this
|
|
|
|
template <class Param1>
|
2012-07-28 22:00:24 +04:00
|
|
|
class SingleParamFunctionObjectWorkaround : public
|
|
|
|
FunctionObjectWithResult<status_t> {
|
2005-05-23 04:58:42 +04:00
|
|
|
public:
|
2012-07-28 22:00:24 +04:00
|
|
|
SingleParamFunctionObjectWorkaround(
|
|
|
|
status_t (*function)(Param1), Param1 param1)
|
2005-05-23 04:58:42 +04:00
|
|
|
: fFunction(function),
|
|
|
|
fParam1(param1)
|
|
|
|
{
|
|
|
|
}
|
2009-04-12 01:24:32 +04:00
|
|
|
|
2005-05-23 04:58:42 +04:00
|
|
|
virtual void operator()()
|
|
|
|
{ (fFunction)(fParam1); }
|
|
|
|
|
|
|
|
virtual ulong Size() const { return sizeof(*this); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
status_t (*fFunction)(Param1);
|
|
|
|
Param1 fParam1;
|
|
|
|
};
|
|
|
|
|
2013-10-09 03:46:21 +04:00
|
|
|
|
2005-05-23 04:58:42 +04:00
|
|
|
template <class T>
|
2012-07-28 22:00:24 +04:00
|
|
|
class SimpleMemberFunctionObjectWorkaround : public
|
|
|
|
FunctionObjectWithResult<status_t> {
|
2005-05-23 04:58:42 +04:00
|
|
|
public:
|
2012-07-28 03:07:09 +04:00
|
|
|
SimpleMemberFunctionObjectWorkaround(status_t (T::*function)(), T* onThis)
|
2005-05-23 04:58:42 +04:00
|
|
|
: fFunction(function),
|
|
|
|
fOnThis(onThis)
|
|
|
|
{
|
|
|
|
}
|
2009-04-12 01:24:32 +04:00
|
|
|
|
2005-05-23 04:58:42 +04:00
|
|
|
virtual void operator()()
|
|
|
|
{ (fOnThis->*fFunction)(); }
|
|
|
|
|
|
|
|
virtual ulong Size() const { return sizeof(*this); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
status_t (T::*fFunction)();
|
|
|
|
T fOnThis;
|
|
|
|
};
|
|
|
|
|
2012-07-28 03:07:09 +04:00
|
|
|
|
2005-05-23 04:58:42 +04:00
|
|
|
template <class Param1, class Param2>
|
2012-07-28 22:00:24 +04:00
|
|
|
class TwoParamFunctionObjectWorkaround : public
|
|
|
|
FunctionObjectWithResult<status_t> {
|
2005-05-23 04:58:42 +04:00
|
|
|
public:
|
|
|
|
TwoParamFunctionObjectWorkaround(status_t (*callThis)(Param1, Param2),
|
|
|
|
Param1 param1, Param2 param2)
|
|
|
|
: function(callThis),
|
|
|
|
fParam1(param1),
|
|
|
|
fParam2(param2)
|
|
|
|
{
|
|
|
|
}
|
2009-04-12 01:24:32 +04:00
|
|
|
|
2005-05-23 04:58:42 +04:00
|
|
|
virtual void operator()()
|
|
|
|
{ (function)(fParam1, fParam2); }
|
|
|
|
|
|
|
|
virtual uint32 Size() const { return sizeof(*this); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
status_t (*function)(Param1, Param2);
|
|
|
|
Param1 fParam1;
|
|
|
|
Param2 fParam2;
|
|
|
|
};
|
|
|
|
|
2012-07-28 03:07:09 +04:00
|
|
|
|
2005-05-23 04:58:42 +04:00
|
|
|
template <class Param1, class Param2, class Param3>
|
2012-07-28 22:00:24 +04:00
|
|
|
class ThreeParamFunctionObjectWorkaround : public
|
|
|
|
FunctionObjectWithResult<status_t> {
|
2005-05-23 04:58:42 +04:00
|
|
|
public:
|
2012-07-28 22:00:24 +04:00
|
|
|
ThreeParamFunctionObjectWorkaround(
|
|
|
|
status_t (*callThis)(Param1, Param2, Param3),
|
2005-05-23 04:58:42 +04:00
|
|
|
Param1 param1, Param2 param2, Param3 param3)
|
|
|
|
: function(callThis),
|
|
|
|
fParam1(param1),
|
|
|
|
fParam2(param2),
|
|
|
|
fParam3(param3)
|
|
|
|
{
|
|
|
|
}
|
2009-04-12 01:24:32 +04:00
|
|
|
|
2005-05-23 04:58:42 +04:00
|
|
|
virtual void operator()()
|
|
|
|
{ (function)(fParam1, fParam2, fParam3); }
|
|
|
|
|
|
|
|
virtual uint32 Size() const { return sizeof(*this); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
status_t (*function)(Param1, Param2, Param3);
|
|
|
|
Param1 fParam1;
|
|
|
|
Param2 fParam2;
|
|
|
|
Param3 fParam3;
|
|
|
|
};
|
|
|
|
|
2012-07-28 03:07:09 +04:00
|
|
|
|
2005-05-23 04:58:42 +04:00
|
|
|
template <class Param1, class Param2, class Param3, class Param4>
|
2012-07-28 22:00:24 +04:00
|
|
|
class FourParamFunctionObjectWorkaround : public
|
|
|
|
FunctionObjectWithResult<status_t> {
|
2005-05-23 04:58:42 +04:00
|
|
|
public:
|
2012-07-28 22:00:24 +04:00
|
|
|
FourParamFunctionObjectWorkaround(
|
|
|
|
status_t (*callThis)(Param1, Param2, Param3, Param4),
|
2005-05-23 04:58:42 +04:00
|
|
|
Param1 param1, Param2 param2, Param3 param3, Param4 param4)
|
|
|
|
: function(callThis),
|
|
|
|
fParam1(param1),
|
|
|
|
fParam2(param2),
|
|
|
|
fParam3(param3),
|
|
|
|
fParam4(param4)
|
|
|
|
{
|
|
|
|
}
|
2009-04-12 01:24:32 +04:00
|
|
|
|
2005-05-23 04:58:42 +04:00
|
|
|
virtual void operator()()
|
|
|
|
{ (function)(fParam1, fParam2, fParam3, fParam4); }
|
|
|
|
|
|
|
|
virtual uint32 Size() const { return sizeof(*this); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
status_t (*function)(Param1, Param2, Param3, Param4);
|
|
|
|
Param1 fParam1;
|
|
|
|
Param2 fParam2;
|
|
|
|
Param3 fParam3;
|
|
|
|
Param4 fParam4;
|
|
|
|
};
|
|
|
|
|
2012-07-28 03:07:09 +04:00
|
|
|
|
2005-05-23 04:58:42 +04:00
|
|
|
template<class Param1>
|
2009-04-12 01:24:32 +04:00
|
|
|
void
|
2012-07-28 03:07:09 +04:00
|
|
|
LaunchInNewThread(const char* name, int32 priority, status_t (*func)(Param1),
|
|
|
|
Param1 p1)
|
2005-05-23 04:58:42 +04:00
|
|
|
{
|
|
|
|
Thread::Launch(new SingleParamFunctionObjectWorkaround<Param1>(func, p1),
|
|
|
|
priority, name);
|
|
|
|
}
|
|
|
|
|
2012-07-28 03:07:09 +04:00
|
|
|
|
2005-05-23 04:58:42 +04:00
|
|
|
template<class T>
|
2009-04-12 01:24:32 +04:00
|
|
|
void
|
2012-07-28 03:07:09 +04:00
|
|
|
LaunchInNewThread(const char* name, int32 priority, status_t (T::*function)(),
|
|
|
|
T* onThis)
|
2005-05-23 04:58:42 +04:00
|
|
|
{
|
2012-07-28 03:07:09 +04:00
|
|
|
Thread::Launch(new SimpleMemberFunctionObjectWorkaround<T>(function,
|
|
|
|
onThis), priority, name);
|
2005-05-23 04:58:42 +04:00
|
|
|
}
|
|
|
|
|
2012-07-28 03:07:09 +04:00
|
|
|
|
2005-05-23 04:58:42 +04:00
|
|
|
template<class Param1, class Param2>
|
2009-04-12 01:24:32 +04:00
|
|
|
void
|
2012-07-28 03:07:09 +04:00
|
|
|
LaunchInNewThread(const char* name, int32 priority,
|
2005-05-23 04:58:42 +04:00
|
|
|
status_t (*func)(Param1, Param2),
|
|
|
|
Param1 p1, Param2 p2)
|
|
|
|
{
|
2012-07-28 03:07:09 +04:00
|
|
|
Thread::Launch(new
|
|
|
|
TwoParamFunctionObjectWorkaround<Param1, Param2>(func, p1, p2),
|
|
|
|
priority, name);
|
2005-05-23 04:58:42 +04:00
|
|
|
}
|
|
|
|
|
2012-07-28 03:07:09 +04:00
|
|
|
|
2005-05-23 04:58:42 +04:00
|
|
|
template<class Param1, class Param2, class Param3>
|
2009-04-12 01:24:32 +04:00
|
|
|
void
|
2012-07-28 03:07:09 +04:00
|
|
|
LaunchInNewThread(const char* name, int32 priority,
|
2005-05-23 04:58:42 +04:00
|
|
|
status_t (*func)(Param1, Param2, Param3),
|
|
|
|
Param1 p1, Param2 p2, Param3 p3)
|
|
|
|
{
|
|
|
|
Thread::Launch(new ThreeParamFunctionObjectWorkaround<Param1, Param2,
|
|
|
|
Param3>(func, p1, p2, p3), priority, name);
|
|
|
|
}
|
|
|
|
|
2012-07-28 03:07:09 +04:00
|
|
|
|
2005-05-23 04:58:42 +04:00
|
|
|
template<class Param1, class Param2, class Param3, class Param4>
|
2009-04-12 01:24:32 +04:00
|
|
|
void
|
2012-07-28 03:07:09 +04:00
|
|
|
LaunchInNewThread(const char* name, int32 priority,
|
2005-05-23 04:58:42 +04:00
|
|
|
status_t (*func)(Param1, Param2, Param3, Param4),
|
|
|
|
Param1 p1, Param2 p2, Param3 p3, Param4 p4)
|
|
|
|
{
|
|
|
|
Thread::Launch(new FourParamFunctionObjectWorkaround<Param1, Param2,
|
|
|
|
Param3, Param4>(func, p1, p2, p3, p4), priority, name);
|
|
|
|
}
|
|
|
|
|
2012-07-28 03:07:09 +04:00
|
|
|
|
2005-05-23 04:58:42 +04:00
|
|
|
} // namespace BPrivate
|
|
|
|
|
|
|
|
using namespace BPrivate;
|
|
|
|
|
2012-07-28 03:07:09 +04:00
|
|
|
#endif // __THREAD__
|