-
Notifications
You must be signed in to change notification settings - Fork 155
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
Open
liuzicheng1987
wants to merge
5
commits into
main
Choose a base branch
from
f/atomic
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 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 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,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
| 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 |
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,38 @@ | ||
| #ifndef RFL_PARSING_PARSER_ATOMIC_FLAG_HPP_ | ||
| #define RFL_PARSING_PARSER_ATOMIC_FLAG_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 ProcessorsType> | ||
| requires AreReaderAndWriter<R, W, std::atomic_flag> | ||
| struct Parser<R, W, std::atomic_flag, 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_flag& _a, const P& _parent) { | ||
| Parser<R, W, bool, ProcessorsType>::write( | ||
| _w, _a.test(std::memory_order_relaxed), _parent); | ||
| } | ||
|
|
||
| static schema::Type to_schema( | ||
| std::map<std::string, schema::Type>* _definitions) { | ||
| return schema::Type{ | ||
| Parser<R, W, bool, ProcessorsType>::to_schema(_definitions)}; | ||
| } | ||
| }; | ||
|
|
||
| } // namespace rfl::parsing | ||
|
|
||
| #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
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.