ntex-util 4.5.0

Utilities for ntex framework
Documentation
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
//! Task wake-up and scheduling utilities.
use std::{cell::Cell, fmt, task::Waker};

/// A single-threaded synchronization primitive for task wake-up.
///
/// Sometimes the task interested in a given event will change over time.
/// A `LocalWaker` can coordinate notifications with the consumer
/// potentially "updating" the underlying task to wake up. This is useful in
/// scenarios where a computation completes in another task and wants to
/// notify the consumer, but the consumer is in the process of being migrated to
/// a new logical task.
///
/// Consumers should call `register` before checking the result of a computation
/// and producers should call `wake` after producing the computation (this
/// differs from the usual `thread::park` pattern). It is also permitted for
/// `wake` to be called **before** `register`. This results in a no-op.
///
/// A single `LocalWaker` may be reused for any number of calls to `register` or
/// `wake`.
///
/// The slot and its owned local handle must remain on their acquiring thread.
/// Use [`take`](Self::take) to transfer a registered standard waker instead.
///
/// ```compile_fail
/// fn assert_send<T: Send>() {}
/// assert_send::<ntex_util::task::LocalWaker>();
/// ```
///
/// ```compile_fail
/// fn assert_sync<T: Sync>() {}
/// assert_sync::<ntex_util::task::LocalWaker>();
/// ```
#[derive(Default)]
pub struct LocalWaker {
    waker: Cell<Option<ntex_rt::LocalWaker>>,
}

#[cfg(target_pointer_width = "64")]
const _: () = assert!(size_of::<LocalWaker>() == 16);

impl LocalWaker {
    /// Creates an empty `LocalWaker`.
    pub fn new() -> Self {
        LocalWaker::with(None)
    }

    /// Creates a `LocalWaker` with an optional initial waker.
    pub fn with(waker: Option<Waker>) -> Self {
        LocalWaker {
            waker: Cell::new(waker.map(ntex_rt::LocalWaker::from)),
        }
    }

    #[inline]
    /// Registers the waker to be notified on calls to `wake`.
    ///
    /// Returns `true` if a waker was already registered.
    ///
    /// If cloning the replacement panics, the previous registration is retained
    /// unless the clone callback itself changes the slot.
    pub fn register(&self, waker: &Waker) -> bool {
        let prev = self.waker.take();
        let registered = prev.is_some();
        let same = prev.as_ref().is_some_and(|prev| prev.will_wake(waker));
        self.waker.set(prev);
        if !same {
            // Clone and drop callbacks may reenter the slot. Keep the old
            // registration until cloning succeeds, then publish before dropping.
            let next = ntex_rt::LocalWaker::from_waker(waker);
            drop(self.waker.replace(Some(next)));
        }
        registered
    }

    #[inline]
    /// Calls `wake` on the last `Waker` passed to `register`.
    ///
    /// The waker is consumed, so a later `wake` does nothing until `register`
    /// is called again. If no waker is registered, this does nothing.
    pub fn wake(&self) {
        if let Some(waker) = self.take_local() {
            waker.wake();
        }
    }

    #[inline]
    /// Calls `wake` on the last `Waker` passed to `register`.
    ///
    /// Returns `false` if no waker is registered. Like [`wake`](Self::wake),
    /// this consumes the registered waker.
    pub fn wake_checked(&self) -> bool {
        if let Some(waker) = self.take_local() {
            waker.wake();
            true
        } else {
            false
        }
    }

    /// Returns the last `Waker` passed to `register`, so that the user can wake it.
    ///
    /// This empties the slot and converts the owned handle into a thread-safe
    /// standard waker without cloning. If no waker is registered, returns `None`.
    /// Use [`take_local`](Self::take_local) to retain native local operations.
    pub fn take(&self) -> Option<Waker> {
        self.take_local().map(ntex_rt::LocalWaker::into_waker)
    }

    /// Takes the registered wake handle, leaving the slot empty.
    ///
    /// The returned handle retains native local operations and cannot be sent
    /// to another thread. If no waker is registered, returns `None`.
    #[inline]
    pub fn take_local(&self) -> Option<ntex_rt::LocalWaker> {
        self.waker.take()
    }

    #[doc(hidden)]
    /// Returns `true` if a waker is registered.
    pub fn is_set(&self) -> bool {
        let waker = self.waker.take();
        let set = waker.is_some();
        self.waker.set(waker);
        set
    }
}

/// Cloning creates an empty `LocalWaker`; the registered waker is not copied.
impl Clone for LocalWaker {
    fn clone(&self) -> Self {
        LocalWaker::new()
    }
}

impl fmt::Debug for LocalWaker {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "LocalWaker")
    }
}

