Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F123431
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
)
{
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
Details
Attached
Mime Type
text/x-c++
Expires
Wed, Jan 15, 10:51 PM (5 h, 28 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
72077
Default Alt Text
Thread.cpp (2 KB)
Attached To
Mode
rEE Echo 3
Attached
Detach File
Event Timeline
Log In to Comment