@@ -7,16 +7,19 @@ use std::ffi::c_longlong;
77use std:: fmt:: Debug ;
88use std:: sync:: atomic:: { AtomicBool , AtomicU32 , AtomicUsize , Ordering } ;
99
10+ /// The highest precedence.
11+ pub const HIGHEST_PRECEDENCE : c_longlong = c_longlong:: MIN ;
12+
13+ /// The lowest precedence.
14+ pub const LOWEST_PRECEDENCE : c_longlong = c_longlong:: MAX ;
15+
16+ /// The default precedence.
17+ pub const DEFAULT_PRECEDENCE : c_longlong = 0 ;
18+
1019/// Ordered trait for user's datastructures.
1120pub 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 ;
1821 /// Get the priority of the element.
19- fn priority ( & self ) -> c_longlong ;
22+ fn priority ( & self ) -> Option < c_longlong > ;
2023}
2124
2225/// Work stealing global queue, shared by multiple threads.
@@ -48,7 +51,7 @@ impl<T: Debug> Drop for OrderedWorkStealQueue<T> {
4851impl < T : Debug + Ordered > OrderedWorkStealQueue < T > {
4952 /// Push an element to the global queue.
5053 pub fn push ( & self , item : T ) {
51- self . push_with_priority ( item. priority ( ) , item) ;
54+ self . push_with_priority ( item. priority ( ) . unwrap_or ( DEFAULT_PRECEDENCE ) , item) ;
5255 }
5356}
5457
@@ -159,7 +162,7 @@ impl<T: Debug + Ordered> OrderedLocalQueue<'_, T> {
159162 /// If the queue is full, first push half to global,
160163 /// then push the item to global.
161164 pub fn push ( & self , item : T ) {
162- self . push_with_priority ( item. priority ( ) , item) ;
165+ self . push_with_priority ( item. priority ( ) . unwrap_or ( DEFAULT_PRECEDENCE ) , item) ;
163166 }
164167}
165168
@@ -196,10 +199,10 @@ impl<'l, T: Debug> OrderedLocalQueue<'l, T> {
196199 /// local.push_with_priority(i, i);
197200 /// }
198201 /// assert!(local.is_full());
199- /// assert_eq!(local.pop_front (), Some(0));
202+ /// assert_eq!(local.pop (), Some(0));
200203 /// assert_eq!(local.len(), 1);
201- /// assert_eq!(local.pop_front (), Some(1));
202- /// assert_eq!(local.pop_front (), None);
204+ /// assert_eq!(local.pop (), Some(1));
205+ /// assert_eq!(local.pop (), None);
203206 /// assert!(local.is_empty());
204207 /// ```
205208 pub fn is_full ( & self ) -> bool {
@@ -253,9 +256,9 @@ impl<'l, T: Debug> OrderedLocalQueue<'l, T> {
253256 /// local.push_with_priority(i, i);
254257 /// }
255258 /// for i in 0..4 {
256- /// assert_eq!(local.pop_front (), Some(i));
259+ /// assert_eq!(local.pop (), Some(i));
257260 /// }
258- /// assert_eq!(local.pop_front (), None);
261+ /// assert_eq!(local.pop (), None);
259262 /// ```
260263 pub fn push_with_priority ( & self , priority : c_longlong , item : T ) {
261264 if self . is_full ( ) {
@@ -314,9 +317,9 @@ impl<'l, T: Debug> OrderedLocalQueue<'l, T> {
314317 /// }
315318 /// let local = queue.local_queue();
316319 /// for i in 0..4 {
317- /// assert_eq!(local.pop_front (), Some(i));
320+ /// assert_eq!(local.pop (), Some(i));
318321 /// }
319- /// assert_eq!(local.pop_front (), None);
322+ /// assert_eq!(local.pop (), None);
320323 /// assert_eq!(queue.pop(), None);
321324 /// ```
322325 ///
@@ -336,23 +339,23 @@ impl<'l, T: Debug> OrderedLocalQueue<'l, T> {
336339 /// }
337340 /// assert_eq!(local1.len(), 2);
338341 /// for i in 0..2 {
339- /// assert_eq!(local1.pop_front (), Some(i));
342+ /// assert_eq!(local1.pop (), Some(i));
340343 /// }
341344 /// for i in (2..6).rev() {
342- /// assert_eq!(local1.pop_front (), Some(i));
345+ /// assert_eq!(local1.pop (), Some(i));
343346 /// }
344- /// assert_eq!(local0.pop_front (), None);
345- /// assert_eq!(local1.pop_front (), None);
347+ /// assert_eq!(local0.pop (), None);
348+ /// assert_eq!(local1.pop (), None);
346349 /// assert_eq!(queue.pop(), None);
347350 /// ```
348- pub fn pop_front ( & self ) -> Option < T > {
351+ pub fn pop ( & self ) -> Option < T > {
349352 //每从本地弹出61次,就从全局队列弹出
350353 if self . tick ( ) % 61 == 0 {
351354 if let Some ( val) = self . shared . pop ( ) {
352355 return Some ( val) ;
353356 }
354357 }
355- if let Some ( val) = self . pop ( ) {
358+ if let Some ( val) = self . pop_local ( ) {
356359 return Some ( val) ;
357360 }
358361 if self . try_lock ( ) {
@@ -398,7 +401,7 @@ impl<'l, T: Debug> OrderedLocalQueue<'l, T> {
398401 . is_ok ( )
399402 {
400403 self . release_lock ( ) ;
401- return self . pop ( ) ;
404+ return self . pop_local ( ) ;
402405 }
403406 }
404407 }
@@ -409,7 +412,7 @@ impl<'l, T: Debug> OrderedLocalQueue<'l, T> {
409412 self . shared . pop ( )
410413 }
411414
412- fn pop ( & self ) -> Option < T > {
415+ fn pop_local ( & self ) -> Option < T > {
413416 //从本地队列弹出元素
414417 for entry in self . queue {
415418 if let Some ( val) = entry. value ( ) . pop ( ) {
0 commit comments