Added sharedThread library

This commit is contained in:
Anonymous
2014-01-14 01:29:38 -07:00
parent 49412616af
commit f521bff27c
19 changed files with 1053 additions and 0 deletions
@@ -0,0 +1,17 @@
// ======================================================================
//
// FirstThread.h
// copyright (c) 2001 Sony Online Entertainment
//
// ======================================================================
#ifndef INCLUDED_FirstThread_H
#define INCLUDED_FirstThread_H
// ======================================================================
#include "sharedFoundation/FirstSharedFoundation.h"
// ======================================================================
#endif
@@ -0,0 +1,44 @@
// ======================================================================
//
// runThread.cpp
// Acy Stapp
//
// Copyright 6/19/2001 Sony Online Entertainment
//
// ======================================================================
#include "sharedThread/FirstSharedThread.h"
#include "sharedThread/RunThread.h"
FuncPtrThreadZero::Handle runThread(void (* func)())
{
return TypedThreadHandle<FuncPtrThreadZero> (new FuncPtrThreadZero(func));
}
FuncPtrThreadZero::Handle runNamedThread(const std::string &name, void (* func)())
{
return TypedThreadHandle<FuncPtrThreadZero> (new FuncPtrThreadZero(name, func));
}
#if 0
// Testing stuff
class testclass
{
public:
void zeroArg() {}
void oneArg(testclass *) {}
void twoArg(testclass *, int) {}
};
testclass m;
typedef MemberFunctionThreadZero<testclass>::Handle AsyncTestMemberFunctionZero;
AsyncTestMemberFunctionZero temp = runThread(m, testclass::zeroArg);
typedef MemberFunctionThreadOne<testclass, testclass *>::Handle AsyncTestMemberFunctionOne;
AsyncTestMemberFunctionOne temp2 = runThread(m, testclass::oneArg, &m);
typedef MemberFunctionThreadTwo<testclass, testclass *, int>::Handle AsyncTestMemberFunctionTwo;
AsyncTestMemberFunctionTwo temp3 = runThread(m, testclass::twoArg, &m, 4);
#endif
@@ -0,0 +1,201 @@
// ======================================================================
//
// runThread.h
// Acy Stapp
//
// Copyright 6/19/2001 Sony Online Entertainment
//
// ======================================================================
#ifndef INCLUDED_runThread_h
#define INCLUDED_runThread_h
#include "sharedThread/ThreadHandle.h"
#include "sharedThread/Thread.h"
// ------------------------------------------------------------------------------------------
// Thread starters
// ------------------------------------------------------------------------------------------
template <class CLASS>
class MemberFunctionThreadZero: public Thread
{
public:
typedef void (CLASS::* FUNCTION)(void);
typedef TypedThreadHandle<MemberFunctionThreadZero<CLASS> > Handle;
MemberFunctionThreadZero(CLASS &i_object, FUNCTION i_func): Thread(), object(i_object), func(i_func) {}
MemberFunctionThreadZero(const std::string &i_name, CLASS &i_object, FUNCTION i_func): Thread(i_name), object(i_object), func(i_func) {}
private:
MemberFunctionThreadZero(const MemberFunctionThreadZero &o);
MemberFunctionThreadZero &operator =(const MemberFunctionThreadZero &o);
void run() { (object.*func)(); }
CLASS &object;
FUNCTION func;
};
template <class CLASS>
typename MemberFunctionThreadZero<CLASS>::Handle runThread(CLASS &o, typename MemberFunctionThreadZero<CLASS>::FUNCTION function)
{
return TypedThreadHandle<MemberFunctionThreadZero<CLASS> > (new MemberFunctionThreadZero<CLASS>(o, function));
}
template <class CLASS>
TypedThreadHandle<MemberFunctionThreadZero<CLASS> > runNamedThread(const std::string &name, CLASS &o, typename MemberFunctionThreadZero<CLASS>::FUNCTION function)
{
return TypedThreadHandle<MemberFunctionThreadZero<CLASS> > (new MemberFunctionThreadZero<CLASS>(name, o, function));
}
// ------------------------------------------------------------------------------------------
template <class CLASS, class ARG1>
class MemberFunctionThreadOne: public Thread
{
public:
typedef void (CLASS::* FUNCTION)(ARG1 o);
typedef TypedThreadHandle<MemberFunctionThreadOne<CLASS, ARG1> > Handle;
MemberFunctionThreadOne(CLASS &i_object, FUNCTION i_func, ARG1 i_arg1): Thread(), object(i_object), func(i_func), arg1(i_arg1) {}
MemberFunctionThreadOne(const std::string &i_name, CLASS &i_object, FUNCTION i_func, ARG1 i_arg1): Thread(i_name), object(i_object), func(i_func), arg1(i_arg1) {}
private:
MemberFunctionThreadOne(const MemberFunctionThreadOne &o);
MemberFunctionThreadOne &operator =(const MemberFunctionThreadOne &o);
void run() { (object.*func)(arg1); }
CLASS &object;
FUNCTION func;
ARG1 arg1;
};
template <class CLASS, class ARG1>
TypedThreadHandle<MemberFunctionThreadOne<CLASS, ARG1> > runThread(CLASS &o, void (CLASS::* function)(ARG1 o), ARG1 arg1)
{
return TypedThreadHandle<MemberFunctionThreadOne<CLASS, ARG1> > (new MemberFunctionThreadOne<CLASS, ARG1>(o, function, arg1));
}
template <class CLASS, class ARG1>
TypedThreadHandle<MemberFunctionThreadOne<CLASS, ARG1> > runNamedThread(const std::string &name, CLASS &o, void (CLASS::* function)(ARG1 o), ARG1 arg1)
{
return TypedThreadHandle<MemberFunctionThreadOne<CLASS, ARG1> > (new MemberFunctionThreadOne<CLASS, ARG1>(name, o, function, arg1));
}
// ------------------------------------------------------------------------------------------
template <class CLASS, class ARG1, class ARG2>
class MemberFunctionThreadTwo: public Thread
{
public:
typedef void (CLASS::* FUNCTION)(ARG1 o, ARG2 p);
typedef TypedThreadHandle<MemberFunctionThreadTwo<CLASS, ARG1, ARG2> > Handle;
MemberFunctionThreadTwo(CLASS &i_object, FUNCTION i_func, ARG1 i_arg1, ARG2 i_arg2): Thread(), object(i_object), func(i_func), arg1(i_arg1), arg2(i_arg2) {}
MemberFunctionThreadTwo(const std::string &i_name, CLASS &i_object, FUNCTION i_func, ARG1 i_arg1, ARG2 i_arg2): Thread(i_name), object(i_object), func(i_func), arg1(i_arg1), arg2(i_arg2) {}
private:
MemberFunctionThreadTwo(const MemberFunctionThreadTwo &o);
MemberFunctionThreadTwo &operator =(const MemberFunctionThreadTwo &o);
void run() { (object.*func)(arg1, arg2); }
CLASS &object;
FUNCTION func;
ARG1 arg1;
ARG2 arg2;
};
template <class CLASS, class ARG1, class ARG2>
TypedThreadHandle<MemberFunctionThreadTwo<CLASS, ARG1, ARG2> > runThread(CLASS &o, void (CLASS::* function)(ARG1 o, ARG2 p), ARG1 arg1, ARG2 arg2)
{
return TypedThreadHandle<MemberFunctionThreadTwo<CLASS, ARG1, ARG2> > (new MemberFunctionThreadTwo<CLASS, ARG1, ARG2>(o, function, arg1, arg2));
}
template <class CLASS, class ARG1, class ARG2>
TypedThreadHandle<MemberFunctionThreadTwo<CLASS, ARG1, ARG2> > runNamedThread(const std::string &name, CLASS &o, void (CLASS::* function)(ARG1 o, ARG2 p), ARG1 arg1, ARG2 arg2)
{
return TypedThreadHandle<MemberFunctionThreadTwo<CLASS, ARG1, ARG2> > (new MemberFunctionThreadTwo<CLASS, ARG1, ARG2>(name, o, function, arg1, arg2));
}
// -----------------------------------------------------------------------------
class FuncPtrThreadZero: public Thread
{
public:
typedef void (* FUNCTION)();
typedef TypedThreadHandle<FuncPtrThreadZero> Handle;
explicit FuncPtrThreadZero(void (* i_func)()): Thread(), function(i_func) {}
FuncPtrThreadZero(const std::string &i_name, void (* i_func)()): Thread(i_name), function(i_func) {}
private:
FuncPtrThreadZero();
FuncPtrThreadZero(const FuncPtrThreadZero &o);
FuncPtrThreadZero &operator =(const FuncPtrThreadZero &o);
void run() { function(); }
FUNCTION function;
};
TypedThreadHandle<FuncPtrThreadZero> runThread(FuncPtrThreadZero::FUNCTION func);
TypedThreadHandle<FuncPtrThreadZero> runNamedThread(const std::string &name, FuncPtrThreadZero::FUNCTION func);
// -----------------------------------------------------------------------------
template <class ARG1>
class FuncPtrThreadOne: public Thread
{
public:
typedef void (* FUNCTION)(ARG1 arg1);
typedef TypedThreadHandle<FuncPtrThreadOne<ARG1> > Handle;
FuncPtrThreadOne(FUNCTION i_func, ARG1 i_arg1): Thread(), function(i_func), arg1(i_arg1) {}
FuncPtrThreadOne(const std::string &i_name, FUNCTION i_func, ARG1 i_arg1): Thread(i_name), function(i_func), arg1(i_arg1) {}
private:
FuncPtrThreadOne(const FuncPtrThreadOne &o);
FuncPtrThreadOne &operator =(const FuncPtrThreadOne &o);
void run() { function(arg1); }
FUNCTION function;
ARG1 arg1;
};
template <class ARG1>
TypedThreadHandle<FuncPtrThreadOne<ARG1> > runThread(typename FuncPtrThreadOne<ARG1>::FUNCTION func, ARG1 arg1)
{
return TypedThreadHandle<FuncPtrThreadOne<ARG1> > (new FuncPtrThreadOne<ARG1>(func, arg1));
}
template <class ARG1>
TypedThreadHandle<FuncPtrThreadOne<ARG1> > runNamedThread(const std::string &name, typename FuncPtrThreadOne<ARG1>::FUNCTION func, ARG1 arg1)
{
return TypedThreadHandle<FuncPtrThreadOne<ARG1> > (new FuncPtrThreadOne<ARG1>(name, func, arg1));
}
// -----------------------------------------------------------------------------
template <class ARG1, class ARG2>
class FuncPtrThreadTwo: public Thread
{
public:
typedef void (* FUNCTION)(ARG1, ARG2);
typedef TypedThreadHandle<FuncPtrThreadTwo<ARG1,ARG2> > Handle;
FuncPtrThreadTwo(FUNCTION i_func, ARG1 i_arg1, ARG2 i_arg2): Thread(), function(i_func), arg1(i_arg1), arg2(i_arg2) {}
FuncPtrThreadTwo(const std::string &i_name, FUNCTION i_func, ARG1 i_arg1, ARG2 i_arg2): Thread(i_name), function(i_func), arg1(i_arg1), arg2(i_arg2) {}
private:
FuncPtrThreadTwo(const FuncPtrThreadTwo &o);
FuncPtrThreadTwo &operator =(const FuncPtrThreadTwo &o);
void run() { function(arg1, arg2); }
FUNCTION function;
ARG1 arg1;
ARG2 arg2;
};
template <class ARG1, class ARG2>
TypedThreadHandle<FuncPtrThreadTwo<ARG1,ARG2> > runThread(typename FuncPtrThreadTwo<ARG1,ARG2>::FUNCTION func, ARG1 arg1, ARG2 arg2)
{
return TypedThreadHandle<FuncPtrThreadTwo<ARG1,ARG2> > (new FuncPtrThreadTwo<ARG1,ARG2>(func, arg1, arg2));
}
template <class ARG1, class ARG2>
TypedThreadHandle<FuncPtrThreadTwo<ARG1,ARG2> > runNamedThread(const std::string &name, typename FuncPtrThreadTwo<ARG1,ARG2>::FUNCTION func, ARG1 arg1, ARG2 arg2)
{
return TypedThreadHandle<FuncPtrThreadTwo<ARG1,ARG2> > (new FuncPtrThreadTwo<ARG1,ARG2>(name, func, arg1, arg2));
}
#endif
@@ -0,0 +1,33 @@
// ======================================================================
//
// SetupSharedThread.cpp
// copyright (c) 2001 Sony Online Entertainment
//
// ======================================================================
#include "sharedThread/FirstSharedThread.h"
#include "sharedThread/SetupSharedThread.h"
#include "sharedFoundation/PerThreadData.h"
#include "sharedThread/Thread.h"
// ======================================================================
void SetupSharedThread::install()
{
// and the per-thread-data is used early on
PerThreadData::install();
// Install the thread support stuff
Thread::install();
}
// ----------------------------------------------------------------------
void SetupSharedThread::remove()
{
// and the per-thread-data is used early on
PerThreadData::remove();
}
// ======================================================================
@@ -0,0 +1,28 @@
// ======================================================================
//
// SetupSharedThread.h
// copyright (c) 2001 Sony Online Entertainment
//
// ======================================================================
#ifndef INCLUDED_SetupSharedThread_H
#define INCLUDED_SetupSharedThread_H
// ======================================================================
class SetupSharedThread
{
public:
static void install();
static void remove();
private:
SetupSharedThread();
SetupSharedThread(const SetupSharedThread &);
SetupSharedThread &operator =(const SetupSharedThread &);
};
// ======================================================================
#endif
@@ -0,0 +1,127 @@
// ======================================================================
//
// ThreadHandle.h
// Acy Stapp
//
// Copyright 6/19/2001 Sony Online Entertainment
//
// ======================================================================
#ifndef INCLUDED_ThreadHandle_h
#define INCLUDED_ThreadHandle_h
// ======================================================================
#include "sharedThread/Thread.h"
// ======================================================================
template <class T>
class TypedThreadHandle
{
public:
typedef TypedThreadHandle<Thread> ThreadHandle;
TypedThreadHandle();
~TypedThreadHandle();
explicit TypedThreadHandle(T * i);
TypedThreadHandle(const TypedThreadHandle<T> &o);
template <class O>
TypedThreadHandle<T> &operator =(const TypedThreadHandle<O> &o)
{
O * oimpl = o.getImpl();
if (impl != oimpl)
{
if (oimpl)
oimpl->ref();
if (impl)
impl->deref();
impl = oimpl;
}
return *this;
}
operator bool() const { return !(impl == 0); } //lint !e1930 conversion operator found
bool operator !() const { return impl == 0; }
void zero();
void waitZero(); // wait then zero
Thread * operator ->() { return impl; }
const Thread * operator ->() const { return impl; }
static ThreadHandle getCurrentThread();
static ThreadHandle getMainThread();
// TODO AJS - this is not needed once we get proper template friend declarations - friend template<class O> class TypedThreadHandle<O>;
T * getImpl() const { return const_cast<T *>(impl); }
protected:
T * impl;
};
typedef TypedThreadHandle<Thread> ThreadHandle;
template <class T>
TypedThreadHandle<T>::TypedThreadHandle()
: impl(0)
{
}
template <class T>
TypedThreadHandle<T>::TypedThreadHandle(T * i)
: impl(i)
{
if (impl)
{
impl->ref();
impl->start();
}
}
template <class T>
TypedThreadHandle<T>::TypedThreadHandle(const TypedThreadHandle &o)
: impl(o.impl)
{
if (impl)
impl->ref();
}
template <class T>
TypedThreadHandle<T>::~TypedThreadHandle()
{
if (impl)
impl->deref();
impl = 0;
}
template <class T>
TypedThreadHandle<Thread> TypedThreadHandle<T>::getCurrentThread()
{
return TypedThreadHandle<Thread>(Thread::getCurrentThread());
}
template <class T>
TypedThreadHandle<Thread> TypedThreadHandle<T>::getMainThread()
{
return TypedThreadHandle<Thread>(Thread::getMainThread());
}
template <class T>
void TypedThreadHandle<T>::waitZero()
{
if (impl)
{
impl->wait();
impl->deref();
}
impl = 0;
}
template <class T>
void TypedThreadHandle<T>::zero()
{
if (impl)
impl->deref();
impl = 0;
}
#endif