Repository navigation
Expand file tree
/
Copy pathWorkGraphYieldableExample.cpp
More file actions
251 lines (206 loc) · 9.63 KB
/
Copy pathWorkGraphYieldableExample.cpp
File metadata and controls
251 lines (206 loc) · 9.63 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#define NOMINMAX
#include <EntropyCore.h>
#include <atomic>
#include <chrono>
#include <format>
#include <thread>
using namespace EntropyEngine::Core;
using namespace EntropyEngine::Core::Logging;
using namespace EntropyEngine::Core::Concurrency;
using namespace std::chrono_literals;
int main() {
// Setup work service and contract group (like WorkContractExample)
WorkService::Config config;
config.threadCount = 4;
WorkService service(config);
service.start();
WorkContractGroup group(1000);
service.addWorkContractGroup(&group);
ENTROPY_LOG_INFO_CAT("WorkGraphExample", std::format("Group added to service: {}", group.debugString()));
// Example 1: Basic work graph with dependencies and main thread work
{
ENTROPY_LOG_INFO_CAT("WorkGraphExample", "\n=== Example 1: Basic Work Graph with Dependencies ===");
WorkGraph graph(&group);
// Create nodes
auto task1 = graph.addNode(
[]() {
ENTROPY_LOG_INFO_CAT("WorkGraphExample", "Task 1: Background work");
std::this_thread::sleep_for(100ms);
},
"task1");
auto task2 = graph.addNode(
[]() {
ENTROPY_LOG_INFO_CAT("WorkGraphExample", "Task 2: More background work");
std::this_thread::sleep_for(100ms);
},
"task2");
auto mainThreadTask = graph.addNode(
[]() {
ENTROPY_LOG_INFO_CAT("WorkGraphExample", "Main Thread Task: UI Update");
std::this_thread::sleep_for(50ms);
},
"main-thread-task", nullptr, ExecutionType::MainThread);
auto finalTask =
graph.addNode([]() { ENTROPY_LOG_INFO_CAT("WorkGraphExample", "Final Task: Cleanup"); }, "final");
// Set dependencies: task1 -> task2 -> mainThreadTask -> finalTask
graph.addDependency(task1, task2);
graph.addDependency(task2, mainThreadTask);
graph.addDependency(mainThreadTask, finalTask);
// Execute
graph.execute();
// Pump main thread work
while (!graph.isComplete()) {
group.executeMainThreadWork(10);
std::this_thread::sleep_for(10ms);
}
ENTROPY_LOG_INFO_CAT("WorkGraphExample", "Graph 1 complete");
}
// Example 2: Yieldable node that waits for atomic value
{
ENTROPY_LOG_INFO_CAT("WorkGraphExample", "\n=== Example 2: Yieldable Node Waiting for Atomic ===");
WorkGraph graph(&group);
std::atomic<bool> ready{false};
// Producer sets the atomic after 500ms
auto producer = graph.addNode(
[&ready]() {
ENTROPY_LOG_INFO_CAT("WorkGraphExample", "Producer: Working...");
std::this_thread::sleep_for(1000ms);
ready = true;
ENTROPY_LOG_INFO_CAT("WorkGraphExample", "Producer: Data ready!");
},
"producer");
// Consumer yields until atomic is true
auto consumer = graph.addYieldableNode(
[&ready]() -> WorkResultContext {
static int attempts = 0;
attempts++;
ENTROPY_LOG_INFO_CAT("WorkGraphExample", std::format("Consumer: Attempt {} - checking...", attempts));
if (!ready.load()) {
std::this_thread::sleep_for(100ms);
return WorkResultContext::yield();
}
ENTROPY_LOG_INFO_CAT("WorkGraphExample",
std::format("Consumer: Got data after {} attempts!", attempts));
return WorkResultContext::complete();
},
"consumer", nullptr, ExecutionType::AnyThread, 20); // Max 20 attempts
// Execute (no dependency - they run in parallel)
graph.execute();
graph.wait();
ENTROPY_LOG_INFO_CAT("WorkGraphExample", "Graph 2 complete");
}
// Example 3: Suspend/Resume functionality
{
ENTROPY_LOG_INFO_CAT("WorkGraphExample", "\n=== Example 3: Suspend and Resume Graph ===");
WorkGraph graph(&group);
std::atomic<int> counter{0};
// Create several nodes that increment counter
auto node1 = graph.addNode(
[&counter]() {
ENTROPY_LOG_INFO_CAT("WorkGraphExample", "Node 1: Working...");
std::this_thread::sleep_for(200ms);
counter++;
ENTROPY_LOG_INFO_CAT("WorkGraphExample", std::format("Node 1: Done (counter={})", counter.load()));
},
"node1");
auto node2 = graph.addNode(
[&counter]() {
ENTROPY_LOG_INFO_CAT("WorkGraphExample", "Node 2: Working...");
std::this_thread::sleep_for(200ms);
counter++;
ENTROPY_LOG_INFO_CAT("WorkGraphExample", std::format("Node 2: Done (counter={})", counter.load()));
},
"node2");
// Yieldable node that increments counter multiple times
auto yieldNode = graph.addYieldableNode(
[&counter]() -> WorkResultContext {
static int iterations = 0;
iterations++;
ENTROPY_LOG_INFO_CAT("WorkGraphExample", std::format("Yield Node: Iteration {}", iterations));
counter++;
std::this_thread::sleep_for(100ms);
if (iterations < 5) {
return WorkResultContext::yield();
}
ENTROPY_LOG_INFO_CAT("WorkGraphExample",
std::format("Yield Node: Complete (counter={})", counter.load()));
return WorkResultContext::complete();
},
"yield-node");
auto node3 = graph.addNode(
[&counter]() {
ENTROPY_LOG_INFO_CAT("WorkGraphExample", "Node 3: Working...");
std::this_thread::sleep_for(200ms);
counter++;
ENTROPY_LOG_INFO_CAT("WorkGraphExample", std::format("Node 3: Done (counter={})", counter.load()));
},
"node3");
// Set up dependencies: node1 -> node2 -> yieldNode -> node3
graph.addDependency(node1, node2);
graph.addDependency(node2, yieldNode);
graph.addDependency(yieldNode, node3);
// Start execution
graph.execute();
ENTROPY_LOG_INFO_CAT("WorkGraphExample", "Graph started");
// Let it run for a bit
std::this_thread::sleep_for(300ms);
// Suspend the graph
ENTROPY_LOG_INFO_CAT("WorkGraphExample", "\n>>> SUSPENDING GRAPH <<<");
graph.suspend();
ENTROPY_LOG_INFO_CAT("WorkGraphExample", std::format("Graph suspended (counter={})", counter.load()));
// Wait while suspended - nothing new should schedule
ENTROPY_LOG_INFO_CAT("WorkGraphExample", "Waiting 1 second while suspended...");
std::this_thread::sleep_for(1000ms);
ENTROPY_LOG_INFO_CAT("WorkGraphExample", std::format("Counter after suspension wait: {}", counter.load()));
// Resume the graph
ENTROPY_LOG_INFO_CAT("WorkGraphExample", "\n>>> RESUMING GRAPH <<<");
graph.resume();
ENTROPY_LOG_INFO_CAT("WorkGraphExample", "Graph resumed");
// Wait for completion
auto result = graph.wait();
ENTROPY_LOG_INFO_CAT("WorkGraphExample", std::format("Graph 3 complete (final counter={})", counter.load()));
}
// Example 4: Timed Yield - Sleep until specific time (NEW!)
{
ENTROPY_LOG_INFO_CAT("WorkGraphExample", "\n=== Example 4: Timed Yield - Zero-CPU Waiting ===");
WorkGraph graph(&group);
std::atomic<int> pollCount{0};
std::atomic<bool> dataReady{false};
// Simulated async operation that completes after 500ms
auto dataProvider = graph.addNode(
[&dataReady]() {
ENTROPY_LOG_INFO_CAT("WorkGraphExample", "Data Provider: Starting async operation...");
std::this_thread::sleep_for(500ms);
dataReady.store(true);
ENTROPY_LOG_INFO_CAT("WorkGraphExample", "Data Provider: Data is ready!");
},
"data-provider");
// Poller using timed yields - checks every 100ms without busy-waiting
auto poller = graph.addYieldableNode(
[&pollCount, &dataReady]() -> WorkResultContext {
pollCount++;
auto now = std::chrono::steady_clock::now();
ENTROPY_LOG_INFO_CAT("WorkGraphExample", std::format("Poller: Check #{} - data ready: {}",
pollCount.load(), dataReady.load()));
if (!dataReady.load()) {
// NOT READY: Yield until 100ms from now (NO CPU USAGE!)
auto wakeTime = now + 100ms;
ENTROPY_LOG_INFO_CAT("WorkGraphExample", "Poller: Sleeping for 100ms...");
return WorkResultContext::yieldUntil(wakeTime);
}
// READY: Process and complete
ENTROPY_LOG_INFO_CAT("WorkGraphExample",
std::format("Poller: Data ready after {} polls!", pollCount.load()));
return WorkResultContext::complete();
},
"poller");
// Execute (nodes run in parallel)
graph.execute();
graph.wait();
ENTROPY_LOG_INFO_CAT("WorkGraphExample",
std::format("Graph 4 complete - Poller checked {} times (expected ~5)", pollCount.load()));
ENTROPY_LOG_INFO_CAT("WorkGraphExample", "Note: Zero CPU usage while waiting - timer sleeps passively!");
}
service.stop();
return 0;
}