• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /// Set if the task is scheduled for running.
2 ///
3 /// A task is considered to be scheduled whenever its `Runnable` exists.
4 ///
5 /// This flag can't be set when the task is completed. However, it can be set while the task is
6 /// running, in which case it will be rescheduled as soon as polling finishes.
7 pub(crate) const SCHEDULED: usize = 1 << 0;
8 
9 /// Set if the task is running.
10 ///
11 /// A task is in running state while its future is being polled.
12 ///
13 /// This flag can't be set when the task is completed. However, it can be in scheduled state while
14 /// it is running, in which case it will be rescheduled as soon as polling finishes.
15 pub(crate) const RUNNING: usize = 1 << 1;
16 
17 /// Set if the task has been completed.
18 ///
19 /// This flag is set when polling returns `Poll::Ready`. The output of the future is then stored
20 /// inside the task until it becomes closed. In fact, `Task` picks up the output by marking
21 /// the task as closed.
22 ///
23 /// This flag can't be set when the task is scheduled or running.
24 pub(crate) const COMPLETED: usize = 1 << 2;
25 
26 /// Set if the task is closed.
27 ///
28 /// If a task is closed, that means it's either canceled or its output has been consumed by the
29 /// `Task`. A task becomes closed in the following cases:
30 ///
31 /// 1. It gets canceled by `Runnable::drop()`, `Task::drop()`, or `Task::cancel()`.
32 /// 2. Its output gets awaited by the `Task`.
33 /// 3. It panics while polling the future.
34 /// 4. It is completed and the `Task` gets dropped.
35 pub(crate) const CLOSED: usize = 1 << 3;
36 
37 /// Set if the `Task` still exists.
38 ///
39 /// The `Task` is a special case in that it is only tracked by this flag, while all other
40 /// task references (`Runnable` and `Waker`s) are tracked by the reference count.
41 pub(crate) const TASK: usize = 1 << 4;
42 
43 /// Set if the `Task` is awaiting the output.
44 ///
45 /// This flag is set while there is a registered awaiter of type `Waker` inside the task. When the
46 /// task gets closed or completed, we need to wake the awaiter. This flag can be used as a fast
47 /// check that tells us if we need to wake anyone.
48 pub(crate) const AWAITER: usize = 1 << 5;
49 
50 /// Set if an awaiter is being registered.
51 ///
52 /// This flag is set when `Task` is polled and we are registering a new awaiter.
53 pub(crate) const REGISTERING: usize = 1 << 6;
54 
55 /// Set if the awaiter is being notified.
56 ///
57 /// This flag is set when notifying the awaiter. If an awaiter is concurrently registered and
58 /// notified, whichever side came first will take over the reposibility of resolving the race.
59 pub(crate) const NOTIFYING: usize = 1 << 7;
60 
61 /// A single reference.
62 ///
63 /// The lower bits in the state contain various flags representing the task state, while the upper
64 /// bits contain the reference count. The value of `REFERENCE` represents a single reference in the
65 /// total reference count.
66 ///
67 /// Note that the reference counter only tracks the `Runnable` and `Waker`s. The `Task` is
68 /// tracked separately by the `TASK` flag.
69 pub(crate) const REFERENCE: usize = 1 << 8;
70