Plasma Engine  2.0
Loading...
Searching...
No Matches
TaskWorkerThread.h
1#pragma once
2
3#include <Foundation/Threading/Implementation/TaskSystemDeclarations.h>
4
5#include <Foundation/Threading/Thread.h>
6#include <Foundation/Threading/ThreadSignal.h>
7
9class plTaskWorkerThread final : public plThread
10{
11 PL_DISALLOW_COPY_AND_ASSIGN(plTaskWorkerThread);
12
15
16public:
18 plTaskWorkerThread(plWorkerThreadType::Enum threadType, plUInt32 uiThreadNumber);
20
23
24private:
25 // Which types of tasks this thread should work on.
26 plWorkerThreadType::Enum m_WorkerType;
27
28 // Whether the thread is supposed to continue running.
29 volatile bool m_bActive = true;
30
31 // For display purposes.
32 plUInt16 m_uiWorkerThreadNumber = 0xFFFF;
33
35
38
39public:
41 double GetThreadUtilization(plUInt32* pNumTasksExecuted = nullptr);
42
44 void UpdateThreadUtilization(plTime timePassed);
45
46private:
47 bool m_bExecutingTask = false;
48 plUInt16 m_uiLastNumTasksExecuted = 0;
49 plUInt16 m_uiNumTasksExecuted = 0;
50 plTime m_StartedWorkingTime;
51 plTime m_ThreadActiveTime;
52 double m_fLastThreadUtilization = 0.0;
53
55
58
59public:
61 plTaskWorkerState WakeUpIfIdle();
62
63private:
64 // Puts the thread to sleep (idle state)
65 void WaitForWork();
66
67 virtual plUInt32 Run() override;
68
69 // used to wake up idle threads, see m_WorkerState
70 plThreadSignal m_WakeUpSignal;
71
72 // used to indicate whether this thread is currently idle
73 // if so, it can be woken up using m_WakeUpSignal
74 // plAtomicBool m_bIsIdle = false;
75 plAtomicInteger32 m_iWorkerState; // plTaskWorkerState
76
78};
79
82{
84 bool m_bAllowNestedTasks = true;
85 plInt32 m_iWorkerIndex = -1;
86 const char* m_szTaskName = nullptr;
87 plAtomicInteger32* m_pWorkerState = nullptr;
88};
89
90extern thread_local plTaskWorkerInfo tl_TaskWorkerInfo;
Definition TaskWorkerThread.h:10
double GetThreadUtilization(plUInt32 *pNumTasksExecuted=nullptr)
Returns the last utilization value (0 - 1 range). Optionally returns how many tasks it executed recen...
Definition TaskWorkerThread.cpp:140
plTaskWorkerState WakeUpIfIdle()
If the thread is currently idle, this will wake it up and return PL_SUCCESS.
Definition TaskWorkerThread.cpp:107
void UpdateThreadUtilization(plTime timePassed)
Computes the thread utilization by dividing the thread active time by the time that has passed since ...
Definition TaskWorkerThread.cpp:118
virtual plUInt32 Run() override
The run function can be used to implement a long running task in a thread in a platform independent w...
Definition TaskWorkerThread.cpp:41
plTaskWorkerThread(plWorkerThreadType::Enum threadType, plUInt32 uiThreadNumber)
Tells the worker thread what tasks to execute and which thread index it has.
Definition TaskWorkerThread.cpp:16
plResult DeactivateWorker()
Deactivates the thread. Returns failure, if the thread is currently still running.
Definition TaskWorkerThread.cpp:26
This class is the base class for platform independent long running threads.
Definition Thread.h:40
Waiting on a thread signal puts the waiting thread to sleep. Other threads can wake it up by raising ...
Definition ThreadSignal.h:19
Default enum for returning failure or success, instead of using a bool.
Definition Types.h:54
Definition TaskWorkerThread.h:82
The time class encapsulates a double value storing the time in seconds.
Definition Time.h:12
Enum
Definition TaskSystemDeclarations.h:89
@ Unknown
Default for all non-plTaskSystem-worker threads. Will only execute short tasks.
Definition TaskSystemDeclarations.h:90