From c2d4216695b48dfe6bf7083c11e0a7fcbb671e2e Mon Sep 17 00:00:00 2001 From: Steven Le Rouzic Date: Fri, 3 Jan 2025 01:49:42 +0100 Subject: Add atomic & is_integer --- asl/BUILD.bazel | 4 ++++ asl/atomic.hpp | 51 ++++++++++++++++++++++++++++++++++++++++++++++ asl/meta.hpp | 12 +++++++++++ asl/status.cpp | 0 asl/status.hpp | 13 ++++++++++++ asl/tests/status_tests.cpp | 0 todo.txt | 1 + 7 files changed, 81 insertions(+) create mode 100644 asl/atomic.hpp create mode 100644 asl/status.cpp create mode 100644 asl/status.hpp create mode 100644 asl/tests/status_tests.cpp diff --git a/asl/BUILD.bazel b/asl/BUILD.bazel index 54db8c7..da1018e 100644 --- a/asl/BUILD.bazel +++ b/asl/BUILD.bazel @@ -4,6 +4,7 @@ cc_library( "allocator.hpp", "annotations.hpp", "assert.hpp", + "atomic.hpp", "box.hpp", "buffer.hpp", "config.hpp", @@ -19,6 +20,7 @@ cc_library( "option.hpp", "print.hpp", "span.hpp", + "status.hpp", "string.hpp", "string_view.hpp", "utility.hpp", @@ -29,6 +31,7 @@ cc_library( "format.cpp", "format_float.cpp", "print.cpp", + "status.cpp", ], deps = [ "//vendor/dragonbox", @@ -57,6 +60,7 @@ cc_library( "meta", "option", "span", + "status", "string", "string_view", "utility", diff --git a/asl/atomic.hpp b/asl/atomic.hpp new file mode 100644 index 0000000..cf6968c --- /dev/null +++ b/asl/atomic.hpp @@ -0,0 +1,51 @@ +#pragma once + +#include "asl/meta.hpp" + +namespace asl +{ + +enum class memory_order : int // NOLINT(*-enum-size) +{ + relaxed = __ATOMIC_RELAXED, + acquire = __ATOMIC_ACQUIRE, + release = __ATOMIC_RELEASE, + acq_rel = __ATOMIC_ACQ_REL, + seq_cst = __ATOMIC_SEQ_CST, +}; + +template struct atomic { T m_value{}; }; + +inline void atomic_fence(memory_order order) +{ + __atomic_thread_fence((int)order); +} + +template +inline void atomic_store(atomic* a, T value, memory_order order = memory_order::relaxed) +{ + __atomic_store(&a->m_value, &value, (int)order); +} + +template +inline T atomic_load(atomic* a, memory_order order = memory_order::relaxed) +{ + T value; + __atomic_load(&a->m_value, &value, (int)order); + return value; +} + +template +inline T atomic_fetch_increment(atomic* a, memory_order order = memory_order::relaxed) +{ + return __atomic_fetch_add(&a->m_value, 1, (int)order); +} + +template +inline T atomic_fetch_decrement(atomic* a, memory_order order = memory_order::relaxed) +{ + return __atomic_fetch_sub(&a->m_value, 1, (int)order); +} + +} // namespace asl + diff --git a/asl/meta.hpp b/asl/meta.hpp index f69a4a3..959201e 100644 --- a/asl/meta.hpp +++ b/asl/meta.hpp @@ -177,6 +177,18 @@ template<> struct _is_floating_point_helper : true_type {}; template concept is_floating_point = _is_floating_point_helper>::value; +template struct _is_integer_helper : false_type {}; +template<> struct _is_integer_helper : true_type {}; +template<> struct _is_integer_helper : true_type {}; +template<> struct _is_integer_helper : true_type {}; +template<> struct _is_integer_helper : true_type {}; +template<> struct _is_integer_helper : true_type {}; +template<> struct _is_integer_helper : true_type {}; +template<> struct _is_integer_helper : true_type {}; +template<> struct _is_integer_helper : true_type {}; + +template concept is_integer = _is_integer_helper>::value; + template concept equality_comparable_with = requires (const un_cvref_t& a, const un_cvref_t& b) { diff --git a/asl/status.cpp b/asl/status.cpp new file mode 100644 index 0000000..e69de29 diff --git a/asl/status.hpp b/asl/status.hpp new file mode 100644 index 0000000..2e4bae5 --- /dev/null +++ b/asl/status.hpp @@ -0,0 +1,13 @@ +#pragma once + +#include "asl/integers.hpp" +#include "asl/string_view.hpp" + +namespace asl +{ + +class status +{ +}; + +} // namespace asl diff --git a/asl/tests/status_tests.cpp b/asl/tests/status_tests.cpp new file mode 100644 index 0000000..e69de29 diff --git a/todo.txt b/todo.txt index 60d1751..f172e86 100644 --- a/todo.txt +++ b/todo.txt @@ -2,6 +2,7 @@ hashing hash_set hash_map logging +atomic status status_or dynlib -- cgit