@@ -83,24 +83,28 @@ impl<T: Send> fmt::Default for Queue<T> {
83
83
}
84
84
}
85
85
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 {
89
89
pad0 : [ 0 , ..64 ] ,
90
90
head : AtomicPtr :: new ( mut_null ( ) ) ,
91
91
pad1 : [ 0 , ..64 ] ,
92
92
stub : Default :: default ( ) ,
93
93
pad2 : [ 0 , ..64 ] ,
94
94
tail : mut_null ( ) ,
95
95
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
101
101
}
102
102
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 ) {
104
108
unsafe {
105
109
let node = cast:: transmute ( ~Node :: new ( value) ) ;
106
110
self . push_node ( node) ;
@@ -110,65 +114,63 @@ impl<T: Send> Queue<T> {
110
114
fn push_node ( & mut self , node : * mut Node < T > ) {
111
115
unsafe {
112
116
( * node) . next . store ( mut_null ( ) , Release ) ;
113
- let prev = self . get_head ( ) . swap ( node, Relaxed ) ;
117
+ let prev = self . head . swap ( node, Relaxed ) ;
114
118
( * prev) . next . store ( node, Release ) ;
115
119
}
116
120
}
117
121
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 > {
139
123
unsafe {
140
- let mut tail = self . get_tail ( ) ;
124
+ let mut tail = self . tail ;
141
125
let mut next = ( * tail) . next . load ( Acquire ) ;
142
126
let stub = self . get_stub_unsafe ( ) ;
143
127
if tail == stub {
144
128
if mut_null ( ) == next {
145
129
return None
146
130
}
147
- self . set_tail ( next) ;
131
+ self . tail = next;
148
132
tail = next;
149
133
next = ( * next) . next . load ( Acquire ) ;
150
134
}
151
135
if next != mut_null ( ) {
152
136
let tail: ~Node < T > = cast:: transmute ( tail) ;
153
- self . set_tail ( next) ;
137
+ self . tail = next;
154
138
return tail. value
155
139
}
156
- let head = self . get_head ( ) . load ( Relaxed ) ;
140
+ let head = self . head . load ( Relaxed ) ;
157
141
if tail != head {
158
142
return None
159
143
}
160
144
self . push_node ( stub) ;
161
145
next = ( * tail) . next . load ( Acquire ) ;
162
146
if next != mut_null ( ) {
163
147
let tail: ~Node < T > = cast:: transmute ( tail) ;
164
- self . set_tail ( next) ;
148
+ self . tail = next;
165
149
return tail. value
166
150
}
167
151
}
168
152
None
169
153
}
170
154
}
171
155
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
+
172
174
#[ cfg( test) ]
173
175
mod tests {
174
176
use prelude:: * ;
0 commit comments