multipool/pool/
modes.rs

1//! Task fetch modes for the thread pool.
2//!
3//! This module defines traits and structures that represent different task fetching strategies
4//! used by the thread pool. These modes determine how tasks are fetched and scheduled
5//! for execution, and whether they support task prioritization.
6
7/// A trait representing a task fetching mode.
8///
9/// Implementations of this trait define how tasks are fetched from the task queues.
10/// This trait is the base for more specific modes, such as priority and non-priority modes.
11pub trait TaskFetchMode {
12    /// Returns the name of the task fetching mode.
13    fn mode(&self) -> &'static str;
14}
15
16/// A marker trait for task fetching modes that do not support priorities.
17///
18/// Modes implementing this trait fetch tasks without considering their priorities.
19pub trait NonPriorityTaskFetchMode: TaskFetchMode {}
20
21/// A marker trait for task fetching modes that support task priorities.
22///
23/// Modes implementing this trait fetch tasks based on their priorities.
24pub trait PriorityTaskFetchMode: TaskFetchMode {}
25
26/// A task fetching mode using a global queue.
27///
28/// In this mode, all tasks are placed into a single global queue, and workers fetch
29/// tasks directly from it without considering task priorities.
30pub struct GlobalQueueMode;
31
32impl TaskFetchMode for GlobalQueueMode {
33    fn mode(&self) -> &'static str {
34        "GlobalQueue"
35    }
36}
37
38impl NonPriorityTaskFetchMode for GlobalQueueMode {}
39
40/// A task fetching mode using work stealing.
41///
42/// In this mode, each worker has its own local queue. Workers fetch tasks from their
43/// local queues, and if a worker's queue is empty, it can steal tasks from other workers.
44/// This mode does not consider task priorities.
45pub struct WorkStealingMode;
46
47impl TaskFetchMode for WorkStealingMode {
48    fn mode(&self) -> &'static str {
49        "WorkStealing"
50    }
51}
52
53impl NonPriorityTaskFetchMode for WorkStealingMode {}
54
55/// A task fetching mode using a priority global queue.
56///
57/// In this mode, all tasks are placed into a single global queue that orders tasks
58/// based on their priorities. Workers fetch the highest-priority tasks from the queue.
59pub struct PriorityGlobalQueueMode;
60
61impl TaskFetchMode for PriorityGlobalQueueMode {
62    fn mode(&self) -> &'static str {
63        "PriorityGlobalQueue"
64    }
65}
66
67impl PriorityTaskFetchMode for PriorityGlobalQueueMode {}
68
69/// A task fetching mode using priority-based work stealing.
70///
71/// In this mode, each worker has its own local priority queue. Workers fetch tasks
72/// based on priority from their local queues, and can steal tasks from other workers
73/// if their own queues are empty.
74pub struct PriorityWorkStealingMode;
75
76impl TaskFetchMode for PriorityWorkStealingMode {
77    fn mode(&self) -> &'static str {
78        "PriorityWorkStealing"
79    }
80}
81
82impl PriorityTaskFetchMode for PriorityWorkStealingMode {}