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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::Arc; use neon_runtime::raw::Env; use neon_runtime::tsfn::ThreadsafeFunction; use crate::context::{Context, TaskContext}; use crate::result::NeonResult; type Callback = Box<dyn FnOnce(Env) + Send + 'static>; /// Channel for scheduling Rust closures to execute on the JavaScript main thread. /// /// Cloning a `Channel` will create a new channel that shares a backing queue for /// events. /// /// # Example /// /// The following example spawns a standard Rust thread to complete a computation /// and calls back to a JavaScript function asynchronously with the result. /// /// ``` /// # use neon::prelude::*; /// # fn fibonacci(_: f64) -> f64 { todo!() } /// fn async_fibonacci(mut cx: FunctionContext) -> JsResult<JsUndefined> { /// // These types (`f64`, `Root<JsFunction>`, `Channel`) may all be sent /// // across threads. /// let n = cx.argument::<JsNumber>(0)?.value(&mut cx); /// let callback = cx.argument::<JsFunction>(1)?.root(&mut cx); /// let channel = cx.channel(); /// /// // Spawn a thread to complete the execution. This will _not_ block the /// // JavaScript event loop. /// std::thread::spawn(move || { /// let result = fibonacci(n); /// /// // Send a closure as a task to be executed by the JavaScript event /// // loop. This _will_ block the event loop while executing. /// channel.send(move |mut cx| { /// let callback = callback.into_inner(&mut cx); /// let this = cx.undefined(); /// let null = cx.null(); /// let args = vec![ /// cx.null().upcast::<JsValue>(), /// cx.number(result).upcast(), /// ]; /// /// callback.call(&mut cx, this, args)?; /// /// Ok(()) /// }); /// }); /// /// Ok(cx.undefined()) /// } /// ``` pub struct Channel { state: Arc<ChannelState>, has_ref: bool, } impl std::fmt::Debug for Channel { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_str("Channel") } } impl Channel { /// Creates an unbounded channel for scheduling closures on the JavaScript /// main thread pub fn new<'a, C: Context<'a>>(cx: &mut C) -> Self { Self { state: Arc::new(ChannelState::new(cx)), has_ref: true, } } /// Allow the Node event loop to exit while this `Channel` exists. /// _Idempotent_ pub fn unref<'a, C: Context<'a>>(&mut self, cx: &mut C) -> &mut Self { // Already unreferenced if !self.has_ref { return self; } self.has_ref = false; self.state.unref(cx); self } /// Prevent the Node event loop from exiting while this `Channel` exists. (Default) /// _Idempotent_ pub fn reference<'a, C: Context<'a>>(&mut self, cx: &mut C) -> &mut Self { // Already referenced if self.has_ref { return self; } self.has_ref = true; self.state.reference(cx); self } /// Schedules a closure to execute on the JavaScript thread that created this Channel /// Panics if there is a libuv error pub fn send<F>(&self, f: F) where F: FnOnce(TaskContext) -> NeonResult<()> + Send + 'static, { self.try_send(f).unwrap() } /// Schedules a closure to execute on the JavaScript thread that created this Channel /// Returns an `Error` if the task could not be scheduled. /// /// See [`SendError`] for additional details on failure causes. pub fn try_send<F>(&self, f: F) -> Result<(), SendError> where F: FnOnce(TaskContext) -> NeonResult<()> + Send + 'static, { let callback = Box::new(move |env| { let env = unsafe { std::mem::transmute(env) }; // Note: It is sufficient to use `TaskContext`'s `InheritedHandleScope` because // N-API creates a `HandleScope` before calling the callback. TaskContext::with_context(env, move |cx| { let _ = f(cx); }); }); self.state.tsfn.call(callback, None).map_err(|_| SendError) } /// Returns a boolean indicating if this `Channel` will prevent the Node event /// loop from exiting. pub fn has_ref(&self) -> bool { self.has_ref } } impl Clone for Channel { /// Returns a clone of the Channel instance that shares the internal /// unbounded queue with the original channel. Scheduling callbacks on the /// same queue is faster than using separate channels, but might lead to /// starvation if one of the threads posts significantly more callbacks on /// the channel than the other one. /// /// Cloned and referenced Channel instances might trigger additional /// event-loop tick when dropped. Channel can be wrapped into an Arc and /// shared between different threads/callers to avoid this. fn clone(&self) -> Self { // Not referenced, we can simply clone the fields if !self.has_ref { return Self { state: self.state.clone(), has_ref: false, }; } let state = Arc::clone(&self.state); // Only need to increase the ref count since the tsfn is already referenced state.ref_count.fetch_add(1, Ordering::Relaxed); Self { state, has_ref: true, } } } impl Drop for Channel { fn drop(&mut self) { // Not a referenced event queue if !self.has_ref { return; } // It was only us who kept the `ChannelState` alive. No need to unref // the `tsfn`, because it is going to be dropped once this function // returns. if Arc::strong_count(&self.state) == 1 { return; } // The ChannelState is dropped on a worker thread. We have to `unref` // the tsfn on the UV thread after all pending closures. Note that in // the most of scenarios the optimization in N-API layer would coalesce // `send()` with a user-supplied closure and the unref send here into a // single UV tick. // // If this ever has to be optimized a second `Arc` could be used to wrap // the `state` and it could be cloned in `try_send` and unref'ed on the // UV thread if strong reference count goes to 0. let state = Arc::clone(&self.state); self.send(move |mut cx| { state.unref(&mut cx); Ok(()) }); } } /// Error indicating that a closure was unable to be scheduled to execute on the event loop. /// /// The most likely cause of a failure is that Node is shutting down. This may occur if the /// process is forcefully exiting even if the channel is referenced. For example, by calling /// `process.exit()`. // // NOTE: These docs will need to be updated to include `QueueFull` if bounded queues are // implemented. pub struct SendError; impl std::fmt::Display for SendError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "SendError") } } impl std::fmt::Debug for SendError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { std::fmt::Display::fmt(self, f) } } impl std::error::Error for SendError {} struct ChannelState { tsfn: ThreadsafeFunction<Callback>, ref_count: AtomicUsize, } impl ChannelState { fn new<'a, C: Context<'a>>(cx: &mut C) -> Self { let tsfn = unsafe { ThreadsafeFunction::new(cx.env().to_raw(), Self::callback) }; Self { tsfn, ref_count: AtomicUsize::new(1), } } fn reference<'a, C: Context<'a>>(&self, cx: &mut C) { // We can use relaxed ordering because `reference()` can only be called // on the Event-Loop thread. if self.ref_count.fetch_add(1, Ordering::Relaxed) != 0 { return; } unsafe { self.tsfn.reference(cx.env().to_raw()); } } fn unref<'a, C: Context<'a>>(&self, cx: &mut C) { // We can use relaxed ordering because `unref()` can only be called // on the Event-Loop thread. if self.ref_count.fetch_sub(1, Ordering::Relaxed) != 1 { return; } unsafe { self.tsfn.unref(cx.env().to_raw()); } } // Monomorphized trampoline funciton for calling the user provided closure fn callback(env: Option<Env>, callback: Callback) { if let Some(env) = env { callback(env); } else { crate::context::internal::IS_RUNNING.with(|v| { *v.borrow_mut() = false; }); } } }