-
Notifications
You must be signed in to change notification settings - Fork 182
/
async_trace.hpp
116 lines (99 loc) · 3.21 KB
/
async_trace.hpp
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed under the Apache License Version 2.0 with LLVM Exceptions
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* https://llvm.org/LICENSE.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <unifex/config.hpp>
#include <unifex/blocking.hpp>
#include <unifex/continuations.hpp>
#include <unifex/coroutine.hpp>
#include <unifex/receiver_concepts.hpp>
#include <unifex/tag_invoke.hpp>
#include <unifex/type_index.hpp>
#include <functional>
#include <vector>
#include <unifex/detail/prologue.hpp>
namespace unifex {
namespace _async_trace {
struct entry {
entry(
std::size_t depth,
std::size_t parentIndex,
const continuation_info& continuation) noexcept
: depth(depth)
, parentIndex(parentIndex)
, continuation(continuation) {}
std::size_t depth;
std::size_t parentIndex;
continuation_info continuation;
};
} // namespace _async_trace
using async_trace_entry = _async_trace::entry;
namespace _async_trace_cpo {
inline const struct _fn {
template <typename Continuation>
std::vector<async_trace_entry> operator()(const Continuation& c) const {
std::vector<async_trace_entry> results;
results.emplace_back(0, 0, continuation_info::from_continuation(c));
// Breadth-first search of async call-stack graph.
for (std::size_t i = 0; i < results.size(); ++i) {
auto [depth, parentIndex, info] = results[i];
visit_continuations(
info, [depth = depth, i, &results](const continuation_info& x) {
results.emplace_back(depth + 1, i, x);
});
}
return results;
}
} async_trace{};
} // namespace _async_trace_cpo
using _async_trace_cpo::async_trace;
namespace _async_trace {
template <typename Receiver>
struct _op {
struct type {
Receiver receiver_;
void start() noexcept {
UNIFEX_TRY {
auto trace = async_trace(receiver_);
unifex::set_value(std::move(receiver_), std::move(trace));
}
UNIFEX_CATCH(...) {
unifex::set_error(std::move(receiver_), std::current_exception());
}
}
};
};
template <typename Receiver>
using operation = typename _op<remove_cvref_t<Receiver>>::type;
struct sender {
template <
template <typename...>
class Variant,
template <typename...>
class Tuple>
using value_types = Variant<Tuple<std::vector<entry>>>;
template <template <typename...> class Variant>
using error_types = Variant<std::exception_ptr>;
static constexpr bool sends_done = false;
static constexpr blocking_kind blocking = blocking_kind::always_inline;
template <typename Receiver>
operation<Receiver> connect(Receiver&& r) const& {
return operation<Receiver>{(Receiver &&) r};
}
};
} // namespace _async_trace
using async_trace_sender = _async_trace::sender;
} // namespace unifex
#include <unifex/detail/epilogue.hpp>