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