COROIO: coroio/completions.hpp Source File
COROIO
Loading...
Searching...
No Matches
completions.hpp
1#pragma once
2
3#include "base.hpp"
4
5#include <utility>
6#include <vector>
7
8namespace NNet {
9namespace NDetail {
10
21struct TOperationState {
22 TOperationState() = default;
23 TOperationState(const TOperationState&) = delete;
24 TOperationState& operator=(const TOperationState&) = delete;
25
26 THandle Handle;
27 int Result = 0;
28 unsigned Index = 0;
29};
30
38public:
40 void Collect(TOperationState* op, int result) {
41 op->Result = result;
42 Add(Ready_, op);
43 }
44
46 void Reap(TOperationState* op, int result) {
47 op->Result = result;
48 Add(Reaped_, op);
49 }
50
53 // The lists hold only completions not delivered yet, so an entry that
54 // points to op is its completion, whatever Index held before.
55 return Remove(Ready_, op) || Remove(Reaped_, op);
56 }
57
66 bool Carry() {
67 size_t size = 0;
68 for (auto* op : Ready_) {
69 if (op) {
70 op->Index = static_cast<unsigned>(size);
71 Ready_[size++] = op;
72 }
73 }
74 Ready_.resize(size);
75 for (auto* op : Reaped_) {
76 if (op) {
77 Add(Ready_, op);
78 }
79 }
80 Reaped_.clear();
81 return !Ready_.empty();
82 }
83
85 void Wakeup() {
86 for (size_t i = 0; i < Ready_.size(); i++) {
87 // Taken out before resuming: the coroutine may post its next operation
88 // at the same address, and Drop() must not find this entry for it.
89 if (auto* op = std::exchange(Ready_[i], nullptr)) {
90 std::exchange(op->Handle, {}).resume();
91 }
92 }
93 Ready_.clear();
94 }
95
96private:
97 static void Add(std::vector<TOperationState*>& list, TOperationState* op) {
98 op->Index = static_cast<unsigned>(list.size());
99 list.push_back(op);
100 }
101
102 static bool Remove(std::vector<TOperationState*>& list, TOperationState& op) {
103 if (op.Index < list.size() && list[op.Index] == &op) {
104 list[op.Index] = nullptr;
105 return true;
106 }
107 return false;
108 }
109
110 std::vector<TOperationState*> Ready_;
111 std::vector<TOperationState*> Reaped_;
112};
113
114} // namespace NDetail
115} // namespace NNet
Completions taken from the system and not delivered yet.
Definition completions.hpp:37
void Reap(TOperationState *op, int result)
Adds a completion taken while waiting for a cancelled operation.
Definition completions.hpp:46
bool Drop(TOperationState &op)
Drops the completion of a cancelled operation. Returns false if it is not taken yet.
Definition completions.hpp:52
void Collect(TOperationState *op, int result)
Adds a completion taken by the poller's wait.
Definition completions.hpp:40
void Wakeup()
Resumes the coroutines of the ready completions.
Definition completions.hpp:85
bool Carry()
Starts the next batch of ready completions.
Definition completions.hpp:66
State of one operation of a completion-based poller (TUring, TIOCp).
Definition completions.hpp:21
int Result
Result of the completion, e.g. the number of bytes transferred.
Definition completions.hpp:27
unsigned Index
Position of the completion in TCompletions once taken; before that, whatever the poller keeps there.
Definition completions.hpp:28
THandle Handle
Coroutine to resume, set by the caller before posting. Cleared when posting fails,...
Definition completions.hpp:26