-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforge_accel_request_runtime_example.cpp
More file actions
89 lines (78 loc) · 2.62 KB
/
Copy pathforge_accel_request_runtime_example.cpp
File metadata and controls
89 lines (78 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <forge/accel.hpp>
#include <forge/start_detached.hpp>
#include <forge/wait_result.hpp>
#include "example_support.hpp"
#include <condition_variable>
#include <execution>
#include <mutex>
int main() {
forge::accel::mock::context ctx;
auto device = ctx.get_device();
forge::accel::mock::request_session requests{device.open_session()};
auto sync = forge::wait_result(
requests.submit_request(
21,
0,
[](int& request, int& response) noexcept {
response = request * 2;
}));
forge_example::require(sync.has_value());
auto sync_packet = std::get<0>(std::move(sync.value()));
forge_example::require(sync_packet.id.value == 1);
forge_example::require(sync_packet.response == 42);
std::mutex mtx;
std::condition_variable cv;
bool done = false;
int posted_response = 0;
auto posted = requests.submit_request(
7,
0,
[](int& request, int& response) noexcept {
response = request + 5;
})
| std::execution::then([&](auto packet) noexcept {
{
std::lock_guard lk{mtx};
posted_response = packet.response;
done = true;
}
cv.notify_all();
})
| std::execution::upon_error([&](std::exception_ptr) noexcept {
{
std::lock_guard lk{mtx};
done = true;
}
cv.notify_all();
})
| std::execution::upon_stopped([&] noexcept {
{
std::lock_guard lk{mtx};
done = true;
}
cv.notify_all();
});
forge::start_detached(std::move(posted));
{
std::unique_lock lk{mtx};
cv.wait(lk, [&] { return done; });
}
forge_example::require(posted_response == 12);
forge_example::require(requests.pending_count() == 0);
forge::accel::mock::context failure_ctx;
auto failure_device = failure_ctx.get_device();
forge::accel::mock::request_session failing_requests{
failure_device.open_session()};
failure_device.mark_lost();
auto failure = forge::wait_result(
failing_requests.submit_request_typed(
1,
0,
[](int& request, int& response) noexcept {
response = request;
}));
forge_example::require(failure.has_error());
auto* error = failure.error_if<forge::accel::error>();
forge_example::require(error != nullptr);
forge_example::require(error->kind == forge::accel::error_kind::device_lost);
}