[libc-commits] [libc] [libc][arc4random][patch 2/4] add ABA protected MPMCStack (PR #151361)

Michael Jones via libc-commits libc-commits at lists.llvm.org
Tue Sep 2 13:53:10 PDT 2025


================
@@ -0,0 +1,88 @@
+//===-- Transactional Ptr for ABA prevention --------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_ABA_PTR_H
+#define LLVM_LIBC_SRC___SUPPORT_ABA_PTR_H
+
+#include "hdr/types/size_t.h"
+#include "src/__support/CPP/atomic.h"
+#include "src/__support/common.h"
+#include "src/__support/threads/sleep.h"
+
+#ifdef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_16
+#define LIBC_ABA_PTR_IS_ATOMIC true
+#else
+#define LIBC_ABA_PTR_IS_ATOMIC false
+#endif
+
+namespace LIBC_NAMESPACE_DECL {
+
+template <class T, bool IsAtomic> struct AbaPtrImpl {
+  union Impl {
+    struct alignas(2 * alignof(void *)) Atomic {
+      T *ptr;
+      size_t tag;
+    } atomic;
+    struct Mutex {
+      T *ptr;
+      bool locked;
+    } mutex;
+  } impl;
----------------
michaelrj-google wrote:

It seems like `IsAtomic` switches basically everything about this class except for its interface. If you want to have the two implementations it would be cleaner to use static inheritance to keep these separate.

https://github.com/llvm/llvm-project/pull/151361


More information about the libc-commits mailing list