@@ -15,8 +15,10 @@ import ll = uv_ll;
15
15
16
16
native mod rustrt {
17
17
fn rust_uv_get_kernel_global_chan_ptr ( ) -> * libc:: uintptr_t ;
18
- fn rust_uv_get_kernel_global_async_handle ( ) -> * * libc:: c_void ;
19
- fn rust_uv_set_kernel_global_async_handle ( handle : * ll:: uv_async_t ) ;
18
+ fn rust_uv_get_kernel_global_async_handle ( ) -> * libc:: uintptr_t ;
19
+ fn rust_compare_and_swap_ptr ( address : * libc:: uintptr_t ,
20
+ oldval : libc:: uintptr_t ,
21
+ newval : libc:: uintptr_t ) -> bool ;
20
22
}
21
23
22
24
#[ doc = "
@@ -75,7 +77,8 @@ fn get_global_loop() -> high_level_loop unsafe {
75
77
outer_global_loop_body ( port) ;
76
78
} ;
77
79
log ( debug, "after priv::chan_from_global_ptr" ) ;
78
- let handle = get_global_async_handle ( ) ;
80
+ let handle = get_global_async_handle_native_representation ( )
81
+ as * * ll:: uv_async_t ;
79
82
ret { async_handle : handle, op_chan : chan } ;
80
83
}
81
84
}
@@ -104,7 +107,7 @@ unsafe fn run_high_level_loop(loop_ptr: *libc::c_void,
104
107
msg_po : comm:: port < high_level_msg > ,
105
108
before_run : fn ~( * global_loop_data ) ,
106
109
before_msg_drain : fn ~( ) -> bool ,
107
- before_tear_down : fn ~( ) ) {
110
+ before_tear_down : fn ~( * global_loop_data ) ) {
108
111
// set up the special async handle we'll use to allow multi-task
109
112
// communication with this loop
110
113
let async = ll:: async_t ( ) ;
@@ -117,7 +120,7 @@ unsafe fn run_high_level_loop(loop_ptr: *libc::c_void,
117
120
async_handle: async_handle,
118
121
mut active: true ,
119
122
before_msg_drain: before_msg_drain,
120
- before_tear_down: before_tear_down,
123
+ before_tear_down: gdc_callback ( before_tear_down) ,
121
124
msg_po_ptr: ptr:: addr_of ( msg_po) ,
122
125
mut refd_handles: [ mut] ,
123
126
mut unrefd_handles: [ mut]
@@ -263,7 +266,11 @@ crust fn tear_down_close_cb(handle: *ll::uv_async_t) unsafe {
263
266
fn high_level_tear_down ( data : * global_loop_data ) unsafe {
264
267
log ( debug, "high_level_tear_down() called, close async_handle" ) ;
265
268
// call user-suppled before_tear_down cb
266
- ( * data) . before_tear_down ( ) ;
269
+ alt ( * data) . before_tear_down {
270
+ gdc_callback ( cb) {
271
+ cb ( data) ;
272
+ }
273
+ }
267
274
let async_handle = ( * data) . async_handle ;
268
275
ll:: close ( async_handle as * libc:: c_void , tear_down_close_cb) ;
269
276
}
@@ -330,19 +337,32 @@ enum high_level_msg {
330
337
tear_down
331
338
}
332
339
333
- fn get_global_async_handle( ) -> * * ll:: uv_async_t {
334
- ret rustrt:: rust_uv_get_kernel_global_async_handle ( ) as * * ll:: uv_async_t ;
340
+ unsafe fn get_global_async_handle_native_representation( )
341
+ -> * libc:: uintptr_t {
342
+ ret rustrt:: rust_uv_get_kernel_global_async_handle ( ) ;
343
+ }
344
+
345
+ unsafe fn get_global_async_handle ( ) -> * ll:: uv_async_t {
346
+ ret ( * get_global_async_handle_native_representation ( ) ) as * ll:: uv_async_t ;
335
347
}
336
348
337
- fn set_global_async_handle ( handle : * ll:: uv_async_t ) {
338
- rustrt:: rust_uv_set_kernel_global_async_handle ( handle) ;
349
+ unsafe fn set_global_async_handle ( old : * ll:: uv_async_t ,
350
+ new_ptr : * ll:: uv_async_t ) {
351
+ rustrt:: rust_compare_and_swap_ptr (
352
+ get_global_async_handle_native_representation ( ) ,
353
+ old as libc:: uintptr_t ,
354
+ new_ptr as libc:: uintptr_t ) ;
355
+ }
356
+
357
+ enum global_data_callback {
358
+ gdc_callback( fn ~( * global_loop_data ) )
339
359
}
340
360
341
361
type global_loop_data = {
342
362
async_handle : * ll:: uv_async_t ,
343
363
mut active : bool ,
344
364
before_msg_drain : fn ~( ) -> bool ,
345
- before_tear_down : fn ~ ( ) ,
365
+ before_tear_down : global_data_callback ,
346
366
msg_po_ptr : * comm:: port < high_level_msg > ,
347
367
mut refd_handles : [ mut * libc:: c_void ] ,
348
368
mut unrefd_handles : [ mut * libc:: c_void ]
@@ -399,7 +419,8 @@ unsafe fn inner_global_loop_body(weak_exit_po_in: comm::port<()>,
399
419
// before_run
400
420
{ |data|
401
421
// set the handle as the global
402
- set_global_async_handle ( ( * data) . async_handle ) ;
422
+ set_global_async_handle ( 0 u as * ll:: uv_async_t ,
423
+ ( * data) . async_handle ) ;
403
424
// when this is ran, our async_handle is set up, so let's
404
425
// do an async_send with it
405
426
ll:: async_send ( ( * data) . async_handle ) ;
@@ -422,8 +443,9 @@ unsafe fn inner_global_loop_body(weak_exit_po_in: comm::port<()>,
422
443
}
423
444
} ,
424
445
// before_tear_down
425
- { ||
426
- set_global_async_handle ( 0 as * ll:: uv_async_t ) ;
446
+ { |data|
447
+ set_global_async_handle ( ( * data) . async_handle ,
448
+ 0 as * ll:: uv_async_t ) ;
427
449
} ) ;
428
450
// supposed to return a bool to indicate to the enclosing loop whether
429
451
// it should continue or not..
0 commit comments