Skip to content
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

Add unlock() alias or rename unLock() to unlock() in Os::Mutex to support std::lock_guard #1229

Closed
vietjtnguyen opened this issue Jan 18, 2022 · 4 comments

Comments

@vietjtnguyen
Copy link

std::lock_guard is a handy type to lock/unlock a mutex automatically according the lifetime of a std::lock_guard instance. It only requires that the provided mutex type satisfy BasicLockable which only requires the existence of a lock() and unlock() member function.

The least intrusive method is to add an alias function:

diff --git a/Os/Mutex.hpp b/Os/Mutex.hpp
index 94ebab234..31934015a 100644
--- a/Os/Mutex.hpp
+++ b/Os/Mutex.hpp
@@ -14,6 +14,14 @@ namespace Os {
             void lock(); //!<  lock the mutex
             void unLock(); //!<  unlock the mutex

+            /**
+             * An alias to @ref unLock() that allows the type to satisfy the
+             * BasicLockable requirements
+             * (https://en.cppreference.com/w/cpp/named_req/BasicLockable)
+             * needed to work with @ref std::lock_guard.
+             */
+            void unlock() { this->unLock(); }
+
         private:

             POINTER_CAST m_handle; //!<  Stored handle to mutex
@connorfuhrman
Copy link
Contributor

Is there a PR associated with this? I can take this assignment if it's still available.

@LeStarch
Copy link
Collaborator

LeStarch commented May 3, 2022

There is not PR for this. We should add an alias as opposed to renaming the function.

@connorfuhrman
Copy link
Contributor

connorfuhrman commented May 3, 2022

Working at https://github.com/connorfuhrman/fprime/tree/enh/mutex_basiclockable

Will simply add alias for unlock and write some basic tests.

@LeStarch
Copy link
Collaborator

This was done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants