-
Notifications
You must be signed in to change notification settings - Fork 157
Allow support for atomic variables; resolves #575 #577
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
718b928
Began developing an atomic wrapper
liuzicheng1987 89b17c1
Added more comprehensive support for atomic variables
liuzicheng1987 c7ab925
Removed unnecessary overload
liuzicheng1987 731e0f0
Removed overloads that were no longer needed
liuzicheng1987 9a57392
Added support for atomic flags
liuzicheng1987 4fc5c40
Added static_asserts
liuzicheng1987 59c4578
Added documentation for the atomic vars
liuzicheng1987 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| # Atomic variables (`std::atomic` and `std::atomic_flag`) | ||
|
|
||
| reflect-cpp supports serializing and deserializing atomic types. The library treats atomic wrappers as containers around an underlying value and provides helpers to read plain (non-atomic) representations from input and to set atomic fields afterwards. | ||
|
|
||
| ## Supported atomic types | ||
|
|
||
| - `std::atomic<T>` | ||
| - `std::atomic_flag` (serialized as a boolean) | ||
| - Arrays of atomic types (std::array<T, N> and C-style arrays) | ||
| - Aggregate types (structs/NamedTuple) containing atomic fields — each atomic field is handled independently | ||
|
|
||
| ## Example (writing) | ||
|
|
||
| ```cpp | ||
| struct Stats { | ||
| std::atomic<std::uint64_t> bytes_downloaded; | ||
| std::atomic<bool> finished; | ||
| std::atomic_flag atomic_flag; | ||
| }; | ||
|
|
||
| Stats stats{.bytes_downloaded = 123456789, .finished = true, .atomic_flag = ATOMIC_FLAG_INIT}; | ||
| const auto json_str = rfl::json::write(stats); | ||
| // -> {"bytes_downloaded":123456789,"finished":true,"atomic_flag":false} | ||
| ``` | ||
|
|
||
| Note: the exact boolean value for `atomic_flag` depends on whether it is set or cleared. | ||
|
|
||
| ## Example (reading) | ||
|
|
||
| Reading atomic variables is not quite trivial, because atomic fields cannot be copied or moved. Consider the following example: | ||
|
|
||
| ```cpp | ||
| // const auto res = rfl::json::read<Stats>(json_str); | ||
| // This will NOT compile because std::atomic<T> is neither copyable nor movable | ||
| ``` | ||
|
|
||
| There are two ways around this problem: | ||
|
|
||
| ### 1. Wrap in `rfl::Ref`, `rfl::Box`, `std::shared_ptr` or `std::unique_ptr` | ||
|
|
||
| The easiest way to read structs with atomic fields is to wrap them in a pointer-like type such as `rfl::Ref`, `rfl::Box`, `std::shared_ptr` or `std::unique_ptr`. This works because the pointer-like types themselves are copyable/movable, even if the underlying type is not. | ||
|
|
||
| ```cpp | ||
| const auto res = rfl::json::read<rfl::Ref<Stats>>(json_str); | ||
| ``` | ||
|
|
||
| ### 2. Read into a non-atomic representation and then set atomic fields | ||
|
|
||
| The second way is to read into a non-atomic representation of the struct and then set the atomic fields afterwards using `rfl::atomic::set_atomic_fields`. The non-atomic representation can be obtained using `rfl::atomic::remove_atomic_t`. | ||
|
|
||
| ```cpp | ||
| Stats stats; | ||
|
|
||
| const rfl::Result<rfl::Nothing> res = | ||
| rfl::json::read<rfl::atomic::remove_atomic_t<Stats>>(json_str) | ||
| .transform([&](auto&& non_atomic_stats) { | ||
| return rfl::atomic::set_atomic_fields(non_atomic_stats, &stats); | ||
| }); | ||
|
|
||
| if (!res) { | ||
| // handle error | ||
| std::cerr << "Error reading JSON: " << res.error().what() << std::endl; | ||
| } | ||
| ``` | ||
|
|
||
| ## Limitations and notes | ||
|
|
||
| - Structs containing atomic fields must be default-constructible. | ||
| - Atomic types cannot be mixed with `rfl::DefaultVal` or the `rfl::DefaultIfMissing` processor; attempting to do so triggers a static assertion at compile-time (see parser implementations). | ||
| - The semantics used for setting atomic values use relaxed memory order (`std::memory_order_relaxed`). | ||
| - For complex aggregates, `rfl::atomic` will recurse into nested fields and arrays to set atomic members. | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| #ifndef RFL_ATOMIC_ISATOMIC_HPP_ | ||
| #define RFL_ATOMIC_ISATOMIC_HPP_ | ||
|
|
||
| #include <array> | ||
| #include <atomic> | ||
| #include <type_traits> | ||
|
|
||
| #include "../NamedTuple.hpp" | ||
| #include "../Tuple.hpp" | ||
| #include "../named_tuple_t.hpp" | ||
| #include "../to_view.hpp" | ||
|
|
||
| namespace rfl::atomic { | ||
|
|
||
| template <class T> | ||
| struct is_atomic; | ||
|
|
||
| template <class T> | ||
| struct is_atomic { | ||
| static constexpr bool value = false; | ||
| using RemoveAtomicT = T; | ||
| static void set(RemoveAtomicT&& val, T* _t) { *_t = std::forward<T>(val); }; | ||
| }; | ||
|
|
||
| template <class T> | ||
| struct is_atomic<std::atomic<T>> { | ||
| static constexpr bool value = true; | ||
| using RemoveAtomicT = T; | ||
| static void set(RemoveAtomicT&& val, std::atomic<T>* _t) { | ||
| _t->store(std::forward<RemoveAtomicT>(val), std::memory_order_relaxed); | ||
| }; | ||
| }; | ||
|
|
||
| template <> | ||
| struct is_atomic<std::atomic_flag> { | ||
| static constexpr bool value = true; | ||
| using RemoveAtomicT = bool; | ||
| static void set(RemoveAtomicT&& val, std::atomic_flag* _t) { | ||
| if (val) { | ||
| _t->test_and_set(std::memory_order_relaxed); | ||
| } else { | ||
| _t->clear(std::memory_order_relaxed); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| template <class T, size_t N> | ||
| struct is_atomic<std::array<T, N>> { | ||
| using Type = std::remove_cvref_t<T>; | ||
|
|
||
| static constexpr bool value = is_atomic<Type>::value; | ||
| using RemoveAtomicT = std::array<typename is_atomic<Type>::RemoveAtomicT, N>; | ||
| static void set(RemoveAtomicT&& val, std::array<T, N>* _t) { | ||
| for (size_t i = 0; i < N; ++i) { | ||
| is_atomic<T>::set( | ||
| std::forward<typename is_atomic<Type>::RemoveAtomicT>(val[i]), | ||
| &((*_t)[i])); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| template <class T, size_t N> | ||
| struct is_atomic<T[N]> { | ||
| using Type = std::remove_cvref_t<T>; | ||
|
|
||
| static constexpr bool value = is_atomic<Type>::value; | ||
| using RemoveAtomicT = std::array<typename is_atomic<Type>::RemoveAtomicT, N>; | ||
| static void set(RemoveAtomicT&& val, T (*_t)[N]) { | ||
| for (size_t i = 0; i < N; ++i) { | ||
| is_atomic<T>::set( | ||
| std::forward<typename is_atomic<Type>::RemoveAtomicT>(val[i]), | ||
| &((*_t)[i])); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| template <class... Fields> | ||
| struct is_atomic<NamedTuple<Fields...>> { | ||
| static constexpr bool value = | ||
| (is_atomic<typename Fields::Type>::value || ...); | ||
|
|
||
| using RemoveAtomicT = NamedTuple< | ||
| rfl::Field<Fields::name_, | ||
| typename is_atomic<typename Fields::Type>::RemoveAtomicT>...>; | ||
|
|
||
| static void set(RemoveAtomicT&& val, NamedTuple<Fields...>* _t) { | ||
| (is_atomic<typename Fields::Type>::set( | ||
| std::forward<typename is_atomic< | ||
| std::remove_cvref_t<typename Fields::Type>>::RemoveAtomicT>( | ||
| val.template get<Fields::name_>()), | ||
| &(_t->template get<Fields::name_>())), | ||
| ...); | ||
| } | ||
| }; | ||
|
|
||
| template <class T> | ||
| requires(std::is_class_v<T> && std::is_aggregate_v<T>) | ||
| struct is_atomic<T> { | ||
| static constexpr bool value = is_atomic<named_tuple_t<T>>::value; | ||
|
|
||
| using RemoveAtomicT = typename is_atomic<named_tuple_t<T>>::RemoveAtomicT; | ||
|
|
||
| static void set(RemoveAtomicT&& val, T* _t) { | ||
| using Fields = typename named_tuple_t<T>::Fields; | ||
|
|
||
| const auto view = to_view(*_t); | ||
|
|
||
| const auto set_field = [&]<size_t _i>(std::integral_constant<size_t, _i>) { | ||
| using FieldType = typename rfl::tuple_element_t<_i, Fields>::Type; | ||
| using FieldRemoveAtomicT = | ||
| typename is_atomic<std::remove_cvref_t<FieldType>>::RemoveAtomicT; | ||
|
|
||
| is_atomic<std::remove_cvref_t<FieldType>>::set( | ||
| std::forward<FieldRemoveAtomicT>(val.template get<_i>()), | ||
| view.template get<_i>()); | ||
| }; | ||
|
|
||
| constexpr size_t num_fields = std::remove_cvref_t<decltype(view)>::size(); | ||
|
|
||
| [&]<size_t... _is>(std::index_sequence<_is...>) { | ||
| (set_field(std::integral_constant<size_t, _is>{}), ...); | ||
| }(std::make_index_sequence<num_fields>{}); | ||
| } | ||
| }; | ||
|
|
||
| template <class T> | ||
| constexpr bool is_atomic_v = is_atomic<std::remove_cvref_t<T>>::value; | ||
|
|
||
| } // namespace rfl::atomic | ||
|
|
||
| #endif | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| #ifndef RFL_ATOMIC_REMOVE_ATOMIC_T_HPP_ | ||
| #define RFL_ATOMIC_REMOVE_ATOMIC_T_HPP_ | ||
|
|
||
| #include <type_traits> | ||
|
|
||
| #include "is_atomic.hpp" | ||
|
|
||
| namespace rfl::atomic { | ||
|
|
||
| template <class T> | ||
| using remove_atomic_t = | ||
| typename is_atomic<std::remove_cvref_t<T>>::RemoveAtomicT; | ||
|
|
||
| } // namespace rfl::atomic | ||
|
|
||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| #ifndef RFL_ATOMIC_SET_ATOMIC_HPP_ | ||
| #define RFL_ATOMIC_SET_ATOMIC_HPP_ | ||
|
|
||
| #include <type_traits> | ||
|
|
||
| #include "../Result.hpp" | ||
| #include "is_atomic.hpp" | ||
|
|
||
| namespace rfl::atomic { | ||
|
|
||
| template <class T, class U> | ||
| Nothing set_atomic(U&& val, T* _t) { | ||
| is_atomic<std::remove_cvref_t<T>>::set(std::forward<U>(val), _t); | ||
| return Nothing{}; | ||
| } | ||
|
|
||
| template <class T, class U> | ||
| Nothing set_atomic(U&& val, T& _t) { | ||
| return set_atomic(std::forward<U>(val), &_t); | ||
| } | ||
|
|
||
| } // namespace rfl::atomic | ||
|
|
||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| #ifndef RFL_PARSING_PARSER_ATOMIC_HPP_ | ||
| #define RFL_PARSING_PARSER_ATOMIC_HPP_ | ||
|
|
||
| #include <atomic> | ||
| #include <map> | ||
| #include <type_traits> | ||
|
|
||
| #include "../DefaultVal.hpp" | ||
| #include "AreReaderAndWriter.hpp" | ||
| #include "Parent.hpp" | ||
| #include "Parser_base.hpp" | ||
| #include "schema/Type.hpp" | ||
|
|
||
| namespace rfl::parsing { | ||
|
|
||
| template <class R, class W, class T, class ProcessorsType> | ||
| requires AreReaderAndWriter<R, W, std::atomic<T>> | ||
| struct Parser<R, W, std::atomic<T>, ProcessorsType> { | ||
| using InputVarType = typename R::InputVarType; | ||
|
|
||
| /// Read is not supported for atomic types - we must used rfl::atomic instead. | ||
|
|
||
| template <class P> | ||
| static void write(const W& _w, const std::atomic<T>& _a, const P& _parent) { | ||
| Parser<R, W, std::remove_cvref_t<T>, ProcessorsType>::write( | ||
| _w, _a.load(std::memory_order_relaxed), _parent); | ||
| } | ||
|
|
||
| static schema::Type to_schema( | ||
| std::map<std::string, schema::Type>* _definitions) { | ||
| using U = std::remove_cvref_t<T>; | ||
| return schema::Type{ | ||
| Parser<R, W, U, ProcessorsType>::to_schema(_definitions)}; | ||
| } | ||
| }; | ||
|
|
||
| } // namespace rfl::parsing | ||
|
|
||
| #endif |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's an inconsistency in the use of
TversusType(which isstd::remove_cvref_t<T>). Thesetfunction usesis_atomic<T>::set, butTcan have cv-qualifiers, which would cause the base template ofis_atomicto be instantiated instead of the correct specialization. This can lead to incorrect behavior or compilation errors. You should consistently useType, which is the cv-ref removed version ofT.