#ifndef THWEAVER_ATOMIC_H #define THWEAVER_ATOMIC_H #include #include #include #include template concept Additive = std::is_integral_v && !std::is_same_v; template concept AtomicAdditive = requires(A a, T v) { { a.load() } -> std::same_as; { a.store(v) }; { a.fetch_add(v) } -> std::same_as; { a.fetch_sub(v) } -> std::same_as; }; template concept Bitwise = (std::is_integral_v || std::is_enum_v) && !std::is_same_v; template concept AtomicBitwise = requires(A a, T v) { { a.load() } -> std::same_as; { a.store(v) }; { a.fetch_or(v) } -> std::same_as; { a.fetch_and(v) } -> std::same_as; { a.fetch_xor(v) } -> std::same_as; }; // Uses the full bitmap template requires Bitwise && AtomicBitwise && Additive && std::is_unsigned_v class AtomicBitmap { public: static_assert(NumBits <= std::numeric_limits::digits); AtomicBitmap() noexcept : bits(T {}), rotation(0) {} void reset(const T &bits, const Index rotation) noexcept; bool get_bit(const Index n) const noexcept; void set_bit(const Index n) noexcept; void clear_bit(const Index n) noexcept; void rotate(const Index n) noexcept; Index get_rotation() const noexcept; Index get_low_bit() const noexcept; Index rotate_to_low_bit(const Index offset = 0) noexcept; private: Atomic bits; Index rotation; }; #endif