aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelix Morgner <felix.morgner@ost.ch>2026-07-15 12:19:17 +0200
committerFelix Morgner <felix.morgner@ost.ch>2026-07-15 12:19:17 +0200
commit8a647acda62a1a86386d6ce0d8ccfa5902899634 (patch)
tree54c3c73b676b7aec753da8fdd278bf05e79340c6
parentba0cf20847d9aa8eb6c540ef7be20972010d6d64 (diff)
downloadkernel-8a647acda62a1a86386d6ce0d8ccfa5902899634.tar.xz
kernel-8a647acda62a1a86386d6ce0d8ccfa5902899634.zip
kstd: fix shared_ptr constructor race
-rw-r--r--libs/kstd/kstd/bits/shared_ptr.hpp15
1 files changed, 11 insertions, 4 deletions
diff --git a/libs/kstd/kstd/bits/shared_ptr.hpp b/libs/kstd/kstd/bits/shared_ptr.hpp
index ce4bfb7f..f4aed34e 100644
--- a/libs/kstd/kstd/bits/shared_ptr.hpp
+++ b/libs/kstd/kstd/bits/shared_ptr.hpp
@@ -266,11 +266,18 @@ namespace kstd
: pointer(nullptr)
, control(nullptr)
{
- if (other.control != nullptr && other.control->shared_count != 0)
+ if (other.control)
{
- pointer = other.pointer;
- control = other.control;
- ++(control->shared_count);
+ auto count = other.control->shared_count.load(std::memory_order::relaxed);
+ while (count != 0 && !other.control->shared_count.compare_exchange_weak(
+ count, count + 1, std::memory_order::acquire, std::memory_order::relaxed))
+ ;
+
+ if (count != 0)
+ {
+ pointer = other.pointer;
+ control = other.control;
+ }
}
}