/// Yields execution back to the current runtime.
pub async fn yield_to() {
    use std::{future::Future, pin::Pin, task::Context, task::Poll};

    struct Yield {
        completed: bool,
    }

    impl Future for Yield {
        type Output = ();

        fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
            if self.completed {
                return Poll::Ready(());
            }

            self.completed = true;
            cx.waker().wake_by_ref();

            Poll::Pending
        }
    }

    Yield { completed: false }.await;
}

#[cfg(test)]
mod test {
    use super::*;

    #[ntex::test]
    async fn yield_test() {
        yield_to().await;
    }

    #[ntex::test]
    async fn local_slot_wakes_current_task() {
        crate::spawn(async {
            let slot = LocalWaker::new();
            let mut polls = 0;
            std::future::poll_fn(|cx| {
                polls += 1;
                if polls == 4 {
                    return std::task::Poll::Ready(());
                }
                assert!(!slot.register(cx.waker()));
                match polls {
                    1 => slot.wake(),
                    2 => slot.take_local().unwrap().wake(),
                    _ => slot.take().unwrap().wake(),
                }
                std::task::Poll::Pending
            })
            .await;
            assert!(!slot.is_set());
        })
        .await
        .unwrap();
    }
}

#[cfg(test)]
mod tests {
    use std::panic::{AssertUnwindSafe, catch_unwind};
    use std::rc::Rc;
    use std::sync::Arc;
    use std::sync::atomic::{AtomicUsize, Ordering};
    use std::task::{RawWaker, RawWakerVTable, Wake};
    use std::thread::LocalKey;

    use super::*;

    static CLONES: AtomicUsize = AtomicUsize::new(0);

    static VTABLE: RawWakerVTable = RawWakerVTable::new(
        |p| {
            CLONES.fetch_add(1, Ordering::Relaxed);
            RawWaker::new(p, &VTABLE)
        },
        |_| {},
        |_| {},
        |_| {},
    );

    type Hook = Cell<Option<Box<dyn FnOnce()>>>;

    // Waker callbacks may run on any thread; local test closures stay in TLS.
    thread_local! {
        static ON_CLONE: Hook = Cell::new(None);
        static ON_DROP: Hook = Cell::new(None);
        static ON_WAKE: Hook = Cell::new(None);
    }

    fn run_hook(key: &'static LocalKey<Hook>) {
        if let Some(hook) = key.with(Cell::take) {
            hook();
        }
    }

    fn hook_waker() -> Waker {
        static HOOK_VTABLE: RawWakerVTable = RawWakerVTable::new(
            |p| {
                run_hook(&ON_CLONE);
                RawWaker::new(p, &HOOK_VTABLE)
            },
            |_| run_hook(&ON_WAKE),
            |_| run_hook(&ON_WAKE),
            |_| run_hook(&ON_DROP),
        );
        unsafe { Waker::from_raw(RawWaker::new(std::ptr::null(), &HOOK_VTABLE)) }
    }

    #[derive(Default)]
    struct WakeCounter(AtomicUsize);

    impl Wake for WakeCounter {
        fn wake(self: Arc<Self>) {
            self.0.fetch_add(1, Ordering::Relaxed);
        }
    }

    fn counted_waker() -> (Arc<WakeCounter>, Waker) {
        let count = Arc::new(WakeCounter::default());
        let waker = Waker::from(count.clone());
        (count, waker)
    }

    #[test]
    fn test_register_same_waker() {
        static A: u8 = 0;
        static B: u8 = 0;
        let a = unsafe { Waker::from_raw(RawWaker::new((&raw const A).cast(), &VTABLE)) };
        let b = unsafe { Waker::from_raw(RawWaker::new((&raw const B).cast(), &VTABLE)) };

        let w = LocalWaker::new();
        assert!(!w.register(&a));
        assert!(w.register(&a));
        assert!(w.register(&a));
        assert_eq!(CLONES.load(Ordering::Relaxed), 1);

        assert!(w.register(&b));
        assert_eq!(CLONES.load(Ordering::Relaxed), 2);
        assert!(w.take().unwrap().will_wake(&b));
    }

    #[test]
    fn local_waker_clone_is_empty() {
        let waker = LocalWaker::new();
        waker.register(std::task::Waker::noop());
        assert!(waker.is_set());
        let cloned = waker.clone();
        assert!(!cloned.is_set());
        assert!(!cloned.wake_checked());
        assert!(waker.is_set());
        assert_eq!(format!("{waker:?}"), "LocalWaker");
    }

    #[test]
    fn owning_conversions_and_wakes_do_not_clone() {
        ON_CLONE.set(Some(Box::new(|| panic!("unexpected clone"))));
        let calls = Rc::new(Cell::new(0));
        for path in 0..4 {
            let calls = calls.clone();
            ON_WAKE.set(Some(Box::new(move || calls.set(calls.get() + 1))));
            let slot = LocalWaker::with(Some(hook_waker()));
            match path {
                0 => slot.take().unwrap().wake(),
                1 => slot.take_local().unwrap().wake(),
                2 => slot.wake(),
                _ => assert!(slot.wake_checked()),
            }
            assert!(slot.take().is_none());
            assert!(slot.take_local().is_none());
            assert!(!slot.wake_checked());
        }
        assert_eq!(calls.get(), 4);
        assert!(ON_CLONE.with(Cell::take).is_some());
    }

