From f24803a32f7a2ca7fd8fb5053b6942b3ca7b1020 Mon Sep 17 00:00:00 2001 From: dragon-zhang Date: Fri, 29 Nov 2024 09:31:00 +0800 Subject: [PATCH] add Ordered trait --- core/src/common/ordered_work_steal.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/core/src/common/ordered_work_steal.rs b/core/src/common/ordered_work_steal.rs index c2bf8dab..a7493dc4 100644 --- a/core/src/common/ordered_work_steal.rs +++ b/core/src/common/ordered_work_steal.rs @@ -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)] @@ -33,6 +45,13 @@ impl Drop for OrderedWorkStealQueue { } } +impl OrderedWorkStealQueue { + /// Push an element to the global queue. + pub fn push(&self, item: T) { + self.push_with_priority(item.priority(), item); + } +} + impl OrderedWorkStealQueue { /// Create a new `WorkStealQueue` instance. #[must_use] @@ -136,6 +155,14 @@ impl Drop for OrderedLocalQueue<'_, T> { } } +impl 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,