Page MenuHomePhorge

Thread.cpp
No OneTemporary

Size
2 KB
Referenced Files
None
Subscribers
None

Thread.cpp

#include <echo/Kernel/Thread.h>
#include <algorithm>
#include <iostream>
#include "echo/Kernel/Kernel.h"
namespace Echo
{
Thread::Thread(const std::string& name) :
mKernel(nullptr),
mName(name),
mTerminate(false),
mExecuting(false)
{
InitialiseImplementation();
}
Thread::Thread(const std::string& name, Kernel* kernel) :
mKernel(kernel),
mName(name),
mTerminate(false),
mExecuting(false)
{
InitialiseImplementation();
}
Thread::Thread(const std::string& name, ThreadFunction threadFunction) :
mKernel(nullptr),
mThreadFunction(threadFunction),
mName(name),
mTerminate(false),
mExecuting(false)
{
InitialiseImplementation();
}
Thread::~Thread()
{
CleanImplementation();
}
const std::string& Thread::GetName()
{
return mName;
}
void Thread::_Execute()
{
//Has the thread function been set?
if(mThreadFunction)
{
std::cout << "Thread \"" << GetName() << "\" about to execute a bound function." << std::endl;
mExecuting=true;
mThreadFunction();
mExecuting=false;
std::cout << "Thread completed: " << GetName() << std::endl;
return;
}
if(!mKernel)
{
std::cout << "Error: Neither a thread function or Kernel was provided for the thread: " << GetName() << std::endl;
return;
}
if(mKernel->GetExecuting())
{
std::cout << "Error: The provided Kernel object is already executing so this thread will exit: " << GetName() << std::endl;
return;
}
if(!mKernel->GetExecutionModel())
{
//No execution model was found so lets set a continuous one.
mKernel->SetExecutionModel(shared_ptr<ThreadExecutionModel>(new ThreadExecutionModel()));
}
mExecuting=true;
std::cout << "Thread \"" << GetName() << "\" about to execute a kernel." << std::endl;
Kernel::ExecutionResult result=mKernel->Execute();
mExecuting=false;
switch(result)
{
case Kernel::ExecutionResults::SUCCESS:
std::cout << "Thread \"" << GetName() << "\" completed successfully." << std::endl;
break;
case Kernel::ExecutionResults::FAILURE:
std::cout << "Thread \"" << GetName() << "\" completed with failure." << std::endl;
break;
case Kernel::ExecutionResults::INCOMPLETE:
std::cout << "Thread \"" << GetName() << "\" completed with the Kernel status INCOMPLETE." << std::endl;
break;
}
}
void Thread::Terminate(bool waitForTermination)
{
if(!mExecuting)
{
return;
}
std::cout << GetName() << ": setting kill flag" << std::endl;
if(mKernel)
{
mKernel->Stop();
}
if(waitForTermination)
{
std::cout << "Waiting for termination of " << GetName() << std::endl;
Join();
std::cout << GetName() << " Terminated" << std::endl;
}
}
}

File Metadata

Mime Type
text/x-c++
Expires
Wed, Jan 15, 10:51 PM (11 h, 12 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
72077
Default Alt Text
Thread.cpp (2 KB)

Event Timeline