File tree Expand file tree Collapse file tree 2 files changed +57
-2
lines changed Expand file tree Collapse file tree 2 files changed +57
-2
lines changed Original file line number Diff line number Diff line change 1
1
// Runtime support for pipes.
2
2
3
- import unsafe:: { forget, reinterpret_cast} ;
3
+ import unsafe:: { forget, reinterpret_cast, transmute} ;
4
+ import either:: { either, left, right} ;
4
5
5
6
enum state {
6
7
empty,
@@ -243,6 +244,30 @@ fn wait_many(pkts: ~[&a.packet_header]) -> uint {
243
244
ready_packet
244
245
}
245
246
247
+ fn select2 < A : send , B : send > (
248
+ +a : recv_packet < A > ,
249
+ +b : recv_packet < B > )
250
+ -> either < ( option < A > , recv_packet < B > ) , ( recv_packet < A > , option < B > ) >
251
+ {
252
+ let a = unsafe { uniquify ( a. unwrap ( ) ) } ;
253
+ let b = unsafe { uniquify ( b. unwrap ( ) ) } ;
254
+ let i = {
255
+ let headers = ~[ & a. header ,
256
+ & b. header ] ;
257
+ wait_many ( headers)
258
+ } ;
259
+
260
+ unsafe {
261
+ alt i {
262
+ 0 { left( ( recv ( recv_packet ( transmute ( a) ) ) ,
263
+ recv_packet ( transmute ( b) ) ) ) }
264
+ 1 { right ( ( recv_packet ( transmute ( a) ) ,
265
+ recv ( recv_packet ( transmute ( b) ) ) ) ) }
266
+ _ { fail "select2 return an invalid packet" }
267
+ }
268
+ }
269
+ }
270
+
246
271
#[ doc = "Waits on a set of endpoints. Returns a message, its index,
247
272
and a list of the remaining endpoints." ]
248
273
fn select < T : send > ( +endpoints : ~[ recv_packet < T > ] )
Original file line number Diff line number Diff line change @@ -72,4 +72,34 @@ fn main() {
72
72
sleep ( iotask, 1000 ) ;
73
73
74
74
signal ( c2) ;
75
- }
75
+
76
+ test_select2 ( ) ;
77
+ }
78
+
79
+ fn test_select2( ) {
80
+ let ( ac, ap) = stream:: init ( ) ;
81
+ let ( bc, bp) = stream:: init ( ) ;
82
+
83
+ stream:: client:: send ( ac, 42 ) ;
84
+
85
+ alt pipes:: select2 ( ap, bp) {
86
+ either:: left( * ) { }
87
+ either:: right ( * ) { fail }
88
+ }
89
+
90
+ stream:: client:: send ( bc, "abc" ) ;
91
+
92
+ #error ( "done with first select2" ) ;
93
+
94
+ let ( ac, ap) = stream:: init ( ) ;
95
+ let ( bc, bp) = stream:: init ( ) ;
96
+
97
+ stream:: client:: send ( bc, "abc" ) ;
98
+
99
+ alt pipes:: select2 ( ap, bp) {
100
+ either:: left( * ) { fail }
101
+ either:: right( * ) { }
102
+ }
103
+
104
+ stream:: client:: send ( ac, 42 ) ;
105
+ }
You can’t perform that action at this time.
0 commit comments