mirror of
https://github.com/rust-lang/rust.git
synced 2024-12-15 10:07:30 +00:00
core::rt: Put a lock on the work queue
This commit is contained in:
parent
f03c9bd08c
commit
ee06ed2bfd
@ -11,39 +11,48 @@
|
|||||||
use container::Container;
|
use container::Container;
|
||||||
use option::*;
|
use option::*;
|
||||||
use vec::OwnedVector;
|
use vec::OwnedVector;
|
||||||
|
use unstable::sync::{Exclusive, exclusive};
|
||||||
|
use cell::Cell;
|
||||||
|
use kinds::Owned;
|
||||||
|
|
||||||
pub struct WorkQueue<T> {
|
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> {
|
fn new() -> WorkQueue<T> {
|
||||||
WorkQueue {
|
WorkQueue {
|
||||||
queue: ~[]
|
queue: ~exclusive(~[])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn push(&mut self, value: T) {
|
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> {
|
fn pop(&mut self) -> Option<T> {
|
||||||
if !self.queue.is_empty() {
|
do self.queue.with |q| {
|
||||||
Some(self.queue.shift())
|
if !q.is_empty() {
|
||||||
} else {
|
Some(q.shift())
|
||||||
None
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn steal(&mut self) -> Option<T> {
|
fn steal(&mut self) -> Option<T> {
|
||||||
if !self.queue.is_empty() {
|
do self.queue.with |q| {
|
||||||
Some(self.queue.pop())
|
if !q.is_empty() {
|
||||||
} else {
|
Some(q.pop())
|
||||||
None
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_empty(&self) -> bool {
|
fn is_empty(&self) -> bool {
|
||||||
return self.queue.is_empty();
|
self.queue.with_imm(|q| q.is_empty() )
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user