blob: 908def31abd8396abd90bef434a7d72e08e570da [file] [log] [blame]
Patrick Williams0139cac2022-09-20 17:04:17 -05001#include <sdbusplus/async/scope.hpp>
2
3#include <exception>
4
5namespace sdbusplus::async
6{
7scope::~scope() noexcept(false)
8{
Patrick Williamsc5b5ff52022-09-21 08:34:22 -05009 if (pending_count != 0)
Patrick Williams0139cac2022-09-20 17:04:17 -050010 {
11 throw std::logic_error(
12 "sdbusplus::async::scope destructed while tasks are pending.");
13 }
14}
15
16void scope::started_task() noexcept
17{
Patrick Williamsc5b5ff52022-09-21 08:34:22 -050018 std::lock_guard l{lock};
19 ++pending_count;
Patrick Williams0139cac2022-09-20 17:04:17 -050020}
21
22void scope::ended_task() noexcept
23{
Patrick Williamsc5b5ff52022-09-21 08:34:22 -050024 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 Williams0139cac2022-09-20 17:04:17 -050040}
41
Patrick Williamsc5b5ff52022-09-21 08:34:22 -050042scope_ns::scope_sender scope::empty() noexcept
43{
44 return scope_ns::scope_sender(*this);
45}
46
47namespace scope_ns
48{
49void 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 Williams0139cac2022-09-20 17:04:17 -050072} // namespace sdbusplus::async