Add cost and return false if the limit is exceeded.
| 83 | |
| 84 | /// Add cost and return false if the limit is exceeded. |
| 85 | bool addCost(uint64_t Cost) { |
| 86 | using namespace std::literals; |
| 87 | const auto Limit = CostLimit; |
| 88 | uint64_t OldCostSum = CostSum.load(std::memory_order_relaxed); |
| 89 | uint64_t NewCostSum; |
| 90 | do { |
| 91 | NewCostSum = OldCostSum + Cost; |
| 92 | if (unlikely(NewCostSum > Limit)) { |
| 93 | spdlog::error("Cost exceeded limit. Force terminate the execution."sv); |
| 94 | return false; |
| 95 | } |
| 96 | } while (!CostSum.compare_exchange_weak(OldCostSum, NewCostSum, |
| 97 | std::memory_order_relaxed)); |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | /// Return cost back. |
| 102 | bool subCost(uint64_t Cost) { |
no test coverage detected