core::rt: Put a lock on the work queue

This commit is contained in:
Brian Anderson 2013-05-18 01:07:16 -07:00
parent f03c9bd08c
commit ee06ed2bfd

View File

@ -11,39 +11,48 @@
use container::Container;
use option::*;
use vec::OwnedVector;
use unstable::sync::{Exclusive, exclusive};
use cell::Cell;
use kinds::Owned;
pub struct WorkQueue<T> {
priv queue: ~[T]
// XXX: Another mystery bug fixed by boxing this lock
priv queue: ~Exclusive<~[T]>
}
pub impl<T> WorkQueue<T> {
pub impl<T: Owned> WorkQueue<T> {
fn new() -> WorkQueue<T> {
WorkQueue {
queue: ~[]
queue: ~exclusive(~[])
}
}
fn push(&mut self, value: T) {
self.queue.unshift(value)
let value = Cell(value);
self.queue.with(|q| q.unshift(value.take()) );
}
fn pop(&mut self) -> Option<T> {
if !self.queue.is_empty() {
Some(self.queue.shift())
} else {
None
do self.queue.with |q| {
if !q.is_empty() {
Some(q.shift())
} else {
None
}
}
}
fn steal(&mut self) -> Option<T> {
if !self.queue.is_empty() {
Some(self.queue.pop())
} else {
None
do self.queue.with |q| {
if !q.is_empty() {
Some(q.pop())
} else {
None
}
}
}
fn is_empty(&self) -> bool {
return self.queue.is_empty();
self.queue.with_imm(|q| q.is_empty() )
}
}