2013-10-22 15:13:18 -07:00
|
|
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2014-06-04 00:00:59 -07:00
|
|
|
use alloc::arc::Arc;
|
core: Remove the cast module
This commit revisits the `cast` module in libcore and libstd, and scrutinizes
all functions inside of it. The result was to remove the `cast` module entirely,
folding all functionality into the `mem` module. Specifically, this is the fate
of each function in the `cast` module.
* transmute - This function was moved to `mem`, but it is now marked as
#[unstable]. This is due to planned changes to the `transmute`
function and how it can be invoked (see the #[unstable] comment).
For more information, see RFC 5 and #12898
* transmute_copy - This function was moved to `mem`, with clarification that is
is not an error to invoke it with T/U that are different
sizes, but rather that it is strongly discouraged. This
function is now #[stable]
* forget - This function was moved to `mem` and marked #[stable]
* bump_box_refcount - This function was removed due to the deprecation of
managed boxes as well as its questionable utility.
* transmute_mut - This function was previously deprecated, and removed as part
of this commit.
* transmute_mut_unsafe - This function doesn't serve much of a purpose when it
can be achieved with an `as` in safe code, so it was
removed.
* transmute_lifetime - This function was removed because it is likely a strong
indication that code is incorrect in the first place.
* transmute_mut_lifetime - This function was removed for the same reasons as
`transmute_lifetime`
* copy_lifetime - This function was moved to `mem`, but it is marked
`#[unstable]` now due to the likelihood of being removed in
the future if it is found to not be very useful.
* copy_mut_lifetime - This function was also moved to `mem`, but had the same
treatment as `copy_lifetime`.
* copy_lifetime_vec - This function was removed because it is not used today,
and its existence is not necessary with DST
(copy_lifetime will suffice).
In summary, the cast module was stripped down to these functions, and then the
functions were moved to the `mem` module.
transmute - #[unstable]
transmute_copy - #[stable]
forget - #[stable]
copy_lifetime - #[unstable]
copy_mut_lifetime - #[unstable]
[breaking-change]
2014-05-09 10:34:51 -07:00
|
|
|
use std::mem;
|
2014-06-04 00:00:59 -07:00
|
|
|
use std::rt::exclusive::Exclusive;
|
2013-11-04 12:45:05 -08:00
|
|
|
use std::rt::rtio::{Callback, RemoteCallback};
|
2013-10-22 15:13:18 -07:00
|
|
|
|
|
|
|
use uvll;
|
2013-11-04 12:45:05 -08:00
|
|
|
use super::{Loop, UvHandle};
|
2013-10-22 15:13:18 -07:00
|
|
|
|
2013-11-04 12:45:05 -08:00
|
|
|
// The entire point of async is to call into a loop from other threads so it
|
|
|
|
// does not need to home.
|
|
|
|
pub struct AsyncWatcher {
|
|
|
|
handle: *uvll::uv_async_t,
|
|
|
|
|
|
|
|
// A flag to tell the callback to exit, set from the dtor. This is
|
|
|
|
// almost never contested - only in rare races with the dtor.
|
2014-06-04 00:00:59 -07:00
|
|
|
exit_flag: Arc<Exclusive<bool>>,
|
2013-11-04 12:45:05 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Payload {
|
2014-06-14 11:03:34 -07:00
|
|
|
callback: Box<Callback + Send>,
|
2014-06-04 00:00:59 -07:00
|
|
|
exit_flag: Arc<Exclusive<bool>>,
|
2013-11-04 12:45:05 -08:00
|
|
|
}
|
2013-10-22 15:13:18 -07:00
|
|
|
|
|
|
|
impl AsyncWatcher {
|
2014-06-14 11:03:34 -07:00
|
|
|
pub fn new(loop_: &mut Loop, cb: Box<Callback + Send>) -> AsyncWatcher {
|
2013-11-04 12:45:05 -08:00
|
|
|
let handle = UvHandle::alloc(None::<AsyncWatcher>, uvll::UV_ASYNC);
|
|
|
|
assert_eq!(unsafe {
|
2013-11-05 11:29:45 -08:00
|
|
|
uvll::uv_async_init(loop_.handle, handle, async_cb)
|
2013-11-04 12:45:05 -08:00
|
|
|
}, 0);
|
2014-06-04 00:00:59 -07:00
|
|
|
let flag = Arc::new(Exclusive::new(false));
|
2014-04-25 01:08:02 -07:00
|
|
|
let payload = box Payload { callback: cb, exit_flag: flag.clone() };
|
2013-10-22 15:13:18 -07:00
|
|
|
unsafe {
|
core: Remove the cast module
This commit revisits the `cast` module in libcore and libstd, and scrutinizes
all functions inside of it. The result was to remove the `cast` module entirely,
folding all functionality into the `mem` module. Specifically, this is the fate
of each function in the `cast` module.
* transmute - This function was moved to `mem`, but it is now marked as
#[unstable]. This is due to planned changes to the `transmute`
function and how it can be invoked (see the #[unstable] comment).
For more information, see RFC 5 and #12898
* transmute_copy - This function was moved to `mem`, with clarification that is
is not an error to invoke it with T/U that are different
sizes, but rather that it is strongly discouraged. This
function is now #[stable]
* forget - This function was moved to `mem` and marked #[stable]
* bump_box_refcount - This function was removed due to the deprecation of
managed boxes as well as its questionable utility.
* transmute_mut - This function was previously deprecated, and removed as part
of this commit.
* transmute_mut_unsafe - This function doesn't serve much of a purpose when it
can be achieved with an `as` in safe code, so it was
removed.
* transmute_lifetime - This function was removed because it is likely a strong
indication that code is incorrect in the first place.
* transmute_mut_lifetime - This function was removed for the same reasons as
`transmute_lifetime`
* copy_lifetime - This function was moved to `mem`, but it is marked
`#[unstable]` now due to the likelihood of being removed in
the future if it is found to not be very useful.
* copy_mut_lifetime - This function was also moved to `mem`, but had the same
treatment as `copy_lifetime`.
* copy_lifetime_vec - This function was removed because it is not used today,
and its existence is not necessary with DST
(copy_lifetime will suffice).
In summary, the cast module was stripped down to these functions, and then the
functions were moved to the `mem` module.
transmute - #[unstable]
transmute_copy - #[stable]
forget - #[stable]
copy_lifetime - #[unstable]
copy_mut_lifetime - #[unstable]
[breaking-change]
2014-05-09 10:34:51 -07:00
|
|
|
let payload: *u8 = mem::transmute(payload);
|
2013-11-04 12:45:05 -08:00
|
|
|
uvll::set_data_for_uv_handle(handle, payload);
|
2013-10-22 15:13:18 -07:00
|
|
|
}
|
2013-11-04 12:45:05 -08:00
|
|
|
return AsyncWatcher { handle: handle, exit_flag: flag, };
|
|
|
|
}
|
|
|
|
}
|
2013-10-22 15:13:18 -07:00
|
|
|
|
2013-11-04 12:45:05 -08:00
|
|
|
impl UvHandle<uvll::uv_async_t> for AsyncWatcher {
|
|
|
|
fn uv_handle(&self) -> *uvll::uv_async_t { self.handle }
|
2013-11-04 14:03:32 -08:00
|
|
|
unsafe fn from_uv_handle<'a>(_: &'a *uvll::uv_async_t) -> &'a mut AsyncWatcher {
|
2013-11-04 12:45:05 -08:00
|
|
|
fail!("async watchers can't be built from their handles");
|
2013-10-22 15:13:18 -07:00
|
|
|
}
|
2013-11-04 12:45:05 -08:00
|
|
|
}
|
|
|
|
|
2014-04-18 19:09:31 -07:00
|
|
|
extern fn async_cb(handle: *uvll::uv_async_t) {
|
2013-11-04 12:45:05 -08:00
|
|
|
let payload: &mut Payload = unsafe {
|
core: Remove the cast module
This commit revisits the `cast` module in libcore and libstd, and scrutinizes
all functions inside of it. The result was to remove the `cast` module entirely,
folding all functionality into the `mem` module. Specifically, this is the fate
of each function in the `cast` module.
* transmute - This function was moved to `mem`, but it is now marked as
#[unstable]. This is due to planned changes to the `transmute`
function and how it can be invoked (see the #[unstable] comment).
For more information, see RFC 5 and #12898
* transmute_copy - This function was moved to `mem`, with clarification that is
is not an error to invoke it with T/U that are different
sizes, but rather that it is strongly discouraged. This
function is now #[stable]
* forget - This function was moved to `mem` and marked #[stable]
* bump_box_refcount - This function was removed due to the deprecation of
managed boxes as well as its questionable utility.
* transmute_mut - This function was previously deprecated, and removed as part
of this commit.
* transmute_mut_unsafe - This function doesn't serve much of a purpose when it
can be achieved with an `as` in safe code, so it was
removed.
* transmute_lifetime - This function was removed because it is likely a strong
indication that code is incorrect in the first place.
* transmute_mut_lifetime - This function was removed for the same reasons as
`transmute_lifetime`
* copy_lifetime - This function was moved to `mem`, but it is marked
`#[unstable]` now due to the likelihood of being removed in
the future if it is found to not be very useful.
* copy_mut_lifetime - This function was also moved to `mem`, but had the same
treatment as `copy_lifetime`.
* copy_lifetime_vec - This function was removed because it is not used today,
and its existence is not necessary with DST
(copy_lifetime will suffice).
In summary, the cast module was stripped down to these functions, and then the
functions were moved to the `mem` module.
transmute - #[unstable]
transmute_copy - #[stable]
forget - #[stable]
copy_lifetime - #[unstable]
copy_mut_lifetime - #[unstable]
[breaking-change]
2014-05-09 10:34:51 -07:00
|
|
|
mem::transmute(uvll::get_data_for_uv_handle(handle))
|
2013-11-04 12:45:05 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
// The synchronization logic here is subtle. To review,
|
|
|
|
// the uv async handle type promises that, after it is
|
|
|
|
// triggered the remote callback is definitely called at
|
|
|
|
// least once. UvRemoteCallback needs to maintain those
|
|
|
|
// semantics while also shutting down cleanly from the
|
|
|
|
// dtor. In our case that means that, when the
|
|
|
|
// UvRemoteCallback dtor calls `async.send()`, here `f` is
|
|
|
|
// always called later.
|
|
|
|
|
|
|
|
// In the dtor both the exit flag is set and the async
|
|
|
|
// callback fired under a lock. Here, before calling `f`,
|
|
|
|
// we take the lock and check the flag. Because we are
|
|
|
|
// checking the flag before calling `f`, and the flag is
|
|
|
|
// set under the same lock as the send, then if the flag
|
|
|
|
// is set then we're guaranteed to call `f` after the
|
|
|
|
// final send.
|
|
|
|
|
|
|
|
// If the check was done after `f()` then there would be a
|
|
|
|
// period between that call and the check where the dtor
|
|
|
|
// could be called in the other thread, missing the final
|
|
|
|
// callback while still destroying the handle.
|
|
|
|
|
2014-06-04 00:00:59 -07:00
|
|
|
let should_exit = unsafe { *payload.exit_flag.lock() };
|
2013-11-04 12:45:05 -08:00
|
|
|
|
|
|
|
payload.callback.call();
|
|
|
|
|
|
|
|
if should_exit {
|
2013-11-04 14:03:32 -08:00
|
|
|
unsafe { uvll::uv_close(handle, close_cb) }
|
2013-11-04 12:45:05 -08:00
|
|
|
}
|
|
|
|
}
|
2013-10-22 15:13:18 -07:00
|
|
|
|
2013-11-04 12:45:05 -08:00
|
|
|
extern fn close_cb(handle: *uvll::uv_handle_t) {
|
|
|
|
// drop the payload
|
2014-05-05 18:56:44 -07:00
|
|
|
let _payload: Box<Payload> = unsafe {
|
core: Remove the cast module
This commit revisits the `cast` module in libcore and libstd, and scrutinizes
all functions inside of it. The result was to remove the `cast` module entirely,
folding all functionality into the `mem` module. Specifically, this is the fate
of each function in the `cast` module.
* transmute - This function was moved to `mem`, but it is now marked as
#[unstable]. This is due to planned changes to the `transmute`
function and how it can be invoked (see the #[unstable] comment).
For more information, see RFC 5 and #12898
* transmute_copy - This function was moved to `mem`, with clarification that is
is not an error to invoke it with T/U that are different
sizes, but rather that it is strongly discouraged. This
function is now #[stable]
* forget - This function was moved to `mem` and marked #[stable]
* bump_box_refcount - This function was removed due to the deprecation of
managed boxes as well as its questionable utility.
* transmute_mut - This function was previously deprecated, and removed as part
of this commit.
* transmute_mut_unsafe - This function doesn't serve much of a purpose when it
can be achieved with an `as` in safe code, so it was
removed.
* transmute_lifetime - This function was removed because it is likely a strong
indication that code is incorrect in the first place.
* transmute_mut_lifetime - This function was removed for the same reasons as
`transmute_lifetime`
* copy_lifetime - This function was moved to `mem`, but it is marked
`#[unstable]` now due to the likelihood of being removed in
the future if it is found to not be very useful.
* copy_mut_lifetime - This function was also moved to `mem`, but had the same
treatment as `copy_lifetime`.
* copy_lifetime_vec - This function was removed because it is not used today,
and its existence is not necessary with DST
(copy_lifetime will suffice).
In summary, the cast module was stripped down to these functions, and then the
functions were moved to the `mem` module.
transmute - #[unstable]
transmute_copy - #[stable]
forget - #[stable]
copy_lifetime - #[unstable]
copy_mut_lifetime - #[unstable]
[breaking-change]
2014-05-09 10:34:51 -07:00
|
|
|
mem::transmute(uvll::get_data_for_uv_handle(handle))
|
2013-11-04 12:45:05 -08:00
|
|
|
};
|
|
|
|
// and then free the handle
|
|
|
|
unsafe { uvll::free_handle(handle) }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RemoteCallback for AsyncWatcher {
|
|
|
|
fn fire(&mut self) {
|
2013-11-04 14:03:32 -08:00
|
|
|
unsafe { uvll::uv_async_send(self.handle) }
|
2013-11-04 12:45:05 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for AsyncWatcher {
|
|
|
|
fn drop(&mut self) {
|
2014-06-04 00:00:59 -07:00
|
|
|
let mut should_exit = unsafe { self.exit_flag.lock() };
|
|
|
|
// NB: These two things need to happen atomically. Otherwise
|
|
|
|
// the event handler could wake up due to a *previous*
|
|
|
|
// signal and see the exit flag, destroying the handle
|
|
|
|
// before the final send.
|
|
|
|
*should_exit = true;
|
|
|
|
unsafe { uvll::uv_async_send(self.handle) }
|
2013-10-22 15:13:18 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-04 12:45:05 -08:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test_remote {
|
2014-01-07 00:57:12 -08:00
|
|
|
use std::rt::rtio::{Callback, RemoteCallback};
|
2013-11-04 12:45:05 -08:00
|
|
|
use std::rt::thread::Thread;
|
|
|
|
|
2013-12-13 11:30:59 -08:00
|
|
|
use super::AsyncWatcher;
|
2013-11-07 15:13:06 -08:00
|
|
|
use super::super::local_loop;
|
2013-11-06 11:03:11 -08:00
|
|
|
|
2013-11-07 15:13:06 -08:00
|
|
|
// Make sure that we can fire watchers in remote threads and that they
|
|
|
|
// actually trigger what they say they will.
|
2013-11-04 12:45:05 -08:00
|
|
|
#[test]
|
2013-11-07 15:13:06 -08:00
|
|
|
fn smoke_test() {
|
2014-03-09 14:58:32 -07:00
|
|
|
struct MyCallback(Option<Sender<int>>);
|
2013-11-06 11:03:11 -08:00
|
|
|
impl Callback for MyCallback {
|
|
|
|
fn call(&mut self) {
|
|
|
|
// this can get called more than once, but we only want to send
|
|
|
|
// once
|
2013-11-01 18:06:31 -07:00
|
|
|
let MyCallback(ref mut s) = *self;
|
|
|
|
if s.is_some() {
|
|
|
|
s.take_unwrap().send(1);
|
2013-11-06 11:03:11 -08:00
|
|
|
}
|
2013-11-04 12:45:05 -08:00
|
|
|
}
|
2013-11-06 11:03:11 -08:00
|
|
|
}
|
|
|
|
|
2014-03-09 14:58:32 -07:00
|
|
|
let (tx, rx) = channel();
|
2014-04-25 01:08:02 -07:00
|
|
|
let cb = box MyCallback(Some(tx));
|
2014-03-08 18:21:49 -08:00
|
|
|
let watcher = AsyncWatcher::new(&mut local_loop().loop_, cb);
|
2013-11-04 12:45:05 -08:00
|
|
|
|
2014-01-26 22:57:42 -05:00
|
|
|
let thread = Thread::start(proc() {
|
2013-12-05 17:37:02 -08:00
|
|
|
let mut watcher = watcher;
|
2013-12-03 16:44:16 -08:00
|
|
|
watcher.fire();
|
2014-01-26 22:57:42 -05:00
|
|
|
});
|
2013-11-06 11:03:11 -08:00
|
|
|
|
2014-03-09 14:58:32 -07:00
|
|
|
assert_eq!(rx.recv(), 1);
|
2013-11-07 15:13:06 -08:00
|
|
|
thread.join();
|
2013-10-22 15:13:18 -07:00
|
|
|
}
|
|
|
|
}
|