RPNX::QueryGraph
Typed, memoized, concurrent query evaluation for C++23
Loading...
Searching...
No Matches
querygraph.hpp
Go to the documentation of this file.
1// Copyright (c) 2025 Ryan P. Nicholl <rnicholl@protonmail.com>
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14#ifndef NODEGRAPH2_NODEGRAPH_HPP
15#define NODEGRAPH2_NODEGRAPH_HPP
16
25
27#include "rpnx/result.hpp"
28#include "rpnx/typelist.hpp"
29
30#include <algorithm>
31#include <any>
32#include <array>
33#include <atomic>
34#include <cassert>
35#include <chrono>
36#include <condition_variable>
37#include <concepts>
38#include <coroutine>
39#include <cstddef>
40#include <deque>
41#include <exception>
42#include <filesystem>
43#include <format>
44#include <fstream>
45#include <functional>
46#include <future>
47#include <iostream>
48#include <iterator>
49#include <map>
50#include <memory>
51#include <mutex>
52#include <optional>
53#include <print>
54#include <set>
55#include <shared_mutex>
56#include <source_location>
57#include <stdexcept>
58#include <string>
59#include <thread>
60#include <tuple>
61#include <type_traits>
62#include <typeindex>
63#include <variant>
64#include <vector>
65
68#include "rpnx/serialization4.hpp"
69
70#ifdef RPNX_QUERYGRAPH_USE_CONC_UNORDERED_MAP
71#include "rpnx/sharded_unordered_map.hpp"
72#endif
73
74#ifdef NDEBUG
75#define RPNX_DEBUGONLY(x)
76#define RPNX_RELEASEONLY(x) x
77#else
78#define RPNX_DEBUGONLY(x) x
79#define RPNX_RELEASEONLY(x)
80#endif
81
82
83// TODO: Attach estimated memory class to node spec, to allow for scheduling nodes.
84RPNX_ENUM(rpnx::querygraph, memory_class, std::uint8_t, unknown, tiny, small, medium, large, huge);
85
101
102
103namespace rpnx::querygraph
104{
106 using shared_mutex = std::shared_mutex;
107
108 // Win32 std::mutex is rather slow, std::shared_mutex was added later
109 // and is generally faster. However, the improvements couldn't be backported to std::mutex
110 // because it would break ABI compatibility.
111 // On most other platforms, std::mutex is generally faster than std::shared_mutex for exclusive locking.
112#ifdef _WIN32
114 using fast_mutex = std::shared_mutex;
116 using fast_cv = std::condition_variable_any;
117#else
119 using fast_mutex = std::mutex;
121 using fast_cv = std::condition_variable;
122#endif
123
133 template < typename T >
134 concept query_spec_c = requires {
135 typename T::input_type;
136 typename T::output_type;
137 { T::query_id } -> std::convertible_to< std::string >;
138 };
139
149 template < typename T >
150 concept subquery_spec_c = requires {
151 typename T::parent_query;
152 typename T::input_type;
153 typename T::output_type;
155 { T::subquery_id } -> std::convertible_to< std::string >;
156 };
157
158 namespace detail
159 {
160 template < query_spec_c QuerySpec, rpnx::typelist_c Deps, rpnx::typelist_c ProducedSubqueries = rpnx::typelist<> >
161 struct query_handler_spec
162 {
163 using query = QuerySpec;
164 using dependencies = Deps;
165 using produced_subqueries = ProducedSubqueries;
166 };
167 } // namespace detail
168
175 template < typename T >
176 concept query_handler_spec_struct_c = requires {
177 typename T::query;
178 typename T::dependencies;
179 } && query_spec_c< typename T::query > && rpnx::typelist_c< typename T::dependencies >;
180
182 template < typename T >
184 typename T::produced_subqueries;
185 } && rpnx::typelist_c< typename T::produced_subqueries >;
186
191 template < typename T >
193
195 template < typename T >
197
202 template < typename T >
204 {
206 using produced_subqueries = rpnx::typelist<>;
207 };
208
210 template < query_handler_produced_subqueries_struct_c T >
212 {
214 using produced_subqueries = typename T::produced_subqueries;
215 };
216
218 template < query_handler_spec_struct_c T >
220 {
222 using query = typename T::query;
224 using dependencies = typename T::dependencies;
227 };
228
230 template < query_handler_spec_c HandlerSpec >
232
234 template < query_handler_spec_c HandlerSpec >
236
238 template < query_handler_spec_c HandlerSpec >
240
242 template < query_handler_spec_c HandlerSpec >
244
246 template < query_handler_spec_c HandlerSpec >
248
250 template < typename T >
251 struct is_query_handler_spec : std::bool_constant< query_handler_spec_c< T > >
252 {
253 };
254
265 template < typename T >
267 {
273 static std::vector< std::byte > serialize_to_binary(T const& value)
274 {
275 std::vector< std::byte > buffer;
276 rpnx::serial4::serialize_iter(value, std::back_inserter(buffer));
277 return buffer;
278 }
279 };
280
281 template <>
283 struct binary_traits< std::monostate >
284 {
286 static std::vector< std::byte > serialize_to_binary(std::monostate const&)
287 {
288 return {};
289 }
290 };
291
296 template < typename T >
297 concept type_with_to_debug_string = requires(T const& t) {
298 { t.to_debug_string() } -> std::convertible_to< std::string >;
299 };
300
310 template < typename T >
312 {
314 static std::string to_debug_string(T const& value)
315 {
316 if constexpr (type_with_to_debug_string< T >)
317 {
318 return value.to_debug_string();
319 }
320 else if constexpr (std::formattable< T, char >)
321 {
322 return std::format("{}", value);
323 }
324 else
325 {
326 return "<no debug string available>";
327 }
328 }
329 };
330
331 template <>
333 struct debug_traits< std::string >
334 {
336 static std::string to_debug_string(std::string const& value)
337 {
338 return std::format("{:?}", value);
339 }
340 };
341
342 template <>
344 struct debug_traits< std::monostate >
345 {
347 static std::string to_debug_string(std::monostate const&)
348 {
349 return "std::monostate{}";
350 }
351 };
352
361 {
363 std::string message;
365 std::source_location location;
366
367 public:
373 template < typename... Ts >
374 debug_message(std::format_string< Ts... > format_string, Ts&&... args) : message(std::format(format_string, std::forward< Ts >(args)...))
375 {
376 }
377
379 debug_message(debug_message const& other) = default;
380 };
381
389
398 {
404 std::function< void(std::any const&) > m_throw_copy;
406 std::function< std::vector< std::byte >(std::any const&) > m_serialize;
408 std::function< std::string(std::any const&) > m_message;
409
411 void throw_copy() const;
412
414 std::exception_ptr make_exception_ptr() const;
415
417 std::string message() const;
418
421 };
422
429 template < typename Error >
431 {
432 static_assert(std::derived_from< Error, std::exception >, "Canonical errors must derive from std::exception");
433 static_assert(std::copy_constructible< Error >, "Canonical errors must be copy constructible");
434
436 storage.m_error_typesig = rpnx::serial4::get_type_typesig_hash16< Error >();
437 storage.m_error_value.emplace< Error >(error);
438 storage.m_throw_copy = [](std::any const& value)
439 {
440 throw std::any_cast< Error const& >(value);
441 };
442 storage.m_serialize = [](std::any const& value)
443 {
444 return binary_traits< Error >::serialize_to_binary(std::any_cast< Error const& >(value));
445 };
446 storage.m_message = [](std::any const& value)
447 {
448 return std::string(std::any_cast< Error const& >(value).what());
449 };
450 return storage;
451 }
452
461 {
462 std::optional< canonical_error_storage > m_canonical_error;
463 std::exception_ptr m_unexpected_exception;
464
465 public:
467 error_result() = default;
468
471
473 static error_result unexpected(std::exception_ptr exception);
474
476 bool has_error() const;
477
480
483
486
488 std::exception_ptr unexpected_exception() const;
489
491 std::exception_ptr make_exception_ptr() const;
492
494 void throw_error() const;
495
497 std::string message() const;
498
500 std::optional< canonical_error_result_data > canonical_dump() const;
501 };
502
512 template < typename T >
513 class result
514 {
515 std::optional< T > m_value;
516 error_result m_error;
517
518 public:
520 result() = default;
521
523 result(T value) : m_value(std::move(value))
524 {
525 }
526
528 result(std::exception_ptr exception) : m_error(error_result::unexpected(exception))
529 {
530 }
531
533 void test() const
534 {
535 if (m_error.has_error())
536 {
537 m_error.throw_error();
538 }
539 }
540
542 T& get() &
543 {
544 test();
545 assert(m_value.has_value());
546 return m_value.value();
547 }
548
550 T const& get() const&
551 {
552 test();
553 assert(m_value.has_value());
554 return m_value.value();
555 }
556
558 T&& get() &&
559 {
560 test();
561 assert(m_value.has_value());
562 return std::move(m_value.value());
563 }
564
566 T const&& get() const&&
567 {
568 test();
569 assert(m_value.has_value());
570 return std::move(m_value.value());
571 }
572
574 T& value() &
575 {
576 return get();
577 }
578
580 T const& value() const&
581 {
582 return get();
583 }
584
586 T&& value() &&
587 {
588 return std::move(*this).get();
589 }
590
592 T const&& value() const&&
593 {
594 return std::move(*this).get();
595 }
596
599 {
600 m_error = error_result();
601 m_value = std::move(value);
602 }
603
606 {
607 m_value.reset();
608 m_error = std::move(error);
609 }
610
612 void set_error(std::exception_ptr exception)
613 {
615 }
616
618 void set_exception(std::exception_ptr exception)
619 {
620 set_error(exception);
621 }
622
624 bool has_result() const
625 {
626 return m_value.has_value() || m_error.has_error();
627 }
628
630 bool has_value() const
631 {
632 return m_value.has_value();
633 }
634
636 bool has_error() const
637 {
638 return m_error.has_error();
639 }
640
642 bool has_exception() const
643 {
644 return has_error();
645 }
646
649 {
650 return m_error.has_canonical_error();
651 }
652
655 {
656 return m_error.has_unexpected_exception();
657 }
658
661 {
662 return m_error;
663 }
664
666 std::exception_ptr get_error() const
667 {
668 return m_error.make_exception_ptr();
669 }
670
673 {
674 if (m_value.has_value())
675 {
676 return status::completed;
677 }
678 return m_error.result_status();
679 }
680
682 explicit operator bool() const
683 {
684 return has_result();
685 }
686 };
687
689 template <>
690 class result< void >
691 {
692 bool m_has_value = false;
693 error_result m_error;
694
695 public:
697 result() = default;
698
700 result(std::exception_ptr exception) : m_error(error_result::unexpected(exception))
701 {
702 }
703
705 void test() const
706 {
707 if (m_error.has_error())
708 {
709 m_error.throw_error();
710 }
711 }
712
714 void get() const
715 {
716 test();
717 if (!m_has_value)
718 {
719 throw std::logic_error("No value");
720 }
721 }
722
724 void value() const
725 {
726 get();
727 }
728
731 {
732 m_error = error_result();
733 m_has_value = true;
734 }
735
738 {
739 m_has_value = false;
740 m_error = std::move(error);
741 }
742
744 void set_error(std::exception_ptr exception)
745 {
747 }
748
750 void set_exception(std::exception_ptr exception)
751 {
752 set_error(exception);
753 }
754
756 bool has_result() const
757 {
758 return m_has_value || m_error.has_error();
759 }
760
762 bool has_value() const
763 {
764 return m_has_value;
765 }
766
768 bool has_error() const
769 {
770 return m_error.has_error();
771 }
772
774 bool has_exception() const
775 {
776 return has_error();
777 }
778
781 {
782 return m_error.has_canonical_error();
783 }
784
787 {
788 return m_error.has_unexpected_exception();
789 }
790
793 {
794 return m_error;
795 }
796
798 std::exception_ptr get_error() const
799 {
800 return m_error.make_exception_ptr();
801 }
802
805 {
806 if (m_has_value)
807 {
808 return status::completed;
809 }
810 return m_error.result_status();
811 }
812
814 explicit operator bool() const
815 {
816 return has_result();
817 }
818 };
819
820 inline namespace RPNX_DEBUGONLY(debug_abi) RPNX_RELEASEONLY(release_abi)
821 {
822 class graph;
823 class scheduler;
824
835 template < query_spec_c QuerySpec >
836 struct request
837 {
839 typename QuerySpec::input_type m_input;
840
841 public:
843 request(typename QuerySpec::input_type input) : m_input(std::move(input))
844 {
845 }
846 };
847
856 template < query_spec_c QuerySpec >
858 {
861
862 public:
867 };
868
878 template < subquery_spec_c SubquerySpec >
880 {
882 typename SubquerySpec::parent_query::input_type m_parent_input;
884 typename SubquerySpec::input_type m_input;
885
886 public:
892 subquery_request(typename SubquerySpec::parent_query::input_type parent_input, typename SubquerySpec::input_type input) : m_parent_input(std::move(parent_input)), m_input(std::move(input))
893 {
894 }
895 };
896
906 template < subquery_spec_c SubquerySpec >
908 {
910 typename SubquerySpec::input_type m_input;
912 typename SubquerySpec::output_type m_output;
913
914 public:
916 subquery_result(typename SubquerySpec::input_type input, typename SubquerySpec::output_type output) : m_input(std::move(input)), m_output(std::move(output))
917 {
918 }
919 };
920
927 class subquery_does_not_exist : public std::logic_error
928 {
929 std::string m_subquery_id;
930 std::string m_message;
931
932 public:
933 RPNX_MEMBER_METADATA(subquery_does_not_exist, m_subquery_id);
934
936 subquery_does_not_exist() : std::logic_error("")
937 {
939 }
940
942 explicit subquery_does_not_exist(std::string subquery_id) : std::logic_error(""), m_subquery_id(std::move(subquery_id))
943 {
945 }
946
948 std::string const& subquery_id() const noexcept
949 {
950 return m_subquery_id;
951 }
952
955 {
956 m_message = std::format("Subquery does not exist: {}", m_subquery_id);
957 }
958
960 char const* what() const noexcept override
961 {
962 return m_message.c_str();
963 }
964 };
965
972 class subquery_parent_failed : public std::runtime_error
973 {
974 std::string m_subquery_id;
975
976 public:
977 RPNX_MEMBER_METADATA(subquery_parent_failed, m_subquery_id);
978
980 explicit subquery_parent_failed(std::string subquery_id) : std::runtime_error(std::format("Parent query failed before producing subquery: {}", subquery_id)), m_subquery_id(std::move(subquery_id))
981 {
982 }
983
985 std::string const& subquery_id() const noexcept
986 {
987 return m_subquery_id;
988 }
989 };
990
997 class recursive_dependency_error : public std::logic_error
998 {
999 public:
1000 recursive_dependency_error() : std::logic_error("recursive dependency")
1001 {
1002 }
1003 };
1004
1011 class bad_continuation : public std::logic_error
1012 {
1013 public:
1014 bad_continuation() : std::logic_error("no continuation")
1015 {
1016 }
1017 };
1018
1019 template < typename Result >
1020 struct query_result;
1021
1022 template < subquery_spec_c SubquerySpec >
1023 struct subquery_query_result;
1024
1025 template < query_handler_spec_c HandlerSpec >
1026 struct coroutine;
1027
1029 template < query_handler_spec_c HandlerSpec, typename Ret >
1030 class cosubroutine_impl;
1032
1049 class graph
1050 {
1051
1052 public:
1054 struct options
1055 {
1057 bool enable_timing = true;
1059 std::chrono::nanoseconds long_running_node_threshold = std::chrono::milliseconds(100);
1064 std::chrono::nanoseconds long_running_progress_threshold = std::chrono::seconds(1);
1072 std::function< void(std::string const&) > timing_logger = [](std::string const& message)
1073 {
1074 std::cerr << message;
1075 };
1076 };
1077
1078 private:
1079 bool m_enable_node_debug_io_capture = false;
1080 bool m_enable_node_serialization = true;
1081 scheduler& m_scheduler;
1082 options m_options;
1084 std::size_t m_active_groups = 0;
1086 std::size_t m_queued_work = 0;
1087 fast_cv m_quiescence_cv;
1088 std::mutex m_descriptor_mutex;
1089 std::mutex m_diagnostic_mutex;
1090
1091 static constexpr std::size_t long_running_input_max_length = 200;
1092
1093 public:
1095 explicit graph(scheduler& execution_scheduler);
1096
1104 graph(scheduler& execution_scheduler, options configuration);
1105
1111
1113 struct promise_base;
1114 struct query_node;
1115
1116 struct resumption
1117 {
1118 promise_base* m_promise = nullptr;
1119 std::coroutine_handle< void > handle;
1120 };
1121
1122 private:
1123 template < typename Result >
1124 friend struct query_result;
1125 template < subquery_spec_c SubquerySpec >
1126 friend struct subquery_query_result;
1127 template < query_handler_spec_c HandlerSpec >
1128 friend struct coroutine;
1129 template < query_handler_spec_c HandlerSpec, typename Ret >
1130 friend class cosubroutine_impl;
1131
1132 friend class scheduler;
1133
1135 struct alignas(std::hardware_destructive_interference_size) worker
1136 {
1137 scheduler* m_scheduler = nullptr;
1139 graph* m_graph = nullptr;
1140 RPNX_DEBUGONLY(std::thread::id m_id);
1141 fast_mutex m_mutex;
1142 std::deque< resumption > m_runnables;
1143 RPNX_DEBUGONLY(bool m_idle = false;)
1144
1145 static constexpr std::size_t workgroup_threshold = 128;
1146
1147 worker(scheduler* scheduler);
1148
1149 worker(worker const& other) = delete;
1150
1151 void check_legal_access();
1152
1153 void run();
1154
1155 void declare_runnable(std::coroutine_handle< void > h, promise_base* ptr, bool immediate);
1156
1157 void declare_runnable(resumption r, bool immediate);
1158
1159 bool try_exec_one();
1160
1161 std::vector< resumption > try_steal(std::size_t max_items);
1162 };
1163
1165 class submission_scope
1166 {
1167 graph& m_graph;
1168 worker& m_worker;
1169
1170 public:
1172 submission_scope(graph& owning_graph, worker& submitting_worker);
1174 ~submission_scope();
1176 submission_scope(submission_scope const&) = delete;
1178 submission_scope& operator=(submission_scope const&) = delete;
1179 };
1180
1182 std::mutex m_request_mutex;
1184 std::map< query_node*, std::function< void(worker*, std::exception_ptr) > > m_pending_requests;
1185
1187 template < typename Output >
1188 void track_request(query_node* node)
1189 {
1190 std::unique_lock node_lock(node->m_mutex);
1191 if (std::any_cast< result< Output > const& >(node->m_result).has_result())
1192 {
1193 return;
1194 }
1195 std::unique_lock request_lock(m_request_mutex);
1196 m_pending_requests.try_emplace(node, [this, node](worker* current_worker, std::exception_ptr exception)
1197 {
1198 abort_query_node_with_unexpected_exception< Output >(node, current_worker, exception);
1199 });
1200 node->m_external_request = true;
1201 }
1202
1204 void publish_work(worker& submitting_worker);
1205
1207 void wait_for_quiescence();
1208
1210 template < query_spec_c QuerySpec >
1211 auto execute_request(typename QuerySpec::input_type const& input) -> query_result< typename QuerySpec::output_type >
1212 {
1213 worker submitting_worker(&m_scheduler);
1214 query_result< typename QuerySpec::output_type > query_result = [&]()
1215 {
1216 submission_scope submission(*this, submitting_worker);
1217 query_descriptor< QuerySpec >& desc = get_query_descriptor< QuerySpec >();
1218 rpnx::querygraph::query_result< typename QuerySpec::output_type > requested = desc.m_get(&submitting_worker, input, false);
1219 track_request< typename QuerySpec::output_type >(requested.m_node);
1220 return requested;
1221 }();
1222 if (query_result.started_by_current_request())
1223 {
1224 wait_for_quiescence();
1225 }
1226 query_result.wait();
1227 return query_result;
1228 }
1230
1231 public:
1250 template < query_spec_c QuerySpec >
1251 auto make_request(typename QuerySpec::input_type input) -> typename QuerySpec::output_type
1252 {
1253 query_result< typename QuerySpec::output_type > query_result = execute_request< QuerySpec >(input);
1254 return query_result.await_resume();
1255 }
1256
1273 template < query_spec_c QuerySpec >
1274 auto async_make_request(typename QuerySpec::input_type input) -> std::future< typename QuerySpec::output_type >
1275 {
1276 worker submitting_worker(&m_scheduler);
1277 submission_scope submission(*this, submitting_worker);
1278 query_descriptor< QuerySpec >& desc = get_query_descriptor< QuerySpec >();
1279 query_result< typename QuerySpec::output_type > requested = desc.m_get(&submitting_worker, input, false);
1280 track_request< typename QuerySpec::output_type >(requested.m_node);
1281
1282 std::promise< typename QuerySpec::output_type > promise;
1283 std::future< typename QuerySpec::output_type > future = promise.get_future();
1284 std::unique_lock node_lock(requested.m_node->m_mutex);
1285 result< typename QuerySpec::output_type > const& res = std::any_cast< result< typename QuerySpec::output_type > const& >(requested.m_node->m_result);
1286 if (res.has_result())
1287 {
1288 node_lock.unlock();
1289 try
1290 {
1291 promise.set_value(res.value());
1292 }
1293 catch (...)
1294 {
1295 promise.set_exception(std::current_exception());
1296 }
1297 }
1298 else
1299 {
1300 if (!requested.m_node->m_request_completion)
1301 {
1302 requested.m_node->m_request_completion = std::make_unique< typed_request_completion< typename QuerySpec::output_type > >();
1303 }
1304 typed_request_completion< typename QuerySpec::output_type >& completion = static_cast< typed_request_completion< typename QuerySpec::output_type >& >(*requested.m_node->m_request_completion);
1305 completion.m_promises.push_back(std::move(promise));
1306 }
1307 return future;
1308 }
1309
1324 template < subquery_spec_c SubquerySpec >
1325 auto make_subquery_request(typename SubquerySpec::parent_query::input_type parent_input, typename SubquerySpec::input_type input) -> typename SubquerySpec::output_type
1326 {
1327 worker submitting_worker(&m_scheduler);
1328 subquery_query_result< SubquerySpec > query_result = [&]()
1329 {
1330 submission_scope submission(*this, submitting_worker);
1331 {
1332 std::unique_lock lock(m_descriptor_mutex);
1333 register_subquery_descriptors< SubquerySpec >();
1334 }
1335 query_descriptor< typename SubquerySpec::parent_query >& desc = get_query_descriptor< typename SubquerySpec::parent_query >();
1336 rpnx::querygraph::query_result< typename SubquerySpec::parent_query::output_type > parent_result = desc.m_get(&submitting_worker, parent_input, false);
1337 track_request< typename SubquerySpec::parent_query::output_type >(parent_result.m_node);
1338 return get_subquery_result< SubquerySpec >(&submitting_worker, parent_result.m_node, input);
1339 }();
1340 query_result.wait();
1341 return query_result.await_resume();
1342 }
1343
1358 template < query_spec_c QuerySpec >
1359 auto dump_query_to_file(std::filesystem::path output_path, typename QuerySpec::input_type input) -> std::filesystem::path
1360 {
1361 query_result< typename QuerySpec::output_type > query_result = execute_request< QuerySpec >(input);
1362
1363 std::vector< std::byte > const marshaled_dump = marshall(query_result.m_node);
1364 std::ofstream out(output_path, std::ios::binary | std::ios::trunc);
1365 if (!out)
1366 {
1367 throw std::runtime_error(std::format("Failed to open dump file for writing: {}", output_path.string()));
1368 }
1369
1370 if (!marshaled_dump.empty())
1371 {
1372 out.write(reinterpret_cast< char const* >(marshaled_dump.data()), static_cast< std::streamsize >(marshaled_dump.size()));
1373 }
1374 if (!out)
1375 {
1376 throw std::runtime_error(std::format("Failed to write dump file: {}", output_path.string()));
1377 }
1378
1379 return output_path;
1380 }
1381
1382 // struct handler_spec_typeinfo
1383 // {
1384 // std::type_index question_type;
1385 // std::vector< std::type_index > dependencies;
1386 // };
1387
1388 // bindable_type_descriptor is a runtime type descriptor
1389 // for types that can be used as inputs or outputs
1390 // to queries.
1391 // struct bindable_type_descriptor
1392 // {
1393 // std::function< std::vector< std::byte >(std::any const&) > serialize_binary;
1394 // std::function< void(std::any&, std::vector< std::byte > const&) > deserialize_binary;
1395 // std::function< std::string(std::any const&) > to_string;
1396 // };
1397
1398 // struct handler_entry_any
1399 // {
1400 // std::type_index type;
1401 // std::any handler;
1402 // };
1403
1405 template < subquery_spec_c SubquerySpec >
1406 struct subquery_slot
1407 {
1408 std::any m_input;
1409 std::any m_result;
1410 std::string m_input_string;
1411 std::string m_output_string;
1412 std::vector< resumption > waiters;
1413 };
1414
1415 template < subquery_spec_c SubquerySpec >
1416 struct subquery_state
1417 {
1418 std::map< typename SubquerySpec::input_type, subquery_slot< SubquerySpec > > m_instances;
1419 };
1420
1421 struct subquery_state_any
1422 {
1423 std::any m_state;
1424 std::type_index m_input_type = std::type_index(typeid(void));
1425 std::type_index m_output_type = std::type_index(typeid(void));
1426 std::string m_subquery_id;
1427 std::function< std::vector< resumption >(std::any&) > m_finalize_missing;
1428 std::function< std::vector< resumption >(std::any&, error_result const&) > m_finalize_parent_failed;
1429 std::function< void(std::any&, std::function< void(std::any&, std::any&) >) > m_iterate_all;
1430 };
1431
1433 struct request_completion
1434 {
1436 virtual ~request_completion() = default;
1438 virtual void complete(std::any const& stored_result) = 0;
1439 };
1440
1442 template < typename Output >
1443 struct typed_request_completion : request_completion
1444 {
1446 std::vector< std::promise< Output > > m_promises;
1447
1449 void complete(std::any const& stored_result) override
1450 {
1451 result< Output > const& res = std::any_cast< result< Output > const& >(stored_result);
1452 for (std::promise< Output >& promise : m_promises)
1453 {
1454 try
1455 {
1456 promise.set_value(res.value());
1457 }
1458 catch (...)
1459 {
1460 promise.set_exception(std::current_exception());
1461 }
1462 }
1463 }
1464 };
1465
1466 struct query_node
1467 {
1468 std::shared_mutex m_mutex;
1469 std::condition_variable_any m_external_cv;
1470
1471 std::type_index m_query_type = typeid(void);
1472 std::string m_input_string;
1473 std::string m_output_string;
1474
1475 // Nodes store a pointer to their own input so the input can be reached from dependency pointers without doing a lookup.
1476 std::any m_input;
1477 // m_result stores the output of the node as a result<T> object where T is the output type.
1478 std::any m_result;
1480 std::any m_cort;
1481 std::vector< resumption > waiters;
1483 std::unique_ptr< request_completion > m_request_completion;
1485 bool m_external_request = false;
1486 std::vector< debug_message > m_messages;
1487 std::set< query_node* > dependencies;
1488 std::set< query_node* > dependents;
1489 std::map< std::type_index, subquery_state_any > m_subquery_states;
1490 std::atomic< std::chrono::nanoseconds::rep > m_self_time = 0;
1491 std::atomic< bool > m_long_running_threshold_reported = false;
1492 std::atomic< bool > m_long_running_completion_reported = false;
1493 std::atomic< bool > m_finished = false;
1494
1495 void unblock_waiters(worker* worker);
1496
1497 void unblock_waiters(worker* worker, std::unique_lock< std::shared_mutex >& lock);
1498
1499 void abort_waiters(worker* worker, std::unique_lock< std::shared_mutex >& lock, std::exception_ptr exception);
1500 };
1501
1502 static void declare_dependency(query_node* dependent, query_node* dependency);
1503
1504 template < query_spec_c HandlerSpec >
1505 struct query_descriptor;
1506
1507 struct promise_base
1508 {
1510 graph* m_graph = nullptr;
1511 worker* m_worker = nullptr;
1512 query_node* m_node = nullptr;
1513 std::mutex m_run_mutex;
1515 std::any* m_coroutine_storage = nullptr;
1517 virtual std::string query_name() const = 0;
1519 virtual std::string input_text() const = 0;
1520 virtual void abort_with_unexpected_exception(worker* worker, std::exception_ptr exception) = 0;
1521 virtual ~promise_base() = default;
1522 };
1523
1524 template < query_spec_c QuerySpec >
1525 struct query_descriptor
1526 {
1527 std::function< query_result< typename QuerySpec::output_type >(worker*, typename QuerySpec::input_type const&, bool) > m_get;
1528 std::any m_query_handler_impl;
1529 };
1530
1531 struct query_descriptor_any
1532 {
1533 std::function< void(std::function< void(query_node& node) >) > m_iterate_all;
1534 std::type_index m_input_type = std::type_index(typeid(void));
1535 std::type_index m_output_type = std::type_index(typeid(void));
1536 std::string m_query_id;
1537 };
1538
1539 template < typename T >
1540 struct dependency_descriptor_type;
1541
1542 template < query_spec_c QuerySpec >
1543 struct dependency_descriptor_type< QuerySpec >
1544 {
1545 using type = query_descriptor< QuerySpec >*;
1546 };
1547
1548 template < subquery_spec_c SubquerySpec >
1549 struct dependency_descriptor_type< SubquerySpec >
1550 {
1551 using type = query_descriptor< typename SubquerySpec::parent_query >*;
1552 };
1553
1554 template < typename T >
1555 struct dependency_descriptor_table;
1556
1557 template < typename... Ts >
1558 struct dependency_descriptor_table< rpnx::typelist< Ts... > >
1559 {
1560 using type = std::tuple< typename dependency_descriptor_type< Ts >::type... >;
1561 };
1562
1563 template < rpnx::typelist_c Typelist >
1564 using dependency_descriptor_table_from_typelist = typename dependency_descriptor_table< Typelist >::type;
1565
1566 template < typename T >
1567 std::string datavalue_to_string(T const& input)
1568 {
1569 auto desc = this->m_text_descriptors.get_or_create(std::type_index(typeid(T)),
1570 []()
1571 {
1572 return text_descriptor{};
1573 });
1574
1575 if (desc.m_to_string == nullptr)
1576 {
1577 return "<no to_string available>";
1578 }
1579
1580 return desc.m_to_string(std::any(input), std::any());
1581 }
1582
1583 template < query_handler_spec_c HandlerSpec >
1584 struct query_handler_descriptor_memoized_impl
1585 {
1586#ifdef RPNX_QUERYGRAPH_USE_CONC_UNORDERED_MAP
1587 rpnx::conc_sharded_unordered_map< query_handler_input_t< HandlerSpec >, query_node, hasher< query_handler_input_t< HandlerSpec > > > m_mt_cache;
1588#else
1589 std::shared_mutex mutex;
1590 std::map< query_handler_input_t< HandlerSpec >, query_node > cache;
1591#endif
1592
1593 graph* m_graph = nullptr;
1594 query_descriptor< query_handler_query_t< HandlerSpec > >* m_handler_entry = nullptr;
1595
1596 dependency_descriptor_table_from_typelist< query_handler_dependencies_t< HandlerSpec > > dependency_descriptors = {};
1597
1598 std::function< coroutine< HandlerSpec >(query_handler_input_t< HandlerSpec > const&) > m_startup;
1599
1600#ifdef RPNX_QUERYGRAPH_USE_CONC_UNORDERED_MAP
1601 query_result< query_handler_output_t< HandlerSpec > > get(worker* current_worker, query_handler_input_t< HandlerSpec > const& input_ask, bool immediate)
1602 {
1603 std::coroutine_handle< void > cr;
1604 bool started = false;
1605 promise_base* pbase = nullptr;
1606 auto& node = m_mt_cache.get_or_init_iter(input_ask,
1607 [&](query_handler_input_t< HandlerSpec > const& key, query_node& node)
1608 {
1609 // Identify the query type for the node so UI shows a proper name.
1610 node.m_query_type = std::type_index(typeid(query_handler_query_t< HandlerSpec >));
1611
1612 node.m_input.emplace< query_handler_input_t< HandlerSpec > const* >(&key);
1613
1614 if (m_graph->m_enable_node_debug_io_capture)
1615 {
1616 node.m_input_string = m_graph->datavalue_to_string(input_ask);
1617 }
1618 coroutine< HandlerSpec > co = m_startup(key);
1619 node.m_result = result< query_handler_output_t< HandlerSpec > >();
1620
1621 co.m_promise->m_graph = m_graph;
1622 co.m_promise->m_node = &node;
1623 co.m_promise->m_coroutine_storage = &node.m_cort;
1624 co.m_promise->m_handler_entry = m_handler_entry;
1625 cr = std::coroutine_handle< typename coroutine< HandlerSpec >::promise_type >::from_promise(*co.m_promise);
1626 pbase = co.m_promise;
1627 node.m_cort = std::make_shared< coroutine< HandlerSpec > >(std::move(co));
1628
1629 started = true;
1630 });
1631 if (started)
1632 {
1633 current_worker->declare_runnable(cr, pbase, immediate);
1634 }
1635
1636 return query_result< query_handler_output_t< HandlerSpec > >(&node, started);
1637 }
1638
1639 void iterate_all(std::function< void(query_node& node) > func)
1640 {
1641 auto iter = m_mt_cache.range_exclusive();
1642 for (auto it = iter.begin(); it != iter.end(); ++it)
1643 {
1644 func(it->second);
1645 }
1646 }
1647#else
1648
1649 query_result< query_handler_output_t< HandlerSpec > > get(worker* current_worker, query_handler_input_t< HandlerSpec > const& input_ask, bool immediate)
1650 {
1651 std::shared_lock lock(mutex);
1652 typename std::map< query_handler_input_t< HandlerSpec >, query_node >::iterator it = cache.find(input_ask);
1653 if (it != cache.end())
1654 {
1655 return query_result< query_handler_output_t< HandlerSpec > >(&it->second, false);
1656 }
1657 lock.unlock();
1658 std::unique_lock ulock(mutex);
1659 // Double-check pattern
1660 it = cache.find(input_ask);
1661 if (it != cache.end())
1662 {
1663 return query_result< query_handler_output_t< HandlerSpec > >(&it->second, false);
1664 }
1665 auto [node_it, inserted] = cache.try_emplace(input_ask);
1666 assert(inserted);
1667 coroutine< HandlerSpec > co = m_startup(node_it->first);
1668 query_node& node = node_it->second;
1669 // Identify the query type and record input string (for debug UI) similarly to the concurrent path.
1670 node.m_query_type = std::type_index(typeid(query_handler_query_t< HandlerSpec >));
1671 node.m_input.emplace< query_handler_input_t< HandlerSpec > const* >(&node_it->first);
1672 if (m_graph->m_enable_node_debug_io_capture)
1673 {
1674 node.m_input_string = m_graph->datavalue_to_string(input_ask);
1675 }
1676 node.m_result = result< query_handler_output_t< HandlerSpec > >();
1677 co.m_promise->m_graph = m_graph;
1678 co.m_promise->m_node = &node;
1679 co.m_promise->m_coroutine_storage = &node.m_cort;
1680 co.m_promise->m_handler_entry = m_handler_entry;
1681 std::coroutine_handle< void > cr = std::coroutine_handle< typename coroutine< HandlerSpec >::promise_type >::from_promise(*co.m_promise);
1682 promise_base* pbase = co.m_promise;
1683 node.m_cort = std::make_shared< coroutine< HandlerSpec > >(std::move(co));
1684 // ulock.unlock();
1685 current_worker->declare_runnable(cr, pbase, immediate);
1686 return query_result< query_handler_output_t< HandlerSpec > >(&node, true);
1687 }
1688#endif
1689 template < query_spec_c QuerySpec >
1690 auto get_dependency_descriptor() -> query_descriptor< QuerySpec >&
1691 {
1692 static_assert(query_handler_dependencies_t< HandlerSpec >::template contains< QuerySpec >(), "Failed to resolve index of dependency descriptor: QuerySpec is not listed in the HandlerSpec dependencies, no entry in dependency_descriptors exists");
1693
1694 constexpr std::size_t index = *query_handler_dependencies_t< HandlerSpec >::template index_of< QuerySpec >();
1695
1696 // Descriptor dependencies should be set during the binding stage, which should occur before
1697 // any queries are executed
1698 return *std::get< index >(dependency_descriptors);
1699 }
1700
1701 template < subquery_spec_c SubquerySpec >
1702 auto get_subquery_parent_descriptor() -> query_descriptor< typename SubquerySpec::parent_query >&
1703 {
1704 static_assert(query_handler_dependencies_t< HandlerSpec >::template contains< SubquerySpec >(), "Failed to resolve index of subquery descriptor: SubquerySpec is not listed in HandlerSpec dependencies");
1705 static_assert(query_handler_dependencies_t< HandlerSpec >::template contains< typename SubquerySpec::parent_query >(), "Making a subquery request requires declaring the subquery's parent query in HandlerSpec dependencies");
1706
1707 constexpr std::size_t index = *query_handler_dependencies_t< HandlerSpec >::template index_of< SubquerySpec >();
1708
1709 return *std::get< index >(dependency_descriptors);
1710 }
1711
1712 void bind(graph* graph)
1713 {
1714 bind_spec< 0 >(graph);
1715 }
1716
1717 template < size_t N >
1718 void bind_spec(graph* graph)
1719 {
1720 if constexpr (N < query_handler_dependencies_t< HandlerSpec >::size)
1721 {
1722 using dep = query_handler_dependencies_t< HandlerSpec >::template type_at< N >;
1723 if constexpr (query_spec_c< dep >)
1724 {
1725 query_descriptor< dep >& dep_descriptor = graph->get_query_descriptor< dep >();
1726 std::get< N >(dependency_descriptors) = &dep_descriptor;
1727 }
1728 else if constexpr (subquery_spec_c< dep >)
1729 {
1730 static_assert(query_handler_dependencies_t< HandlerSpec >::template contains< typename dep::parent_query >(), "A handler that depends on a subquery must also list the subquery's parent query in HandlerSpec dependencies");
1731 query_descriptor< typename dep::parent_query >& dep_descriptor = graph->get_query_descriptor< typename dep::parent_query >();
1732 std::get< N >(dependency_descriptors) = &dep_descriptor;
1733 }
1734 bind_spec< N + 1 >(graph);
1735 }
1736 }
1737 };
1738
1739 struct text_descriptor
1740 {
1741 std::function< std::string(std::any, std::any) > m_to_string;
1742 };
1743
1744 struct binary_descriptor
1745 {
1746 struct result_binary_data
1747 {
1748 std::vector< std::byte > m_output_data;
1749 status m_status = status::pending;
1750 std::optional< canonical_error_result_data > m_error_result;
1751 };
1752
1753 std::function< std::vector< std::byte >(std::any&) > m_serialize_from_cptr;
1754 std::function< result_binary_data(std::any&) > m_serialize_from_result;
1755
1756 std::function< std::any(std::vector< std::byte > const&) > m_deserialize;
1757
1758 std::array< std::byte, 16 > m_typesig;
1759 };
1760
1761 struct canonical_error_descriptor
1762 {
1763 std::function< error_result(std::exception const&) > m_capture;
1764 };
1765
1766 std::map< std::type_index, std::any > query_handler_registry;
1767 std::map< std::type_index, query_descriptor_any > query_descriptor_any_registry;
1768 std::vector< std::function< void(graph*) > > m_pending_binds;
1769 rpnx::conc_sharded_unordered_map< std::type_index, text_descriptor, hasher< std::type_index > > m_text_descriptors;
1770 std::map< std::type_index, binary_descriptor > m_binary_descriptors;
1771 std::map< std::type_index, canonical_error_descriptor > m_canonical_error_descriptors;
1772 std::map< std::type_index, std::string > m_query_names;
1773
1775 auto long_running_node_description(promise_base* promise) -> std::string;
1776
1778 void log_long_running_node_progress_diagnostic(promise_base* promise);
1779
1781 void log_long_running_node_completion_diagnostic(promise_base* promise, std::chrono::nanoseconds self_time);
1782
1784 void print_noncanonical_exception_diagnostic(promise_base* promise, error_result const& error);
1785
1786 static void declare_resumptions(worker* worker, std::vector< resumption > waiters);
1787
1788 static void abort_resumptions(worker* worker, std::vector< resumption > waiters, std::exception_ptr exception);
1789
1790 template < subquery_spec_c SubquerySpec >
1791 static std::vector< resumption > take_subquery_waiters(subquery_slot< SubquerySpec >& slot)
1792 {
1793 std::vector< resumption > waiters;
1794 waiters.assign(std::make_move_iterator(slot.waiters.begin()), std::make_move_iterator(slot.waiters.end()));
1795 slot.waiters.clear();
1796 return waiters;
1797 }
1798
1799 static canonical_error_storage make_subquery_parent_failed_storage(std::string subquery_id, error_result parent_error);
1800
1801 static error_result make_subquery_does_not_exist_error(std::string subquery_id);
1802
1803 static error_result make_subquery_parent_failed_error(std::string subquery_id, error_result const& parent_error);
1804
1805 template < subquery_spec_c SubquerySpec >
1806 static auto make_subquery_state_any() -> subquery_state_any
1807 {
1808 subquery_state_any state_any;
1809 state_any.m_state.emplace< subquery_state< SubquerySpec > >();
1810 state_any.m_input_type = std::type_index(typeid(typename SubquerySpec::input_type));
1811 state_any.m_output_type = std::type_index(typeid(typename SubquerySpec::output_type));
1812 state_any.m_subquery_id = std::string(SubquerySpec::subquery_id);
1813 state_any.m_finalize_missing = [](std::any& state_value) -> std::vector< resumption >
1814 {
1815 std::vector< resumption > waiters;
1816 subquery_state< SubquerySpec >& state = std::any_cast< subquery_state< SubquerySpec >& >(state_value);
1817 for (auto& [input, slot] : state.m_instances)
1818 {
1819 result< typename SubquerySpec::output_type >& res = std::any_cast< result< typename SubquerySpec::output_type >& >(slot.m_result);
1820 if (!res.has_result())
1821 {
1822 res.set_error(make_subquery_does_not_exist_error(std::string(SubquerySpec::subquery_id)));
1823 std::vector< resumption > slot_waiters = take_subquery_waiters< SubquerySpec >(slot);
1824 waiters.insert(waiters.end(), std::make_move_iterator(slot_waiters.begin()), std::make_move_iterator(slot_waiters.end()));
1825 }
1826 }
1827 return waiters;
1828 };
1829 state_any.m_finalize_parent_failed = [](std::any& state_value, error_result const& parent_error) -> std::vector< resumption >
1830 {
1831 std::vector< resumption > waiters;
1832 subquery_state< SubquerySpec >& state = std::any_cast< subquery_state< SubquerySpec >& >(state_value);
1833 for (auto& [input, slot] : state.m_instances)
1834 {
1835 result< typename SubquerySpec::output_type >& res = std::any_cast< result< typename SubquerySpec::output_type >& >(slot.m_result);
1836 if (!res.has_result())
1837 {
1838 res.set_error(make_subquery_parent_failed_error(std::string(SubquerySpec::subquery_id), parent_error));
1839 std::vector< resumption > slot_waiters = take_subquery_waiters< SubquerySpec >(slot);
1840 waiters.insert(waiters.end(), std::make_move_iterator(slot_waiters.begin()), std::make_move_iterator(slot_waiters.end()));
1841 }
1842 }
1843 return waiters;
1844 };
1845 state_any.m_iterate_all = [](std::any& state_value, std::function< void(std::any&, std::any&) > callback)
1846 {
1847 subquery_state< SubquerySpec >& state = std::any_cast< subquery_state< SubquerySpec >& >(state_value);
1848 for (auto& [input, slot] : state.m_instances)
1849 {
1850 callback(slot.m_input, slot.m_result);
1851 }
1852 };
1853 return state_any;
1854 }
1855
1856 template < subquery_spec_c SubquerySpec >
1857 auto get_or_create_subquery_slot_locked(query_node* parent_node, typename SubquerySpec::input_type const& input) -> subquery_slot< SubquerySpec >&
1858 {
1859 std::type_index const state_key = std::type_index(typeid(SubquerySpec));
1860 auto state_it = parent_node->m_subquery_states.find(state_key);
1861 if (state_it == parent_node->m_subquery_states.end())
1862 {
1863 auto [inserted_it, inserted] = parent_node->m_subquery_states.emplace(state_key, make_subquery_state_any< SubquerySpec >());
1864 assert(inserted);
1865 state_it = inserted_it;
1866 }
1867
1868 subquery_state< SubquerySpec >& state = std::any_cast< subquery_state< SubquerySpec >& >(state_it->second.m_state);
1869 auto [slot_it, inserted] = state.m_instances.try_emplace(input);
1870 subquery_slot< SubquerySpec >& slot = slot_it->second;
1871 if (inserted)
1872 {
1873 slot.m_input.template emplace< typename SubquerySpec::input_type const* >(&slot_it->first);
1874 slot.m_result.template emplace< result< typename SubquerySpec::output_type > >();
1875 if (node_debug_io_capture_enabled())
1876 {
1877 slot.m_input_string = datavalue_to_string(input);
1878 }
1879 }
1880 return slot;
1881 }
1882
1883 template < subquery_spec_c SubquerySpec >
1884 std::vector< resumption > complete_subquery_from_parent_result_locked(query_node* parent_node, subquery_slot< SubquerySpec >& slot)
1885 {
1886 std::vector< resumption > waiters;
1887 result< typename SubquerySpec::output_type >& subquery_res = std::any_cast< result< typename SubquerySpec::output_type >& >(slot.m_result);
1888 if (subquery_res.has_result())
1889 {
1890 return waiters;
1891 }
1892
1893 result< typename SubquerySpec::parent_query::output_type > const& parent_res = std::any_cast< result< typename SubquerySpec::parent_query::output_type > const& >(parent_node->m_result);
1894 if (parent_res.has_value())
1895 {
1896 subquery_res.set_error(make_subquery_does_not_exist_error(std::string(SubquerySpec::subquery_id)));
1897 waiters = take_subquery_waiters< SubquerySpec >(slot);
1898 }
1899 else if (parent_res.has_exception())
1900 {
1901 subquery_res.set_error(make_subquery_parent_failed_error(std::string(SubquerySpec::subquery_id), parent_res.get_error_result()));
1902 waiters = take_subquery_waiters< SubquerySpec >(slot);
1903 }
1904 return waiters;
1905 }
1906
1907 template < subquery_spec_c SubquerySpec >
1908 auto get_subquery_result(worker* worker, query_node* parent_node, typename SubquerySpec::input_type const& input) -> subquery_query_result< SubquerySpec >
1909 {
1910 std::unique_lock lock(parent_node->m_mutex);
1911 subquery_slot< SubquerySpec >& slot = get_or_create_subquery_slot_locked< SubquerySpec >(parent_node, input);
1912 std::vector< resumption > waiters = complete_subquery_from_parent_result_locked< SubquerySpec >(parent_node, slot);
1913 bool const abort_waiters = std::any_cast< result< typename SubquerySpec::output_type > const& >(slot.m_result).has_unexpected_exception();
1914 std::exception_ptr const unexpected_exception = abort_waiters ? std::any_cast< result< typename SubquerySpec::output_type > const& >(slot.m_result).get_error_result().unexpected_exception() : nullptr;
1915 lock.unlock();
1916 if (abort_waiters)
1917 {
1918 abort_resumptions(worker, std::move(waiters), unexpected_exception);
1919 }
1920 else
1921 {
1922 declare_resumptions(worker, std::move(waiters));
1923 }
1924 return subquery_query_result< SubquerySpec >(parent_node, &slot);
1925 }
1926
1927 template < subquery_spec_c SubquerySpec >
1928 void set_subquery_result(worker* worker, query_node* parent_node, typename SubquerySpec::input_type input, typename SubquerySpec::output_type output)
1929 {
1930 std::unique_lock lock(parent_node->m_mutex);
1931 subquery_slot< SubquerySpec >& slot = get_or_create_subquery_slot_locked< SubquerySpec >(parent_node, input);
1932 result< typename SubquerySpec::output_type >& subquery_res = std::any_cast< result< typename SubquerySpec::output_type >& >(slot.m_result);
1933 if (subquery_res.has_result())
1934 {
1935 throw std::logic_error(std::format("Subquery result already produced: {}", std::string(SubquerySpec::subquery_id)));
1936 }
1937 if (node_debug_io_capture_enabled())
1938 {
1939 slot.m_output_string = datavalue_to_string(output);
1940 }
1941 subquery_res.set_value(std::move(output));
1942 std::vector< resumption > waiters = take_subquery_waiters< SubquerySpec >(slot);
1943 lock.unlock();
1944 parent_node->m_external_cv.notify_all();
1945 declare_resumptions(worker, std::move(waiters));
1946 }
1947
1948 std::vector< resumption > finalize_missing_subqueries_locked(query_node* node);
1949
1950 std::vector< resumption > finalize_parent_failed_subqueries_locked(query_node* node, error_result const& parent_error);
1951
1952 template < subquery_spec_c SubquerySpec >
1953 void register_subquery_descriptors()
1954 {
1955 register_inputoutput_text_descriptor< typename SubquerySpec::input_type >();
1956 register_inputoutput_text_descriptor< typename SubquerySpec::output_type >();
1957 register_binary_descriptors< typename SubquerySpec::input_type >();
1958 register_binary_descriptors< typename SubquerySpec::output_type >();
1959 }
1960
1961 template < rpnx::typelist_c Typelist, std::size_t N = 0 >
1962 void register_subquery_descriptors_from_typelist()
1963 {
1964 if constexpr (N < Typelist::size)
1965 {
1966 using dep = typename Typelist::template type_at< N >;
1967 if constexpr (subquery_spec_c< dep >)
1968 {
1969 register_subquery_descriptors< dep >();
1970 }
1971 register_subquery_descriptors_from_typelist< Typelist, N + 1 >();
1972 }
1973 }
1974
1975 std::optional< error_result > capture_subquery_parent_failed(subquery_parent_failed const& exception) const;
1976
1977 error_result capture_exception(std::exception_ptr exception) const;
1978
1979 error_result capture_current_exception() const;
1980
1981 template < typename Output >
1982 void abort_query_node_with_unexpected_exception(query_node* node, worker* worker, std::exception_ptr exception)
1983 {
1984 std::vector< resumption > subquery_waiters;
1985 {
1986 std::unique_lock lock(node->m_mutex);
1987 result< Output >& res = std::any_cast< result< Output >& >(node->m_result);
1988 if (res.has_result())
1989 {
1990 return;
1991 }
1992 res.set_error(error_result::unexpected(exception));
1993 subquery_waiters = finalize_parent_failed_subqueries_locked(node, res.get_error_result());
1994 node->abort_waiters(worker, lock, exception);
1995 }
1996 abort_resumptions(worker, std::move(subquery_waiters), exception);
1997 }
1999
2000 public:
2012 template < typename Error >
2014 {
2015 static_assert(std::derived_from< Error, std::exception >, "Canonical errors must derive from std::exception");
2016 static_assert(std::copy_constructible< Error >, "Canonical errors must be copy constructible");
2017
2018 canonical_error_descriptor descriptor;
2019 descriptor.m_capture = [](std::exception const& exception)
2020 {
2021 return error_result::canonical(make_canonical_error_storage(static_cast< Error const& >(exception)));
2022 };
2023 m_canonical_error_descriptors[std::type_index(typeid(Error))] = std::move(descriptor);
2024 }
2025
2035 template < typename T >
2037 {
2038 m_text_descriptors.get_or_init(std::type_index(typeid(T)),
2039 [](text_descriptor& desc)
2040 {
2041 desc.m_to_string = [](std::any val, std::any codec) -> std::string
2042 {
2043 return debug_traits< T >::to_debug_string(std::any_cast< T >(val));
2044 };
2045 });
2046 }
2047
2059 template < typename T >
2061 {
2062 binary_descriptor desc;
2063 desc.m_serialize_from_cptr = [](std::any& val) -> std::vector< std::byte >
2064 {
2065 T const* value = std::any_cast< T const* >(val);
2067 };
2068 desc.m_serialize_from_result = [](std::any& val) -> binary_descriptor::result_binary_data
2069 {
2070 result< T >& res = std::any_cast< result< T >& >(val);
2071 if (res.has_value())
2072 {
2073 std::vector< std::byte > data = binary_traits< T >::serialize_to_binary(res.value());
2074 return binary_descriptor::result_binary_data{
2075 .m_output_data = std::move(data),
2076 .m_status = status::completed,
2077 .m_error_result = std::nullopt,
2078 };
2079 }
2080 if (res.has_exception())
2081 {
2082 std::vector< std::byte > data;
2083 std::string str = "Exception: ";
2084 str += res.get_error_result().message();
2085 for (auto c : str)
2086 {
2087 data.push_back(std::byte(c));
2088 }
2089 return binary_descriptor::result_binary_data{
2090 .m_output_data = std::move(data),
2091 .m_status = res.result_status(),
2092 .m_error_result = res.get_error_result().canonical_dump(),
2093 };
2094 }
2095 return binary_descriptor::result_binary_data{
2096 .m_output_data = {},
2097 .m_status = status::pending,
2098 .m_error_result = std::nullopt,
2099 };
2100 };
2101 desc.m_deserialize = [](std::vector< std::byte > const& data) -> std::any
2102 {
2103 T value{};
2104 rpnx::serial4::deserialize_iter(value, data.begin(), data.end());
2105 return std::any(std::move(value));
2106 };
2107 desc.m_typesig = rpnx::serial4::get_type_typesig_hash16< T >();
2108
2109 m_binary_descriptors[std::type_index(typeid(T))] = desc;
2110 }
2111
2123 template < query_handler_spec_c QuerySpec >
2125 {
2126 // TODO: Validate that this works.
2128 [handler_map = std::make_shared< std::map< query_handler_input_t< QuerySpec >, query_handler_output_t< QuerySpec > > >(std::move(handler_map))](query_handler_input_t< QuerySpec > const& input) -> coroutine< QuerySpec >
2129 {
2130 auto it = handler_map->find(input);
2131 if (it == handler_map->end())
2132 {
2133 throw std::logic_error("No handler found for the given input in handler map");
2134 }
2135 co_return it->second;
2136 });
2137 }
2138
2144 template < query_handler_spec_c HandlerSpec >
2145 requires std::same_as< query_handler_input_t< HandlerSpec >, std::monostate >
2147 {
2148 using output_type = query_handler_output_t< HandlerSpec >;
2149
2151 [value = std::make_shared< output_type >(std::move(value))](std::monostate const&) -> coroutine< HandlerSpec >
2152 {
2153 co_return *value;
2154 });
2155 }
2156
2165 template < query_spec_c QuerySpec >
2166 requires std::same_as< typename QuerySpec::input_type, std::monostate >
2167 void register_handler_singleton(typename QuerySpec::output_type value)
2168 {
2170 }
2171
2188 template < query_handler_spec_c HandlerSpec, typename Handler >
2190 {
2191 auto query_index = std::type_index(typeid(query_handler_query_t< HandlerSpec >));
2192 if (query_handler_registry.find(query_index) != query_handler_registry.end())
2193 {
2194 throw std::logic_error("Handler already registered");
2195 }
2196 query_handler_registry[query_index].emplace< query_descriptor< query_handler_query_t< HandlerSpec > > >();
2197 query_descriptor_any& qd_any = query_descriptor_any_registry[query_index];
2198
2199 query_descriptor< query_handler_query_t< HandlerSpec > >& descriptor = std::any_cast< query_descriptor< query_handler_query_t< HandlerSpec > >& >(query_handler_registry[query_index]);
2200 descriptor.m_query_handler_impl = std::make_shared< query_handler_descriptor_memoized_impl< HandlerSpec > >();
2201 query_handler_descriptor_memoized_impl< HandlerSpec >& memoized_impl = *std::any_cast< std::shared_ptr< query_handler_descriptor_memoized_impl< HandlerSpec > >& >(descriptor.m_query_handler_impl);
2202 memoized_impl.m_graph = this;
2203 memoized_impl.m_startup = std::move(h);
2204 memoized_impl.m_handler_entry = &descriptor;
2205
2206 // This is safe because the memoized_impl lifetime is tied to the query_handler_descriptor lifetime
2207 descriptor.m_get = [&memoized_impl](worker* current_worker, query_handler_input_t< HandlerSpec > const& input_ask, bool immediate)
2208 {
2209 return memoized_impl.get(current_worker, input_ask, immediate);
2210 };
2211
2212 qd_any.m_iterate_all = [&memoized_impl](std::function< void(query_node&) > callback)
2213 {
2214 memoized_impl.iterate_all(std::move(callback));
2215 };
2216
2217 qd_any.m_input_type = std::type_index(typeid(query_handler_input_t< HandlerSpec >));
2218 qd_any.m_output_type = std::type_index(typeid(query_handler_output_t< HandlerSpec >));
2220
2225 register_subquery_descriptors_from_typelist< query_handler_dependencies_t< HandlerSpec > >();
2226 register_subquery_descriptors_from_typelist< query_handler_produced_subqueries_t< HandlerSpec > >();
2227
2229
2230 m_pending_binds.push_back(
2231 [&memoized_impl](graph* exec)
2232 {
2233 memoized_impl.bind(exec);
2234 });
2235 }
2236
2238 template < query_spec_c QuerySpec >
2239 auto get_query_descriptor() -> query_descriptor< QuerySpec >&
2240 {
2241 auto it = query_handler_registry.find(std::type_index(typeid(QuerySpec)));
2242 if (it == query_handler_registry.end())
2243 {
2244 throw std::logic_error("No handler registered for the requested query spec");
2245 }
2246 return std::any_cast< query_descriptor< QuerySpec >& >(it->second);
2247 }
2249
2260
2271 template < typename Visitor >
2273 {
2274 for (auto const& [type_index, binary_desc] : m_binary_descriptors)
2275 {
2276 auto text_desc = m_text_descriptors.get_or_create(type_index,
2277 []()
2278 {
2279 return text_descriptor{};
2280 });
2281
2282 visitor(binary_desc.m_typesig, binary_desc.m_deserialize, text_desc.m_to_string);
2283 }
2284 }
2285
2291
2293 graph_data dump(query_node* start_node);
2295
2300 std::vector< std::byte > marshall();
2301
2303 std::vector< std::byte > marshall(query_node* start_node);
2305
2311 bool node_debug_io_capture_enabled() const noexcept;
2312 };
2313
2322 {
2323 public:
2325 struct options
2326 {
2328 std::optional< std::size_t > thread_count;
2330 std::optional< std::size_t > global_workitem_threshold;
2331 };
2332
2335
2340 explicit scheduler(options configuration);
2341
2344
2346 scheduler(scheduler const&) = delete;
2348 scheduler& operator=(scheduler const&) = delete;
2349
2350 private:
2351 friend class graph;
2352 friend struct graph::worker;
2353
2355 static std::size_t default_worker_count();
2357 std::size_t global_workitems_to_publish(std::size_t available_items) const;
2359 std::size_t global_workitems_to_take(std::size_t queued_work) const;
2361 void push_group(std::vector< graph::resumption > vals);
2363 void release_graph(graph& owning_graph, graph::worker& current_worker);
2365 void try_steal_work();
2367 std::vector< graph::resumption > get_work(graph::worker* current_worker);
2368
2369 std::size_t m_total_workers = 0;
2370 std::size_t m_idle_workers = 0;
2371 std::atomic< std::size_t > m_global_workitems = 0;
2372 std::size_t m_global_workitem_threshold = 0;
2373 fast_mutex m_mutex;
2374 std::deque< graph::worker > m_workers;
2375 std::vector< std::thread > m_worker_threads;
2376 std::deque< graph::resumption > m_runnables;
2377 fast_cv m_scheduler_cv;
2378 bool m_stopping = false;
2379
2380 static constexpr std::size_t taskgroup_max_size = 1024;
2381 };
2382
2383 template < typename Result >
2384 struct query_result
2385 {
2386 graph::query_node* m_node;
2387 bool m_started_by_current_request = false;
2388
2389 public:
2390 explicit query_result(graph::query_node* node_ptr, bool started_by_current_request = false) : m_node(node_ptr), m_started_by_current_request(started_by_current_request)
2391 {
2392 }
2393
2394 bool started_by_current_request() const noexcept
2395 {
2396 return m_started_by_current_request;
2397 }
2398
2399 bool available() const
2400 {
2401 std::shared_lock lock(m_node->m_mutex);
2402 result< Result > const& res = std::any_cast< result< Result > const& >(m_node->m_result);
2403 return res.has_result();
2404 }
2405
2406 void wait()
2407 {
2408 std::unique_lock lock(m_node->m_mutex);
2409 m_node->m_external_cv.wait(lock,
2410 [this]()
2411 {
2412 result< Result > const& res = std::any_cast< result< Result > const& >(m_node->m_result);
2413 return res.has_result();
2414 });
2415 }
2416
2417 bool await_ready()
2418 {
2419 std::shared_lock lock(m_node->m_mutex);
2420 result< Result > const& res = std::any_cast< result< Result > const& >(m_node->m_result);
2421 return res.has_result() && !res.has_unexpected_exception();
2422 }
2423
2424 bool await_ready([[maybe_unused]] std::unique_lock< std::shared_mutex >& lock)
2425 {
2426 return std::any_cast< result< Result >& >(m_node->m_result).has_result();
2427 }
2428
2429 bool await_ready([[maybe_unused]] std::shared_lock< std::shared_mutex >& lock)
2430 {
2431 return std::any_cast< result< Result > const& >(m_node->m_result).has_result();
2432 }
2433
2434 template < typename U >
2435 void await_suspend(std::coroutine_handle< U > h)
2436 {
2437 await_suspend_helper< typename U::coroutine_type >(h.promise(), h);
2438 }
2439
2440 template < typename C >
2441 void await_suspend_helper(typename C::promise_type& p, std::coroutine_handle< typename C::promise_type > h)
2442 {
2443 await_suspend_helper2< C, typename C::spec_type >(p, h);
2444 }
2445
2446 template < typename C, typename HandlerSpec >
2447 void await_suspend_helper2(typename C::promise_type& p, std::coroutine_handle< typename C::promise_type > h)
2448 {
2449 std::unique_lock lock(m_node->m_mutex);
2450
2451 graph::worker* current_worker = p.m_worker;
2452 assert(current_worker != nullptr);
2453 current_worker->check_legal_access();
2454
2455 if (await_ready(lock))
2456 {
2457 result< Result > const& res = std::any_cast< result< Result > const& >(m_node->m_result);
2458 if (res.has_unexpected_exception())
2459 {
2460 std::exception_ptr unexpected_exception = res.get_error_result().unexpected_exception();
2461 lock.unlock();
2462 p.abort_with_unexpected_exception(current_worker, unexpected_exception);
2463 }
2464 else
2465 {
2466 current_worker->declare_runnable(graph::resumption{.m_promise = &p, .handle = h}, false);
2467 }
2468 }
2469 else
2470 {
2471 m_node->waiters.push_back(graph::resumption{.m_promise = &p, .handle = h});
2472 }
2473 }
2474
2475 Result const& await_resume()
2476 {
2477 std::shared_lock lock(m_node->m_mutex);
2478 auto& res = std::any_cast< result< Result > const& >(m_node->m_result);
2479 return res.value();
2480 }
2481 };
2482
2483 template < subquery_spec_c SubquerySpec >
2484 struct subquery_query_result
2485 {
2486 graph::query_node* m_parent_node;
2487 graph::subquery_slot< SubquerySpec >* m_slot;
2488
2489 public:
2490 subquery_query_result(graph::query_node* parent_node, graph::subquery_slot< SubquerySpec >* slot) : m_parent_node(parent_node), m_slot(slot)
2491 {
2492 }
2493
2494 bool available() const
2495 {
2496 std::shared_lock lock(m_parent_node->m_mutex);
2497 result< typename SubquerySpec::output_type > const& res = std::any_cast< result< typename SubquerySpec::output_type > const& >(m_slot->m_result);
2498 return res.has_result();
2499 }
2500
2501 void wait()
2502 {
2503 std::unique_lock lock(m_parent_node->m_mutex);
2504 m_parent_node->m_external_cv.wait(lock,
2505 [this]()
2506 {
2507 result< typename SubquerySpec::output_type > const& res = std::any_cast< result< typename SubquerySpec::output_type > const& >(m_slot->m_result);
2508 return res.has_result();
2509 });
2510 }
2511
2512 bool await_ready()
2513 {
2514 std::shared_lock lock(m_parent_node->m_mutex);
2515 result< typename SubquerySpec::output_type > const& res = std::any_cast< result< typename SubquerySpec::output_type > const& >(m_slot->m_result);
2516 return res.has_result() && !res.has_unexpected_exception();
2517 }
2518
2519 bool await_ready([[maybe_unused]] std::unique_lock< std::shared_mutex >& lock)
2520 {
2521 return std::any_cast< result< typename SubquerySpec::output_type >& >(m_slot->m_result).has_result();
2522 }
2523
2524 bool await_ready([[maybe_unused]] std::shared_lock< std::shared_mutex >& lock)
2525 {
2526 return std::any_cast< result< typename SubquerySpec::output_type > const& >(m_slot->m_result).has_result();
2527 }
2528
2529 template < typename U >
2530 void await_suspend(std::coroutine_handle< U > h)
2531 {
2532 await_suspend_helper< typename U::coroutine_type >(h.promise(), h);
2533 }
2534
2535 template < typename C >
2536 void await_suspend_helper(typename C::promise_type& p, std::coroutine_handle< typename C::promise_type > h)
2537 {
2538 std::unique_lock lock(m_parent_node->m_mutex);
2539
2540 graph::worker* current_worker = p.m_worker;
2541 assert(current_worker != nullptr);
2542 current_worker->check_legal_access();
2543
2544 if (await_ready(lock))
2545 {
2546 result< typename SubquerySpec::output_type > const& res = std::any_cast< result< typename SubquerySpec::output_type > const& >(m_slot->m_result);
2547 if (res.has_unexpected_exception())
2548 {
2549 std::exception_ptr unexpected_exception = res.get_error_result().unexpected_exception();
2550 lock.unlock();
2551 p.abort_with_unexpected_exception(current_worker, unexpected_exception);
2552 }
2553 else
2554 {
2555 current_worker->declare_runnable(graph::resumption{.m_promise = &p, .handle = h}, false);
2556 }
2557 }
2558 else
2559 {
2560 m_slot->waiters.push_back(graph::resumption{.m_promise = &p, .handle = h});
2561 }
2562 }
2563
2564 typename SubquerySpec::output_type const& await_resume()
2565 {
2566 std::shared_lock lock(m_parent_node->m_mutex);
2567 result< typename SubquerySpec::output_type > const& res = std::any_cast< result< typename SubquerySpec::output_type > const& >(m_slot->m_result);
2568 return res.value();
2569 }
2570 };
2571
2584 template < query_handler_spec_c HandlerSpec >
2586 {
2588 using spec_type = HandlerSpec;
2589
2598 template < typename Ret >
2599 using cosubroutine = cosubroutine_impl< HandlerSpec, Ret >;
2600
2602 struct promise_type : public graph::promise_base
2603 {
2604 using root_coroutine_type = coroutine< HandlerSpec >;
2605 using coroutine_type = coroutine< HandlerSpec >;
2606
2607 typename graph::template query_descriptor< query_handler_query_t< HandlerSpec > >* m_handler_entry = nullptr;
2608 coroutine< HandlerSpec >* m_coroutine = nullptr;
2609
2610 std::string query_name() const override
2611 {
2613 }
2614
2615 std::string input_text() const override
2616 {
2617 using input_type = query_handler_input_t< HandlerSpec >;
2618 input_type const* input = std::any_cast< input_type const* >(m_node->m_input);
2619 return m_graph->datavalue_to_string(*input);
2620 }
2621
2622 template < query_spec_c QuerySpec >
2623 auto await_transform(request< QuerySpec > a) -> query_result< typename QuerySpec::output_type >
2624 {
2625 static_assert(query_handler_dependencies_t< HandlerSpec >::template contains< QuerySpec >(), "Making a request to a query which is not listed in the HandlerSpec::dependencies");
2626 assert(m_worker != nullptr);
2627 assert(m_node != nullptr);
2628
2629 typename graph::template query_handler_descriptor_memoized_impl< HandlerSpec >& handler_impl = *std::any_cast< std::shared_ptr< typename graph::template query_handler_descriptor_memoized_impl< HandlerSpec > >& >(m_handler_entry->m_query_handler_impl);
2630
2631 typename graph::template query_descriptor< QuerySpec >& dep_query_descriptor = handler_impl.template get_dependency_descriptor< QuerySpec >();
2632
2633 query_result< typename QuerySpec::output_type > await_obj = dep_query_descriptor.m_get(m_worker, a.m_input, false);
2634
2635 graph::query_node* await_node = await_obj.m_node;
2636 graph::declare_dependency(m_node, await_node);
2637 return await_obj;
2638 }
2639
2640 template < subquery_spec_c SubquerySpec >
2641 auto await_transform(subquery_request< SubquerySpec > a) -> subquery_query_result< SubquerySpec >
2642 {
2643 static_assert(query_handler_dependencies_t< HandlerSpec >::template contains< SubquerySpec >(), "Making a subquery request requires listing the subquery in query_handler_dependencies_t< HandlerSpec >");
2644 static_assert(query_handler_dependencies_t< HandlerSpec >::template contains< typename SubquerySpec::parent_query >(), "Making a subquery request requires listing the subquery parent query in query_handler_dependencies_t< HandlerSpec >");
2645 assert(m_worker != nullptr);
2646 assert(m_node != nullptr);
2647
2648 typename graph::template query_handler_descriptor_memoized_impl< HandlerSpec >& handler_impl = *std::any_cast< std::shared_ptr< typename graph::template query_handler_descriptor_memoized_impl< HandlerSpec > >& >(m_handler_entry->m_query_handler_impl);
2649
2650 typename graph::template query_descriptor< typename SubquerySpec::parent_query >& parent_query_descriptor = handler_impl.template get_subquery_parent_descriptor< SubquerySpec >();
2651
2652 query_result< typename SubquerySpec::parent_query::output_type > parent_await_obj = parent_query_descriptor.m_get(m_worker, a.m_parent_input, false);
2653
2654 graph::query_node* parent_node = parent_await_obj.m_node;
2655 graph::declare_dependency(m_node, parent_node);
2656 return m_graph->template get_subquery_result< SubquerySpec >(m_worker, parent_node, a.m_input);
2657 }
2658
2659 template < query_spec_c QuerySpec >
2660 auto yield_value(dependency< QuerySpec > dep)
2661 {
2662 static_assert(query_handler_dependencies_t< HandlerSpec >::template contains< QuerySpec >(), "Making a request to a query which is not listed in the HandlerSpec::dependencies");
2663 assert(m_worker != nullptr);
2664 assert(m_node != nullptr);
2665
2666 typename graph::template query_handler_descriptor_memoized_impl< HandlerSpec >& handler_impl = *std::any_cast< std::shared_ptr< typename graph::template query_handler_descriptor_memoized_impl< HandlerSpec > >& >(m_handler_entry->m_query_handler_impl);
2667 typename graph::template query_descriptor< QuerySpec >& dep_query_descriptor = handler_impl.template get_dependency_descriptor< QuerySpec >();
2668 query_result< typename QuerySpec::output_type > await_obj = dep_query_descriptor.m_get(m_worker, dep.m_request.m_input, true);
2669 graph::query_node* await_node = await_obj.m_node;
2670 graph::declare_dependency(m_node, await_node);
2671 return std::suspend_never{};
2672 }
2673
2674 template < subquery_spec_c SubquerySpec >
2675 auto yield_value(subquery_result< SubquerySpec > res)
2676 {
2677 static_assert(query_handler_produced_subqueries_t< HandlerSpec >::template contains< SubquerySpec >(), "Yielding a subquery result requires listing the subquery in query_handler_produced_subqueries_t< HandlerSpec >");
2678 static_assert(std::same_as< typename SubquerySpec::parent_query, query_handler_query_t< HandlerSpec > >, "A handler can only yield subqueries whose parent_query is the handler query");
2679 assert(m_worker != nullptr);
2680 assert(m_node != nullptr);
2681
2682 m_graph->template set_subquery_result< SubquerySpec >(m_worker, m_node, std::move(res.m_input), std::move(res.m_output));
2683 return std::suspend_never{};
2684 }
2685
2686 template < typename Ret >
2687 auto await_transform(cosubroutine< Ret >&& sub)
2688 {
2689 assert(m_node != nullptr);
2690 auto* sub_promise = sub.m_promise;
2691 sub_promise->m_graph = m_graph;
2692 sub_promise->m_node = m_node;
2693 sub_promise->m_handler_entry = m_handler_entry;
2694 m_worker->declare_runnable(std::coroutine_handle< typename cosubroutine< Ret >::promise_type >::from_promise(*sub_promise), sub_promise, false);
2695 return typename cosubroutine< Ret >::awaitable(sub_promise);
2696 }
2697
2698 void return_value(query_handler_output_t< HandlerSpec > value)
2699 {
2700 assert(m_node != nullptr);
2701 std::unique_lock lock(m_node->m_mutex);
2702 result< query_handler_output_t< HandlerSpec > >& res = std::any_cast< result< query_handler_output_t< HandlerSpec > >& >(m_node->m_result);
2703 if (m_graph->node_debug_io_capture_enabled())
2704 {
2705 m_node->m_output_string = m_graph->datavalue_to_string(value);
2706 }
2707 res.set_value(std::move(value));
2708 std::vector< graph::resumption > subquery_waiters = m_graph->finalize_missing_subqueries_locked(m_node);
2709 m_node->unblock_waiters(m_worker, lock);
2710 graph::declare_resumptions(m_worker, std::move(subquery_waiters));
2711 }
2712
2713 auto initial_suspend()
2714 {
2715 return std::suspend_always{};
2716 }
2717
2718 auto final_suspend() noexcept
2719 {
2720 std::unique_lock lock(m_node->m_mutex);
2721 result< query_handler_output_t< HandlerSpec > >& res = std::any_cast< result< query_handler_output_t< HandlerSpec > >& >(m_node->m_result);
2722 if (!res.has_result() && m_node->dependencies.empty())
2723 {
2724 std::exception_ptr exception = std::make_exception_ptr(bad_continuation());
2725 res.set_error(error_result::unexpected(exception));
2726 std::vector< graph::resumption > subquery_waiters = m_graph->finalize_parent_failed_subqueries_locked(m_node, res.get_error_result());
2727 m_node->abort_waiters(m_worker, lock, exception);
2728 graph::abort_resumptions(m_worker, std::move(subquery_waiters), exception);
2729 }
2730 return std::suspend_always{};
2731 }
2732
2733 auto get_return_object()
2734 {
2735 coroutine< HandlerSpec > co(this);
2736 assert(m_coroutine == &co);
2737 assert(co.m_promise == this);
2738 return co;
2739 }
2740
2741 auto yield_value(debug_message msg, std::source_location loc = std::source_location::current())
2742 {
2743 assert(m_node != nullptr);
2744 std::unique_lock lock(m_node->m_mutex);
2745 msg.location = loc;
2746 m_node->m_messages.push_back(std::move(msg));
2747 return std::suspend_never{};
2748 }
2749
2750 auto unhandled_exception()
2751 {
2752 assert(m_node != nullptr);
2753 assert(m_worker != nullptr);
2754 std::unique_lock lock(m_node->m_mutex);
2755 result< query_handler_output_t< HandlerSpec > >& res = std::any_cast< result< query_handler_output_t< HandlerSpec > >& >(m_node->m_result);
2756 error_result captured_error = m_graph->capture_current_exception();
2757 std::exception_ptr unexpected_exception = captured_error.unexpected_exception();
2758 if (unexpected_exception != nullptr)
2759 {
2760 m_graph->print_noncanonical_exception_diagnostic(this, captured_error);
2761 }
2762 res.set_error(std::move(captured_error));
2763 std::vector< graph::resumption > subquery_waiters = m_graph->finalize_parent_failed_subqueries_locked(m_node, res.get_error_result());
2764 if (unexpected_exception != nullptr)
2765 {
2766 m_node->abort_waiters(m_worker, lock, unexpected_exception);
2767 graph::abort_resumptions(m_worker, std::move(subquery_waiters), unexpected_exception);
2768 }
2769 else
2770 {
2771 m_node->unblock_waiters(m_worker, lock);
2772 graph::declare_resumptions(m_worker, std::move(subquery_waiters));
2773 }
2774 }
2775
2776 void abort_with_unexpected_exception(graph::worker* worker, std::exception_ptr exception) override
2777 {
2778 this->m_worker = worker;
2779 assert(m_node != nullptr);
2780 m_graph->template abort_query_node_with_unexpected_exception< query_handler_output_t< HandlerSpec > >(m_node, worker, exception);
2781 }
2782
2783 template < typename Res >
2784 void await_suspend_on(query_result< Res > q)
2785 {
2786 std::unique_lock lock(q.m_node->m_mutex);
2787
2788 graph::worker* current_worker = m_worker;
2789 assert(current_worker != nullptr);
2790 current_worker->check_legal_access();
2791 auto h = std::coroutine_handle< promise_type >::from_promise(*this);
2792
2793 if (q.await_ready(lock))
2794 {
2795 result< Res > const& res = std::any_cast< result< Res > const& >(q.m_node->m_result);
2796 if (res.has_unexpected_exception())
2797 {
2798 std::exception_ptr unexpected_exception = res.get_error_result().unexpected_exception();
2799 lock.unlock();
2800 abort_with_unexpected_exception(current_worker, unexpected_exception);
2801 }
2802 else
2803 {
2804 lock.unlock();
2805 current_worker->declare_runnable(graph::resumption{.m_promise = this, .handle = h}, false);
2806 }
2807 }
2808 else
2809 {
2810 q.m_node->waiters.push_back(graph::resumption{.m_promise = this, .handle = h});
2811 }
2812 }
2813 };
2815
2817 promise_type* m_promise = nullptr;
2818
2819 coroutine() noexcept : m_promise(nullptr)
2820 {
2821 }
2822
2823 coroutine(coroutine&) = delete;
2824
2825 coroutine(coroutine&& other) noexcept : m_promise(other.m_promise)
2826 {
2827 m_promise = other.m_promise;
2828 other.m_promise = nullptr;
2829 if (m_promise)
2830 {
2831 m_promise->m_coroutine = this;
2832 }
2833 }
2834
2835 ~coroutine()
2836 {
2837 if (m_promise)
2838 {
2839 m_promise->m_coroutine = nullptr;
2840 std::coroutine_handle< promise_type >::from_promise(*m_promise).destroy();
2841 }
2842 }
2843
2844 coroutine(promise_type* p) : m_promise(p)
2845 {
2846 assert(p->m_coroutine == nullptr);
2847 p->m_coroutine = this;
2848 }
2850 };
2851
2853 template < query_handler_spec_c HandlerSpec, typename Ret >
2854 class cosubroutine_impl
2855 {
2856 friend struct promise_type;
2857 friend struct coroutine< HandlerSpec >;
2858 template < query_handler_spec_c OtherHandlerSpec, typename OtherRet >
2859 friend class cosubroutine_impl;
2860
2861 public:
2862 using spec_type = HandlerSpec;
2863 struct promise_type : public graph::promise_base
2864 {
2865 using root_coroutine_type = coroutine< HandlerSpec >;
2866 using coroutine_type = cosubroutine_impl< HandlerSpec, Ret >;
2867
2868 std::mutex m_mutex;
2869
2870 typename graph::template query_descriptor< query_handler_query_t< HandlerSpec > >* m_handler_entry = nullptr;
2871 cosubroutine_impl* m_srt = nullptr;
2872 result< Ret > m_result;
2873 std::optional< graph::resumption > m_waiter;
2874
2875 std::string query_name() const override
2876 {
2877 return std::string(query_handler_query_t< HandlerSpec >::query_id);
2878 }
2879
2880 std::string input_text() const override
2881 {
2882 using input_type = query_handler_input_t< HandlerSpec >;
2883 input_type const* input = std::any_cast< input_type const* >(m_node->m_input);
2884 return m_graph->datavalue_to_string(*input);
2885 }
2886
2887 auto get_return_object()
2888 {
2889 return cosubroutine_impl(this);
2890 }
2891
2892 auto initial_suspend()
2893 {
2894 return std::suspend_always{};
2895 }
2896
2897 void return_value(Ret value)
2898 {
2899 assert(m_node != nullptr);
2900 assert(this->m_worker != nullptr);
2901
2902 {
2903 std::unique_lock lock(m_mutex);
2904 m_result.set_value(std::move(value));
2905 unblock_waiter(lock);
2906 }
2907 }
2908
2909 auto yield_value(debug_message msg, std::source_location loc = std::source_location::current())
2910 {
2911 assert(m_node != nullptr);
2912 std::unique_lock lock(m_node->m_mutex);
2913 msg.location = loc;
2914 m_node->m_messages.push_back(std::move(msg));
2915 return std::suspend_never{};
2916 }
2917
2919 template < query_spec_c QuerySpec >
2920 auto yield_value(dependency< QuerySpec > dep)
2921 {
2922 static_assert(query_handler_dependencies_t< HandlerSpec >::template contains< QuerySpec >(), "Making a request to a query which is not listed in the HandlerSpec::dependencies");
2923 assert(m_worker != nullptr);
2924 assert(m_node != nullptr);
2925
2926 typename graph::template query_handler_descriptor_memoized_impl< HandlerSpec >& handler_impl = *std::any_cast< std::shared_ptr< typename graph::template query_handler_descriptor_memoized_impl< HandlerSpec > >& >(m_handler_entry->m_query_handler_impl);
2927 typename graph::template query_descriptor< QuerySpec >& dep_query_descriptor = handler_impl.template get_dependency_descriptor< QuerySpec >();
2928 query_result< typename QuerySpec::output_type > await_obj = dep_query_descriptor.m_get(m_worker, dep.m_request.m_input, true);
2929 graph::query_node* await_node = await_obj.m_node;
2930 graph::declare_dependency(m_node, await_node);
2931 return std::suspend_never{};
2932 }
2933
2934 auto final_suspend() noexcept
2935 {
2936 std::unique_lock lock(m_mutex);
2937 assert(m_result.has_result());
2938 return std::suspend_always{};
2939 }
2940
2941 auto unhandled_exception()
2942 {
2943 std::unique_lock lock(m_mutex);
2944 assert(m_node != nullptr);
2945 assert(this->m_worker != nullptr);
2946 m_result.set_error(m_graph->capture_current_exception());
2947 unblock_waiter(lock);
2948 }
2949
2950 void abort_with_unexpected_exception(graph::worker* worker, std::exception_ptr exception) override
2951 {
2952 this->m_worker = worker;
2953 {
2954 std::unique_lock lock(m_mutex);
2955 if (!m_result.has_result())
2956 {
2957 m_result.set_error(error_result::unexpected(exception));
2958 }
2959 }
2960 assert(m_node != nullptr);
2961 m_graph->template abort_query_node_with_unexpected_exception< query_handler_output_t< HandlerSpec > >(m_node, worker, exception);
2962 }
2963
2964 auto unblock_waiter(std::unique_lock< std::mutex >& lock)
2965 {
2966 if (m_waiter.has_value())
2967 {
2968 auto resumption = m_waiter.value();
2969 m_waiter.reset();
2970 m_worker->check_legal_access();
2971 m_worker->declare_runnable(resumption, false);
2972
2973 lock.unlock();
2974 }
2975 }
2976
2977 template < typename Ret2 >
2978 auto await_transform(typename coroutine< HandlerSpec >::template cosubroutine< Ret2 >&& sub)
2979 {
2980 assert(m_node != nullptr);
2981 sub.m_promise->m_graph = m_graph;
2982 sub.m_promise->m_node = m_node;
2983 sub.m_promise->m_handler_entry = m_handler_entry;
2984 m_worker->declare_runnable(std::coroutine_handle< typename coroutine< HandlerSpec >::template cosubroutine< Ret2 >::promise_type >::from_promise(*sub.m_promise), sub.m_promise, false);
2985 return typename coroutine< HandlerSpec >::template cosubroutine< Ret2 >::awaitable(sub.m_promise);
2986 }
2987
2988 template < query_spec_c QuerySpec >
2989 auto await_transform(request< QuerySpec > a) -> query_result< typename QuerySpec::output_type >
2990 {
2991 static_assert(query_handler_dependencies_t< HandlerSpec >::template contains< QuerySpec >(), "Making a request to a query which is not listed in the HandlerSpec::dependencies");
2992 assert(m_worker != nullptr);
2993 assert(m_node != nullptr);
2994
2995 typename graph::template query_handler_descriptor_memoized_impl< HandlerSpec >& handler_impl = *std::any_cast< std::shared_ptr< typename graph::template query_handler_descriptor_memoized_impl< HandlerSpec > >& >(m_handler_entry->m_query_handler_impl);
2996
2997 typename graph::template query_descriptor< QuerySpec >& dep_query_descriptor = handler_impl.template get_dependency_descriptor< QuerySpec >();
2998
2999 query_result< typename QuerySpec::output_type > await_obj = dep_query_descriptor.m_get(m_worker, a.m_input, false);
3000
3001 graph::query_node* await_node = await_obj.m_node;
3002 graph::declare_dependency(m_node, await_node);
3003 return await_obj;
3004 }
3005
3006 template < subquery_spec_c SubquerySpec >
3007 auto await_transform(subquery_request< SubquerySpec > a) -> subquery_query_result< SubquerySpec >
3008 {
3009 static_assert(query_handler_dependencies_t< HandlerSpec >::template contains< SubquerySpec >(), "Making a subquery request requires listing the subquery in query_handler_dependencies_t< HandlerSpec >");
3010 static_assert(query_handler_dependencies_t< HandlerSpec >::template contains< typename SubquerySpec::parent_query >(), "Making a subquery request requires listing the subquery parent query in query_handler_dependencies_t< HandlerSpec >");
3011 assert(m_worker != nullptr);
3012 assert(m_node != nullptr);
3013
3014 typename graph::template query_handler_descriptor_memoized_impl< HandlerSpec >& handler_impl = *std::any_cast< std::shared_ptr< typename graph::template query_handler_descriptor_memoized_impl< HandlerSpec > >& >(m_handler_entry->m_query_handler_impl);
3015
3016 typename graph::template query_descriptor< typename SubquerySpec::parent_query >& parent_query_descriptor = handler_impl.template get_subquery_parent_descriptor< SubquerySpec >();
3017
3018 query_result< typename SubquerySpec::parent_query::output_type > parent_await_obj = parent_query_descriptor.m_get(m_worker, a.m_parent_input, false);
3019
3020 graph::query_node* parent_node = parent_await_obj.m_node;
3021 graph::declare_dependency(m_node, parent_node);
3022 return m_graph->template get_subquery_result< SubquerySpec >(m_worker, parent_node, a.m_input);
3023 }
3024
3025 template < subquery_spec_c SubquerySpec >
3026 auto yield_value(subquery_result< SubquerySpec > res)
3027 {
3028 static_assert(query_handler_produced_subqueries_t< HandlerSpec >::template contains< SubquerySpec >(), "Yielding a subquery result requires listing the subquery in query_handler_produced_subqueries_t< HandlerSpec >");
3029 static_assert(std::same_as< typename SubquerySpec::parent_query, query_handler_query_t< HandlerSpec > >, "A handler can only yield subqueries whose parent_query is the handler query");
3030 assert(m_worker != nullptr);
3031 assert(m_node != nullptr);
3032
3033 m_graph->template set_subquery_result< SubquerySpec >(m_worker, m_node, std::move(res.m_input), std::move(res.m_output));
3034 return std::suspend_never{};
3035 }
3036
3037 ~promise_type()
3038 {
3039 assert(m_srt == nullptr);
3040 }
3041 };
3042
3043 struct awaitable
3044 {
3045 promise_type* m_promise;
3046
3047 public:
3048 awaitable(promise_type* promise) : m_promise(promise)
3049 {
3050 }
3051
3052 bool await_ready()
3053 {
3054 std::unique_lock lck(m_promise->m_mutex);
3055 return await_ready(lck);
3056 }
3057
3058 bool await_ready([[maybe_unused]] std::unique_lock< std::mutex >& lock) noexcept
3059 {
3060 assert(m_promise != nullptr);
3061 return m_promise->m_result.has_result();
3062 }
3063
3064 Ret await_resume()
3065 {
3066 std::unique_lock lck(m_promise->m_mutex);
3067 assert(m_promise != nullptr);
3068 assert(m_promise->m_result.has_result());
3069 return m_promise->m_result.value();
3070 }
3071
3072 template < typename U >
3073 void await_suspend(std::coroutine_handle< U > h)
3074 {
3075 await_suspend_helper< typename U::coroutine_type >(h.promise(), h);
3076 }
3077
3078 template < typename C >
3079 void await_suspend_helper(typename C::promise_type& p, std::coroutine_handle< typename C::promise_type > h)
3080 {
3081 assert(m_promise != nullptr);
3082
3083 graph::worker* current_worker = p.m_worker;
3084 assert(current_worker != nullptr);
3085 current_worker->check_legal_access();
3086
3087 graph::resumption r{.m_promise = &h.promise(), .handle = h};
3088 std::unique_lock lck(m_promise->m_mutex);
3089
3090 if (await_ready(lck))
3091 {
3092 p.m_worker->declare_runnable(r, false);
3093 }
3094 else
3095 {
3096 assert(m_promise->m_waiter.has_value() == false);
3097 m_promise->m_waiter = r;
3098 }
3099 }
3100 };
3101
3102 private:
3103 promise_type* m_promise;
3104
3105 public:
3106 cosubroutine_impl(promise_type* p) : m_promise(p)
3107 {
3108 assert(p->m_srt == nullptr);
3109 p->m_srt = this;
3110 }
3111
3112 cosubroutine_impl(const cosubroutine_impl&) = delete;
3113 cosubroutine_impl& operator=(const cosubroutine_impl&) = delete;
3114
3115 ~cosubroutine_impl()
3116 {
3117 if (m_promise)
3118 {
3119 std::unique_lock lock(m_promise->m_run_mutex);
3120 lock.unlock();
3121
3122 m_promise->m_srt = nullptr;
3123 std::coroutine_handle< promise_type >::from_promise(*m_promise).destroy();
3124 }
3125 }
3126 };
3127
3128 template < query_handler_spec_c HandlerSpec >
3129 class cosubroutine_impl< HandlerSpec, void >
3130 {
3131 friend struct promise_type;
3132 friend struct coroutine< HandlerSpec >;
3133 template < query_handler_spec_c OtherHandlerSpec, typename OtherRet >
3134 friend class cosubroutine_impl;
3135
3136 public:
3137 using spec_type = HandlerSpec;
3138 struct promise_type : public graph::promise_base
3139 {
3140 using root_coroutine_type = coroutine< HandlerSpec >;
3141 using coroutine_type = cosubroutine_impl< HandlerSpec, void >;
3142
3143 std::mutex m_mutex;
3144
3145 typename graph::template query_descriptor< query_handler_query_t< HandlerSpec > >* m_handler_entry = nullptr;
3146 cosubroutine_impl* m_srt = nullptr;
3147 result< void > m_result;
3148 std::optional< graph::resumption > m_waiter;
3149
3150 std::string query_name() const override
3151 {
3152 return std::string(query_handler_query_t< HandlerSpec >::query_id);
3153 }
3154
3155 std::string input_text() const override
3156 {
3157 using input_type = query_handler_input_t< HandlerSpec >;
3158 input_type const* input = std::any_cast< input_type const* >(m_node->m_input);
3159 return m_graph->datavalue_to_string(*input);
3160 }
3161
3162 auto get_return_object()
3163 {
3164 return cosubroutine_impl(this);
3165 }
3166
3167 auto initial_suspend()
3168 {
3169 return std::suspend_always{};
3170 }
3171
3172 void return_void()
3173 {
3174 assert(m_node != nullptr);
3175 assert(this->m_worker != nullptr);
3176
3177 {
3178 std::unique_lock lock(m_mutex);
3179 m_result.set_value();
3180 unblock_waiter(lock);
3181 }
3182 }
3183
3184 auto yield_value(debug_message msg, std::source_location loc = std::source_location::current())
3185 {
3186 assert(m_node != nullptr);
3187 std::unique_lock lock(m_node->m_mutex);
3188 msg.location = loc;
3189 m_node->m_messages.push_back(std::move(msg));
3190 return std::suspend_never{};
3191 }
3192
3194 template < query_spec_c QuerySpec >
3195 auto yield_value(dependency< QuerySpec > dep)
3196 {
3197 static_assert(query_handler_dependencies_t< HandlerSpec >::template contains< QuerySpec >(), "Making a request to a query which is not listed in the HandlerSpec::dependencies");
3198 assert(m_worker != nullptr);
3199 assert(m_node != nullptr);
3200
3201 typename graph::template query_handler_descriptor_memoized_impl< HandlerSpec >& handler_impl = *std::any_cast< std::shared_ptr< typename graph::template query_handler_descriptor_memoized_impl< HandlerSpec > >& >(m_handler_entry->m_query_handler_impl);
3202 typename graph::template query_descriptor< QuerySpec >& dep_query_descriptor = handler_impl.template get_dependency_descriptor< QuerySpec >();
3203 query_result< typename QuerySpec::output_type > await_obj = dep_query_descriptor.m_get(m_worker, dep.m_request.m_input, true);
3204 graph::query_node* await_node = await_obj.m_node;
3205 graph::declare_dependency(m_node, await_node);
3206 return std::suspend_never{};
3207 }
3208
3209 auto final_suspend() noexcept
3210 {
3211 std::unique_lock lock(m_mutex);
3212 assert(m_result.has_result());
3213 return std::suspend_always{};
3214 }
3215
3216 auto unhandled_exception()
3217 {
3218 std::unique_lock lock(m_mutex);
3219 assert(m_node != nullptr);
3220 assert(this->m_worker != nullptr);
3221 m_result.set_error(m_graph->capture_current_exception());
3222 unblock_waiter(lock);
3223 }
3224
3225 void abort_with_unexpected_exception(graph::worker* worker, std::exception_ptr exception) override
3226 {
3227 this->m_worker = worker;
3228 {
3229 std::unique_lock lock(m_mutex);
3230 if (!m_result.has_result())
3231 {
3232 m_result.set_error(error_result::unexpected(exception));
3233 }
3234 }
3235 assert(m_node != nullptr);
3236 m_graph->template abort_query_node_with_unexpected_exception< query_handler_output_t< HandlerSpec > >(m_node, worker, exception);
3237 }
3238
3239 auto unblock_waiter(std::unique_lock< std::mutex >& lock)
3240 {
3241 if (m_waiter.has_value())
3242 {
3243 auto resumption = m_waiter.value();
3244 m_waiter.reset();
3245 m_worker->check_legal_access();
3246 m_worker->declare_runnable(resumption, false);
3247
3248 lock.unlock();
3249 }
3250 }
3251
3252 template < typename Ret2 >
3253 auto await_transform(typename coroutine< HandlerSpec >::template cosubroutine< Ret2 >&& sub)
3254 {
3255 assert(m_node != nullptr);
3256 sub.m_promise->m_graph = m_graph;
3257 sub.m_promise->m_node = m_node;
3258 sub.m_promise->m_handler_entry = m_handler_entry;
3259 m_worker->declare_runnable(std::coroutine_handle< typename coroutine< HandlerSpec >::template cosubroutine< Ret2 >::promise_type >::from_promise(*sub.m_promise), sub.m_promise, false);
3260 return typename coroutine< HandlerSpec >::template cosubroutine< Ret2 >::awaitable(sub.m_promise);
3261 }
3262
3263 template < query_spec_c QuerySpec >
3264 auto await_transform(request< QuerySpec > a) -> query_result< typename QuerySpec::output_type >
3265 {
3266 static_assert(query_handler_dependencies_t< HandlerSpec >::template contains< QuerySpec >(), "Making a request to a query which is not listed in the HandlerSpec::dependencies");
3267 assert(m_worker != nullptr);
3268 assert(m_node != nullptr);
3269
3270 typename graph::template query_handler_descriptor_memoized_impl< HandlerSpec >& handler_impl = *std::any_cast< std::shared_ptr< typename graph::template query_handler_descriptor_memoized_impl< HandlerSpec > >& >(m_handler_entry->m_query_handler_impl);
3271
3272 typename graph::template query_descriptor< QuerySpec >& dep_query_descriptor = handler_impl.template get_dependency_descriptor< QuerySpec >();
3273
3274 query_result< typename QuerySpec::output_type > await_obj = dep_query_descriptor.m_get(m_worker, a.m_input, false);
3275
3276 graph::query_node* await_node = await_obj.m_node;
3277 graph::declare_dependency(m_node, await_node);
3278 return await_obj;
3279 }
3280
3281 template < subquery_spec_c SubquerySpec >
3282 auto await_transform(subquery_request< SubquerySpec > a) -> subquery_query_result< SubquerySpec >
3283 {
3284 static_assert(query_handler_dependencies_t< HandlerSpec >::template contains< SubquerySpec >(), "Making a subquery request requires listing the subquery in query_handler_dependencies_t< HandlerSpec >");
3285 static_assert(query_handler_dependencies_t< HandlerSpec >::template contains< typename SubquerySpec::parent_query >(), "Making a subquery request requires listing the subquery parent query in query_handler_dependencies_t< HandlerSpec >");
3286 assert(m_worker != nullptr);
3287 assert(m_node != nullptr);
3288
3289 typename graph::template query_handler_descriptor_memoized_impl< HandlerSpec >& handler_impl = *std::any_cast< std::shared_ptr< typename graph::template query_handler_descriptor_memoized_impl< HandlerSpec > >& >(m_handler_entry->m_query_handler_impl);
3290
3291 typename graph::template query_descriptor< typename SubquerySpec::parent_query >& parent_query_descriptor = handler_impl.template get_subquery_parent_descriptor< SubquerySpec >();
3292
3293 query_result< typename SubquerySpec::parent_query::output_type > parent_await_obj = parent_query_descriptor.m_get(m_worker, a.m_parent_input, false);
3294
3295 graph::query_node* parent_node = parent_await_obj.m_node;
3296 graph::declare_dependency(m_node, parent_node);
3297 return m_graph->template get_subquery_result< SubquerySpec >(m_worker, parent_node, a.m_input);
3298 }
3299
3300 template < subquery_spec_c SubquerySpec >
3301 auto yield_value(subquery_result< SubquerySpec > res)
3302 {
3303 static_assert(query_handler_produced_subqueries_t< HandlerSpec >::template contains< SubquerySpec >(), "Yielding a subquery result requires listing the subquery in query_handler_produced_subqueries_t< HandlerSpec >");
3304 static_assert(std::same_as< typename SubquerySpec::parent_query, query_handler_query_t< HandlerSpec > >, "A handler can only yield subqueries whose parent_query is the handler query");
3305 assert(m_worker != nullptr);
3306 assert(m_node != nullptr);
3307
3308 m_graph->template set_subquery_result< SubquerySpec >(m_worker, m_node, std::move(res.m_input), std::move(res.m_output));
3309 return std::suspend_never{};
3310 }
3311
3312 ~promise_type()
3313 {
3314 assert(m_srt == nullptr);
3315 }
3316 };
3317
3318 struct awaitable
3319 {
3320 promise_type* m_promise;
3321
3322 public:
3323 awaitable(promise_type* promise) : m_promise(promise)
3324 {
3325 }
3326
3327 bool await_ready()
3328 {
3329 std::unique_lock lck(m_promise->m_mutex);
3330 return await_ready(lck);
3331 }
3332
3333 bool await_ready([[maybe_unused]] std::unique_lock< std::mutex >& lock) noexcept
3334 {
3335 assert(m_promise != nullptr);
3336 return m_promise->m_result.has_result();
3337 }
3338
3339 void await_resume()
3340 {
3341 std::unique_lock lck(m_promise->m_mutex);
3342 assert(m_promise != nullptr);
3343 assert(m_promise->m_result.has_result());
3344 m_promise->m_result.test();
3345 }
3346
3347 template < typename U >
3348 void await_suspend(std::coroutine_handle< U > h)
3349 {
3350 await_suspend_helper< typename U::coroutine_type >(h.promise(), h);
3351 }
3352
3353 template < typename C >
3354 void await_suspend_helper(typename C::promise_type& p, std::coroutine_handle< typename C::promise_type > h)
3355 {
3356 assert(m_promise != nullptr);
3357
3358 graph::worker* current_worker = p.m_worker;
3359 assert(current_worker != nullptr);
3360 current_worker->check_legal_access();
3361
3362 graph::resumption r{.m_promise = &h.promise(), .handle = h};
3363 std::unique_lock lck(m_promise->m_mutex);
3364
3365 if (await_ready(lck))
3366 {
3367 p.m_worker->declare_runnable(r, false);
3368 }
3369 else
3370 {
3371 assert(m_promise->m_waiter.has_value() == false);
3372 m_promise->m_waiter = r;
3373 }
3374 }
3375 };
3376
3377 private:
3378 promise_type* m_promise;
3379
3380 public:
3381 cosubroutine_impl(promise_type* p) : m_promise(p)
3382 {
3383 assert(p->m_srt == nullptr);
3384 p->m_srt = this;
3385 }
3386
3387 cosubroutine_impl(const cosubroutine_impl&) = delete;
3388 cosubroutine_impl& operator=(const cosubroutine_impl&) = delete;
3389
3390 ~cosubroutine_impl()
3391 {
3392 if (m_promise)
3393 {
3394 std::unique_lock lock(m_promise->m_run_mutex);
3395 lock.unlock();
3396
3397 m_promise->m_srt = nullptr;
3398 std::coroutine_handle< promise_type >::from_promise(*m_promise).destroy();
3399 }
3400 }
3401 };
3403
3404 } // namespace RPNX_DEBUGONLY(debug_abi)RPNX_RELEASEONLY(release_abi)
3405
3406} // namespace rpnx::querygraph
3407
3408#endif // NODEGRAPH2_NODEGRAPH_HPP
Human-readable formatting utilities used by QueryGraph diagnostics.
Indicates that a handler completed without a continuation outcome.
Owns handler registrations, memoized nodes, and query execution.
~graph()
Drain this graph's scheduled work before releasing cached nodes. All external calls on this graph mus...
bool node_debug_io_capture_enabled() const noexcept
Return whether human-readable node input/output capture is enabled.
void register_handler_singleton(query_handler_output_t< HandlerSpec > value)
Register a constant result for a handler with monostate input.
void register_binary_descriptors()
Register serialization operations and a type signature.
void register_handler_map(std::map< query_handler_input_t< QuerySpec >, query_handler_output_t< QuerySpec > > handler_map)
Register a finite map as a query handler.
void register_canonical_error()
Register an exception type for canonical capture and dumping.
void register_handler_singleton(typename QuerySpec::output_type value)
Register a constant result directly from a query spec.
auto make_subquery_request(typename SubquerySpec::parent_query::input_type parent_input, typename SubquerySpec::input_type input) -> typename SubquerySpec::output_type
Execute or reuse a subquery request from an external thread.
void for_each_marshaled_type_descriptor(Visitor visitor)
Visit the serialization and text descriptors of registered types.
auto async_make_request(typename QuerySpec::input_type input) -> std::future< typename QuerySpec::output_type >
Submit a query and return a future without waiting for evaluation.
void register_handler_function(Handler h)
Register the coroutine function implementing a query.
graph(scheduler &execution_scheduler, options configuration)
Construct a graph with timing options and built-in subquery errors.
graph(scheduler &execution_scheduler)
Construct an empty graph using a scheduler that outlives it.
void register_inputoutput_text_descriptor()
Register the diagnostic text conversion for a value type.
std::vector< std::byte > marshall()
Serialize a snapshot of every memoized node.
auto make_request(typename QuerySpec::input_type input) -> typename QuerySpec::output_type
Execute or reuse a top-level query and return its value.
void bind_handlers()
Resolve every registered handler's declared dependencies.
graph_data dump()
Snapshot every currently memoized query node.
auto dump_query_to_file(std::filesystem::path output_path, typename QuerySpec::input_type input) -> std::filesystem::path
Evaluate a root query and write its reachable graph to disk.
Owns a persistent worker pool shared by independently cached graphs.
scheduler(options configuration)
Start a worker pool using the supplied scheduling options.
scheduler & operator=(scheduler const &)=delete
Keep each scheduler's worker pool bound to its original owner.
~scheduler()
Stop and join the worker threads after all graphs are destroyed.
scheduler()
Start a worker pool using default scheduling options.
scheduler(scheduler const &)=delete
Schedulers have stable worker addresses and cannot be copied.
subquery_does_not_exist()
Construct an empty instance for deserialization.
subquery_does_not_exist(std::string subquery_id)
Construct an error naming the missing subquery.
void post_serialize()
Rebuild the derived human-readable message after deserialization.
std::string const & subquery_id() const noexcept
Return the stable identifier of the missing subquery.
char const * what() const noexcept override
Return the derived error message.
std::string const & subquery_id() const noexcept
Return the stable identifier of the affected subquery.
subquery_parent_failed(std::string subquery_id)
Construct an error naming the affected subquery.
Discriminated error state used by query and subquery results.
std::exception_ptr make_exception_ptr() const
Materialize either error representation as an exception pointer.
std::exception_ptr unexpected_exception() const
Return the unexpected exception pointer, if present.
status result_status() const
Return the dump status represented by this error state.
std::string message() const
Return the error message, or an empty string for no error.
bool has_error() const
Return whether either error representation is present.
std::optional< canonical_error_result_data > canonical_dump() const
Return serializable data only when the error is canonical.
static error_result canonical(canonical_error_storage error)
Construct an error state from canonical storage.
bool has_unexpected_exception() const
Return whether an unexpected exception is present.
static error_result unexpected(std::exception_ptr exception)
Construct an error state from an unexpected exception.
void throw_error() const
Rethrow the stored error; do nothing when the state is empty.
bool has_canonical_error() const
Return whether a serializable canonical error is present.
error_result()=default
Construct an empty error state.
void get() const
Verify successful completion or throw the stored error.
result()=default
Construct a pending result.
bool has_exception() const
Compatibility spelling for has_error().
status result_status() const
Return the status used when serializing this result.
void test() const
Rethrow the stored error, if one exists.
bool has_result() const
Return whether evaluation produced success or an error.
void set_exception(std::exception_ptr exception)
Compatibility spelling for set_error(exception).
void set_value()
Replace any prior state with successful completion.
bool has_canonical_error() const
Return whether the result contains a canonical error.
bool has_unexpected_exception() const
Return whether the result contains an unexpected exception.
void set_error(error_result error)
Replace any prior state with error.
error_result const & get_error_result() const
Return the stored error state without throwing.
bool has_value() const
Return whether evaluation completed successfully.
void set_error(std::exception_ptr exception)
Replace any prior state with an unexpected exception.
std::exception_ptr get_error() const
Materialize the stored error as an exception pointer.
void value() const
Alias for get().
bool has_error() const
Return whether evaluation completed with an error.
result(std::exception_ptr exception)
Construct a failed result from an unexpected exception.
Internal value-or-error state shared by query awaitables and dumps.
T & get() &
Return the value or throw the stored error.
bool has_exception() const
Compatibility spelling for has_error().
void set_error(error_result error)
Replace any prior state with error.
void set_error(std::exception_ptr exception)
Replace any prior state with an unexpected exception.
T && get() &&
Return the value or throw the stored error.
status result_status() const
Return the status used when serializing this result.
T const & value() const &
Alias for get() preserving the value category.
void set_exception(std::exception_ptr exception)
Compatibility spelling for set_error(exception).
result()=default
Construct a pending result.
T const & get() const &
Return the value or throw the stored error.
bool has_unexpected_exception() const
Return whether the result contains an unexpected exception.
bool has_value() const
Return whether evaluation completed successfully.
T const && value() const &&
Alias for get() preserving the value category.
void test() const
Rethrow the stored error, if one exists.
T & value() &
Alias for get() preserving the value category.
void set_value(T value)
Replace any prior state with a completed value.
error_result const & get_error_result() const
Return the stored error state without throwing.
T && value() &&
Alias for get() preserving the value category.
bool has_canonical_error() const
Return whether the result contains a canonical error.
std::exception_ptr get_error() const
Materialize the stored error as an exception pointer.
bool has_result() const
Return whether evaluation produced either a value or an error.
bool has_error() const
Return whether evaluation completed with an error.
result(std::exception_ptr exception)
Construct a failed result from an unexpected exception.
result(T value)
Construct a completed result from value.
T const && get() const &&
Return the value or throw the stored error.
Identifies handler specs that explicitly list produced subqueries.
Identifies a complete query handler specification.
Identifies the required structural portion of a handler spec.
Identifies a type that describes a memoized query.
Identifies a result class produced within a parent query node.
Detects a value-provided to_debug_string() operation.
Serializable data model for QueryGraph diagnostic dumps.
std::array< std::byte, 16 > tsig
Fixed-size RPNXSerialization type-signature digest.
Definition graphdata.hpp:42
status
Terminal or in-progress state recorded for a dumped result.
Definition graphdata.hpp:26
Default hashing customization point for memoized query inputs.
std::condition_variable fast_cv
Condition variable compatible with fast_mutex.
std::mutex fast_mutex
Platform-selected mutex used by scheduler queues.
typename query_handler_query_t< HandlerSpec >::output_type query_handler_output_t
Output type of the query implemented by HandlerSpec.
std::string format_debug_message_with_source(debug_message const &msg)
Prefix a diagnostic message with its source file and line.
typename query_handler_query_t< HandlerSpec >::input_type query_handler_input_t
Input type of the query implemented by HandlerSpec.
typename query_handler_traits< HandlerSpec >::query query_handler_query_t
Normalized query type implemented by HandlerSpec.
typename query_handler_traits< HandlerSpec >::produced_subqueries query_handler_produced_subqueries_t
Subquery result classes that HandlerSpec may yield.
memory_class
Coarse estimate of a query node's peak memory requirement.
std::shared_mutex shared_mutex
Shared mutex type used by query result awaitables.
typename query_handler_traits< HandlerSpec >::dependencies query_handler_dependencies_t
Declared query and subquery dependencies of HandlerSpec.
canonical_error_storage make_canonical_error_storage(Error const &error)
Capture a concrete exception in canonical type-erased storage.
static std::vector< std::byte > serialize_to_binary(std::monostate const &)
Return the unique empty encoding of a monostate value.
Binary serialization customization point for dumpable values.
static std::vector< std::byte > serialize_to_binary(T const &value)
Serialize one value into a byte vector.
Serializable representation of a registered canonical error.
Definition graphdata.hpp:46
Type-erased, copyable storage for a registered exception value.
std::function< void(std::any const &) > m_throw_copy
Operation that throws a copy of m_error_value.
void throw_copy() const
Throw a copy of the stored concrete exception.
std::any m_error_value
Type-erased concrete exception value.
tsig m_error_typesig
Type signature of the concrete exception type.
std::function< std::string(std::any const &) > m_message
Operation that obtains the concrete exception message.
canonical_error_result_data dump() const
Convert the error to its serializable dump representation.
std::string message() const
Return the stored exception's human-readable message.
std::function< std::vector< std::byte >(std::any const &) > m_serialize
Operation that serializes m_error_value.
std::exception_ptr make_exception_ptr() const
Return an exception_ptr holding a copy of the error.
Owning coroutine return object for a registered query handler.
HandlerSpec spec_type
Handler specification associated with this coroutine.
cosubroutine_impl< HandlerSpec, Ret > cosubroutine
Coroutine-aware, non-memoized sub-operation return type.
Coroutine yield value that schedules a dependency without waiting.
dependency(request< QuerySpec > request)
Wrap a request for use with co_yield.
request< QuerySpec > m_request
Request to schedule without immediately awaiting it.
Configure cumulative node self-time diagnostics.
std::chrono::nanoseconds long_running_node_threshold
Completion diagnostics require self-time greater than this duration.
std::chrono::nanoseconds long_running_progress_threshold
Progress diagnostics additionally require this much self-time. Progress is checked after coroutine re...
std::function< void(std::string const &) > timing_logger
Receive complete timing messages, including a trailing newline. The default writes to standard error....
bool enable_timing
Collect node self-time and emit timing diagnostics when enabled.
Awaitable request for a memoized query value.
QuerySpec::input_type m_input
Input value used to locate or create the requested node.
request(typename QuerySpec::input_type input)
Construct a request by taking ownership of its input.
Configure the shared worker pool and runnable queue.
std::optional< std::size_t > global_workitem_threshold
Target shared queue depth; defaults to the resolved worker count.
std::optional< std::size_t > thread_count
Worker count; defaults to twice hardware concurrency, or two.
Awaitable request for a subquery value produced by a parent node.
subquery_request(typename SubquerySpec::parent_query::input_type parent_input, typename SubquerySpec::input_type input)
Construct a request scoped to one parent input.
SubquerySpec::parent_query::input_type m_parent_input
Input identifying the parent query node.
SubquerySpec::input_type m_input
Input identifying the subquery slot within the parent.
Coroutine yield value that publishes one subquery result.
subquery_result(typename SubquerySpec::input_type input, typename SubquerySpec::output_type output)
Construct a subquery input/output pair to publish.
SubquerySpec::input_type m_input
Input identifying the produced subquery slot.
SubquerySpec::output_type m_output
Output to publish for the identified slot.
A diagnostic emitted by a running query handler.
debug_message(std::format_string< Ts... > format_string, Ts &&... args)
Construct and format a diagnostic message.
std::string message
Already-formatted diagnostic text.
debug_message(debug_message const &other)=default
Copy a message and its recorded location.
std::source_location location
Source location assigned at the handler's co_yield site.
static std::string to_debug_string(std::monostate const &)
Return the literal std::monostate{}.
static std::string to_debug_string(std::string const &value)
Return the C++ debug-format representation of value.
Human-readable formatting customization point for query values.
static std::string to_debug_string(T const &value)
Convert value to diagnostic text.
Complete serializable snapshot of a QueryGraph instance.
Boolean trait equivalent of query_handler_spec_c.
typename T::produced_subqueries produced_subqueries
Subquery specifications the handler may publish.
Supplies an empty produced-subquery list when a handler omits it.
rpnx::typelist<> produced_subqueries
Default empty list used when T has no declaration.
typename T::query query
Query specification implemented by the handler.
typename query_handler_produced_subquery_traits< T >::produced_subqueries produced_subqueries
Declared produced subqueries, or an empty typelist.
typename T::dependencies dependencies
Declared query and subquery dependencies.
Normalizes the nested types of a handler specification.