2013-02-03 20:15:43 -06:00
|
|
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2013-05-12 19:34:15 -05:00
|
|
|
use container::Container;
|
2013-02-03 20:15:43 -06:00
|
|
|
use option::*;
|
2013-05-12 19:34:15 -05:00
|
|
|
use vec::OwnedVector;
|
2013-07-22 15:57:40 -05:00
|
|
|
use unstable::sync::Exclusive;
|
2013-05-18 03:07:16 -05:00
|
|
|
use cell::Cell;
|
2013-06-05 19:56:24 -05:00
|
|
|
use kinds::Send;
|
2013-05-18 03:53:40 -05:00
|
|
|
use clone::Clone;
|
2013-02-03 20:15:43 -06:00
|
|
|
|
|
|
|
pub struct WorkQueue<T> {
|
2013-05-18 03:07:16 -05:00
|
|
|
// XXX: Another mystery bug fixed by boxing this lock
|
|
|
|
priv queue: ~Exclusive<~[T]>
|
2013-02-03 20:15:43 -06:00
|
|
|
}
|
|
|
|
|
2013-06-05 19:56:24 -05:00
|
|
|
impl<T: Send> WorkQueue<T> {
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn new() -> WorkQueue<T> {
|
2013-02-03 20:15:43 -06:00
|
|
|
WorkQueue {
|
2013-07-22 15:57:40 -05:00
|
|
|
queue: ~Exclusive::new(~[])
|
2013-02-03 20:15:43 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn push(&mut self, value: T) {
|
2013-05-23 21:12:16 -05:00
|
|
|
unsafe {
|
2013-06-04 05:03:58 -05:00
|
|
|
let value = Cell::new(value);
|
2013-05-23 21:12:16 -05:00
|
|
|
self.queue.with(|q| q.unshift(value.take()) );
|
|
|
|
}
|
2013-02-03 20:15:43 -06:00
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn pop(&mut self) -> Option<T> {
|
2013-05-23 21:12:16 -05:00
|
|
|
unsafe {
|
|
|
|
do self.queue.with |q| {
|
|
|
|
if !q.is_empty() {
|
|
|
|
Some(q.shift())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2013-05-18 03:07:16 -05:00
|
|
|
}
|
2013-02-03 20:15:43 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn steal(&mut self) -> Option<T> {
|
2013-05-23 21:12:16 -05:00
|
|
|
unsafe {
|
|
|
|
do self.queue.with |q| {
|
|
|
|
if !q.is_empty() {
|
|
|
|
Some(q.pop())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2013-05-18 03:07:16 -05:00
|
|
|
}
|
2013-02-03 20:15:43 -06:00
|
|
|
}
|
|
|
|
}
|
2013-05-11 02:42:16 -05:00
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn is_empty(&self) -> bool {
|
2013-05-23 21:12:16 -05:00
|
|
|
unsafe {
|
|
|
|
self.queue.with_imm(|q| q.is_empty() )
|
|
|
|
}
|
2013-05-11 02:42:16 -05:00
|
|
|
}
|
2013-02-03 20:15:43 -06:00
|
|
|
}
|
2013-05-18 03:53:40 -05:00
|
|
|
|
|
|
|
impl<T> Clone for WorkQueue<T> {
|
|
|
|
fn clone(&self) -> WorkQueue<T> {
|
|
|
|
WorkQueue {
|
|
|
|
queue: self.queue.clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|