Add atomic & is_integer

This commit is contained in:
2025-01-03 01:49:42 +01:00
parent eb58edf811
commit c2d4216695
7 changed files with 81 additions and 0 deletions

View File

@ -4,6 +4,7 @@ cc_library(
"allocator.hpp", "allocator.hpp",
"annotations.hpp", "annotations.hpp",
"assert.hpp", "assert.hpp",
"atomic.hpp",
"box.hpp", "box.hpp",
"buffer.hpp", "buffer.hpp",
"config.hpp", "config.hpp",
@ -19,6 +20,7 @@ cc_library(
"option.hpp", "option.hpp",
"print.hpp", "print.hpp",
"span.hpp", "span.hpp",
"status.hpp",
"string.hpp", "string.hpp",
"string_view.hpp", "string_view.hpp",
"utility.hpp", "utility.hpp",
@ -29,6 +31,7 @@ cc_library(
"format.cpp", "format.cpp",
"format_float.cpp", "format_float.cpp",
"print.cpp", "print.cpp",
"status.cpp",
], ],
deps = [ deps = [
"//vendor/dragonbox", "//vendor/dragonbox",
@ -57,6 +60,7 @@ cc_library(
"meta", "meta",
"option", "option",
"span", "span",
"status",
"string", "string",
"string_view", "string_view",
"utility", "utility",

51
asl/atomic.hpp Normal file
View File

@ -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<typename T> struct atomic { T m_value{}; };
inline void atomic_fence(memory_order order)
{
__atomic_thread_fence((int)order);
}
template<is_integer T>
inline void atomic_store(atomic<T>* a, T value, memory_order order = memory_order::relaxed)
{
__atomic_store(&a->m_value, &value, (int)order);
}
template<is_integer T>
inline T atomic_load(atomic<T>* a, memory_order order = memory_order::relaxed)
{
T value;
__atomic_load(&a->m_value, &value, (int)order);
return value;
}
template<typename T>
inline T atomic_fetch_increment(atomic<T>* a, memory_order order = memory_order::relaxed)
{
return __atomic_fetch_add(&a->m_value, 1, (int)order);
}
template<typename T>
inline T atomic_fetch_decrement(atomic<T>* a, memory_order order = memory_order::relaxed)
{
return __atomic_fetch_sub(&a->m_value, 1, (int)order);
}
} // namespace asl

View File

@ -177,6 +177,18 @@ template<> struct _is_floating_point_helper<double> : true_type {};
template<typename T> concept is_floating_point = _is_floating_point_helper<un_cv_t<T>>::value; template<typename T> concept is_floating_point = _is_floating_point_helper<un_cv_t<T>>::value;
template<typename T> struct _is_integer_helper : false_type {};
template<> struct _is_integer_helper<int8_t> : true_type {};
template<> struct _is_integer_helper<int16_t> : true_type {};
template<> struct _is_integer_helper<int32_t> : true_type {};
template<> struct _is_integer_helper<int64_t> : true_type {};
template<> struct _is_integer_helper<uint8_t> : true_type {};
template<> struct _is_integer_helper<uint16_t> : true_type {};
template<> struct _is_integer_helper<uint32_t> : true_type {};
template<> struct _is_integer_helper<uint64_t> : true_type {};
template<typename T> concept is_integer = _is_integer_helper<un_cv_t<T>>::value;
template<typename T, typename U> template<typename T, typename U>
concept equality_comparable_with = requires (const un_cvref_t<T>& a, const un_cvref_t<T>& b) concept equality_comparable_with = requires (const un_cvref_t<T>& a, const un_cvref_t<T>& b)
{ {

0
asl/status.cpp Normal file
View File

13
asl/status.hpp Normal file
View File

@ -0,0 +1,13 @@
#pragma once
#include "asl/integers.hpp"
#include "asl/string_view.hpp"
namespace asl
{
class status
{
};
} // namespace asl

View File

View File

@ -2,6 +2,7 @@ hashing
hash_set hash_set
hash_map hash_map
logging logging
atomic
status status
status_or status_or
dynlib dynlib