tokio-tasks 0.5.3

Task management for tokio
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
//! Defines a run token that can be used to cancel async tasks
use futures_util::Future;

use std::{
    mem::MaybeUninit,
    pin::Pin,
    sync::{Arc, atomic::Ordering},
    task::{Context, Poll, Waker},
};

use std::sync::atomic::AtomicPtr;
#[cfg(feature = "runtoken-id")]
use std::sync::atomic::AtomicU64;

#[cfg(feature = "ordered-locks")]
use ordered_locks::{L0, LockToken};

/// Next id used for run token ids
#[cfg(feature = "runtoken-id")]
static IDC: AtomicU64 = AtomicU64::new(0);

/// Intrusive circular linked list of T's
pub struct IntrusiveList<T> {
    /// Pointer to the first element in the list, the last element can be found
    /// as first->prev
    first: *mut ListNode<T>,
}

impl<T> Default for IntrusiveList<T> {
    fn default() -> Self {
        Self {
            first: std::ptr::null_mut(),
        }
    }
}

impl<T> IntrusiveList<T> {
    /// Add node to the list with the content v
    ///
    /// Safety:
    /// - node should be a valid pointer.
    /// - node should only be accessed by us
    /// - node should not be in a list
    /// - node should not have value added to it
    ///
    /// This will write a value to the node
    unsafe fn push_back(&mut self, node: *mut ListNode<T>, v: T) {
        // Safety: node is a valid pointer we may access
        let n = unsafe { &mut *node };
        assert!(n.next.is_null());
        n.data.write(v);
        if self.first.is_null() {
            n.next = n;
            n.prev = n;
            self.first = n;
        } else {
            // Safety: first is not null and can be deref'ed by the invariants of the list
            let f = unsafe { &mut *self.first };
            n.prev = f.prev;
            n.next = self.first;
            // Safety: we have just set prev to a valid pointer
            unsafe {
                (*n.prev).next = node;
            }
            f.prev = node;
        }
    }

    /// Remove node to the list returning its content
    ///
    /// Safety:
    /// - node should be a valid pointer.
    /// - node should only be accessed by us
    /// - node should be in a list
    ///
    /// The value will be read out from the node
    unsafe fn remove(&mut self, node: *mut ListNode<T>) -> T {
        // Safety: node is a valid pointer we may access
        let n = unsafe { &mut *node };
        assert!(!n.next.is_null());
        // Safety: We require that the node is in the list,
        // which by invariant means that data is set
        let v = unsafe { n.data.as_mut_ptr().read() };
        if n.next == node {
            self.first = std::ptr::null_mut();
        } else {
            if self.first == node {
                self.first = n.next;
            }
            // Safety: By list invariants n.next is a valid pointer we may mutate
            unsafe {
                (*n.next).prev = n.prev;
            }
            // Safety: By list invariants n.prev is a valid pointer we may mutate
            unsafe {
                (*n.prev).next = n.next;
            }
        }
        n.next = std::ptr::null_mut();
        n.prev = std::ptr::null_mut();
        v
    }

    /// Remove all entries from this list
    fn drain(&mut self, v: impl Fn(T)) {
        if self.first.is_null() {
            return;
        }
        let mut cur = self.first;
        loop {
            // Safety: By invariant cur points to a list node that has not
            // yet been removed, and we are allowed to mutate it
            let c = unsafe { &mut *cur };
            // Safety: Since c is in the list it has a data value
            let d = unsafe { c.data.as_mut_ptr().read() };
            v(d);
            let next = c.next;
            c.next = std::ptr::null_mut();
            c.prev = std::ptr::null_mut();
            if next == self.first {
                break;
            }
            cur = next;
        }
        self.first = std::ptr::null_mut();
    }

    /// Check if the node is in a list
    ///
    /// Safety:
    /// - node should be a valid pointer
    /// - No one should modify node while we access it
    unsafe fn in_list(&self, node: *mut ListNode<T>) -> bool {
        // Safety: Node is a valid pointer
        unsafe { !(*node).next.is_null() }
    }
}

