src/ex/execution_context.cpp

91.4% Lines (64/70) 100.0% List of functions (7/7) 78.7% Branches (37/47)
f(x) Functions (7)
Line Branch TLA Hits Source Code
1 //
2 // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // Official repository: https://github.com/cppalliance/capy
8 //
9
10 #include <boost/capy/ex/execution_context.hpp>
11 #include <boost/capy/ex/recycling_memory_resource.hpp>
12 #include <boost/capy/detail/except.hpp>
13
14 namespace boost {
15 namespace capy {
16
17 2983x execution_context::
18 2983x execution_context()
19 2983x : frame_alloc_(get_recycling_memory_resource())
20 {
21 2983x }
22
23 2983x execution_context::
24 ~execution_context()
25 {
26 2983x shutdown();
27 2983x destroy();
28 2983x }
29
30 void
31 3134x execution_context::
32 shutdown() noexcept
33 {
34
2/2
✓ Branch 0 taken 151 times.
✓ Branch 1 taken 2983 times.
3134x if(shutdown_)
35 151x return;
36 2983x shutdown_ = true;
37
38 2983x service* p = head_;
39
2/2
✓ Branch 0 taken 57 times.
✓ Branch 1 taken 2983 times.
3040x while(p)
40 {
41 57x p->shutdown();
42 57x p = p->next_;
43 }
44 }
45
46 void
47 3134x execution_context::
48 destroy() noexcept
49 {
50 3134x service* p = head_;
51 3134x head_ = nullptr;
52
2/2
✓ Branch 0 taken 57 times.
✓ Branch 1 taken 3134 times.
3191x while(p)
53 {
54 57x service* next = p->next_;
55
1/2
✓ Branch 0 taken 57 times.
✗ Branch 1 not taken.
57x delete p;
56 57x p = next;
57 }
58 3134x }
59
60 execution_context::service*
61 167x execution_context::
62 find_impl(detail::type_index ti) const noexcept
63 {
64 167x auto p = head_;
65
2/2
✓ Branch 0 taken 53 times.
✓ Branch 1 taken 121 times.
174x while(p)
66 {
67
6/6
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 42 times.
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 7 times.
✓ Branch 6 taken 46 times.
✓ Branch 7 taken 7 times.
53x if(p->t0_ == ti || p->t1_ == ti)
68 46x break;
69 7x p = p->next_;
70 }
71 167x return p;
72 }
73
74 execution_context::service&
75 74x execution_context::
76 use_service_impl(factory& f)
77 {
78
1/1
✓ Branch 1 taken 74 times.
74x std::unique_lock<std::mutex> lock(mutex_);
79
80
2/2
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 50 times.
74x if(auto* p = find_impl(f.t0))
81 24x return *p;
82
83
1/1
✓ Branch 1 taken 50 times.
50x lock.unlock();
84
85 // Create the service outside lock, enabling nested calls
86
1/1
✓ Branch 1 taken 50 times.
50x service* sp = f.create(*this);
87 50x sp->t0_ = f.t0;
88 50x sp->t1_ = f.t1;
89
90
1/1
✓ Branch 1 taken 50 times.
50x lock.lock();
91
92
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
50x if(auto* p = find_impl(f.t0))
93 {
94 delete sp;
95 return *p;
96 }
97
98 50x sp->next_ = head_;
99 50x head_ = sp;
100
101 50x return *sp;
102 74x }
103
104 execution_context::service&
105 10x execution_context::
106 make_service_impl(factory& f)
107 {
108 {
109
1/1
✓ Branch 1 taken 10 times.
10x std::lock_guard<std::mutex> lock(mutex_);
110
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 8 times.
10x if(find_impl(f.t0))
111 2x detail::throw_invalid_argument();
112
6/6
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 6 times.
✓ Branch 4 taken 1 time.
✓ Branch 5 taken 1 time.
✓ Branch 6 taken 1 time.
✓ Branch 7 taken 7 times.
8x if(f.t0 != f.t1 && find_impl(f.t1))
113 1x detail::throw_invalid_argument();
114 10x }
115
116 // Unlocked to allow nested service creation from constructor
117
1/1
✓ Branch 1 taken 7 times.
7x service* p = f.create(*this);
118
119
1/1
✓ Branch 1 taken 7 times.
7x std::lock_guard<std::mutex> lock(mutex_);
120
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
7x if(find_impl(f.t0))
121 {
122 delete p;
123 detail::throw_invalid_argument();
124 }
125
126 7x p->t0_ = f.t0;
127
2/2
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 6 times.
7x if(f.t0 != f.t1)
128 {
129
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
1x if(find_impl(f.t1))
130 {
131 delete p;
132 detail::throw_invalid_argument();
133 }
134 1x p->t1_ = f.t1;
135 }
136 else
137 {
138 6x p->t1_ = f.t0;
139 }
140
141 7x p->next_ = head_;
142 7x head_ = p;
143
144 7x return *p;
145 7x }
146
147 } // namespace capy
148 } // namespace boost
149