From 4b28e4626e744ac9b779a680f8e9647014956dda Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Sun, 26 Jul 2026 21:46:37 +0200 Subject: kapi/devices: implement locking discipline --- kernel/kapi/devices/bus.cpp | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) (limited to 'kernel/kapi/devices/bus.cpp') 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 #include +#include #include #include @@ -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{}; + { + 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{}; { - 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()) { auto grandchildren = kstd::vector>{}; - // 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) { -- cgit v1.2.3