/// Node uned in the linked list
pub struct ListNode<T> {
    /// The previous element in the list
    prev: *mut ListNode<T>,
    /// The next element in the node
    next: *mut ListNode<T>,
    /// The data contained in this node
    data: std::mem::MaybeUninit<T>,
    /// Make sure we do not implement unpin
    _pin: std::marker::PhantomPinned,
}

impl<T> Default for ListNode<T> {
    fn default() -> Self {
        Self {
            prev: std::ptr::null_mut(),
            next: std::ptr::null_mut(),
            data: MaybeUninit::uninit(),
            _pin: Default::default(),
        }
    }
}

/// The state a [RunToken] is in
enum State {
    /// The [RunToken] is running
    Run,
    /// The [RunToken] has been canceled
    Cancel,
    /// The task should paused
    #[cfg(feature = "pause")]
    Pause,
}

/// Inner content of a [RunToken] behind a Mutex
struct Content {
    /// The state of the run token
    state: State,
    /// Wakers to wake when cancelling the [RunToken]
    cancel_wakers: IntrusiveList<Waker>,
    /// Wakers to wake when unpausing the [RunToken]
    run_wakers: IntrusiveList<Waker>,
    /// User supplied data stored in runtoken
    #[cfg(feature = "runtoken-user-data")]
    user_data: Option<String>,
}

// Safety: the intrusive lists may be sent between threads
unsafe impl Send for Content {}

impl Content {
    /// Wake waker when the [RunToken] is cancelled
    ///
    /// Safety:
    /// - node must be a valid pointer
    /// - node must not contain a value if it is not in a list
    unsafe fn add_cancel_waker(&mut self, node: *mut ListNode<Waker>, waker: &Waker) {
        // Safety: We can check if the node is in the list since it is a valid pointer
        // and we own mutation rights
        let in_list = unsafe { self.cancel_wakers.in_list(node) };
        if !in_list {
            // Safety: Node is a valid pointer, we may mutate it,
            // we have checked that it is not part of a list, and thus
            // it has no value
            unsafe { self.cancel_wakers.push_back(node, waker.clone()) }
        }
    }

    /// Wake waker when the [RunToken] is unpaused
    ///
    /// Safety:
    /// - node must be a valid pointer
    /// - node must not contain a value if it is not in a list
    #[cfg(feature = "pause")]
    unsafe fn add_run_waker(&mut self, node: *mut ListNode<Waker>, waker: &Waker) {
        // Safety: We can check if the node is in the list since it is a valid pointer
        // and we own mutation rights
        let in_list = unsafe { self.run_wakers.in_list(node) };
        if !in_list {
            // Safety: Node is a valid pointer, we may mutate it,
            // we have checked that it is not part of a list, and thus
            // it has no value
            unsafe { self.run_wakers.push_back(node, waker.clone()) }
        }
    }

    /// Remove node from the list of nodes to be woken when the run token is
    /// cancelled
    ///
    /// Safety:
    /// - node must be a valid pointer
    /// - if the node is in a list, it mut be the cancel_wakers list
    unsafe fn remove_cancel_waker(&mut self, node: *mut ListNode<Waker>) {
        // Safety: We can check if the node is in the list since it is a valid pointer
        // and we own mutation rights
        let in_list = unsafe { self.cancel_wakers.in_list(node) };
        if in_list {
            // Safety: Node is a valid pointer, we may mutate it and it is in the list
            unsafe { self.cancel_wakers.remove(node) };
        }
    }

    /// Remove node from the list of nodes to be woken when the run token is
    /// unpaused
    ///
    /// Safety:
    /// - node must be a valid pointer
    /// - if the node is in a list, it mut be the run_wakers list
    #[cfg(feature = "pause")]
    unsafe fn remove_run_waker(&mut self, node: *mut ListNode<Waker>) {
        // Safety: We can check if the node is in the list since it is a valid pointer
        // and we own mutation rights
        let in_list = unsafe { self.run_wakers.in_list(node) };
        if in_list {
            // Safety: Node is a valid pointer, we may mutate it and it is in the list
            unsafe { self.run_wakers.remove(node) };
        }
    }
}

