[libc-commits] [libc] [libc] Implement simple lock-free stack data structure (PR #83026)

Joseph Huber via libc-commits libc-commits at lists.llvm.org
Wed Feb 28 11:32:55 PST 2024


================
@@ -0,0 +1,129 @@
+//===-- A lock-free data structure for a fixed capacity stack ---*- 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_FIXEDSTACK_H
+#define LLVM_LIBC_SRC___SUPPORT_FIXEDSTACK_H
+
+#include "src/__support/CPP/array.h"
+#include "src/__support/CPP/atomic.h"
+#include "src/__support/threads/sleep.h"
+
+#include <stdint.h>
+
+namespace LIBC_NAMESPACE {
+
+// A lock-free fixed size stack backed by an underlying cpp::array data
+// structure. It supports push and pop operations in a thread safe manner.
+template <typename T, uint32_t CAPACITY> class alignas(16) FixedStack {
+  static_assert(CAPACITY < 1024 * 1024, "Invalid buffer size");
----------------
jhuber6 wrote:

Done. Unfortunately I don't know of any other use-cases for a strict stack in `libc`. The other uses of `atexit` just get to claim the whole thing with a mutex so they don't need to worry about this and just let it append.

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


More information about the libc-commits mailing list