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 | 4cfc284 | 2022-09-22 09:53:33 -0500 | [diff] [blame] | 31 | if (e && pending_exception) |
| 32 | { |
| 33 | // Received a second exception without delivering the first |
| 34 | // to a pending completion. Terminate using the first one. |
| 35 | try |
| 36 | { |
| 37 | std::rethrow_exception(std::exchange(pending_exception, {})); |
| 38 | } |
| 39 | catch (...) |
| 40 | { |
| 41 | std::terminate(); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | // If the scope is complete, get the pending completion, if it exists. |
| 46 | if (e || (pending_count == 0)) |
Patrick Williams | c5b5ff5 | 2022-09-21 08:34:22 -0500 | [diff] [blame] | 47 | { |
| 48 | p = std::exchange(pending, nullptr); |
| 49 | } |
Patrick Williams | 4cfc284 | 2022-09-22 09:53:33 -0500 | [diff] [blame] | 50 | |
| 51 | // If we have an exception but no pending completion, save it away. |
| 52 | if (e && !p) |
| 53 | { |
| 54 | pending_exception = std::move(e); |
| 55 | } |
Patrick Williams | c5b5ff5 | 2022-09-21 08:34:22 -0500 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | if (p) |
| 59 | { |
Patrick Williams | 4cfc284 | 2022-09-22 09:53:33 -0500 | [diff] [blame] | 60 | p->complete(std::move(e)); |
Patrick Williams | c5b5ff5 | 2022-09-21 08:34:22 -0500 | [diff] [blame] | 61 | } |
Patrick Williams | 0139cac | 2022-09-20 17:04:17 -0500 | [diff] [blame] | 62 | } |
| 63 | |
Patrick Williams | c5b5ff5 | 2022-09-21 08:34:22 -0500 | [diff] [blame] | 64 | scope_ns::scope_sender scope::empty() noexcept |
| 65 | { |
| 66 | return scope_ns::scope_sender(*this); |
| 67 | } |
| 68 | |
| 69 | namespace scope_ns |
| 70 | { |
| 71 | void scope_completion::arm() noexcept |
| 72 | { |
| 73 | bool done = false; |
Patrick Williams | 4cfc284 | 2022-09-22 09:53:33 -0500 | [diff] [blame] | 74 | std::exception_ptr e{}; |
Patrick Williams | c5b5ff5 | 2022-09-21 08:34:22 -0500 | [diff] [blame] | 75 | |
| 76 | { |
| 77 | std::lock_guard l{s.lock}; |
Patrick Williams | 095eff8 | 2022-09-22 17:44:30 -0500 | [diff] [blame] | 78 | if (!s.started) |
| 79 | { |
| 80 | done = false; |
| 81 | } |
| 82 | else if (s.pending_count == 0) |
Patrick Williams | c5b5ff5 | 2022-09-21 08:34:22 -0500 | [diff] [blame] | 83 | { |
| 84 | done = true; |
| 85 | } |
Patrick Williams | 4cfc284 | 2022-09-22 09:53:33 -0500 | [diff] [blame] | 86 | else if (s.pending_exception) |
| 87 | { |
| 88 | e = std::exchange(s.pending_exception, {}); |
| 89 | done = true; |
| 90 | } |
Patrick Williams | c5b5ff5 | 2022-09-21 08:34:22 -0500 | [diff] [blame] | 91 | else |
| 92 | { |
| 93 | s.pending = this; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | if (done) |
| 98 | { |
Patrick Williams | 4cfc284 | 2022-09-22 09:53:33 -0500 | [diff] [blame] | 99 | this->complete(std::move(e)); |
Patrick Williams | c5b5ff5 | 2022-09-21 08:34:22 -0500 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | } // namespace scope_ns |
| 103 | |
Patrick Williams | 0139cac | 2022-09-20 17:04:17 -0500 | [diff] [blame] | 104 | } // namespace sdbusplus::async |