Skip to content

Commit bb89532

Browse files
authored
add Ordered trait (#337)
2 parents 5c81162 + f24803a commit bb89532

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

core/src/common/ordered_work_steal.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@ use std::ffi::c_longlong;
77
use std::fmt::Debug;
88
use std::sync::atomic::{AtomicBool, AtomicU32, AtomicUsize, Ordering};
99

10+
/// Ordered trait for user's datastructures.
11+
pub trait Ordered {
12+
/// The highest precedence.
13+
const HIGHEST_PRECEDENCE: c_longlong = c_longlong::MIN;
14+
/// The lowest precedence.
15+
const LOWEST_PRECEDENCE: c_longlong = c_longlong::MAX;
16+
/// The default precedence.
17+
const DEFAULT_PRECEDENCE: c_longlong = 0;
18+
/// Get the priority of the element.
19+
fn priority(&self) -> c_longlong;
20+
}
21+
1022
/// Work stealing global queue, shared by multiple threads.
1123
#[repr(C)]
1224
#[derive(Debug)]
@@ -33,6 +45,13 @@ impl<T: Debug> Drop for OrderedWorkStealQueue<T> {
3345
}
3446
}
3547

48+
impl<T: Debug + Ordered> OrderedWorkStealQueue<T> {
49+
/// Push an element to the global queue.
50+
pub fn push(&self, item: T) {
51+
self.push_with_priority(item.priority(), item);
52+
}
53+
}
54+
3655
impl<T: Debug> OrderedWorkStealQueue<T> {
3756
/// Create a new `WorkStealQueue` instance.
3857
#[must_use]
@@ -136,6 +155,14 @@ impl<T: Debug> Drop for OrderedLocalQueue<'_, T> {
136155
}
137156
}
138157

158+
impl<T: Debug + Ordered> OrderedLocalQueue<'_, T> {
159+
/// If the queue is full, first push half to global,
160+
/// then push the item to global.
161+
pub fn push(&self, item: T) {
162+
self.push_with_priority(item.priority(), item);
163+
}
164+
}
165+
139166
impl<'l, T: Debug> OrderedLocalQueue<'l, T> {
140167
fn new(
141168
shared: &'l OrderedWorkStealQueue<T>,

0 commit comments

Comments
 (0)