-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add alias for Os::Mutex #5936
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
jrussino
wants to merge
19
commits into
nasa:devel
Choose a base branch
from
jrussino:russino-mutex-delegate
base: devel
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
Add alias for Os::Mutex #5936
Changes from 18 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
161f7f4
Rename Mutex -> DelegateMutex
f816636
Make mutex type link-time discoverable
f1778fe
Register config OsDelegateMutex
f7f290c
Update Mutex.hpp to aggregate definitions
f45d26c
Merge branch 'devel' into russino-mutex-delegate
jrussino 1282b9e
Merge branch 'devel' into russino-mutex-delegate
jrussino 85086f5
Os SDD: add Mutex to list of supported services for Compile-Time Sele…
a8bf713
Os SDD: fix copy-past error
4a698d5
Mutex: Simplify comment explaining why m_handle_storage is not being …
jrussino 4f29d5b
MutexInterface: Add comment justifying header inclusion
jrussino 82c128d
Merge branch 'devel' into russino-mutex-delegate
jrussino 4b5e903
Merge branch 'devel' into russino-mutex-delegate
jrussino fe5f27f
Update Os/MutexInterface.hpp
jrussino deda1bb
Mutex: move common interface defs inline in MutexInterface (devirtual…
978de90
Mutex: clarify include order comment
6a1f104
Mutex: documentation updates
0276cdf
Update Os/DelegateMutex.cpp
jrussino 4628a63
Update Os/DelegateMutex.cpp
jrussino 6a4b1c7
Merge branch 'devel' into russino-mutex-delegate
thomas-bc 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,39 @@ | ||
| // ====================================================================== | ||
| // \title Os/DelegateMutex.cpp | ||
| // \brief implementation of Os::DelegateMutex (link-time delegating Os::Mutex) | ||
| // ====================================================================== | ||
| #include <Fw/Types/Assert.hpp> | ||
| #include <Os/DelegateMutex.hpp> | ||
|
|
||
| namespace Os { | ||
|
|
||
| // ---------------------------------------------------------------------- | ||
| // DelegateMutex: link-time delegating implementation | ||
| // ---------------------------------------------------------------------- | ||
|
|
||
| DelegateMutex::DelegateMutex() : m_delegate(*MutexInterface::getDelegate(m_handle_storage)) { | ||
| // m_handle_storage is placement-new storage populated by getDelegate(); do not value-initialize it (#5297) | ||
| FW_ASSERT(&this->m_delegate == reinterpret_cast<MutexInterface*>(&this->m_handle_storage[0])); | ||
| } | ||
|
|
||
| DelegateMutex::~DelegateMutex() { | ||
| FW_ASSERT(&this->m_delegate == reinterpret_cast<MutexInterface*>(&this->m_handle_storage[0])); | ||
| m_delegate.~MutexInterface(); | ||
| } | ||
|
|
||
| MutexHandle* DelegateMutex::getHandle() { | ||
| FW_ASSERT(&this->m_delegate == reinterpret_cast<MutexInterface*>(&this->m_handle_storage[0])); | ||
| return this->m_delegate.getHandle(); | ||
| } | ||
|
|
||
| DelegateMutex::Status DelegateMutex::take() { | ||
| FW_ASSERT(&this->m_delegate == reinterpret_cast<MutexInterface*>(&this->m_handle_storage[0])); | ||
| return this->m_delegate.take(); | ||
| } | ||
|
|
||
| DelegateMutex::Status DelegateMutex::release() { | ||
| FW_ASSERT(&this->m_delegate == reinterpret_cast<MutexInterface*>(&this->m_handle_storage[0])); | ||
| return this->m_delegate.release(); | ||
| } | ||
|
|
||
| } // namespace Os |
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,48 @@ | ||
| // ====================================================================== | ||
| // \title Os/DelegateMutex.hpp | ||
| // \brief Define the Os::DelegateMutex class | ||
| // ====================================================================== | ||
| #ifndef OS_DELEGATEMUTEX_HPP_ | ||
| #define OS_DELEGATEMUTEX_HPP_ | ||
|
|
||
| #include "Os/MutexInterface.hpp" | ||
|
|
||
| namespace Os { | ||
|
|
||
| //! \brief Link-time delegating Mutex implementation. | ||
| //! | ||
| //! Stores an implementation-defined mutex handle in a byte array and forwards all operations to a | ||
| //! delegate constructed (via placement-new) by MutexInterface::getDelegate(). Which getDelegate() | ||
| //! is linked selects the concrete implementation at link time. This is the default binding of the | ||
| //! Os::Mutex alias; platforms may instead alias Os::Mutex directly to a concrete implementation for | ||
| //! compile-time selection (see config/OsDelegateMutex.hpp). | ||
| class DelegateMutex final : public MutexInterface { | ||
| public: | ||
| DelegateMutex(); //!< Constructor. Mutex is unlocked when created | ||
| ~DelegateMutex() final; //!< Destructor | ||
|
|
||
| //! \brief copy constructor is forbidden | ||
| DelegateMutex(const DelegateMutex& other) = delete; | ||
|
|
||
| //! \brief assignment operator is forbidden | ||
| DelegateMutex& operator=(const DelegateMutex& other) = delete; | ||
|
|
||
| //! \brief return the underlying mutex handle (implementation specific) | ||
| //! \return internal mutex handle representation | ||
| MutexHandle* getHandle() override; | ||
|
|
||
| Status take() override; //!< lock the mutex and get return status | ||
| Status release() override; //!< unlock the mutex and get return status | ||
|
|
||
| private: | ||
| // This section is used to store the implementation-defined mutex handle. To Os::Mutex and fprime, this type is | ||
| // opaque and thus normal allocation cannot be done. Instead, we allow the implementor to store the handle in | ||
| // the byte-array here and set `m_delegate` to that address for storage. | ||
| // | ||
| alignas(FW_HANDLE_ALIGNMENT) MutexHandleStorage m_handle_storage; //!< Mutex handle storage | ||
| MutexInterface& m_delegate; //!< Delegate for the real implementation | ||
| }; | ||
|
|
||
| } // namespace Os | ||
|
|
||
| #endif // OS_DELEGATEMUTEX_HPP_ |
This file was deleted.
Oops, something went wrong.
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 |
|---|---|---|
| @@ -1,105 +1,41 @@ | ||
| // ====================================================================== | ||
| // \title Os/Mutex.hpp | ||
| // \brief common definitions for Os::Mutex | ||
| // \brief public Os::Mutex interface and alias | ||
| // | ||
| // This header aggregates all definitions needed to use Os::Mutex: | ||
| // the interface, the configured alias, and the concrete delegate type. | ||
| // | ||
| // The includes below must appear in the order given: each depends on names | ||
| // defined by the one before it, so reordering them will fail to compile. | ||
| // The ordering constraints are: | ||
| // | ||
| // 1. config/OsDelegateMutex.hpp (CFG) defines the Os::Mutex type alias by | ||
| // forward-declaring a link-time delegate (e.g. DelegateMutex) or directly | ||
| // aliasing a concrete implementation (e.g. Va416x0Os::AtomicMutex::AtomicMutex). | ||
| // Must not include Os OSAL headers (they aren't yet defined). | ||
| // Should only be included in Os/MutexInterface.hpp. | ||
| // | ||
| // 2. Os/MutexInterface.hpp (IF) includes CFG first, then defines | ||
| // MutexHandle, MutexInterface, and ScopeLock. | ||
| // | ||
| // 3. OS_MUTEX_HEADER (IMPL) is defined by CFG and points to the concrete | ||
| // implementation header. If using delegation, this points to | ||
| // Os/DelegateMutex.hpp. If using compile-time selection, it points | ||
| // directly to a platform-specific implementation (e.g. AtomicMutex.hpp). | ||
| // | ||
| // MutexInterface.hpp must precede OS_MUTEX_HEADER here, | ||
| // and CFG must never include either of them (that would form a cycle). | ||
| // ====================================================================== | ||
| #ifndef Os_Mutex_hpp | ||
| #define Os_Mutex_hpp | ||
|
|
||
| #include <Fw/FPrimeBasicTypes.hpp> | ||
| #include <Os/Os.hpp> | ||
| #include "Os/MutexInterface.hpp" | ||
|
LeStarch marked this conversation as resolved.
|
||
|
|
||
| namespace Os { | ||
|
|
||
| struct MutexHandle {}; | ||
|
|
||
| class MutexInterface { | ||
| public: | ||
| enum Status { | ||
| OP_OK, //!< Operation was successful | ||
| ERROR_BUSY, //!< Mutex is busy | ||
| ERROR_DEADLOCK, //!< Deadlock condition detected | ||
| NOT_SUPPORTED, //!< Mutex does not support operation | ||
| ERROR_OTHER //!< All other errors | ||
| }; | ||
|
|
||
| //! \brief default constructor | ||
| MutexInterface() = default; | ||
|
|
||
| //! \brief default virtual destructor | ||
| virtual ~MutexInterface() = default; | ||
|
|
||
| //! \brief copy constructor is forbidden | ||
| MutexInterface(const MutexInterface& other) = delete; | ||
|
|
||
| //! \brief copy constructor is forbidden | ||
| MutexInterface(const MutexInterface* other) = delete; | ||
|
|
||
| //! \brief assignment operator is forbidden | ||
| MutexInterface& operator=(const MutexInterface& other) = delete; | ||
|
|
||
| //! \brief return the underlying mutex handle (implementation specific) | ||
| //! \return internal mutex handle representation | ||
| virtual MutexHandle* getHandle() = 0; | ||
|
|
||
| //! \brief provide a pointer to a Mutex delegate object | ||
| static MutexInterface* getDelegate(MutexHandleStorage& aligned_new_memory); | ||
|
|
||
| virtual Status take() = 0; //!< lock the mutex return status | ||
| virtual Status release() = 0; //!< unlock the mutex return status | ||
| }; | ||
|
|
||
| class Mutex final : public MutexInterface { | ||
| public: | ||
| Mutex(); //!< Constructor. Mutex is unlocked when created | ||
| ~Mutex() final; //!< Destructor | ||
|
|
||
| //! \brief return the underlying mutex handle (implementation specific) | ||
| //! \return internal mutex handle representation | ||
| MutexHandle* getHandle() override; | ||
|
|
||
| Status take() override; //!< lock the mutex and get return status | ||
| Status release() override; //!< unlock the mutex and get return status | ||
| void lock(); //!< lock the mutex and assert success | ||
| void unLock(); //!< unlock the mutex and assert success | ||
| void unlock() { this->unLock(); } //!< alias for unLock to meet BasicLockable requirements | ||
|
|
||
| private: | ||
| // This section is used to store the implementation-defined mutex handle. To Os::Mutex and fprime, this type is | ||
| // opaque and thus normal allocation cannot be done. Instead, we allow the implementor to store then handle in | ||
| // the byte-array here and set `handle` to that address for storage. | ||
| // | ||
| alignas(FW_HANDLE_ALIGNMENT) MutexHandleStorage m_handle_storage; //!< Mutex handle storage | ||
| MutexInterface& m_delegate; //!< Delegate for the real implementation | ||
| }; | ||
| //! \brief locks a mutex within the current scope | ||
| //! | ||
| //! The scope lock will lock the associated mutex immediately and will ensure the mutex is unlock when the scope lock | ||
| //! is destroyed. | ||
| //! | ||
| //! \warning it is unadvisable to dynamically allocate ScopeLock as this violates the implied usage. | ||
| class ScopeLock { | ||
| public: | ||
| //! \brief construct the scope lock | ||
| //! | ||
| //! Will lock the supplied mutex and will unlock the mutex when this object goes out of scope. | ||
| //! \param mutex | ||
| explicit ScopeLock(Mutex& mutex); | ||
|
|
||
| //!\brief unlock the scoped mutex | ||
| ~ScopeLock(); | ||
|
|
||
| //! \brief copy constructor is forbidden | ||
| ScopeLock(const ScopeLock& other) = delete; | ||
|
|
||
| //! \brief copy constructor is forbidden | ||
| ScopeLock(const ScopeLock* other) = delete; | ||
|
|
||
| //! \brief assignment operator is forbidden | ||
| ScopeLock& operator=(const ScopeLock& other) = delete; | ||
| // Validate that OS_MUTEX_HEADER was defined by config/OsDelegateMutex.hpp | ||
| #ifndef OS_MUTEX_HEADER | ||
| #error "OS_MUTEX_HEADER must be defined in config/OsDelegateMutex.hpp" | ||
| #endif | ||
|
|
||
| private: | ||
| Mutex& m_mutex; //!< Stores the mutex reference | ||
| }; | ||
| } // namespace Os | ||
| #include OS_MUTEX_HEADER | ||
|
|
||
| #endif | ||
| #endif // Os_Mutex_hpp | ||
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.