    #[test]
    fn clone_panic_preserves_registration() {
        let (calls, original) = counted_waker();
        let slot = LocalWaker::with(Some(original));
        let replacement = hook_waker();
        ON_CLONE.set(Some(Box::new(|| panic!("clone"))));
        assert!(catch_unwind(AssertUnwindSafe(|| slot.register(&replacement))).is_err());
        assert!(slot.wake_checked());
        assert_eq!(calls.0.load(Ordering::Relaxed), 1);
    }

    #[test]
    fn clone_reentry_preserves_nested_changes_on_unwind() {
        for panic in [false, true] {
            let (previous_calls, previous) = counted_waker();
            let (nested_calls, nested) = counted_waker();
            let slot = Rc::new(LocalWaker::with(Some(previous)));
            let callback_slot = slot.clone();
            ON_CLONE.set(Some(Box::new(move || {
                assert!(callback_slot.wake_checked());
                assert!(!callback_slot.register(&nested));
                assert!(!panic, "clone");
            })));
            let replacement = hook_waker();
            let result = catch_unwind(AssertUnwindSafe(|| slot.register(&replacement)));
            if panic {
                assert!(result.is_err());
            } else {
                assert!(result.unwrap());
            }
            assert_eq!(previous_calls.0.load(Ordering::Relaxed), 1);
            assert!(slot.wake_checked());
            assert_eq!(nested_calls.0.load(Ordering::Relaxed), usize::from(panic));
        }
    }

    #[test]
    fn replaced_drop_reentry_and_unwind() {
        for panic in [false, true] {
            let (replacement_calls, replacement) = counted_waker();
            let (nested_calls, nested) = counted_waker();
            let slot = Rc::new(LocalWaker::with(Some(hook_waker())));
            let callback_slot = slot.clone();
            ON_DROP.set(Some(Box::new(move || {
                assert!(callback_slot.wake_checked());
                assert!(!callback_slot.register(&nested));
                assert!(!panic, "drop");
            })));
            let result = catch_unwind(AssertUnwindSafe(|| slot.register(&replacement)));
            if panic {
                assert!(result.is_err());
            } else {
                assert!(result.unwrap());
            }
            assert_eq!(replacement_calls.0.load(Ordering::Relaxed), 1);
            assert!(slot.wake_checked());
            assert_eq!(nested_calls.0.load(Ordering::Relaxed), 1);
        }
    }

    #[test]
    fn wake_reentry_and_unwind() {
        for panic in [false, true] {
            for checked in [false, true] {
                let (calls, nested) = counted_waker();
                let slot = Rc::new(LocalWaker::with(Some(hook_waker())));
                let callback_slot = slot.clone();
                ON_WAKE.set(Some(Box::new(move || {
                    assert!(!callback_slot.is_set());
                    assert!(!callback_slot.register(&nested));
                    assert!(!panic, "wake");
                })));
                let result = catch_unwind(AssertUnwindSafe(|| {
                    if checked {
                        assert!(slot.wake_checked());
                    } else {
                        slot.wake();
                    }
                }));
                assert_eq!(result.is_err(), panic);
                assert!(slot.wake_checked());
                assert_eq!(calls.0.load(Ordering::Relaxed), 1);
            }
        }
    }

    #[test]
    fn native_slot_delivery_and_shutdown() {
        #[derive(Debug)]
        struct NoopNotify;

        impl ntex_rt::Notify for NoopNotify {
            fn notify(&self) -> std::io::Result<()> {
                Ok(())
            }
        }

        let rt = ntex_rt::Runtime::new(Box::new(NoopNotify));
        let slot = Rc::new(LocalWaker::new());
        let polls = Rc::new(Cell::new(0));
        let (task_slot, task_polls) = (slot.clone(), polls.clone());
        let task = rt.spawn(std::future::poll_fn(move |cx| {
            task_polls.set(task_polls.get() + 1);
            task_slot.register(cx.waker());
            std::task::Poll::<()>::Pending
        }));
        rt.poll();
        slot.take_local().unwrap().wake();
        rt.poll();
        let standard = slot.take().unwrap();
        std::thread::spawn(move || standard.wake()).join().unwrap();
        rt.poll();
        assert_eq!(polls.get(), 3);
        assert_eq!(Rc::strong_count(&slot), 2);
        drop(rt);
        assert!(task.is_finished());
        assert_eq!(Rc::strong_count(&slot), 1);
        assert!(slot.wake_checked());
        assert!(!slot.wake_checked());
    }
}