1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
use crate::{FromInner, HandleTrait, Inner, IntoInner};
use std::convert::TryFrom;
use uv::{uv_async_init, uv_async_send, uv_async_t};

callbacks! {
    pub AsyncCB(handle: AsyncHandle);
}

/// Additional data stored on the handle
#[derive(Default)]
pub(crate) struct AsyncDataFields<'a> {
    async_cb: AsyncCB<'a>,
}

/// Callback for uv_async_init
extern "C" fn uv_async_cb(handle: *mut uv_async_t) {
    let dataptr = crate::Handle::get_data(uv_handle!(handle));
    if !dataptr.is_null() {
        unsafe {
            if let super::AsyncData(d) = &mut (*dataptr).addl {
                d.async_cb.call(handle.into_inner());
            }
        }
    }
}

/// Async handles allow the user to “wakeup” the event loop and get a callback called from another
/// thread.
#[derive(Clone, Copy)]
pub struct AsyncHandle {
    handle: *mut uv_async_t,
}

unsafe impl Send for AsyncHandle {}
unsafe impl Sync for AsyncHandle {}

impl AsyncHandle {
    /// Create and initialize a new async handle
    pub fn new<CB: Into<AsyncCB<'static>>>(
        r#loop: &crate::Loop,
        cb: CB,
    ) -> crate::Result<AsyncHandle> {
        let layout = std::alloc::Layout::new::<uv_async_t>();
        let handle = unsafe { std::alloc::alloc(layout) as *mut uv_async_t };
        if handle.is_null() {
            return Err(crate::Error::ENOMEM);
        }

        // uv_cb is either Some(uv_async_cb) or None
        let async_cb = cb.into();
        let uv_cb = use_c_callback!(uv_async_cb, async_cb);

        let data = AsyncDataFields { async_cb };
        crate::Handle::initialize_data(uv_handle!(handle), super::AsyncData(data));

        let ret = unsafe { uv_async_init(r#loop.into_inner(), handle, uv_cb) };
        if ret < 0 {
            crate::Handle::free_data(uv_handle!(handle));
            unsafe { std::alloc::dealloc(handle as _, layout) };
            return Err(crate::Error::from_inner(ret as uv::uv_errno_t));
        }

        Ok(AsyncHandle { handle })
    }

    /// Wake up the event loop and call the async handle’s callback.
    ///
    /// Note: It’s safe to call this function from any thread. The callback will be called on the
    /// loop thread.
    ///
    /// Note: uv_async_send() is async-signal-safe. It’s safe to call this function from a signal
    /// handler.
    ///
    /// Warning: libuv will coalesce calls to send(), that is, not every call to it will yield an
    /// execution of the callback. For example: if send() is called 5 times in a row before the
    /// callback is called, the callback will only be called once. If send() is called again after
    /// the callback was called, it will be called again.
    pub fn send(&mut self) -> crate::Result<()> {
        crate::uvret(unsafe { uv_async_send(self.handle) })
    }
}

impl FromInner<*mut uv_async_t> for AsyncHandle {
    fn from_inner(handle: *mut uv_async_t) -> AsyncHandle {
        AsyncHandle { handle }
    }
}

impl Inner<*mut uv::uv_handle_t> for AsyncHandle {
    fn inner(&self) -> *mut uv::uv_handle_t {
        uv_handle!(self.handle)
    }
}

impl From<AsyncHandle> for crate::Handle {
    fn from(r#async: AsyncHandle) -> crate::Handle {
        crate::Handle::from_inner(Inner::<*mut uv::uv_handle_t>::inner(&r#async))
    }
}

impl crate::ToHandle for AsyncHandle {
    fn to_handle(&self) -> crate::Handle {
        crate::Handle::from_inner(Inner::<*mut uv::uv_handle_t>::inner(self))
    }
}

impl TryFrom<crate::Handle> for AsyncHandle {
    type Error = crate::ConversionError;

    fn try_from(handle: crate::Handle) -> Result<Self, Self::Error> {
        let t = handle.get_type();
        if t != crate::HandleType::ASYNC {
            Err(crate::ConversionError::new(t, crate::HandleType::ASYNC))
        } else {
            Ok((handle.inner() as *mut uv_async_t).into_inner())
        }
    }
}

impl HandleTrait for AsyncHandle {}

impl crate::Loop {
    /// Create and initialize a new async handle
    pub fn r#async<CB: Into<AsyncCB<'static>>>(&self, cb: CB) -> crate::Result<AsyncHandle> {
        AsyncHandle::new(self, cb)
    }
}