diff options
| author | Felix Morgner <felix.morgner@ost.ch> | 2026-07-26 21:46:37 +0200 |
|---|---|---|
| committer | Felix Morgner <felix.morgner@ost.ch> | 2026-07-26 21:46:37 +0200 |
| commit | 4b28e4626e744ac9b779a680f8e9647014956dda (patch) | |
| tree | 9b5203e083ed27091c70d60e3cdf9284af680c81 /kernel/kapi/devices/bus.cpp | |
| parent | e7cb0a5dab291d453fa34c5d1250d85e82478ecf (diff) | |
| download | kernel-4b28e4626e744ac9b779a680f8e9647014956dda.tar.xz kernel-4b28e4626e744ac9b779a680f8e9647014956dda.zip | |
kapi/devices: implement locking discipline
Diffstat (limited to 'kernel/kapi/devices/bus.cpp')
| -rw-r--r-- | kernel/kapi/devices/bus.cpp | 38 |
1 files changed, 21 insertions, 17 deletions
diff --git a/kernel/kapi/devices/bus.cpp b/kernel/kapi/devices/bus.cpp index d917eb96..71dd01e6 100644 --- a/kernel/kapi/devices/bus.cpp +++ b/kernel/kapi/devices/bus.cpp @@ -6,6 +6,7 @@ #include <kapi/system.hpp> #include <kstd/memory.hpp> +#include <kstd/mutex.hpp> #include <kstd/string.hpp> #include <kstd/vector.hpp> @@ -42,30 +43,32 @@ namespace kapi::devices kapi::system::panic("[OS:DEV] Failed to register child device {}", child->name()); } - // TODO: lock bus + auto attached = kstd::shared_ptr<device>{}; + { + auto guard = kstd::lock_guard{m_lock}; + attached = m_devices.emplace_back(std::move(child)); + } - auto & attached = m_devices.emplace_back(std::move(child)); attached->set_state(state::present); - // TODO: unlock bus - driver_registry::get().device_attached(attached); } auto bus::remove_child(device & device) -> bool // NOLINT(misc-no-recursion) { - // TODO: lock bus - - auto found = std::ranges::find_if(m_devices, [&](auto const & d) { return d.get() == &device; }); - if (!found) + auto lifeline = kstd::shared_ptr<struct device>{}; { - return false; - } + auto guard = kstd::lock_guard{m_lock}; - auto lifeline = *found; - m_devices.erase(found); + auto found = std::ranges::find_if(m_devices, [&](auto const & d) { return d.get() == &device; }); + if (found == m_devices.end()) + { + return false; + } - // TODO: unlock bus + lifeline = *found; + m_devices.erase(found); + } do_remove_child(*lifeline); return true; @@ -100,10 +103,11 @@ namespace kapi::devices if (auto child_bus = device.facet<bus>()) { auto grandchildren = kstd::vector<kstd::shared_ptr<kapi::devices::device>>{}; - // TODO: lock child bus - // NOTE: this could be more efficient one vector::assign is implemented. - grandchildren = kstd::vector(child_bus->m_devices.begin(), child_bus->m_devices.end()); - // TODO: unlock child bus + { + auto guard = kstd::lock_guard{m_lock}; + // NOTE: this could be more efficient one vector::assign is implemented. + grandchildren = kstd::vector(child_bus->m_devices.begin(), child_bus->m_devices.end()); + } for (auto & grandchild : grandchildren) { |
