Expand description
This mod provides the logic for the inner tree structure of the CancellationToken
.
CancellationTokens
are only light handles with references to TreeNode
.
All the logic is actually implemented in the TreeNode
.
A TreeNode
is part of the cancellation tree and may have one parent and an arbitrary number of
children.
A TreeNode
can receive the request to perform a cancellation through a CancellationToken
.
This cancellation request will cancel the node and all of its descendants.
As soon as a node cannot get cancelled any more (because it was already cancelled or it has no
more CancellationTokens
pointing to it any more), it gets removed from the tree, to keep the
tree as small as possible.
ยงInvariants
Those invariants shall be true at any time.
-
A node that has no parents and no handles can no longer be cancelled. This is important during both cancellation and refcounting.
-
If node B is or was a child of node A, then node B was created after node A. This is important for deadlock safety, as it is used for lock order. Node B can only become the child of node A in two ways:
- being created with
child_node()
, in which case it is trivially true that node A already existed when node B was created - being moved A->C->B to A->B because node C was removed in
decrease_handle_refcount()
orcancel()
. In this case the invariant still holds, as B was younger than C, and C was younger than A, therefore B is also younger than A.
- being created with
-
If two nodes are both unlocked and node A is the parent of node B, then node B is a child of node A. It is important to always restore that invariant before dropping the lock of a node.
ยงDeadlock safety
We always lock in the order of creation time. We can prove this through invariant #2. Specifically, through invariant #2, we know that we always have to lock a parent before its child.
Structsยง
- Inner ๐
- The data contained inside a
TreeNode
. - Tree
Node ๐ - A node of the cancellation tree structure
Functionsยง
- cancel ๐
- Cancels a node and its children.
- child_
node ๐ - Creates a child node
- decrease_
handle_ ๐refcount - Decreases the reference count of handles.
- disconnect_
children ๐ - Disconnects the given parent from all of its children.
- increase_
handle_ ๐refcount - Increases the reference count of handles.
- is_
cancelled ๐ - Returns whether or not the node is cancelled
- move_
children_ ๐to_ parent - Moves all children from
node
toparent
. - remove_
child ๐ - Removes a child from the parent.
- with_
locked_ ๐node_ and_ parent - Figures out the parent of the node and locks the node and its parent atomically.