Skip to content

Commit 89c9120

Browse files
toffalettibrson
authored andcommitted
clean up
1 parent c372fa5 commit 89c9120

File tree

1 file changed

+38
-36
lines changed

1 file changed

+38
-36
lines changed

src/libstd/rt/mpsc_queue.rs

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -83,24 +83,28 @@ impl<T: Send> fmt::Default for Queue<T> {
8383
}
8484
}
8585

86-
impl<T: Send> Queue<T> {
87-
pub fn new() -> Queue<T> {
88-
let mut q = Queue{state: UnsafeArc::new(State {
86+
impl<T: Send> State<T> {
87+
pub fn new() -> State<T> {
88+
let mut state = State {
8989
pad0: [0, ..64],
9090
head: AtomicPtr::new(mut_null()),
9191
pad1: [0, ..64],
9292
stub: Default::default(),
9393
pad2: [0, ..64],
9494
tail: mut_null(),
9595
pad3: [0, ..64],
96-
})};
97-
let stub = q.get_stub_unsafe();
98-
q.get_head().store(stub, Relaxed);
99-
q.set_tail(stub);
100-
q
96+
};
97+
let stub = state.get_stub_unsafe();
98+
state.head.store(stub, Relaxed);
99+
state.tail = stub;
100+
state
101101
}
102102

103-
pub fn push(&mut self, value: T) {
103+
fn get_stub_unsafe(&mut self) -> *mut Node<T> {
104+
unsafe { to_mut_unsafe_ptr(&mut self.stub) }
105+
}
106+
107+
fn push(&mut self, value: T) {
104108
unsafe {
105109
let node = cast::transmute(~Node::new(value));
106110
self.push_node(node);
@@ -110,65 +114,63 @@ impl<T: Send> Queue<T> {
110114
fn push_node(&mut self, node: *mut Node<T>) {
111115
unsafe {
112116
(*node).next.store(mut_null(), Release);
113-
let prev = self.get_head().swap(node, Relaxed);
117+
let prev = self.head.swap(node, Relaxed);
114118
(*prev).next.store(node, Release);
115119
}
116120
}
117121

118-
fn get_stub_unsafe(&mut self) -> *mut Node<T> {
119-
unsafe { to_mut_unsafe_ptr(&mut (*self.state.get()).stub) }
120-
}
121-
122-
fn get_head(&mut self) -> &mut AtomicPtr<Node<T>> {
123-
unsafe { &mut (*self.state.get()).head }
124-
}
125-
126-
fn get_tail(&mut self) -> *mut Node<T> {
127-
unsafe { (*self.state.get()).tail }
128-
}
129-
130-
fn set_tail(&mut self, tail: *mut Node<T>) {
131-
unsafe { (*self.state.get()).tail = tail }
132-
}
133-
134-
pub fn casual_pop(&mut self) -> Option<T> {
135-
self.pop()
136-
}
137-
138-
pub fn pop(&mut self) -> Option<T> {
122+
fn pop(&mut self) -> Option<T> {
139123
unsafe {
140-
let mut tail = self.get_tail();
124+
let mut tail = self.tail;
141125
let mut next = (*tail).next.load(Acquire);
142126
let stub = self.get_stub_unsafe();
143127
if tail == stub {
144128
if mut_null() == next {
145129
return None
146130
}
147-
self.set_tail(next);
131+
self.tail = next;
148132
tail = next;
149133
next = (*next).next.load(Acquire);
150134
}
151135
if next != mut_null() {
152136
let tail: ~Node<T> = cast::transmute(tail);
153-
self.set_tail(next);
137+
self.tail = next;
154138
return tail.value
155139
}
156-
let head = self.get_head().load(Relaxed);
140+
let head = self.head.load(Relaxed);
157141
if tail != head {
158142
return None
159143
}
160144
self.push_node(stub);
161145
next = (*tail).next.load(Acquire);
162146
if next != mut_null() {
163147
let tail: ~Node<T> = cast::transmute(tail);
164-
self.set_tail(next);
148+
self.tail = next;
165149
return tail.value
166150
}
167151
}
168152
None
169153
}
170154
}
171155

156+
impl<T: Send> Queue<T> {
157+
pub fn new() -> Queue<T> {
158+
Queue{state: UnsafeArc::new(State::new())}
159+
}
160+
161+
pub fn push(&mut self, value: T) {
162+
unsafe { (*self.state.get()).push(value) }
163+
}
164+
165+
pub fn casual_pop(&mut self) -> Option<T> {
166+
unsafe { (*self.state.get()).pop() }
167+
}
168+
169+
pub fn pop(&mut self) -> Option<T> {
170+
unsafe{ (*self.state.get()).pop() }
171+
}
172+
}
173+
172174
#[cfg(test)]
173175
mod tests {
174176
use prelude::*;

0 commit comments

Comments
 (0)