From 7d9c338e2a100c6ddcbaa03d84c33661196bb717 Mon Sep 17 00:00:00 2001 From: Aiken Harris Date: Mon, 29 Jun 2026 00:31:55 +0200 Subject: [PATCH] Provide atomic bit test and clear functionality --- xtoskrnl/includes/rtl/atomic.hh | 4 +++ xtoskrnl/rtl/atomic.cc | 46 +++++++++++++++++++++++++++++++-- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/xtoskrnl/includes/rtl/atomic.hh b/xtoskrnl/includes/rtl/atomic.hh index 1501795..1325bf5 100644 --- a/xtoskrnl/includes/rtl/atomic.hh +++ b/xtoskrnl/includes/rtl/atomic.hh @@ -36,6 +36,10 @@ namespace RTL IN LONG Mask); STATIC XTFASTCALL LONG_PTR And64(IN PLONG_PTR Address, IN LONG_PTR Mask); + STATIC XTFASTCALL UCHAR BitTestAndReset(IN PLONG Base, + IN LONG Offset); + STATIC XTFASTCALL UCHAR BitTestAndReset64(IN PLONGLONG Base, + IN LONGLONG Offset); STATIC XTFASTCALL UCHAR BitTestAndSet(IN PLONG Base, IN LONG Offset); STATIC XTFASTCALL UCHAR BitTestAndSet64(IN PLONGLONG Base, diff --git a/xtoskrnl/rtl/atomic.cc b/xtoskrnl/rtl/atomic.cc index 4d75041..a115ef1 100644 --- a/xtoskrnl/rtl/atomic.cc +++ b/xtoskrnl/rtl/atomic.cc @@ -199,6 +199,48 @@ RTL::Atomic::And64(IN PLONG_PTR Address, return __sync_fetch_and_and(Address, Mask); } +/** + * Performs an atomic test of the specified bit of the specified long value and resets it to 0. + * + * @param Base + * Supplies a pointer to the variable. + * + * @param Offset + * Specifies the bit position to be tested. + * + * @return This routine returns the original value of the specified bit. + * + * @since XT 1.0 + */ +XTFASTCALL +UCHAR +RTL::Atomic::BitTestAndReset(IN PLONG Base, + IN LONG Offset) +{ + return (__atomic_fetch_and(Base, ~(1l << Offset), __ATOMIC_SEQ_CST) >> Offset) & 1; +} + +/** + * Performs an atomic test of the specified bit of the specified 64-bit long value and resets it to 0. + * + * @param Base + * Supplies a pointer to the variable. + * + * @param Offset + * Specifies the bit position to be tested. + * + * @return This routine returns the original value of the specified bit. + * + * @since XT 1.0 + */ +XTFASTCALL +UCHAR +RTL::Atomic::BitTestAndReset64(IN PLONGLONG Base, + IN LONGLONG Offset) +{ + return (__atomic_fetch_and(Base, ~(1ll << Offset), __ATOMIC_SEQ_CST) >> Offset) & 1; +} + /** * Performs an atomic test of the specified bit of the specified long value and sets it to 1. * @@ -208,7 +250,7 @@ RTL::Atomic::And64(IN PLONG_PTR Address, * @param Offset * Specifies the bit position to be tested. * - * @return Returns a value of the specified bit. + * @return This routine returns a value of the specified bit. * * @since XT 1.0 */ @@ -229,7 +271,7 @@ RTL::Atomic::BitTestAndSet(IN PLONG Base, * @param Offset * Specifies the bit position to be tested. * - * @return Returns a value of the specified bit. + * @return This routine returns a value of the specified bit. * * @since XT 1.0 */