@@ -9,22 +9,18 @@ libuv functionality.
9
9
export high_level_loop, high_level_msg;
10
10
export run_high_level_loop, interact;
11
11
12
+ import libc:: c_void;
13
+ import ptr:: addr_of;
14
+ import comm:: { port, chan, methods} ;
12
15
import ll = uv_ll;
13
16
14
- // FIXME: Newtype syntax
15
17
#[ doc = "
16
18
Used to abstract-away direct interaction with a libuv loop.
17
19
" ]
18
- enum high_level_loop {
19
- #[ doc="
20
- `high_level_loop` variant that carries a `comm::chan` and
21
- a `*ll::uv_async_t`.
22
- " ]
23
- simple_task_loop( {
24
- async_handle : * ll:: uv_async_t ,
25
- op_chan : comm:: chan < high_level_msg >
26
- } )
27
- }
20
+ enum high_level_loop = {
21
+ async_handle: * ll:: uv_async_t,
22
+ op_chan: chan<high_level_msg>
23
+ } ;
28
24
29
25
#[ doc="
30
26
Represents the range of interactions with a `high_level_loop`
@@ -64,29 +60,28 @@ the loop's msg port
64
60
* before_tear_down - called just before the loop invokes `uv_close()` on the
65
61
provided `async_handle`. `uv_run` should return shortly after
66
62
" ]
67
- unsafe fn run_high_level_loop ( loop_ptr : * libc :: c_void ,
68
- msg_po : comm :: port < high_level_msg > ,
63
+ unsafe fn run_high_level_loop ( loop_ptr : * c_void ,
64
+ msg_po : port < high_level_msg > ,
69
65
before_run : fn ~( * ll:: uv_async_t ) ,
70
66
before_msg_process :
71
67
fn ~( * ll:: uv_async_t , bool ) -> bool ,
72
68
before_tear_down : fn ~( * ll:: uv_async_t ) ) {
73
69
// set up the special async handle we'll use to allow multi-task
74
70
// communication with this loop
75
71
let async = ll:: async_t ( ) ;
76
- let async_handle = ptr :: addr_of ( async ) ;
72
+ let async_handle = addr_of ( async ) ;
77
73
// associate the async handle with the loop
78
74
ll:: async_init ( loop_ptr, async_handle, high_level_wake_up_cb) ;
79
75
80
76
// initialize our loop data and store it in the loop
81
- let data: hl_loop_data = default_gl_data ( {
77
+ let data: hl_loop_data = {
82
78
async_handle: async_handle,
83
79
mut active: true ,
84
80
before_msg_process: before_msg_process,
85
81
before_tear_down: before_tear_down,
86
- msg_po_ptr: ptr:: addr_of ( msg_po)
87
- } ) ;
88
- let data_ptr = ptr:: addr_of ( data) ;
89
- ll:: set_data_for_uv_handle ( async_handle, data_ptr) ;
82
+ msg_po_ptr: addr_of ( msg_po)
83
+ } ;
84
+ ll:: set_data_for_uv_handle ( async_handle, addr_of ( data) ) ;
90
85
91
86
// call before_run
92
87
before_run ( async_handle) ;
@@ -120,41 +115,25 @@ module. It is not safe to send the `loop_ptr` param to this callback out
120
115
via ports/chans.
121
116
" ]
122
117
unsafe fn interact ( hl_loop : high_level_loop ,
123
- -cb : fn ~( * libc :: c_void ) ) {
118
+ -cb : fn ~( * c_void ) ) {
124
119
send_high_level_msg ( hl_loop, interaction ( cb) ) ;
125
120
}
126
121
127
122
// INTERNAL API
128
123
129
- // FIXME: Newtype syntax
130
124
// data that lives for the lifetime of the high-evel oo
131
- enum hl_loop_data {
132
- // FIXME: hl, not gl?
133
- default_gl_data( {
134
- async_handle : * ll:: uv_async_t ,
135
- mut active : bool ,
136
- before_msg_process : fn ~( * ll:: uv_async_t , bool ) -> bool ,
137
- before_tear_down : fn ~( * ll:: uv_async_t ) ,
138
- msg_po_ptr : * comm:: port < high_level_msg > } )
139
- }
125
+ type hl_loop_data = {
126
+ async_handle : * ll:: uv_async_t ,
127
+ mut active : bool ,
128
+ before_msg_process : fn ~( * ll:: uv_async_t , bool ) -> bool ,
129
+ before_tear_down : fn ~( * ll:: uv_async_t ) ,
130
+ msg_po_ptr : * port < high_level_msg >
131
+ } ;
140
132
141
- // FIXME: This function can be much simpler
142
133
unsafe fn send_high_level_msg ( hl_loop : high_level_loop ,
143
134
-msg : high_level_msg ) {
144
- let op_chan = alt hl_loop { simple_task_loop( { async_handle, op_chan} ) {
145
- op_chan} } ;
146
- comm:: send ( op_chan, msg) ;
147
-
148
- // if the global async handle == 0, then that means
149
- // the loop isn't active, so we don't need to wake it up,
150
- // (the loop's enclosing task should be blocking on a message
151
- // receive on this port)
152
- alt hl_loop {
153
- simple_task_loop( { async_handle, op_chan} ) {
154
- log ( debug, "simple async handle != 0, waking up loop.." ) ;
155
- ll:: async_send ( ( async_handle) ) ;
156
- }
157
- }
135
+ comm:: send ( hl_loop. op_chan , msg) ;
136
+ ll:: async_send ( hl_loop. async_handle ) ;
158
137
}
159
138
160
139
// this will be invoked by a call to uv::hl::interact() with
@@ -169,43 +148,27 @@ crust fn high_level_wake_up_cb(async_handle: *ll::uv_async_t,
169
148
let loop_ptr = ll:: get_loop_for_uv_handle ( async_handle) ;
170
149
let data = ll:: get_data_for_uv_handle ( async_handle) as * hl_loop_data ;
171
150
// FIXME: What is this checking?
172
- // FIXME: Use if not alt
173
- alt ( * data) . active {
174
- true {
151
+ if ( * data) . active {
175
152
let msg_po = * ( ( * data) . msg_po_ptr ) ;
176
- // FIXME: Convert to while loop
177
- alt comm:: peek ( msg_po) {
178
- true {
179
- loop {
180
- let msg = comm:: recv ( msg_po) ;
181
- alt ( * data) . active {
182
- true {
183
- alt msg {
184
- interaction( cb) {
185
- ( * data) . before_msg_process ( async_handle,
186
- ( * data) . active ) ;
187
- cb ( loop_ptr) ;
188
- }
189
- teardown_loop {
190
- begin_teardown( data) ;
191
- }
192
- }
153
+ while msg_po. peek ( ) {
154
+ let msg = msg_po. recv ( ) ;
155
+ if ( * data) . active {
156
+ alt msg {
157
+ interaction( cb) {
158
+ ( * data) . before_msg_process ( async_handle,
159
+ ( * data) . active ) ;
160
+ cb ( loop_ptr) ;
193
161
}
194
- false {
195
- // drop msg ?
162
+ teardown_loop {
163
+ begin_teardown ( data ) ;
196
164
}
197
165
}
198
- if !comm:: peek ( msg_po) { break ; }
166
+ } else {
167
+ // FIXME: drop msg ?
199
168
}
200
- }
201
- false {
202
- // no pending msgs
203
- }
204
169
}
205
- }
206
- false {
170
+ } else {
207
171
// loop not active
208
- }
209
172
}
210
173
}
211
174
@@ -222,7 +185,7 @@ fn begin_teardown(data: *hl_loop_data) unsafe {
222
185
// call user-suppled before_tear_down cb
223
186
let async_handle = ( * data) . async_handle ;
224
187
( * data) . before_tear_down ( async_handle) ;
225
- ll:: close ( async_handle as * libc :: c_void , tear_down_close_cb) ;
188
+ ll:: close ( async_handle as * c_void , tear_down_close_cb) ;
226
189
}
227
190
228
191
#[ cfg( test) ]
@@ -278,7 +241,7 @@ mod test {
278
241
async_handle) ) ;
279
242
// do an async_send with it
280
243
ll:: async_send ( async_handle) ;
281
- comm:: send ( hl_loop_ch, simple_task_loop ( {
244
+ comm:: send ( hl_loop_ch, high_level_loop ( {
282
245
async_handle: async_handle,
283
246
op_chan: msg_ch
284
247
} ) ) ;
@@ -339,12 +302,8 @@ mod test {
339
302
// anyone rolling their own high_level_loop can decide when to
340
303
// send the msg. it's assert and barf, though, if all of your
341
304
// handles aren't uv_close'd first
342
- alt hl_loop {
343
- simple_task_loop( { async_handle, op_chan} ) {
344
- comm:: send ( op_chan, teardown_loop) ;
345
- ll:: async_send ( async_handle) ;
346
- }
347
- }
305
+ comm:: send ( hl_loop. op_chan , teardown_loop) ;
306
+ ll:: async_send ( hl_loop. async_handle ) ;
348
307
comm:: recv ( exit_po) ;
349
308
log ( debug, "after recv on exit_po.. exiting.." ) ;
350
309
}
0 commit comments