/// Inner content of a [RunToken] not behind a mutex
struct Inner {
    /// Condition notified on cancel and unpause
    cond: std::sync::Condvar,
    /// Inner content of this [RunToken] that must be accessed exclusively
    content: std::sync::Mutex<Content>,
    /// The id unique of this run token
    #[cfg(feature = "runtoken-id")]
    id: u64,
    /// The location last set on this run-token, mut be a valid pointer to a
    /// &' static str of the form "file:line" or null
    location_file_line: AtomicPtr<u8>,
}
/// Similar to a [`tokio_util::sync::CancellationToken`],
/// the RunToken encapsulates the possibility of canceling an async command.
/// However it also allows pausing and resuming the async command,
/// and it is possible to wait in both a blocking fashion and an asynchronous fashion.
#[derive(Clone)]
pub struct RunToken(Arc<Inner>);

impl RunToken {
    /// Construct a new paused run token
    #[cfg(feature = "pause")]
    pub fn new_paused() -> Self {
        Self(Arc::new(Inner {
            cond: std::sync::Condvar::new(),
            content: std::sync::Mutex::new(Content {
                state: State::Pause,
                cancel_wakers: Default::default(),
                run_wakers: Default::default(),
                #[cfg(feature = "runtoken-user-data")]
                user_data: None,
            }),
            location_file_line: Default::default(),
            #[cfg(feature = "runtoken-id")]
            id: IDC.fetch_add(1, std::sync::atomic::Ordering::SeqCst),
        }))
    }

    /// Construct a new running run token
    pub fn new() -> Self {
        Self(Arc::new(Inner {
            cond: std::sync::Condvar::new(),
            content: std::sync::Mutex::new(Content {
                state: State::Run,
                cancel_wakers: Default::default(),
                run_wakers: Default::default(),
                #[cfg(feature = "runtoken-user-data")]
                user_data: None,
            }),
            location_file_line: Default::default(),
            #[cfg(feature = "runtoken-id")]
            id: IDC.fetch_add(1, std::sync::atomic::Ordering::SeqCst),
        }))
    }

    /// Cancel computation
    pub fn cancel(&self) {
        let mut content = self.0.content.lock().unwrap();
        if matches!(content.state, State::Cancel) {
            return;
        }
        content.state = State::Cancel;

        content.run_wakers.drain(|w| w.wake());
        content.cancel_wakers.drain(|w| w.wake());
        self.0.cond.notify_all();
    }

    /// Pause computation if we are running
    #[cfg(feature = "pause")]
    pub fn pause(&self) {
        let mut content = self.0.content.lock().unwrap();
        if !matches!(content.state, State::Run) {
            return;
        }
        content.state = State::Pause;
    }

    /// Resume computation if we are paused
    #[cfg(feature = "pause")]
    pub fn resume(&self) {
        let mut content = self.0.content.lock().unwrap();
        if !matches!(content.state, State::Pause) {
            return;
        }
        content.state = State::Run;
        content.run_wakers.drain(|w| w.wake());
        self.0.cond.notify_all();
    }

    /// Return true iff we are canceled
    pub fn is_cancelled(&self) -> bool {
        matches!(self.0.content.lock().unwrap().state, State::Cancel)
    }

    /// Return true iff we are paused
    #[cfg(feature = "pause")]
    pub fn is_paused(&self) -> bool {
        matches!(self.0.content.lock().unwrap().state, State::Pause)
    }

    /// Return true iff we are runnig
    #[cfg(feature = "pause")]
    pub fn is_running(&self) -> bool {
        matches!(self.0.content.lock().unwrap().state, State::Run)
    }

