-
Notifications
You must be signed in to change notification settings - Fork 88
Added a new implementation of the RealtimeBox with added best effort behaviour #139
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
19 commits
Select commit
Hold shift + click to select a range
b02e501
restructured interface in oder to allow replacing the existing Realti…
613c55b
added some additional comments
93e1401
fix bug
fc2e4a2
fixed wrong return type in templated method
dce1458
added copyright notice
63c51a7
Merge branch 'master' into firesurfer/master
christophfroehlich eaedafe
Fixed issues due to merge with master
e666b93
Merge branch 'master' into master
christophfroehlich aa62239
Merge branch 'master' into master
christophfroehlich a22d150
fixed spelling mistake
186b4aa
fixed ament_cpplint issues
a8b3f8a
fixed last remaining cpplint issue
69b224c
suppress cppcheck missingReturn
250fb85
Merge branch 'master' into master
christophfroehlich 220111a
try to resolve comments wherever possible
c2900a3
Should fix pre-commit formatting warning
firesurfer 6499036
Merge branch 'master' into master
christophfroehlich 4fce184
Implemented suggestions
d7a520c
Merge branch 'master' into master
firesurfer 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
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,232 @@ | ||
| // Copyright (c) 2024, Lennart Nachtigall | ||
| // | ||
| // Redistribution and use in source and binary forms, with or without | ||
| // modification, are permitted provided that the following conditions are met: | ||
| // | ||
| // * Redistributions of source code must retain the above copyright | ||
| // notice, this list of conditions and the following disclaimer. | ||
| // | ||
| // * Redistributions in binary form must reproduce the above copyright | ||
| // notice, this list of conditions and the following disclaimer in the | ||
| // documentation and/or other materials provided with the distribution. | ||
| // | ||
| // * Neither the name of the Willow Garage, Inc. nor the names of its | ||
| // contributors may be used to endorse or promote products derived from | ||
| // this software without specific prior written permission. | ||
| // | ||
| // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
| // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| // POSSIBILITY OF SUCH DAMAGE. | ||
|
|
||
| // Author: Lennart Nachtigall | ||
|
|
||
| #ifndef REALTIME_TOOLS__REALTIME_BOX_BEST_EFFORT_H_ | ||
| #define REALTIME_TOOLS__REALTIME_BOX_BEST_EFFORT_H_ | ||
|
|
||
| #include <initializer_list> | ||
| #include <mutex> | ||
| #include <optional> | ||
| #include <utility> | ||
|
|
||
| #include <rcpputils/pointer_traits.hpp> | ||
|
|
||
| namespace realtime_tools | ||
| { | ||
|
|
||
| template <typename T> | ||
| constexpr auto is_ptr_or_smart_ptr = rcpputils::is_pointer<T>::value; | ||
|
|
||
| /*! | ||
| A Box that ensures thread safe access to the boxed contents. | ||
| Access is best effort. If it can not lock it will return. | ||
|
|
||
| NOTE about pointers: | ||
| You can use pointers with this box but the access will be different. | ||
| Only use the get/set methods that take function pointer for accessing the internal value. | ||
| */ | ||
| template <class T, typename mutex_type = std::mutex> | ||
| class RealtimeBoxBestEffort | ||
| { | ||
| static_assert( | ||
| std::is_same_v<mutex_type, std::mutex> || std::is_same_v<mutex_type, std::recursive_mutex>); | ||
| static_assert(std::is_copy_constructible_v<T>, "Passed type must be copy constructible"); | ||
|
|
||
| public: | ||
| using mutex_t = mutex_type; | ||
| using type = T; | ||
saikishor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Provide various constructors | ||
| constexpr explicit RealtimeBoxBestEffort(const T & init = T{}) : value_(init) {} | ||
| constexpr explicit RealtimeBoxBestEffort(const T && init) : value_(std::move(init)) {} | ||
|
|
||
| // Only enabled for types that can be constructed from an initializer list | ||
| template <typename U = T> | ||
| constexpr RealtimeBoxBestEffort( | ||
| const std::initializer_list<U> & init, | ||
| std::enable_if_t<std::is_constructible_v<U, std::initializer_list>>) | ||
| : value_(init) | ||
| { | ||
| } | ||
|
|
||
| /** | ||
| * @brief set a new content with best effort | ||
| * @return false if mutex could not be locked | ||
| * @note disabled for pointer types | ||
| */ | ||
| template <typename U = T> | ||
| typename std::enable_if_t<!is_ptr_or_smart_ptr<U>, bool> trySet(const T & value) | ||
firesurfer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| std::unique_lock<mutex_t> guard(lock_, std::defer_lock); | ||
| if (!guard.try_lock()) { | ||
| return false; | ||
| } | ||
| value_ = value; | ||
| return true; | ||
| } | ||
| /** | ||
| * @brief access the content readable with best effort | ||
| * @return false if the mutex could not be locked | ||
| * @note only safe way to access pointer type content (rw) | ||
| */ | ||
| bool trySet(const std::function<void(T &)> & func) | ||
| { | ||
| std::unique_lock<mutex_t> guard(lock_, std::defer_lock); | ||
| if (!guard.try_lock()) { | ||
| return false; | ||
| } | ||
|
|
||
| func(value_); | ||
| return true; | ||
| } | ||
| /** | ||
| * @brief get the content with best effort | ||
| * @return std::nullopt if content could not be access, otherwise the content is returned | ||
| */ | ||
| template <typename U = T> | ||
| [[nodiscard]] typename std::enable_if_t<!is_ptr_or_smart_ptr<U>, std::optional<U>> tryGet() const | ||
saikishor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| std::unique_lock<mutex_t> guard(lock_, std::defer_lock); | ||
| if (!guard.try_lock()) { | ||
| return std::nullopt; | ||
| } | ||
| return value_; | ||
| } | ||
| /** | ||
| * @brief access the content (r) with best effort | ||
| * @return false if the mutex could not be locked | ||
| * @note only safe way to access pointer type content (r) | ||
| */ | ||
| bool tryGet(const std::function<void(const T &)> & func) | ||
| { | ||
| std::unique_lock<mutex_t> guard(lock_, std::defer_lock); | ||
| if (!guard.try_lock()) { | ||
| return false; | ||
| } | ||
|
|
||
| func(value_); | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * @brief set the content and wait until the mutex could be locked (RealtimeBox behavior) | ||
| * @return true | ||
| */ | ||
| template <typename U = T> | ||
| typename std::enable_if_t<!is_ptr_or_smart_ptr<U>, void> set(const T & value) | ||
saikishor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| std::lock_guard<mutex_t> guard(lock_); | ||
| // cppcheck-suppress missingReturn | ||
| value_ = value; | ||
| } | ||
| /** | ||
| * @brief access the content (rw) and wait until the mutex could locked | ||
| */ | ||
| void set(const std::function<void(T &)> & func) | ||
| { | ||
| std::lock_guard<mutex_t> guard(lock_); | ||
| func(value_); | ||
| } | ||
|
|
||
| /** | ||
| * @brief get the content and wait until the mutex could be locked (RealtimeBox behaviour) | ||
| * @return copy of the value | ||
| */ | ||
| template <typename U = T> | ||
| [[nodiscard]] typename std::enable_if_t<!is_ptr_or_smart_ptr<U>, U> get() const | ||
firesurfer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| std::lock_guard<mutex_t> guard(lock_); | ||
| return value_; | ||
| } | ||
| /** | ||
| * @brief get the content and wait until the mutex could be locked | ||
| * @note same signature as in the existing RealtimeBox<T> | ||
| */ | ||
| template <typename U = T> | ||
| typename std::enable_if_t<!is_ptr_or_smart_ptr<U>, void> get(T & in) const | ||
firesurfer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| std::lock_guard<mutex_t> guard(lock_); | ||
| // cppcheck-suppress missingReturn | ||
| in = value_; | ||
| } | ||
| /** | ||
| * @brief access the content (r) and wait until the mutex could be locked | ||
| * @note only safe way to access pointer type content (r) | ||
| * @note same signature as in the existing RealtimeBox<T> | ||
| */ | ||
| void get(const std::function<void(const T &)> & func) | ||
| { | ||
| std::lock_guard<mutex_t> guard(lock_); | ||
| func(value_); | ||
| } | ||
|
|
||
| /** | ||
| * @brief provide a custom assignment operator for easier usage | ||
| * @note only to be used from non-RT! | ||
| */ | ||
| template <typename U = T> | ||
| typename std::enable_if_t<!is_ptr_or_smart_ptr<U>, void> operator=(const T & value) | ||
firesurfer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| set(value); | ||
| } | ||
|
|
||
| /** | ||
| * @brief provide a custom conversion operator | ||
| * @note Can only be used from non-RT! | ||
| */ | ||
| template <typename U = T, typename = typename std::enable_if_t<!is_ptr_or_smart_ptr<U>>> | ||
| [[nodiscard]] operator T() const | ||
| { | ||
| // Only makes sense with the getNonRT method otherwise we would return an std::optional | ||
| return get(); | ||
| } | ||
| /** | ||
| * @brief provide a custom conversion operator | ||
| * @note Can be used from non-RT and RT contexts | ||
| */ | ||
| template <typename U = T, typename = typename std::enable_if_t<!is_ptr_or_smart_ptr<U>>> | ||
| [[nodiscard]] operator std::optional<T>() const | ||
| { | ||
| return tryGet(); | ||
| } | ||
|
|
||
| // In case one wants to actually use a pointer | ||
| // in this implementation we allow accessing the lock directly. | ||
| // Note: Be careful with lock.unlock(). | ||
| // It may only be called from the thread that locked the mutex! | ||
| [[nodiscard]] const mutex_t & getMutex() const { return lock_; } | ||
| [[nodiscard]] mutex_t & getMutex() { return lock_; } | ||
|
|
||
| private: | ||
| T value_; | ||
| mutable mutex_t lock_; | ||
| }; | ||
| } // namespace realtime_tools | ||
|
|
||
| #endif // REALTIME_TOOLS__REALTIME_BOX_BEST_EFFORT_H_ | ||
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.
Uh oh!
There was an error while loading. Please reload this page.