C++ Mathematical Expression Toolkit (ExprTk) release
Loading...
Searching...
No Matches
exprtk_loop_timeout_rtc.cpp
Go to the documentation of this file.
1/*
2 **************************************************************
3 * C++ Mathematical Expression Toolkit Library *
4 * *
5 * ExprTk Loop Timeout RTC *
6 * Author: Arash Partow (1999-2024) *
7 * URL: https://www.partow.net/programming/exprtk/index.html *
8 * *
9 * Copyright notice: *
10 * Free use of the Mathematical Expression Toolkit Library is *
11 * permitted under the guidelines and in accordance with the *
12 * most current version of the MIT License. *
13 * https://www.opensource.org/licenses/MIT *
14 * SPDX-License-Identifier: MIT *
15 * *
16 **************************************************************
17*/
18
19
20#include <chrono>
21#include <cstdio>
22#include <string>
23
24#include "exprtk.hpp"
25
26
28{
32
33 class timeout_exception final : public std::runtime_error
34 {
35 public:
36
37 timeout_exception(const std::string& what = "")
38 : std::runtime_error(what)
39 {}
40 };
41
42 using time_point_t = std::chrono::time_point<std::chrono::steady_clock>;
43
44 void set_timeout_time(const time_point_t& timeout_tp)
45 {
46 timeout_tp_ = timeout_tp;
47 }
48
49 bool check() override
50 {
51 static constexpr std::size_t max_iters_per_check = 100000;
52
53 if (++iterations_ >= max_iters_per_check)
54 {
55 if (std::chrono::steady_clock::now() >= timeout_tp_)
56 {
57 return false;
58 }
59
60 iterations_ = 0;
61 }
62
63 return true;
64 }
65
67 {
68 throw timeout_exception("Loop run-time timeout violation.");
69 }
70
71 std::size_t iterations_ = 0;
73};
74
76{
78 {
79 throw std::runtime_error("Runtime vector access violation.");
80 return false;
81 }
82};
83
84template <typename T>
86{
87 typedef exprtk::expression<T> expression_t;
88 typedef exprtk::parser<T> parser_t;
89 typedef exprtk::loop_runtime_check loop_runtime_check_t;
90
91 const std::string sieve_of_eratosthenes_program =
92 " var sieve[10^8] := [false]; "
93 " var m := trunc(sqrt(sieve[])); "
94 " "
95 " sieve[0] := true; "
96 " sieve[1] := true; "
97 " "
98 " for (var i := 0; i <= m; i += 1) "
99 " { "
100 " if (false == sieve[i]) "
101 " { "
102 " for (var j := i^2; j < sieve[]; j += i) "
103 " { "
104 " sieve[j] := true; "
105 " } "
106 " } "
107 " }; "
108 " "
109 " var prime_count := sieve[] - sum(sieve); "
110 " "
111 " prime_count == 5761455; ";
112
113 timeout_rtc_handler loop_runtime_check;
114 loop_runtime_check.loop_set = loop_runtime_check_t::e_all_loops;
115 loop_runtime_check.max_loop_iterations =
116 static_cast<std::uint64_t>(10e8 * std::log10(std::log10(10e8))); // O(n) = nlog(log(n))
117
118 vector_access_rtc vec_rtc;
119
120 expression_t expression;
121
122 parser_t parser;
123
124 parser.register_loop_runtime_check(loop_runtime_check);
125 parser.register_vector_access_runtime_check(vec_rtc);
126
127 parser.compile(sieve_of_eratosthenes_program,expression);
128
129 const auto max_duration = std::chrono::seconds(1);
130 const auto timeout_tp = std::chrono::steady_clock::now() + max_duration;
131 loop_runtime_check.set_timeout_time(timeout_tp);
132
133 try
134 {
135 expression.value();
136 }
138 {
139 printf("Timeout Exception\n");
140 }
141 catch (std::runtime_error& rte)
142 {
143 printf("Exception: %s\n",rte.what());
144 }
145
146 parser.clear_loop_runtime_check();
147}
148
149int main()
150{
151 loop_timeout_rtc<double>();
152 return 0;
153}
void loop_timeout_rtc()
details::_uint64_t max_loop_iterations
Definition exprtk.hpp:2145
std::chrono::time_point< std::chrono::steady_clock > time_point_t
void handle_runtime_violation(const violation_context &) override
void set_timeout_time(const time_point_t &timeout_tp)
bool handle_runtime_violation(violation_context &) override