    /// Block the thread until we are not paused, and then return true if we are canceled or false if we are running
    #[cfg(feature = "pause")]
    pub fn wait_paused_check_cancelled_sync(&self) -> bool {
        let mut content = self.0.content.lock().unwrap();
        loop {
            match &content.state {
                State::Run => return false,
                State::Cancel => return true,
                State::Pause => {
                    content = self.0.cond.wait(content).unwrap();
                }
            }
        }
    }

    /// Suspend the async coroutine until we are not paused, and then return true if we are canceled or false if we are running
    #[cfg(feature = "pause")]
    pub fn wait_paused_check_cancelled(&self) -> WaitForPauseFuture<'_> {
        WaitForPauseFuture {
            token: self,
            waker: Default::default(),
        }
    }

    /// Suspend the async coroutine until cancel() is called
    pub fn cancelled(&self) -> WaitForCancellationFuture<'_> {
        WaitForCancellationFuture {
            token: self,
            waker: Default::default(),
        }
    }

    /// Suspend the async coroutine until cancel() is called, checking that we do not hold
    /// a too deep lock
    #[cfg(feature = "ordered-locks")]
    pub fn cancelled_checked(
        &self,
        _lock_token: LockToken<'_, L0>,
    ) -> WaitForCancellationFuture<'_> {
        WaitForCancellationFuture {
            token: self,
            waker: Default::default(),
        }
    }

    /// Store a file line location in the run_token
    /// The string must be on the form "file:line\0"
    ///
    /// You probably want to call the [set_location] macro instead
    #[inline]
    pub fn set_location_file_line(&self, file_line_str: &'static str) {
        assert!(file_line_str.ends_with('\0'));
        self.0
            .location_file_line
            .store(file_line_str.as_ptr() as *mut u8, Ordering::Relaxed);
    }

    /// Retrieve the stored file,live location in the run_token
    pub fn location(&self) -> Option<(&'static str, u32)> {
        let location_file_line = self.0.location_file_line.load(Ordering::Relaxed) as *const u8;
        if location_file_line.is_null() {
            return None;
        }
        let mut len = 0;
        // Safety: let must be within the string since it is \0 terminated and we
        // have not yet seen a \0
        loop {
            // Safety: let must be within the string since it is \0 terminated and we
            // have not yet seen a \0
            let l = unsafe { location_file_line.add(len) };
            // Safety: let must be within the string since it is \0 terminated and we
            // have not yet seen a \0
            let c = unsafe { *l };
            if c == b'\0' {
                break;
            }
            len += 1;
        }

        // Safety: location_file_line points to a byte array with at least len bytes
        let location_file_line = unsafe { std::slice::from_raw_parts(location_file_line, len) };

        // Safety: location_file_line points to a utf-8 string ending with "\0"
        let location_file_line = unsafe { std::str::from_utf8_unchecked(location_file_line) };

        match location_file_line.rsplit_once(":") {
            Some((file, line)) => match line.parse() {
                Ok(v) => Some((file, v)),
                Err(_) => Some((location_file_line, 0)),
            },
            None => Some((location_file_line, 0)),
        }
    }

    #[cfg(feature = "runtoken-id")]
    /// The unique incremental id of this run token
    #[inline]
    pub fn id(&self) -> u64 {
        self.0.id
    }

    #[cfg(feature = "runtoken-user-data")]
    /// Store user data in the run token
    pub fn set_user_data(&self, data: Option<String>) {
        self.0.content.lock().unwrap().user_data = data;
    }

    #[cfg(feature = "runtoken-user-data")]
    /// Get user data stored in run_token
    pub fn user_data(&self) -> Option<String> {
        self.0.content.lock().unwrap().user_data.clone()
    }
}

/// Update the location stored in a run token to the current file:line
#[macro_export]
macro_rules! set_location {
    ($run_token: expr) => {
        $run_token.set_location_file_line(concat!(file!(), ":", line!(), "\0"));
    };
}

impl Default for RunToken {
    fn default() -> Self {
        Self::new()
    }
}

