blob: 167a37184558a2438375b2e66d32ab9614a62a6d [file] [log] [blame]
Stjepan Glavina1479e862019-08-12 20:18:51 +02001/// Set if the task is scheduled for running.
2///
Stjepan Glavina5c398cf2019-08-20 15:29:43 +02003/// A task is considered to be scheduled whenever its `Task` reference exists. It therefore also
4/// begins in scheduled state at the moment of creation.
Stjepan Glavina1479e862019-08-12 20:18:51 +02005///
6/// This flag can't be set when the task is completed. However, it can be set while the task is
7/// running, in which case it will be rescheduled as soon as polling finishes.
8pub(crate) const SCHEDULED: usize = 1 << 0;
9
10/// Set if the task is running.
11///
Stjepan Glavina5c398cf2019-08-20 15:29:43 +020012/// A task is in running state while its future is being polled.
Stjepan Glavina1479e862019-08-12 20:18:51 +020013///
14/// This flag can't be set when the task is completed. However, it can be in scheduled state while
Stjepan Glavina5c398cf2019-08-20 15:29:43 +020015/// it is running, in which case it will be rescheduled as soon as polling finishes.
Stjepan Glavina1479e862019-08-12 20:18:51 +020016pub(crate) const RUNNING: usize = 1 << 1;
17
18/// Set if the task has been completed.
19///
20/// This flag is set when polling returns `Poll::Ready`. The output of the future is then stored
Stjepan Glavina5c398cf2019-08-20 15:29:43 +020021/// inside the task until it becomes closed. In fact, `JoinHandle` picks up the output by marking
22/// the task as closed.
Stjepan Glavina1479e862019-08-12 20:18:51 +020023///
Stjepan Glavina5c398cf2019-08-20 15:29:43 +020024/// This flag can't be set when the task is scheduled or running.
Stjepan Glavina1479e862019-08-12 20:18:51 +020025pub(crate) const COMPLETED: usize = 1 << 2;
26
27/// Set if the task is closed.
28///
Stjepan Glavina5c398cf2019-08-20 15:29:43 +020029/// If a task is closed, that means it's either cancelled or its output has been consumed by the
Stjepan Glavina1479e862019-08-12 20:18:51 +020030/// `JoinHandle`. A task becomes closed when:
31///
Stjepan Glavina5c398cf2019-08-20 15:29:43 +020032/// 1. It gets cancelled by `Task::cancel()`, `Task::drop()`, or `JoinHandle::cancel()`.
33/// 2. Its output gets awaited by the `JoinHandle`.
Stjepan Glavina1479e862019-08-12 20:18:51 +020034/// 3. It panics while polling the future.
Stjepan Glavina5c398cf2019-08-20 15:29:43 +020035/// 4. It is completed and the `JoinHandle` gets dropped.
Stjepan Glavina1479e862019-08-12 20:18:51 +020036pub(crate) const CLOSED: usize = 1 << 3;
37
38/// Set if the `JoinHandle` still exists.
39///
40/// The `JoinHandle` is a special case in that it is only tracked by this flag, while all other
41/// task references (`Task` and `Waker`s) are tracked by the reference count.
42pub(crate) const HANDLE: usize = 1 << 4;
43
44/// Set if the `JoinHandle` is awaiting the output.
45///
46/// This flag is set while there is a registered awaiter of type `Waker` inside the task. When the
47/// task gets closed or completed, we need to wake the awaiter. This flag can be used as a fast
48/// check that tells us if we need to wake anyone without acquiring the lock inside the task.
49pub(crate) const AWAITER: usize = 1 << 5;
50
Stjepan Glavina921e8a02020-01-06 14:31:28 -060051/// Set if an awaiter is being registered.
Stjepan Glavina1479e862019-08-12 20:18:51 +020052///
Stjepan Glavina921e8a02020-01-06 14:31:28 -060053/// This flag is set when `JoinHandle` is polled and we are registering a new awaiter.
54pub(crate) const REGISTERING: usize = 1 << 6;
55
56/// Set if the awaiter is being notified.
57///
58/// This flag is set when notifying the awaiter. If an awaiter is concurrently registered and
59/// notified, whichever side came first will take over the reposibility of resolving the race.
60pub(crate) const NOTIFYING: usize = 1 << 7;
Stjepan Glavina1479e862019-08-12 20:18:51 +020061
62/// A single reference.
63///
64/// The lower bits in the state contain various flags representing the task state, while the upper
65/// bits contain the reference count. The value of `REFERENCE` represents a single reference in the
66/// total reference count.
67///
68/// Note that the reference counter only tracks the `Task` and `Waker`s. The `JoinHandle` is
69/// tracked separately by the `HANDLE` flag.
Stjepan Glavina921e8a02020-01-06 14:31:28 -060070pub(crate) const REFERENCE: usize = 1 << 8;