1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
|
#include <kapi/devices/facet_registry.hpp>
#include <kapi/devices.hpp>
#include <kapi/system.hpp>
#include <kapi/test_support/devices.hpp>
#include <kstd/memory.hpp>
#include <kstd/mutex.hpp>
#include <kstd/result.hpp>
#include <kstd/string.hpp>
#include <kstd/system_error.hpp>
#include <kstd/vector.hpp>
#include <algorithm>
#include <optional>
#include <string_view>
#include <utility>
namespace kapi::devices
{
namespace
{
constinit auto static registry = std::optional<facet_registry>{};
}
auto facet_registry::init() -> void
{
if (registry.has_value())
{
system::panic("[kernel] Device facet registry has already been initialized.");
}
registry.emplace();
}
auto facet_registry::get() -> facet_registry &
{
if (!registry)
{
system::panic("[kernel] Device facet registry has not been initialized.");
}
return *registry;
}
auto facet_registry::do_publish(kstd::shared_ptr<device> device, kstd::string name, kapi::capabilities::facet_id id,
void * facet) -> kstd::result<void>
{
auto published = std::optional<entry>{};
{
auto guard = kstd::lock_guard{m_lock};
erase_if(m_entries, [id](auto e) { return e.id() == id && !e.device(); });
if (!device || !facet || name.empty())
{
return kstd::failure(make_error_code(kstd::errc::invalid_argument));
}
auto already_published = std::ranges::any_of(
m_entries, [&](auto const & entry) { return entry.id() == id && entry.device().get() == device.get(); });
if (already_published)
{
return kstd::failure(make_error_code(kstd::errc::file_exists));
}
published = m_entries.emplace_back(device, std::move(name), id, facet);
}
do_notify_published(id, *published);
return kstd::success();
}
auto facet_registry::do_notify_published(kapi::capabilities::facet_id id, entry const & published) -> void
{
auto observers = kstd::vector<kstd::weak_ptr<facet_registry_observer>>{};
auto static_observers = kstd::vector<facet_registry_observer *>{};
{
auto guard = kstd::lock_guard{m_lock};
observers = m_observers;
static_observers = m_static_observers;
}
std::ranges::for_each(observers, [&](auto observer) {
if (auto locked_observer = observer.lock())
{
locked_observer->on_facet_published(id, published);
}
});
std::ranges::for_each(static_observers, [&](auto observer) { observer->on_facet_published(id, published); });
}
auto facet_registry::do_notify_withdrawn(kapi::capabilities::facet_id id, device const & device) -> void
{
auto observers = kstd::vector<kstd::weak_ptr<facet_registry_observer>>{};
auto static_observers = kstd::vector<facet_registry_observer *>{};
{
auto guard = kstd::lock_guard{m_lock};
observers = m_observers;
static_observers = m_static_observers;
}
std::ranges::for_each(observers, [&](auto observer) {
if (auto locked_observer = observer.lock())
{
locked_observer->on_facet_withdrawn(id, const_cast<devices::device &>(device));
}
});
std::ranges::for_each(static_observers, [&](auto observer) {
observer->on_facet_withdrawn(id, const_cast<devices::device &>(device));
});
}
auto facet_registry::withdraw(device const & device, kapi::capabilities::facet_id id) -> void
{
auto did_remove = false;
{
auto guard = kstd::lock_guard{m_lock};
did_remove = erase_if(m_entries, [&](auto e) {
auto locked_device = e.device();
return e.id() == id && locked_device && locked_device.get() == &device;
}) != 0;
}
if (did_remove)
{
do_notify_withdrawn(id, device);
}
}
auto facet_registry::withdraw_all_for(device const & device) -> void
{
auto withdrawn_facets = kstd::vector<kapi::capabilities::facet_id>{};
{
auto guard = kstd::lock_guard{m_lock};
erase_if(m_entries, [&](auto e) {
auto locked_device = e.device();
auto do_erase = locked_device && locked_device.get() == &device;
if (do_erase)
{
withdrawn_facets.push_back(e.id());
}
return do_erase;
});
}
std::ranges::for_each(withdrawn_facets, [&](auto facet) { do_notify_withdrawn(facet, device); });
}
auto facet_registry::all(kapi::capabilities::facet_id id) const -> kstd::vector<entry>
{
auto guard = kstd::lock_guard{m_lock};
auto filtered = m_entries;
erase_if(filtered, [&](auto e) {
auto locked_device = e.device();
return !(e.id() == id && locked_device);
});
return filtered;
}
auto facet_registry::resolve(kapi::capabilities::facet_id facet, std::string_view name) -> void *
{
auto found_device = kstd::shared_ptr<device>{};
{
auto guard = kstd::lock_guard{m_lock};
auto found = std::ranges::find_if(m_entries, [&](auto e) { return e.name() == name && e.device(); });
if (found == m_entries.cend())
{
return nullptr;
}
found_device = found->device();
}
if (!found_device)
{
return nullptr;
}
return resolve(facet, *found_device);
}
auto facet_registry::resolve(kapi::capabilities::facet_id id, device & device) -> void *
{
if (auto by_device = device.facet(id))
{
return by_device;
}
auto guard = kstd::lock_guard{m_lock};
auto found = std::ranges::find_if(m_entries, [&](auto e) {
auto locked_device = e.device();
return e.id() == id && locked_device && locked_device.get() == &device;
});
if (found == m_entries.cend())
{
return nullptr;
}
return found->untyped_facet();
}
auto facet_registry::subscribe(kstd::weak_ptr<facet_registry_observer> observer) -> void
{
auto guard = kstd::lock_guard{m_lock};
erase_if(m_observers, [](auto const & observer) { return observer.expired(); });
m_observers.push_back(observer);
}
auto facet_registry::subscribe(facet_registry_observer & observer) -> void
{
auto guard = kstd::lock_guard{m_lock};
m_static_observers.push_back(&observer);
}
auto facet_registry::unsubscribe(facet_registry_observer & observer) -> void
{
auto guard = kstd::lock_guard{m_lock};
erase_if(m_observers, [&](auto const & subscribed) { return subscribed.lock().get() == &observer; });
erase(m_static_observers, &observer);
}
} // namespace kapi::devices
namespace kapi::test_support::devices
{
auto deinit_facet_registry() -> void
{
kapi::devices::registry.reset();
}
} // namespace kapi::test_support::devices
|