-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandQueue.cpp
More file actions
76 lines (60 loc) · 1.68 KB
/
CommandQueue.cpp
File metadata and controls
76 lines (60 loc) · 1.68 KB
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
//
// Created by michal on 21.07.2022.
//
#include "CommandQueue.h"
std::vector<RingBuffer::Range> CommandQueue::WaitForCommands() const
{
std::unique_lock lock(mutex);
if (THREAD_DISPATCH)
cv.wait(lock, [this]() { return !commandsToExecute.empty() || quit.load(std::memory_order_relaxed); });
return std::move(commandsToExecute);
}
void CommandQueue::Flush()
{
if (buffer.empty()) {
return;
}
//ostatnia komenta musi być bez operacji, oznacza ona stop wykonywania się pętli while w funkcji Execute()
new(buffer.Allocate(sizeof(NoopCommand))) NoopCommand(nullptr);
auto range = buffer.getRange();
size_t usedSpace = buffer.size();
buffer.Circularize();
std::unique_lock lock(mutex);
commandsToExecute.push_back(range);
freeSpace -= usedSpace;
const size_t requiredSize = this->requiredSize;
cv.notify_one();
if (freeSpace < requiredSize) {
if(THREAD_DISPATCH)
cv.wait(lock, [this, requiredSize]() { return freeSpace >= requiredSize; });
}
}
bool CommandQueue::Execute()
{
auto ranges = WaitForCommands();
if (ranges.empty()) {
return false;
}
for (auto& item : ranges) {
if (item.begin) {
CommandBase* base = static_cast<CommandBase*>(item.begin);
while (base) {
base = base->Execute();
} ReleaseRange(item);
}
}
return true;
}
void CommandQueue::ReleaseRange(const RingBuffer::Range& range)
{
{
std::lock_guard guard(mutex);
freeSpace += range.size();
}
cv.notify_one();
}
void CommandQueue::Quit()
{
quit.store(true, std::memory_order_relaxed);
cv.notify_one();
}