MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
Completion.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Contributors to the Media eXchange Layer project.
2//
3// SPDX-License-Identifier: Apache-2.0
4
5#include "Completion.hpp"
6#include <optional>
7#include <variant>
8#include <rdma/fi_eq.h>
9#include "CompletionQueue.hpp" // IWYU pragma: keep
10#include "Exception.hpp"
11#include "VariantUtils.hpp"
12
14{
15 Completion::Data::Data(::fi_cq_data_entry const& raw)
16 : _raw(raw)
17 {}
18
19 std::optional<std::uint64_t> Completion::Data::data() const noexcept
20 {
21 if (!(_raw.flags & FI_REMOTE_CQ_DATA))
22 {
23 return std::nullopt;
24 }
25
26 return _raw.data;
27 }
28
29 bool Completion::Data::isRemoteWrite() const noexcept
30 {
31 return (_raw.flags & FI_RMA) && (_raw.flags & FI_REMOTE_WRITE);
32 }
33
34 bool Completion::Data::isLocalWrite() const noexcept
35 {
36 return (_raw.flags & FI_RMA) && (_raw.flags & FI_WRITE);
37 }
38
39 Completion::Error::Error(::fi_cq_err_entry const& raw, std::shared_ptr<CompletionQueue> cq)
40 : _raw(raw)
41 , _cq(std::move(cq))
42 , _message(::fi_cq_strerror(_cq->raw(), _raw.prov_errno, _raw.err_data, nullptr, 0))
43 {
44 _raw.err_data = nullptr;
45 _raw.err_data_size = 0;
46 }
47
48 std::string Completion::Error::toString() const
49 {
50 return _message;
51 }
52
54 {
55 return tokenFromContextValue(_raw.op_context);
56 }
57
59 {
60 if (auto data = std::get_if<Completion::Data>(&_inner); data)
61 {
62 return *data;
63 }
64
65 throw Exception::invalidState("Failed to unwrap completion queue entry as data entry.");
66 }
67
69 {
70 return tokenFromContextValue(_raw.op_context);
71 }
72
74 : _inner(entry)
75 {}
76
78 : _inner(entry)
79 {}
80
82 {
83 std::uniform_int_distribution<Completion::Token> dist;
84 std::random_device rd;
85 std::mt19937 eng{rd()};
86
87 return dist(eng);
88 }
89
91 {
92 if (auto error = std::get_if<Error>(&_inner); error)
93 {
94 return *error;
95 }
96
97 throw Exception::invalidState("Failed to unwrap completion queue entry as error.");
98 }
99
100 std::optional<Completion::Data> Completion::tryData() const noexcept
101 {
102 if (auto data = std::get_if<Data>(&_inner); data)
103 {
104 return {*data};
105 }
106
107 return std::nullopt;
108 }
109
110 std::optional<Completion::Error> Completion::tryErr() const noexcept
111 {
112 if (auto error = std::get_if<Error>(&_inner); error)
113 {
114 return {*error};
115 }
116
117 return std::nullopt;
118 }
119
120 bool Completion::isDataEntry() const noexcept
121 {
122 return std::holds_alternative<Data>(_inner);
123 }
124
125 bool Completion::isErrEntry() const noexcept
126 {
127 return std::holds_alternative<Error>(_inner);
128 }
129
131 {
132 return std::visit(
134 [](Completion::Data const& data) { return data.token(); },
135 [](Completion::Error const& err) { return err.token(); },
136 },
137 _inner);
138 }
139}
Data variant of the completion entry instance.
bool isLocalWrite() const noexcept
Indicates whether the completion entry represents a local write operation.
Completion::Token token() const noexcept
This used to associate a completion entry with an endpoint when multiple endpoints use the same compl...
std::optional< std::uint64_t > data() const noexcept
Accessor for the immediate data associated with the completion entry.
Data(::fi_cq_data_entry const &raw)
bool isRemoteWrite() const noexcept
Indicates whether the completion entry represents a remote write operation.
Error variant of the completion entry instance.
std::string toString() const
Generate a hman-readable string representation of the error entry.
Token token() const noexcept
This used to associate a completion entry with an endpoint when multiple endpoints use the same compl...
Error(::fi_cq_err_entry const &raw, std::shared_ptr< CompletionQueue > queue)
std::shared_ptr< CompletionQueue > _cq
Token token() const noexcept
Get the token that was associated with the operation that has completed.
static constexpr Token tokenFromContextValue(void *contextValue) noexcept
Convert between completion tokens and context values that can be written to and read from raw libfabr...
bool isErrEntry() const noexcept
check whether this is an error entry
Data data() const
Accessor for the data variant. Calling this on an error entry will throw.
std::uintptr_t Token
A token used to associate a completion entry with an endpoint.
std::optional< Data > tryData() const noexcept
Attempt to access the data variant. Returns std::nullopt if this is an error entry.
std::optional< Error > tryErr() const noexcept
Attempt to access the error variant. Returns std::nullopt if this is a data entry.
bool isDataEntry() const noexcept
Check whether this is a data entry.
Error err() const
Accessor for the error variant. Calling this on a data entry will throw.
static Exception invalidState(fmt::format_string< T... > fmt, T &&... args)
Make an MXL_ERR_INVALID_STATE exception.
Definition Exception.hpp:59
::fi_info * raw() noexcept
Returns the raw fi_info object managed by this FabricInfo instance.