Patrick Williams | 0139cac | 2022-09-20 17:04:17 -0500 | [diff] [blame] | 1 | #include <sdbusplus/async/scope.hpp> |
| 2 | |
| 3 | #include <exception> |
| 4 | |
| 5 | namespace sdbusplus::async |
| 6 | { |
| 7 | scope::~scope() noexcept(false) |
| 8 | { |
Patrick Williams | c5b5ff5 | 2022-09-21 08:34:22 -0500 | [diff] [blame] | 9 | if (pending_count != 0) |
Patrick Williams | 0139cac | 2022-09-20 17:04:17 -0500 | [diff] [blame] | 10 | { |
| 11 | throw std::logic_error( |
| 12 | "sdbusplus::async::scope destructed while tasks are pending."); |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | void scope::started_task() noexcept |
| 17 | { |
Patrick Williams | c5b5ff5 | 2022-09-21 08:34:22 -0500 | [diff] [blame] | 18 | std::lock_guard l{lock}; |
Patrick Williams | 095eff8 | 2022-09-22 17:44:30 -0500 | [diff] [blame] | 19 | started = true; |
Patrick Williams | c5b5ff5 | 2022-09-21 08:34:22 -0500 | [diff] [blame] | 20 | ++pending_count; |
Patrick Williams | 0139cac | 2022-09-20 17:04:17 -0500 | [diff] [blame] | 21 | } |
| 22 | |
Patrick Williams | 4cfc284 | 2022-09-22 09:53:33 -0500 | [diff] [blame] | 23 | void scope::ended_task(std::exception_ptr&& e) noexcept |
Patrick Williams | 0139cac | 2022-09-20 17:04:17 -0500 | [diff] [blame] | 24 | { |
Patrick Williams | c5b5ff5 | 2022-09-21 08:34:22 -0500 | [diff] [blame] | 25 | scope_ns::scope_completion* p = nullptr; |
| 26 | |
| 27 | { |
| 28 | std::lock_guard l{lock}; |
Patrick Williams | 4cfc284 | 2022-09-22 09:53:33 -0500 | [diff] [blame] | 29 | --pending_count; // decrement count. |
Patrick Williams | c5b5ff5 | 2022-09-21 08:34:22 -0500 | [diff] [blame] | 30 | |
Patrick Williams | 115e9b3 | 2022-09-23 16:20:59 -0500 | [diff] [blame] | 31 | if (e) |
Patrick Williams | 4cfc284 | 2022-09-22 09:53:33 -0500 | [diff] [blame] | 32 | { |
Patrick Williams | 115e9b3 | 2022-09-23 16:20:59 -0500 | [diff] [blame] | 33 | pending_exceptions.emplace_back(std::exchange(e, {})); |
Patrick Williams | 4cfc284 | 2022-09-22 09:53:33 -0500 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | // If the scope is complete, get the pending completion, if it exists. |
Patrick Williams | 115e9b3 | 2022-09-23 16:20:59 -0500 | [diff] [blame] | 37 | if (!pending_exceptions.empty() || (pending_count == 0)) |
Patrick Williams | c5b5ff5 | 2022-09-21 08:34:22 -0500 | [diff] [blame] | 38 | { |
| 39 | p = std::exchange(pending, nullptr); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | if (p) |
| 44 | { |
Patrick Williams | 115e9b3 | 2022-09-23 16:20:59 -0500 | [diff] [blame] | 45 | std::exception_ptr fwd_exception = {}; |
| 46 | if (!pending_exceptions.empty()) |
| 47 | { |
| 48 | fwd_exception = pending_exceptions.front(); |
| 49 | pending_exceptions.pop_front(); |
| 50 | } |
| 51 | p->complete(std::move(fwd_exception)); |
Patrick Williams | c5b5ff5 | 2022-09-21 08:34:22 -0500 | [diff] [blame] | 52 | } |
Patrick Williams | 0139cac | 2022-09-20 17:04:17 -0500 | [diff] [blame] | 53 | } |
| 54 | |
Patrick Williams | c5b5ff5 | 2022-09-21 08:34:22 -0500 | [diff] [blame] | 55 | scope_ns::scope_sender scope::empty() noexcept |
| 56 | { |
| 57 | return scope_ns::scope_sender(*this); |
| 58 | } |
| 59 | |
| 60 | namespace scope_ns |
| 61 | { |
| 62 | void scope_completion::arm() noexcept |
| 63 | { |
| 64 | bool done = false; |
Patrick Williams | 4cfc284 | 2022-09-22 09:53:33 -0500 | [diff] [blame] | 65 | std::exception_ptr e{}; |
Patrick Williams | c5b5ff5 | 2022-09-21 08:34:22 -0500 | [diff] [blame] | 66 | |
| 67 | { |
| 68 | std::lock_guard l{s.lock}; |
Patrick Williams | 095eff8 | 2022-09-22 17:44:30 -0500 | [diff] [blame] | 69 | if (!s.started) |
| 70 | { |
| 71 | done = false; |
| 72 | } |
Patrick Williams | 115e9b3 | 2022-09-23 16:20:59 -0500 | [diff] [blame] | 73 | else if (!s.pending_exceptions.empty()) |
Patrick Williams | c5b5ff5 | 2022-09-21 08:34:22 -0500 | [diff] [blame] | 74 | { |
Patrick Williams | 115e9b3 | 2022-09-23 16:20:59 -0500 | [diff] [blame] | 75 | e = s.pending_exceptions.front(); |
| 76 | s.pending_exceptions.pop_front(); |
Patrick Williams | c5b5ff5 | 2022-09-21 08:34:22 -0500 | [diff] [blame] | 77 | done = true; |
| 78 | } |
Patrick Williams | 115e9b3 | 2022-09-23 16:20:59 -0500 | [diff] [blame] | 79 | else if (s.pending_count == 0) |
Patrick Williams | 4cfc284 | 2022-09-22 09:53:33 -0500 | [diff] [blame] | 80 | { |
Patrick Williams | 4cfc284 | 2022-09-22 09:53:33 -0500 | [diff] [blame] | 81 | done = true; |
| 82 | } |
Patrick Williams | c5b5ff5 | 2022-09-21 08:34:22 -0500 | [diff] [blame] | 83 | else |
| 84 | { |
| 85 | s.pending = this; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | if (done) |
| 90 | { |
Patrick Williams | 4cfc284 | 2022-09-22 09:53:33 -0500 | [diff] [blame] | 91 | this->complete(std::move(e)); |
Patrick Williams | c5b5ff5 | 2022-09-21 08:34:22 -0500 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | } // namespace scope_ns |
| 95 | |
Patrick Williams | 0139cac | 2022-09-20 17:04:17 -0500 | [diff] [blame] | 96 | } // namespace sdbusplus::async |