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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
use std::sync::{Arc, Mutex, Condvar};
use std::sync::atomic::{AtomicUsize, AtomicBool, Ordering};
use std::thread;
use std::collections::{HashMap, VecDeque};
use crate::taskflow::Taskflow;
use crate::task::{TaskWork, TaskId, TaskNode};
use crate::future::TaskflowFuture;
use crate::subflow::Subflow;
use crate::condition::BranchId;
/// Executor - Thread pool for executing taskflows
pub struct Executor {
num_workers: usize,
}
impl Executor {
/// Create a new executor with specified number of worker threads
pub fn new(num_workers: usize) -> Self {
let num_workers = if num_workers == 0 {
num_cpus::get()
} else {
num_workers
};
Self {
num_workers,
}
}
/// Count all tasks reachable from a set of starting tasks
fn count_reachable_tasks(graph: &Arc<Mutex<Vec<TaskNode>>>, start_tasks: &[TaskId]) -> usize {
let graph_guard = graph.lock().unwrap();
let mut visited = std::collections::HashSet::new();
let mut to_visit: Vec<TaskId> = start_tasks.to_vec();
while let Some(task_id) = to_visit.pop() {
if visited.insert(task_id) {
// Find this task's successors
if let Some(node) = graph_guard.iter().find(|n| n.id == task_id) {
for succ in &node.successors {
if !visited.contains(succ) {
to_visit.push(*succ);
}
}
}
}
}
visited.len()
}
/// Run a taskflow once
pub fn run(&mut self, taskflow: &Taskflow) -> TaskflowFuture {
let graph = taskflow.get_graph();
let conditional_branches = taskflow.get_conditional_branches();
let queue = Arc::new(Mutex::new(VecDeque::new()));
let condvar = Arc::new(Condvar::new());
let shutdown = Arc::new(AtomicBool::new(false));
let tasks_remaining = Arc::new(AtomicUsize::new(0));
let dep_count = Arc::new(Mutex::new(HashMap::new()));
// Build dependency count map and enqueue initial tasks
{
let graph_guard = graph.lock().unwrap();
let mut dep_map = dep_count.lock().unwrap();
let mut queue_guard = queue.lock().unwrap();
for node in graph_guard.iter() {
dep_map.insert(node.id, AtomicUsize::new(node.dependents.len()));
}
let total = graph_guard.len();
tasks_remaining.store(total, Ordering::Release);
// Find initial tasks (no dependencies)
for node in graph_guard.iter() {
if node.dependents.is_empty() {
queue_guard.push_back(node.id);
}
}
}
// Spawn worker threads
let handles: Vec<_> = (0..self.num_workers)
.map(|worker_id| {
let graph = Arc::clone(&graph);
let conditional_branches = Arc::clone(&conditional_branches);
let queue = Arc::clone(&queue);
let condvar = Arc::clone(&condvar);
let shutdown = Arc::clone(&shutdown);
let dep_count = Arc::clone(&dep_count);
let tasks_remaining = Arc::clone(&tasks_remaining);
thread::spawn(move || {
Self::worker_loop(
worker_id,
graph,
conditional_branches,
queue,
condvar,
shutdown,
dep_count,
tasks_remaining,
);
})
})
.collect();
// Wake up all workers
condvar.notify_all();
TaskflowFuture::new(handles, shutdown)
}
/// Run a taskflow N times sequentially
///
/// **Note**: Creates a new taskflow for each iteration using the provided factory function.
/// This is necessary because task closures (FnOnce) can only execute once.
///
/// # Example
/// ```
/// use taskflow_rs::{Executor, Taskflow};
/// use std::sync::{Arc, atomic::{AtomicUsize, Ordering}};
///
/// let mut executor = Executor::new(4);
/// let counter = Arc::new(AtomicUsize::new(0));
///
/// executor.run_n(5, || {
/// let mut taskflow = Taskflow::new();
/// let c = counter.clone();
/// taskflow.emplace(move || {
/// c.fetch_add(1, Ordering::Relaxed);
/// println!("Processing...");
/// });
/// taskflow
/// }).wait();
///
/// assert_eq!(counter.load(Ordering::Relaxed), 5);
/// ```
/// Run N instances of a taskflow sequentially
///
/// Creates N taskflows using the factory and runs them one after another.
///
/// # Example
/// ```
/// let mut executor = Executor::new(4);
/// executor.run_n_sequential(3, || {
/// let mut taskflow = Taskflow::new();
/// taskflow.emplace(|| println!("Task"));
/// taskflow
/// }).wait();
/// ```
pub fn run_n_sequential<F>(&mut self, n: usize, mut factory: F) -> TaskflowFuture
where
F: FnMut() -> Taskflow,
{
if n == 0 {
return self.run(&factory());
}
// Run n-1 times and wait
for _ in 0..n-1 {
let taskflow = factory();
self.run(&taskflow).wait();
}
// Return the future for the last run
let taskflow = factory();
self.run(&taskflow)
}
/// Run N instances of a taskflow concurrently
///
/// Creates N taskflows and runs them in parallel.
/// Uses thread-based parallelism to execute multiple instances simultaneously.
///
/// # Example
/// ```
/// use std::sync::{Arc, atomic::{AtomicUsize, Ordering}};
/// let mut executor = Executor::new(4);
/// let counter = Arc::new(AtomicUsize::new(0));
///
/// executor.run_n(3, || {
/// let c = counter.clone();
/// let mut taskflow = Taskflow::new();
/// taskflow.emplace(move || {
/// c.fetch_add(1, Ordering::Relaxed);
/// });
/// taskflow
/// }).wait();
/// ```
pub fn run_n<F>(&mut self, n: usize, factory: F) -> TaskflowFuture
where
F: Fn() -> Taskflow + Send + Sync + 'static,
{
if n == 0 {
return self.run(&factory());
}
if n == 1 {
return self.run(&factory());
}
let factory = Arc::new(factory);
let num_workers = self.num_workers;
let shutdown = Arc::new(std::sync::atomic::AtomicBool::new(false));
// Spawn threads to run instances in parallel
let mut handles = Vec::new();
for _ in 0..n {
let factory = factory.clone();
let handle = std::thread::spawn(move || {
// Create a new executor for this instance
let mut executor = Executor::new(num_workers);
let taskflow = factory();
executor.run(&taskflow).wait();
});
handles.push(handle);
}
// Return a TaskflowFuture that will wait for all threads
TaskflowFuture::new(handles, shutdown)
}
/// Run a taskflow repeatedly until a predicate returns true
///
/// Creates a new taskflow for each iteration using the provided factory function.
/// The predicate is checked after each execution.
///
/// # Example
/// ```
/// use taskflow_rs::{Executor, Taskflow};
/// use std::sync::{Arc, atomic::{AtomicUsize, Ordering}};
///
/// let mut executor = Executor::new(4);
/// let sum = Arc::new(AtomicUsize::new(0));
///
/// let s = sum.clone();
/// executor.run_until(
/// || {
/// let mut taskflow = Taskflow::new();
/// let s = sum.clone();
/// taskflow.emplace(move || {
/// s.fetch_add(5, Ordering::Relaxed);
/// });
/// taskflow
/// },
/// move || s.load(Ordering::Relaxed) >= 50
/// ).wait();
/// ```
pub fn run_until<F, P>(&mut self, mut factory: F, mut predicate: P) -> TaskflowFuture
where
F: FnMut() -> Taskflow,
P: FnMut() -> bool,
{
loop {
let taskflow = factory();
self.run(&taskflow).wait();
if predicate() {
break;
}
}
// Return a completed future (last iteration already waited)
let taskflow = factory();
self.run(&taskflow)
}
/// Run multiple taskflows concurrently
///
/// Executes all taskflows in parallel and returns a vector of futures.
///
/// # Example
/// ```
/// let mut executor = Executor::new(4);
/// let flow1 = create_taskflow_1();
/// let flow2 = create_taskflow_2();
/// let flow3 = create_taskflow_3();
///
/// let futures = executor.run_many(&[&flow1, &flow2, &flow3]);
///
/// // Wait for all to complete
/// for future in futures {
/// future.wait();
/// }
/// ```
pub fn run_many(&mut self, taskflows: &[&Taskflow]) -> Vec<TaskflowFuture> {
taskflows.iter()
.map(|tf| self.run(tf))
.collect()
}
/// Run multiple taskflows concurrently and wait for all to complete
///
/// This is a convenience method that runs all taskflows and waits for completion.
///
/// # Example
/// ```
/// let mut executor = Executor::new(4);
/// executor.run_many_and_wait(&[&flow1, &flow2, &flow3]);
/// ```
pub fn run_many_and_wait(&mut self, taskflows: &[&Taskflow]) {
let futures = self.run_many(taskflows);
for future in futures {
future.wait();
}
}
fn worker_loop(
_worker_id: usize,
graph: Arc<Mutex<Vec<TaskNode>>>,
conditional_branches: Arc<Mutex<HashMap<TaskId, HashMap<BranchId, Vec<TaskId>>>>>,
queue: Arc<Mutex<VecDeque<TaskId>>>,
condvar: Arc<Condvar>,
shutdown: Arc<AtomicBool>,
dep_count: Arc<Mutex<HashMap<TaskId, AtomicUsize>>>,
tasks_remaining: Arc<AtomicUsize>,
) {
loop {
// Get next task
let task_id = {
let mut queue_guard = queue.lock().unwrap();
loop {
if let Some(id) = queue_guard.pop_front() {
break Some(id);
}
// Check if we should shutdown
if shutdown.load(Ordering::Acquire) {
return;
}
// Check if all tasks are done
let remaining = tasks_remaining.load(Ordering::Acquire);
if remaining == 0 && queue_guard.is_empty() {
shutdown.store(true, Ordering::Release);
drop(queue_guard);
condvar.notify_all();
return;
}
// Wait for work
queue_guard = condvar.wait(queue_guard).unwrap();
}
};
if let Some(task_id) = task_id {
// Get task work and successors
let (work, successors) = {
let mut graph_guard = graph.lock().unwrap();
let node = graph_guard.iter_mut().find(|n| n.id == task_id).unwrap();
let work = node.work.take();
let successors = node.successors.clone();
(work, successors)
};
// Execute work outside the lock
let branch_taken = if let Some(work) = work {
match work {
TaskWork::Static(func) => {
func();
None // Not a conditional task
}
TaskWork::Subflow(func) => {
let next_id = Arc::new(Mutex::new({
let g = graph.lock().unwrap();
g.len()
}));
let initial_graph_size = {
let g = graph.lock().unwrap();
g.len()
};
let mut subflow = Subflow::new(Arc::clone(&graph), next_id);
func(&mut subflow);
let new_tasks = {
let g = graph.lock().unwrap();
g.len() - initial_graph_size
};
if new_tasks > 0 {
tasks_remaining.fetch_add(new_tasks, Ordering::Release);
let mut dep_map = dep_count.lock().unwrap();
let graph_guard = graph.lock().unwrap();
let mut queue_guard = queue.lock().unwrap();
for i in initial_graph_size..(initial_graph_size + new_tasks) {
let node = &graph_guard[i];
dep_map.insert(node.id, AtomicUsize::new(node.dependents.len()));
if node.dependents.is_empty() {
queue_guard.push_back(node.id);
}
}
drop(queue_guard);
condvar.notify_all();
}
None // Not a conditional task
}
TaskWork::Condition(func) => {
let branch_result = func();
Some(branch_result) // Return which branch was taken
}
#[cfg(feature = "async")]
TaskWork::Async(_) => {
panic!("Async tasks require AsyncExecutor. Use AsyncExecutor::new() instead of Executor::new()");
}
}
} else {
None
};
// Decrement tasks remaining
tasks_remaining.fetch_sub(1, Ordering::Release);
// Determine which successors to activate
let successors_to_activate: Vec<TaskId> = if let Some(branch) = branch_taken {
// This was a conditional task - check if branches are registered
let branches_map = conditional_branches.lock().unwrap();
if let Some(task_branches) = branches_map.get(&task_id) {
let branch_id = BranchId(branch);
// Count how many tasks are in branches that were NOT selected
// These tasks will never execute, so we need to decrement tasks_remaining for them
let mut unreachable_count = 0;
for (other_branch_id, other_successors) in task_branches.iter() {
if *other_branch_id != branch_id {
// This branch was not selected - count its tasks as unreachable
unreachable_count += Self::count_reachable_tasks(&graph, other_successors);
}
}
// Decrement tasks_remaining for unreachable tasks
if unreachable_count > 0 {
tasks_remaining.fetch_sub(unreachable_count, Ordering::Release);
}
if let Some(branch_successors) = task_branches.get(&branch_id) {
// Use the specific branch successors
branch_successors.clone()
} else {
// Branch not found, use default successors (convert from HashSet)
successors.into_iter().collect()
}
} else {
// No branches registered, use default successors (convert from HashSet)
successors.into_iter().collect()
}
} else {
// Not a conditional task, use all successors (convert from HashSet)
successors.into_iter().collect()
};
// Update successor dependencies and enqueue ready tasks
let dep_map = dep_count.lock().unwrap();
let mut ready_tasks = Vec::new();
for succ_id in successors_to_activate {
if let Some(count) = dep_map.get(&succ_id) {
let prev = count.fetch_sub(1, Ordering::AcqRel);
if prev == 1 {
ready_tasks.push(succ_id);
}
}
}
drop(dep_map);
if !ready_tasks.is_empty() {
let mut queue_guard = queue.lock().unwrap();
for task_id in ready_tasks {
queue_guard.push_back(task_id);
}
drop(queue_guard);
condvar.notify_all();
}
}
}
}
/// Block until all submitted taskflows complete
pub fn wait_for_all(&self) {
// This executor doesn't maintain state between runs
}
}
impl Drop for Executor {
fn drop(&mut self) {
// Executor doesn't maintain persistent state
}
}