forked from nulang-org/nulang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscheduler.rs
More file actions
97 lines (83 loc) · 2.35 KB
/
Copy pathscheduler.rs
File metadata and controls
97 lines (83 loc) · 2.35 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
//! Work-stealing scheduler with M:N threading.
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
/// Work-stealing task queue.
pub struct WorkQueue {
tasks: Vec<Box<dyn FnOnce() + Send>>,
head: AtomicUsize,
tail: AtomicUsize,
}
impl WorkQueue {
pub fn new() -> Self {
WorkQueue {
tasks: Vec::with_capacity(256),
head: AtomicUsize::new(0),
tail: AtomicUsize::new(0),
}
}
pub fn push(&mut self, task: Box<dyn FnOnce() + Send>) {
self.tasks.push(task);
self.tail.fetch_add(1, Ordering::Release);
}
pub fn pop(&mut self) -> Option<Box<dyn FnOnce() + Send>> {
let tail = self.tail.load(Ordering::Relaxed);
if tail == 0 {
return None;
}
let new_tail = tail - 1;
self.tail.store(new_tail, Ordering::Relaxed);
// In a real implementation, we'd synchronize with steal
self.tasks.pop()
}
pub fn steal(&self) -> Option<Box<dyn FnOnce() + Send>> {
let head = self.head.load(Ordering::Acquire);
let tail = self.tail.load(Ordering::Acquire);
if head >= tail {
return None;
}
// In a real implementation, use CAS
None
}
}
/// Thread pool scheduler.
pub struct Scheduler {
worker_count: usize,
shutdown: Arc<AtomicBool>,
global_queue: Vec<WorkQueue>,
}
impl Scheduler {
pub fn new(worker_count: usize) -> Self {
let mut queues = Vec::new();
for _ in 0..worker_count {
queues.push(WorkQueue::new());
}
Scheduler {
worker_count,
shutdown: Arc::new(AtomicBool::new(false)),
global_queue: queues,
}
}
pub fn start(&mut self) {
// Placeholder: would spawn worker threads
}
pub fn shutdown(&mut self) {
self.shutdown.store(true, Ordering::Release);
}
pub fn submit(&mut self, worker: usize, task: Box<dyn FnOnce() + Send>) {
if worker < self.global_queue.len() {
self.global_queue[worker].push(task);
}
}
pub fn worker_count(&self) -> usize {
self.worker_count
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_scheduler_creation() {
let scheduler = Scheduler::new(4);
assert_eq!(scheduler.worker_count(), 4);
}
}