提交 5d042348 编写于 作者: B Brian Anderson

std::rt: Reduce SleeperList contention

This makes the lock much less contended. In the test I'm running the
number of times it's contended goes from ~100000 down to ~1000.
上级 5bcb6398
...@@ -469,10 +469,7 @@ pub fn enqueue_task(&mut self, task: ~Task) { ...@@ -469,10 +469,7 @@ pub fn enqueue_task(&mut self, task: ~Task) {
// We've made work available. Notify a // We've made work available. Notify a
// sleeping scheduler. // sleeping scheduler.
// XXX: perf. Check for a sleeper without match this.sleeper_list.casual_pop() {
// synchronizing memory. It's not critical
// that we always find it.
match this.sleeper_list.pop() {
Some(handle) => { Some(handle) => {
let mut handle = handle; let mut handle = handle;
handle.send(Wake) handle.send(Wake)
......
...@@ -15,33 +15,68 @@ ...@@ -15,33 +15,68 @@
use vec::OwnedVector; use vec::OwnedVector;
use option::{Option, Some, None}; use option::{Option, Some, None};
use cell::Cell; use cell::Cell;
use unstable::sync::Exclusive; use unstable::sync::{UnsafeAtomicRcBox, LittleLock};
use rt::sched::SchedHandle; use rt::sched::SchedHandle;
use clone::Clone; use clone::Clone;
pub struct SleeperList { pub struct SleeperList {
priv stack: Exclusive<~[SchedHandle]> priv state: UnsafeAtomicRcBox<State>
}
struct State {
count: uint,
stack: ~[SchedHandle],
lock: LittleLock
} }
impl SleeperList { impl SleeperList {
pub fn new() -> SleeperList { pub fn new() -> SleeperList {
SleeperList { SleeperList {
stack: Exclusive::new(~[]) state: UnsafeAtomicRcBox::new(State {
count: 0,
stack: ~[],
lock: LittleLock::new()
})
} }
} }
pub fn push(&mut self, handle: SchedHandle) { pub fn push(&mut self, handle: SchedHandle) {
let handle = Cell::new(handle); let handle = Cell::new(handle);
unsafe { unsafe {
self.stack.with(|s| s.push(handle.take())); let state = self.state.get();
do (*state).lock.lock {
(*state).count += 1;
(*state).stack.push(handle.take());
}
} }
} }
pub fn pop(&mut self) -> Option<SchedHandle> { pub fn pop(&mut self) -> Option<SchedHandle> {
unsafe { unsafe {
do self.stack.with |s| { let state = self.state.get();
if !s.is_empty() { do (*state).lock.lock {
Some(s.pop()) if !(*state).stack.is_empty() {
(*state).count -= 1;
Some((*state).stack.pop())
} else {
None
}
}
}
}
/// A pop that may sometimes miss enqueued elements, but is much faster
/// to give up without doing any synchronization
pub fn casual_pop(&mut self) -> Option<SchedHandle> {
unsafe {
let state = self.state.get();
// NB: Unsynchronized check
if (*state).count == 0 { return None; }
do (*state).lock.lock {
if !(*state).stack.is_empty() {
// NB: count is also protected by the lock
(*state).count -= 1;
Some((*state).stack.pop())
} else { } else {
None None
} }
...@@ -53,7 +88,7 @@ pub fn pop(&mut self) -> Option<SchedHandle> { ...@@ -53,7 +88,7 @@ pub fn pop(&mut self) -> Option<SchedHandle> {
impl Clone for SleeperList { impl Clone for SleeperList {
fn clone(&self) -> SleeperList { fn clone(&self) -> SleeperList {
SleeperList { SleeperList {
stack: self.stack.clone() state: self.state.clone()
} }
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册