-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRLinearTaskMgr.h
82 lines (65 loc) · 1.39 KB
/
RLinearTaskMgr.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#pragma once
#include <thread>
#include "RTask.h"
#include "RTaskMgr.h"
#include "RTaskStopException.h"
#include "Linq/Remove.h"
namespace RFramework
{
class RLinearTaskMgr : public RTaskMgr
{
std::vector<RTask*> tasks;
RunningState runningState;
void _Start()
{
std::unique_lock <std::mutex> lock(runningState.lock);
runningState.isRunning = true;
}
public:
void AddTask(RTask& task)
{
task.SetRunningPtr(runningState);
tasks.emplace_back(&task);
}
void RemoveTask(RTask* task)
{
tasks < -Remove([&task](RTask* _task) {return _task == task; });
}
void Run()
{
for (auto&& task : tasks)
{
if (!runningState.isRunning)
{
throw RTaskStopException();
}
auto result = task->Run();
if (result == TaskResult::FAILED)
break;
}
}
void Start()
{
_Start();
auto rLog = RFramework::RApplication::GetRLog();
auto rLocal = RFramework::RApplication::GetRLocalization();
rLog->Add(RFramework::LogLevel::Info, LogType::TaskStarted, rLocal->GetA("TASK_STARTED"));
std::chrono::milliseconds timespan(5);
while(1)
{
Run();
std::this_thread::sleep_for(timespan);
}
}
void Stop()
{
std::unique_lock <std::mutex> lock(runningState.lock);
runningState.isRunning = false;
runningState.condition.notify_all();
}
bool IsRunning()
{
return runningState.isRunning;
}
};
}