impl core::fmt::Debug for RunToken {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        let mut d = f.debug_tuple("RunToken");
        match self.0.content.lock().unwrap().state {
            State::Run => d.field(&"Running"),
            State::Cancel => d.field(&"Canceled"),
            #[cfg(feature = "pause")]
            State::Pause => d.field(&"Paused"),
        };
        d.finish()
    }
}

/// Wait until task cancellation is completed
///
/// Note: [std::mem::forget]ting this future may crash your application,
/// a pointer to the content of this future is stored in the RunToken
#[must_use = "futures do nothing unless polled"]
pub struct WaitForCancellationFuture<'a> {
    /// The token to wait for
    token: &'a RunToken,
    /// Entry in the cancel_wakers list of the RunToken
    waker: ListNode<Waker>,
}

impl<'a> core::fmt::Debug for WaitForCancellationFuture<'a> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("WaitForCancellationFuture").finish()
    }
}

impl<'a> Future for WaitForCancellationFuture<'a> {
    type Output = ();

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
        let mut content = self.token.0.content.lock().unwrap();
        match content.state {
            State::Cancel => Poll::Ready(()),
            State::Run => {
                // Safety: We do not move the node
                let node = unsafe { &mut Pin::get_unchecked_mut(self).waker };
                // Safety: The node is either not in the list and has no value, or it is
                // in the list and has a value
                unsafe { content.add_cancel_waker(node, cx.waker()) };
                Poll::Pending
            }
            #[cfg(feature = "pause")]
            State::Pause => {
                // Safety: We do not move the node
                let node = unsafe { &mut Pin::get_unchecked_mut(self).waker };
                // Safety: The node is either not in the list and has no value, or it is
                // in the list and has a value
                unsafe { content.add_cancel_waker(node, cx.waker()) };
                Poll::Pending
            }
        }
    }
}

impl<'a> Drop for WaitForCancellationFuture<'a> {
    fn drop(&mut self) {
        // Safety: The node is valid
        unsafe {
            self.token
                .0
                .content
                .lock()
                .unwrap()
                .remove_cancel_waker(&mut self.waker);
        }
    }
}

// Safety: We can safely move WaitForCancellationFuture when it is not pinned
unsafe impl<'a> Send for WaitForCancellationFuture<'a> {}

#[cfg(feature = "pause")]
/// Wait until task is not paused
///
/// Note: [std::mem::forget]ting this future may crash your application,
/// a pointer to the content of this future is stored in the RunToken
#[must_use = "futures do nothing unless polled"]
pub struct WaitForPauseFuture<'a> {
    /// The run toke to wait to unpause
    token: &'a RunToken,
    /// Entry in the run_wakers list of the RunToken
    waker: ListNode<Waker>,
}

#[cfg(feature = "pause")]
impl<'a> core::fmt::Debug for WaitForPauseFuture<'a> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("WaitForPauseFuture").finish()
    }
}

#[cfg(feature = "pause")]
impl<'a> Future for WaitForPauseFuture<'a> {
    type Output = bool;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<bool> {
        let mut content = self.token.0.content.lock().unwrap();
        match content.state {
            State::Cancel => Poll::Ready(true),
            State::Run => Poll::Ready(false),
            State::Pause => {
                // Safety: We will not move the node
                let node = unsafe { &mut Pin::get_unchecked_mut(self).waker };
                // Safety: The node is a valid pointer
                unsafe { content.add_run_waker(node, cx.waker()) };
                Poll::Pending
            }
        }
    }
}

#[cfg(feature = "pause")]
impl<'a> Drop for WaitForPauseFuture<'a> {
    fn drop(&mut self) {
        // Safety: The node is valid
        unsafe {
            self.token
                .0
                .content
                .lock()
                .unwrap()
                .remove_run_waker(&mut self.waker);
        }
    }
}

#[cfg(feature = "pause")]
// Safety: We can safely move WaitForPauseFuture when it is not pinned
unsafe impl<'a> Send for WaitForPauseFuture<'a> {}