Repository: tokio-rs/tokio
File: tokio/src/runtime/task/mod.rs (Rust)

Code before:
//!    thread the runtime may complete a future and read the waker field to
//!    invoke the waker, and in another thread the future's JoinHandle may be
//!    polled and if the future hasn't completed, JoinHandle may install a waker
//!    in the waker field. The JOIN_WAKER bit ensures safe access by multiple
//!    threads to the waker field using the following rules:
//!
//!    1. JOIN_WAKER is initialized to zero.
//!
//!    2. If JOIN_WAKER is zero, then the JoinHandle has exclusive (mutable)
//!       access to the waker field.
//!
//!    3. If JOIN_WAKER is one, then the JoinHandle has shared (read-only)
//!       access to the waker field.
//!
//!    4. If the JoinHandle needs to install a waker, then the JoinHandle needs
//!       to (i) successfully set JOIN_WAKER to zero if it is not already zero
//!       to gain exclusive access to the waker field per rule 2, (ii) install a
//!       waker, and (iii) successfully set JOIN_WAKER to one.
//!
//!    5. The JoinHandle can only change JOIN_WAKER if COMPLETE is zero (i.e.
//!       the task hasn't yet completed). Hence, steps (i) or (iii) of rule 4
//!       may fail due to a race.
//!
//!    6. Once COMPLETE is one (i.e. task has completed), the JoinHandle will
//!       not modify JOIN_WAKER and the waker field. At this point the runtime
//!       invokes the waker if there is one.
//!
//! All other fields are immutable and can be accessed immutably without
//! synchronization by anyone.
//!
//! # Safety
//!
//! This section goes through various situations and explains why the API is
//! safe in that situation.
//!
//! ## Polling or dropping the future
//!
//! Any mutable access to the future happens after obtaining a lock by modifying
//! the RUNNING field, so exclusive access is ensured.
//!
//! When the task completes, exclusive access to the output is transferred to
//! the JoinHandle. If the JoinHandle is already dropped when the transition to
//! complete happens, the thread performing that transition retains exclusive
//! access to the output and should immediately drop it.
//!
//! ## Non-Send futures
//!
//! If a future is not Send, then it is bound to a LocalOwnedTasks.  The future
//! will only ever be polled or dropped given a LocalNotified or inside a call
//! to LocalOwnedTasks::shutdown_all. In either case, it is guaranteed that the
//! future is on the right thread.

Review comment (suggestion):
The join handle _can_ still modify the waker field when COMPLETE is one. This happens if COMPLETE is changed to one during step 4. (ii).

Code after:
//!    thread the runtime may complete a task and *read* the waker field to
//!    invoke the waker, and in another thread the task's JoinHandle may be
//!    polled, and if the task hasn't yet completed, the JoinHandle may *write*
//!    a waker to the waker field. The JOIN_WAKER bit ensures safe access by
//!    multiple threads to the waker field using the following rules:
//!
//!    1. JOIN_WAKER is initialized to zero.
//!
//!    2. If JOIN_WAKER is zero, then the JoinHandle has exclusive (mutable)
//!       access to the waker field.
//!
//!    3. If JOIN_WAKER is one, then the JoinHandle has shared (read-only)
//!       access to the waker field.
//!
//!    4. If JOIN_WAKER is one and COMPLETE is one, then the runtime has shared
//!       (read-only) access to the waker field.
//!
//!    5. If the JoinHandle needs to write to the waker field, then the
//!       JoinHandle needs to (i) successfully set JOIN_WAKER to zero if it is
//!       not already zero to gain exclusive access to the waker field per rule
//!       2, (ii) write a waker, and (iii) successfully set JOIN_WAKER to one.
//!
//!    6. The JoinHandle can change JOIN_WAKER only if COMPLETE is zero (i.e.
//!       the task hasn't yet completed).
//!
//!    Rule 6 implies that the steps (i) or (iii) of rule 5 may fail due to a
//!    race. If step (i) fails, then the attempt to write a waker is aborted. If
//!    step (iii) fails because COMPLETE is set to one by another thread after
//!    step (i), then the waker field is cleared. Once COMPLETE is one (i.e.
//!    task has completed), the JoinHandle will not modify JOIN_WAKER. After the
//!    runtime sets COMPLETE to one, it invokes the waker if there is one.
//!
//! All other fields are immutable and can be accessed immutably without
//! synchronization by anyone.
//!
//! # Safety
//!
//! This section goes through various situations and explains why the API is
//! safe in that situation.
//!
//! ## Polling or dropping the future
//!
//! Any mutable access to the future happens after obtaining a lock by modifying
//! the RUNNING field, so exclusive access is ensured.
//!
//! When the task completes, exclusive access to the output is transferred to
//! the JoinHandle. If the JoinHandle is already dropped when the transition to
//! complete happens, the thread performing that transition retains exclusive
//! access to the output and should immediately drop it.
//!
//! ## Non-Send futures