@@ -94,27 +94,31 @@ fn listen<T: send, U>(f: fn(chan<T>) -> U) -> U {
94
94
f ( po. chan ( ) )
95
95
}
96
96
97
- resource port_ptr<T : send>( po: * rust_port) unsafe {
97
+ class port_ptr<T : send> {
98
+ let po: * rust_port ;
99
+ new ( po: * rust_port) { self . po = po; }
100
+ drop unsafe {
98
101
task:: unkillable { ||
99
102
// Once the port is detached it's guaranteed not to receive further
100
103
// messages
101
104
let yield = 0 u;
102
105
let yieldp = ptr:: addr_of ( yield ) ;
103
- rustrt:: rust_port_begin_detach ( po, yieldp) ;
106
+ rustrt:: rust_port_begin_detach ( self . po , yieldp) ;
104
107
if yield != 0 u {
105
108
// Need to wait for the port to be detached
106
109
// FIXME: If this fails then we're going to leave our port
107
110
// in a bogus state. (Issue #1988)
108
111
task:: yield ( ) ;
109
112
}
110
- rustrt:: rust_port_end_detach ( po) ;
113
+ rustrt:: rust_port_end_detach ( self . po ) ;
111
114
112
115
// Drain the port so that all the still-enqueued items get dropped
113
- while rustrt:: rust_port_size ( po) > 0 u as size_t {
114
- recv_ :: < T > ( po) ;
116
+ while rustrt:: rust_port_size ( self . po ) > 0 u as size_t {
117
+ recv_ :: < T > ( self . po ) ;
115
118
}
116
- rustrt:: del_port ( po) ;
119
+ rustrt:: del_port ( self . po ) ;
117
120
}
121
+ }
118
122
}
119
123
120
124
#[ doc = "
@@ -126,29 +130,34 @@ Fails if the port is detached or dead. Fails if the port
126
130
is owned by a different task.
127
131
" ]
128
132
fn as_raw_port < T : send , U > ( ch : comm:: chan < T > , f : fn ( * rust_port ) -> U ) -> U {
129
- resource portref( p: * rust_port) {
130
- if !ptr:: is_null ( p) {
131
- rustrt:: rust_port_drop ( p) ;
132
- }
133
+
134
+ class portref {
135
+ let p: * rust_port;
136
+ new ( p: * rust_port) { self . p = p; }
137
+ drop {
138
+ if !ptr:: is_null ( self . p ) {
139
+ rustrt:: rust_port_drop ( self . p ) ;
140
+ }
141
+ }
133
142
}
134
143
135
144
let p = portref ( rustrt:: rust_port_take ( * ch) ) ;
136
145
137
- if ptr:: is_null ( * p) {
146
+ if ptr:: is_null ( p . p ) {
138
147
fail "unable to locate port for channel"
139
- } else if rustrt:: get_task_id ( ) != rustrt:: rust_port_task ( * p) {
148
+ } else if rustrt:: get_task_id ( ) != rustrt:: rust_port_task ( p . p ) {
140
149
fail "unable to access unowned port"
141
150
}
142
151
143
- f ( * p)
152
+ f ( p . p )
144
153
}
145
154
146
155
#[ doc = "
147
156
Constructs a channel. The channel is bound to the port used to
148
157
construct it.
149
158
" ]
150
159
fn chan < T : send > ( p : port < T > ) -> chan < T > {
151
- chan_t ( rustrt:: get_port_id ( * * * p ) )
160
+ chan_t ( rustrt:: get_port_id ( ( * * p ) . po ) )
152
161
}
153
162
154
163
#[ doc = "
@@ -170,10 +179,10 @@ fn send<T: send>(ch: chan<T>, -data: T) {
170
179
Receive from a port. If no data is available on the port then the
171
180
task will block until data becomes available.
172
181
" ]
173
- fn recv < T : send > ( p : port < T > ) -> T { recv_ ( * * * p ) }
182
+ fn recv < T : send > ( p : port < T > ) -> T { recv_ ( ( * * p ) . po ) }
174
183
175
184
#[ doc = "Returns true if there are messages available" ]
176
- fn peek < T : send > ( p : port < T > ) -> bool { peek_ ( * * * p ) }
185
+ fn peek < T : send > ( p : port < T > ) -> bool { peek_ ( ( * * p ) . po ) }
177
186
178
187
#[ doc( hidden) ]
179
188
fn recv_chan < T : send > ( ch : comm:: chan < T > ) -> T {
@@ -196,7 +205,7 @@ fn recv_<T: send>(p: *rust_port) -> T {
196
205
// Data isn't available yet, so res has not been initialized.
197
206
task:: yield ( ) ;
198
207
} else {
199
- // In the absense of compiler-generated preemption points
208
+ // In the absence of compiler-generated preemption points
200
209
// this is a good place to yield
201
210
task:: yield ( ) ;
202
211
}
@@ -210,7 +219,7 @@ fn peek_(p: *rust_port) -> bool unsafe {
210
219
#[ doc = "Receive on one of two ports" ]
211
220
fn select2 < A : send , B : send > ( p_a : port < A > , p_b : port < B > )
212
221
-> either < A , B > unsafe {
213
- let ports = [ * * * p_a, * * * p_b] ;
222
+ let ports = [ ( * * p_a) . po , ( * * p_b) . po ] ;
214
223
let n_ports = 2 as libc:: size_t ;
215
224
let yield = 0 u, yieldp = ptr:: addr_of ( yield ) ;
216
225
@@ -233,9 +242,9 @@ fn select2<A: send, B: send>(p_a: port<A>, p_b: port<B>)
233
242
// Now we know the port we're supposed to receive from
234
243
assert resport != ptr:: null ( ) ;
235
244
236
- if resport == * * * p_a {
245
+ if resport == ( * * p_a) . po {
237
246
either:: left ( recv ( p_a) )
238
- } else if resport == * * * p_b {
247
+ } else if resport == ( * * p_b) . po {
239
248
either:: right ( recv ( p_b) )
240
249
} else {
241
250
fail "unexpected result from rust_port_select" ;
@@ -482,4 +491,4 @@ fn test_port_detach_fail() {
482
491
}
483
492
}
484
493
}
485
- }
494
+ }
0 commit comments