parallel_processor/
scheduler.rs

1use std::{future::Future, sync::atomic::AtomicUsize};
2
3// use parking_lot::{Condvar, Mutex};
4
5pub struct ThreadPriorityHandle {
6    #[allow(dead_code)]
7    priority: usize,
8}
9pub struct PriorityScheduler;
10
11pub const MAX_PRIORITY: usize = 4;
12
13static MAX_THREADS_COUNT: AtomicUsize = AtomicUsize::new(usize::MAX);
14// static RUNNING_THREADS_COUNT_GUARD: Mutex<usize> = Mutex::new(0);
15// static RUNNING_THREADS_COUNT_CONDVAR: Condvar = Condvar::new();
16
17impl PriorityScheduler {
18    /// Declares a new thread that does busy work with an execution priority (lower => more priority)
19    pub fn declare_thread(priority: usize) -> ThreadPriorityHandle {
20        // Register the thread as running
21        // *RUNNING_THREADS_COUNT_GUARD.lock() += 1;
22        ThreadPriorityHandle { priority }
23    }
24
25    /// Sets the maximum allowed number of running threads
26    pub fn set_max_threads_count(threads_count: usize) {
27        MAX_THREADS_COUNT.store(threads_count, std::sync::atomic::Ordering::Relaxed);
28    }
29
30    fn decrease_running_threads_count() {
31        // let mut running_threads_count = RUNNING_THREADS_COUNT_GUARD.lock();
32        // *running_threads_count -= 1;
33
34        // if *running_threads_count < MAX_THREADS_COUNT.load(Ordering::Relaxed) {
35        //     // Wake a sleeping thread
36        //     RUNNING_THREADS_COUNT_CONDVAR.notify_one();
37        // }
38    }
39
40    fn wait_waking() {
41        // let mut running_threads_count = RUNNING_THREADS_COUNT_GUARD.lock();
42        // while *running_threads_count >= MAX_THREADS_COUNT.load(Ordering::Relaxed) {
43        //     // Wait for a thread to finish
44        //     RUNNING_THREADS_COUNT_CONDVAR.wait(&mut running_threads_count);
45        // }
46        // *running_threads_count += 1;
47    }
48
49    /// Declares that the current thread is waiting inside the lambda, not counting it in the running threads
50    pub fn execute_blocking_call<T>(
51        _handle: &ThreadPriorityHandle,
52        waiting_fn: impl FnOnce() -> T,
53    ) -> T {
54        // Decrease the running threads count
55        Self::decrease_running_threads_count();
56        let result = waiting_fn();
57        // Increase the running threads count
58        // *RUNNING_THREADS_COUNT_GUARD.lock() += 1;
59        result
60    }
61
62    /// Declares that the current thread is waiting inside the lambda, not counting it in the running threads
63    pub async fn execute_blocking_call_async<T>(
64        _handle: &ThreadPriorityHandle,
65        waiting_fn: impl Future<Output = T>,
66    ) -> T {
67        // Decrease the running threads count
68        Self::decrease_running_threads_count();
69        let result = waiting_fn.await;
70        // Wait for threads waking
71        Self::wait_waking();
72        result
73    }
74}
75
76impl Drop for ThreadPriorityHandle {
77    fn drop(&mut self) {
78        // The thread is terminating, decrease the running threads count
79        PriorityScheduler::decrease_running_threads_count();
80    }
81}