Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions core/src/common/ordered_work_steal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ use std::ffi::c_longlong;
use std::fmt::Debug;
use std::sync::atomic::{AtomicBool, AtomicU32, AtomicUsize, Ordering};

/// Ordered trait for user's datastructures.
pub trait Ordered {
/// The highest precedence.
const HIGHEST_PRECEDENCE: c_longlong = c_longlong::MIN;
/// The lowest precedence.
const LOWEST_PRECEDENCE: c_longlong = c_longlong::MAX;
/// The default precedence.
const DEFAULT_PRECEDENCE: c_longlong = 0;
/// Get the priority of the element.
fn priority(&self) -> c_longlong;
}

/// Work stealing global queue, shared by multiple threads.
#[repr(C)]
#[derive(Debug)]
Expand All @@ -33,6 +45,13 @@ impl<T: Debug> Drop for OrderedWorkStealQueue<T> {
}
}

impl<T: Debug + Ordered> OrderedWorkStealQueue<T> {
/// Push an element to the global queue.
pub fn push(&self, item: T) {
self.push_with_priority(item.priority(), item);
}
}

impl<T: Debug> OrderedWorkStealQueue<T> {
/// Create a new `WorkStealQueue` instance.
#[must_use]
Expand Down Expand Up @@ -136,6 +155,14 @@ impl<T: Debug> Drop for OrderedLocalQueue<'_, T> {
}
}

impl<T: Debug + Ordered> OrderedLocalQueue<'_, T> {
/// If the queue is full, first push half to global,
/// then push the item to global.
pub fn push(&self, item: T) {
self.push_with_priority(item.priority(), item);
}
}

impl<'l, T: Debug> OrderedLocalQueue<'l, T> {
fn new(
shared: &'l OrderedWorkStealQueue<T>,
Expand Down
Loading