1 #pragma once 2 3 4 #include <utility> 5 #include <memory> 6 #include <uv.h> 7 #include "handle.hpp" 8 #include "loop.hpp" 9 10 11 namespace uvw { 12 13 14 /** 15 * @brief AsyncEvent event. 16 * 17 * It will be emitted by AsyncHandle according with its functionalities. 18 */ 19 struct AsyncEvent {}; 20 21 22 /** 23 * @brief The AsyncHandle handle. 24 * 25 * Async handles allow the user to _wakeup_ the event loop and get an event 26 * emitted from another thread. 27 * 28 * To create an `AsyncHandle` through a `Loop`, no arguments are required. 29 */ 30 class AsyncHandle final: public Handle<AsyncHandle, uv_async_t> { sendCallback(uv_async_t * handle)31 static void sendCallback(uv_async_t *handle) { 32 AsyncHandle &async = *(static_cast<AsyncHandle*>(handle->data)); 33 async.publish(AsyncEvent{}); 34 } 35 36 public: 37 using Handle::Handle; 38 39 /** 40 * @brief Initializes the handle. 41 * 42 * Unlike other handle initialization functions, it immediately starts the 43 * handle. 44 * 45 * @return True in case of success, false otherwise. 46 */ init()47 bool init() { 48 return initialize(&uv_async_init, &sendCallback); 49 } 50 51 /** 52 * @brief Wakeups the event loop and emits the AsyncEvent event. 53 * 54 * It’s safe to call this function from any thread.<br/> 55 * An AsyncEvent event will be emitted on the loop thread. 56 * 57 * See the official 58 * [documentation](http://docs.libuv.org/en/v1.x/async.html#c.uv_async_send) 59 * for further details. 60 */ send()61 void send() { 62 invoke(&uv_async_send, get()); 63 } 64 }; 65 66 67 } 68