VLink  2.0.0
A high-performance communication middleware
cancellation.h 文件参考

Cooperative one-shot cancellation primitives shared by VLink async building blocks. 更多...

#include <cstddef>
#include <memory>
#include "./functional.h"
#include "./macros.h"
cancellation.h 的引用(Include)关系图:
此图展示该文件直接或间接的被哪些文件引用了:

浏览源代码.

class  vlink::CancellationRegistration
 Move-only RAII handle that represents one subscribed cancellation callback. 更多...
 
class  vlink::CancellationToken
 Lightweight observer of a CancellationSource shared across worker threads. 更多...
 
class  vlink::CancellationSource
 Mutator that mints observer tokens and signals one-shot cancellation. 更多...
 

命名空间

 

详细描述

Cooperative one-shot cancellation primitives shared by VLink async building blocks.

Cancellation is split into a writable CancellationSource owned by the work producer, one or more read-only CancellationToken observers held by workers, and per-callback CancellationRegistration handles used to unsubscribe pending callbacks. All three share a refcounted internal state allocated on the producer side.

Role table
Type Role Copy / Move
CancellationSource Mutator; signals cancellation Implicit (refcount)
CancellationToken Observer; poll / throw / subscribe Copyable
CancellationRegistration RAII slot for one subscribed callback Move-only
State diagram
*   +------------+   request_cancel()    +---------------+   no-ops on further calls
*   |  active    | --------------------> |  cancelled    | -------------------------+
*   |  (default) |                       |  (terminal)   |                          |
*   +------------+                       +---------------+                          |
*      ^   ^                                ^                                       |
*      |   |  is_cancellation_requested()=false                                     |
*      |   |  throw_if_cancellation_requested()=no-op                               |
*      |   |                                                                        v
*      |   +-- new tokens via token() share the same state ------------------ read-only
*      |
*      +------ register_callback() inserts a slot; fires synchronously on transition
* 

Cancellation is one-shot. After the first successful request_cancel the state is terminal; subsequent requests return false and never re-invoke callbacks. The cancelled type re-exported as vlink::Exception::OperationCancelled is the canonical thrown type at structured cancellation points.

Lock ordering
Internal mutexes are released before user callbacks fire, so callbacks may freely cancel sibling sources or register more callbacks without self-deadlock.
Example
auto token = source.token();
auto reg = token.register_callback([] {
// Runs once on the cancelling thread, or inline if already cancelled.
});
while (!token.is_cancellation_requested()) {
token.throw_if_cancellation_requested();
run_work_unit();
}
source.request_cancel();