From d10b2cc2a128c27517c2ad8cb98f975c2d3aa4bc Mon Sep 17 00:00:00 2001 From: Aiken Harris Date: Tue, 23 Jun 2026 10:40:13 +0200 Subject: [PATCH] Add atomic 128-bit compare-exchange function --- xtoskrnl/includes/rtl/atomic.hh | 3 +++ xtoskrnl/rtl/atomic.cc | 35 +++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/xtoskrnl/includes/rtl/atomic.hh b/xtoskrnl/includes/rtl/atomic.hh index b98df71..08f74e6 100644 --- a/xtoskrnl/includes/rtl/atomic.hh +++ b/xtoskrnl/includes/rtl/atomic.hh @@ -52,6 +52,9 @@ namespace RTL STATIC XTFASTCALL LONG_PTR CompareExchange64(IN PLONG_PTR Address, IN LONG_PTR Comperand, IN LONG_PTR Exchange); + STATIC XTFASTCALL DOUBLE_ULONG_PTR CompareExchange128(IN PDOUBLE_ULONG_PTR Address, + IN DOUBLE_ULONG_PTR Comperand, + IN DOUBLE_ULONG_PTR Exchange); STATIC XTFASTCALL PVOID CompareExchangePointer(IN PVOID *Address, IN PVOID Comperand, IN PVOID Exchange); diff --git a/xtoskrnl/rtl/atomic.cc b/xtoskrnl/rtl/atomic.cc index cf8a28d..ab0a726 100644 --- a/xtoskrnl/rtl/atomic.cc +++ b/xtoskrnl/rtl/atomic.cc @@ -341,6 +341,41 @@ RTL::Atomic::CompareExchange64(IN PLONG_PTR Address, return __sync_val_compare_and_swap(Address, Comperand, Exchange); } +/** + * Performs an atomic compare-exchange operation on the 64-bit or 128-bit value depending on architecture. + * + * @param Address + * Supplies the address of the value to compare and potentially exchange. + * + * @param Comperand + * Supplies the value to compare against. + * + * @param Exchange + * Supplies the value to write if the comparison returns equality. + * + * @return This routine returns the original value at the given address. + * + * @since XT 1.0 + */ +XTFASTCALL +DOUBLE_ULONG_PTR +RTL::Atomic::CompareExchange128(IN PDOUBLE_ULONG_PTR Address, + IN DOUBLE_ULONG_PTR Comperand, + IN DOUBLE_ULONG_PTR Exchange) +{ + DOUBLE_ULONG_PTR Value; + + /* Make a local copy of the comperand */ + Value = Comperand; + + /* Perform an atomic compare-exchange operation */ + __atomic_compare_exchange((VOLATILE PDOUBLE_ULONG_PTR)Address, &Value, &Exchange, + FALSE, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST); + + /* Return value */ + return Value; +} + /** * Performs atomically compare exchange operation. *