Danbias/Code/Misc/Thread/OysterThread_Impl.cpp

304 lines
7.7 KiB
C++
Raw Normal View History

2013-11-28 15:17:25 +01:00
/////////////////////////////////////////////////////////////////////
// Created by [Dennis Andersen] [2013]
/////////////////////////////////////////////////////////////////////
#include "OysterThread.h"
#include "..\Utilities.h"
#include <thread>
#include <assert.h>
#include <atomic>
2013-12-19 12:32:23 +01:00
#include <future>
2013-11-28 15:17:25 +01:00
using namespace Oyster::Thread;
using namespace Utility::DynamicMemory;
2013-11-28 15:17:25 +01:00
2013-12-19 12:32:23 +01:00
#pragma region Declerations
2013-11-28 15:17:25 +01:00
enum OYSTER_THREAD_STATE
{
2013-12-19 12:32:23 +01:00
OYSTER_THREAD_STATE_IDLE,
OYSTER_THREAD_STATE_NORMAL,
2013-11-28 15:17:25 +01:00
OYSTER_THREAD_STATE_DEAD,
};
struct ThreadData
{
2013-12-13 23:47:16 +01:00
OYSTER_THREAD_STATE state; //<! The current thread state.
OYSTER_THREAD_PRIORITY prio; //<! The thread priority
2013-12-19 12:32:23 +01:00
IThreadObject *owner; //<! The worker.
2013-11-28 15:17:25 +01:00
std::atomic<int> msec; //<! A timer in miliseconds.
2013-12-19 12:32:23 +01:00
//std::timed_mutex threadFunctionLock;
//std::mutex threadWaitFunctionLock;
};
/** A typical Oyster thread function */
typedef void (*ThreadFunction)(ThreadData* w);
struct RefData
{
bool isCreated;
ThreadData *threadData;
std::thread workerThread;
RefData()
{
threadData = 0;
isCreated = false;
}
~RefData()
{
Terminate(true);
delete threadData;
}
OYSTER_THREAD_ERROR Terminate(bool wait)
2013-12-13 23:47:16 +01:00
{
2013-12-19 12:32:23 +01:00
if(!threadData) return OYSTER_THREAD_ERROR_SUCCESS;
2013-12-13 23:47:16 +01:00
2013-12-19 12:32:23 +01:00
this->threadData->state = OYSTER_THREAD_STATE_DEAD;
if(wait)
{
if(std::this_thread::get_id() != this->workerThread.get_id())
if(this->workerThread.joinable())
this->workerThread.join();
}
else
2013-12-13 23:47:16 +01:00
{
2013-12-19 12:32:23 +01:00
if(this->workerThread.joinable())
this->workerThread.detach();
2013-12-13 23:47:16 +01:00
}
2013-12-19 12:32:23 +01:00
return OYSTER_THREAD_ERROR_SUCCESS;
}
OYSTER_THREAD_ERROR Create(ThreadFunction fnc, IThreadObject* worker, bool start, bool detach)
{
if(this->isCreated ) return OYSTER_THREAD_ERROR_ThreadAlreadyCreated;
threadData = new ThreadData();
if(start)
this->threadData->state = OYSTER_THREAD_STATE_NORMAL;
else
this->threadData->state = OYSTER_THREAD_STATE_IDLE;
threadData->owner = worker;
threadData->prio = OYSTER_THREAD_PRIORITY_3;
workerThread = std::thread(fnc, this->threadData);
if(detach)
this->workerThread.detach();
isCreated = true;
return OYSTER_THREAD_ERROR_SUCCESS;
2013-12-13 23:47:16 +01:00
}
2013-11-28 15:17:25 +01:00
};
struct OysterThread::PrivateData
{
2013-12-19 12:32:23 +01:00
SmartPointer<RefData> data;
OYSTER_THREAD_ERROR Create(ThreadFunction fnc, IThreadObject* worker, bool start, bool detach)
{
if(data) return OYSTER_THREAD_ERROR_ThreadAlreadyCreated;
data = new RefData();
return data->Create(fnc, worker, start, detach);
}
OYSTER_THREAD_ERROR Terminate(bool wait)
2013-11-28 15:17:25 +01:00
{
2013-12-19 12:32:23 +01:00
return data->Terminate(wait);
2013-11-28 15:17:25 +01:00
}
2013-12-19 12:32:23 +01:00
};
class ThreadHelp
{
public:
static void CheckPriority(ThreadData* w)
2013-11-28 15:17:25 +01:00
{
2013-12-19 12:32:23 +01:00
switch (w->prio)
{
case Oyster::Thread::OYSTER_THREAD_PRIORITY_1:
std::this_thread::sleep_for(std::chrono::milliseconds(1));
break;
case Oyster::Thread::OYSTER_THREAD_PRIORITY_2:
std::this_thread::sleep_for(std::chrono::milliseconds(50));
break;
case Oyster::Thread::OYSTER_THREAD_PRIORITY_3:
std::this_thread::sleep_for(std::chrono::milliseconds(100));
break;
}
2013-11-28 15:17:25 +01:00
}
2013-12-19 12:32:23 +01:00
static bool DoWork(ThreadData* w)
2013-12-12 12:54:08 +01:00
{
2013-12-19 12:32:23 +01:00
if(w->owner)
return w->owner->DoWork();
return true;
2013-12-12 12:54:08 +01:00
}
2013-12-19 12:32:23 +01:00
static void CheckStatus(ThreadData* w)
2013-11-28 15:17:25 +01:00
{
2013-12-19 12:32:23 +01:00
if(w->msec > 0)
std::this_thread::sleep_for(std::chrono::milliseconds(w->msec));
while (w->state == OYSTER_THREAD_STATE_IDLE)
std::this_thread::yield();
2013-11-28 15:17:25 +01:00
}
2013-12-19 12:32:23 +01:00
static void ThreadingFunction(ThreadData* w)
{
CheckStatus(w);
2013-11-28 15:17:25 +01:00
2013-12-19 12:32:23 +01:00
if(w->owner) w->owner->ThreadEntry();
2013-11-28 15:17:25 +01:00
2013-12-19 12:32:23 +01:00
while (w->state == OYSTER_THREAD_STATE_NORMAL)
{
CheckPriority(w);
if(!DoWork(w)) break;
CheckStatus(w);
}
2013-11-28 15:17:25 +01:00
2013-12-19 12:32:23 +01:00
if(w->owner) w->owner->ThreadExit();
2013-11-28 15:17:25 +01:00
2013-12-19 12:32:23 +01:00
w->state = OYSTER_THREAD_STATE_DEAD;
2013-11-28 15:17:25 +01:00
}
2013-12-19 12:32:23 +01:00
};
2013-12-13 23:47:16 +01:00
2013-12-19 12:32:23 +01:00
#pragma endregion
2013-11-28 15:17:25 +01:00
OysterThread::OysterThread()
{
this->privateData = new PrivateData();
}
OysterThread::OysterThread(const OysterThread& original)
{
this->privateData = new PrivateData(*original.privateData);
}
const OysterThread& OysterThread::operator=(const OysterThread& original)
{
2013-12-12 12:54:08 +01:00
delete this->privateData;
2013-12-19 12:32:23 +01:00
this->privateData = new PrivateData();
this->privateData->data = original.privateData->data;
2013-11-28 15:17:25 +01:00
return *this;
}
OysterThread::~OysterThread()
{
delete this->privateData;
this->privateData = 0;
}
2013-12-19 12:32:23 +01:00
OYSTER_THREAD_ERROR OysterThread::Create(IThreadObject* worker, bool start, bool detach)
2013-11-28 15:17:25 +01:00
{
2013-12-19 12:32:23 +01:00
if(!this->privateData)
this->privateData = new PrivateData();
return this->privateData->Create(ThreadHelp::ThreadingFunction, worker, start, detach);
}
OYSTER_THREAD_ERROR OysterThread::Start()
{
if(!this->privateData->data->threadData->owner)
return OYSTER_THREAD_ERROR_ThreadHasNoWorker;
2013-11-28 15:17:25 +01:00
2013-12-19 12:32:23 +01:00
if(this->privateData->data->threadData->state == OYSTER_THREAD_STATE_DEAD)
return OYSTER_THREAD_ERROR_ThreadIsDead;
2013-11-28 15:17:25 +01:00
2013-12-19 12:32:23 +01:00
this->privateData->data->threadData->state = OYSTER_THREAD_STATE_NORMAL;
2013-12-17 10:25:34 +01:00
2013-11-28 15:17:25 +01:00
return OYSTER_THREAD_ERROR_SUCCESS;
}
2013-12-19 12:32:23 +01:00
OYSTER_THREAD_ERROR OysterThread::Stop(bool wait)
2013-11-28 15:17:25 +01:00
{
2013-12-19 12:32:23 +01:00
return this->Terminate(wait);
2013-11-28 15:17:25 +01:00
}
2013-12-19 12:32:23 +01:00
OYSTER_THREAD_ERROR OysterThread::Pause()
2013-11-28 15:17:25 +01:00
{
2013-12-19 12:32:23 +01:00
this->privateData->data->threadData->state = OYSTER_THREAD_STATE_IDLE;
return OYSTER_THREAD_ERROR_SUCCESS;
2013-11-28 15:17:25 +01:00
}
2013-12-19 12:32:23 +01:00
OYSTER_THREAD_ERROR OysterThread::Pause(int msec)
2013-11-28 15:17:25 +01:00
{
2013-12-19 12:32:23 +01:00
this->privateData->data->threadData->msec = msec;
return OYSTER_THREAD_ERROR_SUCCESS;
2013-11-28 15:17:25 +01:00
}
2013-12-19 12:32:23 +01:00
OYSTER_THREAD_ERROR OysterThread::Resume()
2013-11-28 15:17:25 +01:00
{
2013-12-19 12:32:23 +01:00
if(this->privateData->data->threadData->state == OYSTER_THREAD_STATE_DEAD)
return OYSTER_THREAD_ERROR_ThreadIsDead;
2013-11-28 15:17:25 +01:00
2013-12-19 12:32:23 +01:00
this->privateData->data->threadData->state = OYSTER_THREAD_STATE_NORMAL;
return OYSTER_THREAD_ERROR_SUCCESS;
2013-11-28 15:17:25 +01:00
}
OYSTER_THREAD_ERROR OysterThread::Reset(IThreadObject* worker)
{
2013-12-19 12:32:23 +01:00
this->privateData->data->threadData->owner = worker;
2013-11-28 15:17:25 +01:00
2013-12-19 12:32:23 +01:00
this->privateData->data->threadData->msec = 0;
return OYSTER_THREAD_ERROR_SUCCESS;;
2013-11-28 15:17:25 +01:00
}
2013-12-19 12:32:23 +01:00
OYSTER_THREAD_ERROR OysterThread::Terminate(bool wait)
2013-11-28 15:17:25 +01:00
{
2013-12-19 12:32:23 +01:00
//this->privateData->data->threadData->state = OYSTER_THREAD_STATE_DEAD;
//
//if(std::this_thread::get_id() == this->privateData->data->workerThread->get_id())
// return OYSTER_THREAD_ERROR_SUCCESS;
//
//if(wait)
//{
// if(this->privateData->data->workerThread->joinable())
// this->privateData->data->workerThread->detach();
//
// this->privateData->data->threadData->threadFunctionLock.lock();
// this->privateData->data->threadData->threadFunctionLock.unlock();
//}
return this->privateData->Terminate(wait);
//return OYSTER_THREAD_ERROR_SUCCESS;
2013-11-28 15:17:25 +01:00
}
2013-12-19 12:32:23 +01:00
OYSTER_THREAD_ERROR OysterThread::Wait()
2013-11-28 15:17:25 +01:00
{
2013-12-19 12:32:23 +01:00
if(this->privateData->data->threadData->state == OYSTER_THREAD_STATE_DEAD)
return OYSTER_THREAD_ERROR_ThreadIsDead;
2013-11-28 15:17:25 +01:00
2013-12-19 12:32:23 +01:00
if( this->privateData->data->workerThread.get_id() == std::this_thread::get_id())
return OYSTER_THREAD_ERROR_ThreadCannotWaintOnItselfe;
2013-11-28 15:17:25 +01:00
2013-12-19 12:32:23 +01:00
//this->privateData->data->threadData->threadFunctionLock.lock();
//this->privateData->data->threadData->threadFunctionLock.unlock();
2013-12-13 23:47:16 +01:00
2013-12-19 12:32:23 +01:00
return OYSTER_THREAD_ERROR_SUCCESS;
2013-11-28 15:17:25 +01:00
}
2013-12-19 12:32:23 +01:00
OYSTER_THREAD_ERROR OysterThread::Wait(int msec)
2013-11-28 15:17:25 +01:00
{
2013-12-19 12:32:23 +01:00
if(this->privateData->data->workerThread.get_id() == std::this_thread::get_id())
return OYSTER_THREAD_ERROR_ThreadCannotWaintOnItselfe;
2013-12-13 23:47:16 +01:00
2013-12-19 12:32:23 +01:00
//if(this->privateData->data->threadData->threadFunctionLock.try_lock_for(std::chrono::milliseconds(msec)))
// this->privateData->data->threadData->threadFunctionLock.unlock();
return OYSTER_THREAD_ERROR_SUCCESS;
2013-11-28 15:17:25 +01:00
}
OYSTER_THREAD_ERROR OysterThread::Swap(const OysterThread* other)
{
2013-12-19 12:32:23 +01:00
this->privateData->data->workerThread.swap(other->privateData->data->workerThread);
2013-11-28 15:17:25 +01:00
return OYSTER_THREAD_ERROR_SUCCESS;
}
2013-12-19 12:32:23 +01:00
void OysterThread::SetPriority(OYSTER_THREAD_PRIORITY priority)
{
this->privateData->data->threadData->prio = priority;
}
2013-11-28 15:17:25 +01:00
bool OysterThread::IsActive()
{
2013-12-19 12:32:23 +01:00
if (this->privateData->data->threadData->state == OYSTER_THREAD_STATE_NORMAL)
2013-11-28 15:17:25 +01:00
return true;
return false;
}
bool OysterThread::IsCreated() const
{
2013-12-19 12:32:23 +01:00
return privateData->data->isCreated;
}