Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F96964
Thread.cpp
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Award Token
Size
2 KB
Referenced Files
None
Subscribers
None
Thread.cpp
View Options
#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
)
{
ECHO_LOG_INFO
(
"Thread
\"
"
<<
GetName
()
<<
"
\"
executing bound function."
);
mExecuting
=
true
;
mThreadFunction
();
mExecuting
=
false
;
ECHO_LOG_INFO
(
"Thread completed: "
<<
GetName
());
return
;
}
if
(
!
mKernel
)
{
ECHO_LOG_ERROR_LOCATION
(
"Neither a thread function or Kernel was provided for the thread: "
<<
GetName
());
return
;
}
if
(
mKernel
->
GetExecuting
())
{
ECHO_LOG_ERROR_LOCATION
(
"The provided Kernel object is already executing so this thread will exit: "
<<
GetName
());
return
;
}
if
(
!
mKernel
->
GetExecutionModel
())
{
//No execution model was found so lets set a continuous one.
mKernel
->
SetExecutionModel
(
shared_ptr
<
ThreadExecutionModel
>
(
new
ThreadExecutionModel
()));
}
mExecuting
=
true
;
ECHO_LOG_INFO
(
"Thread
\"
"
<<
GetName
()
<<
"
\"
executing a kernel."
);
Kernel
::
ExecutionResult
result
=
mKernel
->
Execute
();
mExecuting
=
false
;
switch
(
result
)
{
case
Kernel
::
ExecutionResults
::
SUCCESS
:
ECHO_LOG_INFO
(
"Thread
\"
"
<<
GetName
()
<<
"
\"
completed successfully."
);
break
;
case
Kernel
::
ExecutionResults
::
FAILURE
:
ECHO_LOG_INFO
(
"Thread
\"
"
<<
GetName
()
<<
"
\"
completed with failure."
);
break
;
case
Kernel
::
ExecutionResults
::
INCOMPLETE
:
ECHO_LOG_INFO
(
"Thread
\"
"
<<
GetName
()
<<
"
\"
completed with the Kernel status INCOMPLETE."
);
break
;
}
}
void
Thread
::
Terminate
(
bool
waitForTermination
)
{
if
(
!
mExecuting
)
{
return
;
}
ECHO_LOG_INFO
(
"Setting kill flag for thread: "
<<
GetName
());
if
(
mKernel
)
{
mKernel
->
Stop
();
}
if
(
waitForTermination
)
{
ECHO_LOG_INFO
(
"Waiting for termination of thread: "
<<
GetName
());
Join
();
ECHO_LOG_INFO
(
"Thread "
<<
GetName
()
<<
" terminated"
);
}
}
}
File Metadata
Details
Attached
Mime Type
text/x-c++
Expires
Thu, Dec 5, 2:14 AM (7 h, 30 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
63573
Default Alt Text
Thread.cpp (2 KB)
Attached To
Mode
rEE Echo 3
Attached
Detach File
Event Timeline
Log In to Comment