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 | |
| 22 | void scope::ended_task() noexcept |
| 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}; |
| 28 | --pending_count; |
| 29 | |
| 30 | if (pending_count == 0) |
| 31 | { |
| 32 | p = std::exchange(pending, nullptr); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | if (p) |
| 37 | { |
| 38 | p->complete(); |
| 39 | } |
Patrick Williams | 0139cac | 2022-09-20 17:04:17 -0500 | [diff] [blame] | 40 | } |
| 41 | |
Patrick Williams | c5b5ff5 | 2022-09-21 08:34:22 -0500 | [diff] [blame^] | 42 | scope_ns::scope_sender scope::empty() noexcept |
| 43 | { |
| 44 | return scope_ns::scope_sender(*this); |
| 45 | } |
| 46 | |
| 47 | namespace scope_ns |
| 48 | { |
| 49 | void scope_completion::arm() noexcept |
| 50 | { |
| 51 | bool done = false; |
| 52 | |
| 53 | { |
| 54 | std::lock_guard l{s.lock}; |
| 55 | if (s.pending_count == 0) |
| 56 | { |
| 57 | done = true; |
| 58 | } |
| 59 | else |
| 60 | { |
| 61 | s.pending = this; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | if (done) |
| 66 | { |
| 67 | this->complete(); |
| 68 | } |
| 69 | } |
| 70 | } // namespace scope_ns |
| 71 | |
Patrick Williams | 0139cac | 2022-09-20 17:04:17 -0500 | [diff] [blame] | 72 | } // namespace sdbusplus::async |