MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
QueueHelpers.hpp
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#pragma once
6
7#include <chrono>
8#include <optional>
9#include <utility>
10#include <fmt/base.h>
11#include <fmt/color.h>
13#include <rdma/fi_errno.h>
14#include "Completion.hpp"
15#include "Endpoint.hpp"
16#include "Event.hpp"
17#include "EventQueue.hpp"
18#include "Exception.hpp"
19
21{
29
38 template<QueueReadMode qrm>
39 std::pair<std::optional<Completion>, std::optional<Event>> readEndpointQueues(Endpoint& ep, std::chrono::steady_clock::duration timeout)
40 {
41 static_assert((qrm == QueueReadMode::Blocking) || (qrm == QueueReadMode::NonBlocking), "Unsupported queue behaviour parameter");
42
43 auto result = std::pair<std::optional<Completion>, std::optional<Event>>{};
44
45 if constexpr (qrm == QueueReadMode::Blocking)
46 {
47 result = ep.readQueuesBlocking(timeout);
48 }
49 else if constexpr (qrm == QueueReadMode::NonBlocking)
50 {
51 result = ep.readQueues();
52 }
53
54 return result;
55 }
56
64 template<QueueReadMode qrm>
65 std::optional<Event> readEventQueue(EventQueue& eq, std::chrono::steady_clock::duration timeout)
66 {
67 static_assert((qrm == QueueReadMode::Blocking) || (qrm == QueueReadMode::NonBlocking), "Unsupported queue behaviour parameter");
68
69 auto event = std::optional<Event>{};
70
71 if constexpr (qrm == QueueReadMode::Blocking)
72 {
73 event = eq.readBlocking(timeout);
74 }
75 else if constexpr (qrm == QueueReadMode::NonBlocking)
76 {
77 event = eq.read();
78 }
79
80 return event;
81 }
82
90 template<QueueReadMode qrm>
91 std::optional<Completion> readCompletionQueue(CompletionQueue& eq, std::chrono::steady_clock::duration timeout)
92 {
93 static_assert((qrm == QueueReadMode::Blocking) || (qrm == QueueReadMode::NonBlocking), "Unsupported queue behaviour parameter");
94
95 auto completion = std::optional<Completion>{};
96
97 if constexpr (qrm == QueueReadMode::Blocking)
98 {
99 completion = eq.readBlocking(timeout);
100 }
101 else if constexpr (qrm == QueueReadMode::NonBlocking)
102 {
103 completion = eq.read();
104 }
105
106 return completion;
107 }
108
116 {
117 try
118 {
119 while (auto event = eq.read())
120 {
121 if (event->isError())
122 {
123 auto const& error = event->error();
124 // The provider code is logged as a number: fi_strerror() would name an unrelated error.
125 MXL_WARN("Event queue error: {} ({}), provider code {}", ::fi_strerror(error.code()), error.code(), error.providerCode());
126 }
127 }
128 }
129 catch (Exception const& ex)
130 {
131 MXL_WARN("Unhandled entry on the event queue: {}", ex.what());
132 }
133 }
134}
#define MXL_WARN(...)
Definition Logging.hpp:56
RAII Wrapper around a libfabric completion queue (fi_cq).
std::optional< Completion > readBlocking(std::chrono::steady_clock::duration timeout)
Perform a blocking read on the completion queue.
std::optional< Completion > read()
Perform a non-blocking read on the completion queue.
RAII Wrapper around a libfabric endpoint (fi_ep).
Definition Endpoint.hpp:29
std::pair< std::optional< Completion >, std::optional< Event > > readQueues()
Do a non-blocking read of both the event and completion queue associated with this endpoint.
Definition Endpoint.cpp:224
std::pair< std::optional< Completion >, std::optional< Event > > readQueuesBlocking(std::chrono::steady_clock::duration timeout)
Do a combined blocking read of both the event and completion queue associated with this endpoint.
Definition Endpoint.cpp:242
RAII Wrapper around a libfabric event queue (fi_eq).
std::optional< Event > readBlocking(std::chrono::steady_clock::duration timeout)
Perform a blocking read on the event queue.
std::optional< Event > read()
Perform a non-blocking read on the event queue. If no event is available in the queue,...
Custom exception type used in the fabrics library.
Definition Exception.hpp:28
char const * what() const noexcept override
Implements std::exception, returns a descriptive string about the error.
Definition Exception.cpp:21
QueueReadMode
Enum to select blocking or non-blocking queue read behaviour.
void drainEventQueue(EventQueue &eq)
Read all entries from a connectionless endpoint's event queue and log the errors among them.
std::optional< Event > readEventQueue(EventQueue &eq, std::chrono::steady_clock::duration timeout)
Helper to read an event queue with blocking or non-blocking behaviour.
std::pair< std::optional< Completion >, std::optional< Event > > readEndpointQueues(Endpoint &ep, std::chrono::steady_clock::duration timeout)
Helper to read both completion and event queues of an endpoint with blocking or non-blocking behaviou...
std::optional< Completion > readCompletionQueue(CompletionQueue &eq, std::chrono::steady_clock::duration timeout)
Helper to read a completion queue with blocking or non-blocking behaviour.