F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
MutexInterface.hpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title Os/MutexInterface.hpp
3 // \brief Os::MutexHandle, Os::MutexInterface, and Os::ScopeLock definitions
4 // ======================================================================
5 #ifndef OS_MUTEXINTERFACE_HPP_
6 #define OS_MUTEXINTERFACE_HPP_
7 
9 #include <Fw/Types/Assert.hpp>
10 #include <Os/Os.hpp>
11 #include "config/OsDelegateMutex.hpp" // defines Os::Mutex alias and OS_MUTEX_HEADER consumed by Os/Mutex.hpp; do not remove
12 
13 namespace Os {
14 
15 struct MutexHandle {};
16 
18  public:
19  enum Status {
25  };
26 
28  MutexInterface() = default;
29 
31  virtual ~MutexInterface() = default;
32 
34  MutexInterface(const MutexInterface& other) = delete;
35 
37  MutexInterface(const MutexInterface* other) = delete;
38 
40  MutexInterface& operator=(const MutexInterface& other) = delete;
41 
44  virtual MutexHandle* getHandle() = 0;
45 
47  static MutexInterface* getDelegate(MutexHandleStorage& aligned_new_memory);
48 
49  // ------------------------------------------------------------------
50  // Mutex operations to be implemented by an OSAL implementation
51  // ------------------------------------------------------------------
52 
53  virtual Status take() = 0;
54  virtual Status release() = 0;
55 
56  // ------------------------------------------------------------------
57  // Common (non-virtual) functions built on top of take()/release().
58  // Defined inline on the interface (per fprime#5249) so they are available
59  // regardless of which implementation Os::Mutex is configured to be, and so
60  // that under compile-time selection the call site can devirtualize take()/
61  // release() and inline the whole acquisition without relying on LTO.
62  // ------------------------------------------------------------------
63 
65  void lock() {
66  const Status status = this->take();
67  FW_ASSERT(status == Status::OP_OK,
68  static_cast<FwAssertArgType>(reinterpret_cast<PlatformPointerCastType>(this)), status);
69  }
70 
72  void unLock() {
73  const Status status = this->release();
74  FW_ASSERT(status == Status::OP_OK,
75  static_cast<FwAssertArgType>(reinterpret_cast<PlatformPointerCastType>(this)), status);
76  }
77 
79  void unlock() { this->unLock(); }
80 };
81 
88 class ScopeLock {
89  public:
94  explicit ScopeLock(MutexInterface& mutex) : m_mutex(mutex) { this->m_mutex.lock(); }
95 
97  ~ScopeLock() { this->m_mutex.unLock(); }
98 
100  ScopeLock(const ScopeLock& other) = delete;
101 
103  ScopeLock(const ScopeLock* other) = delete;
104 
106  ScopeLock& operator=(const ScopeLock& other) = delete;
107 
108  private:
109  MutexInterface& m_mutex;
110 };
111 
112 } // namespace Os
113 
114 #endif // OS_MUTEXINTERFACE_HPP_
Operation succeeded.
Definition: Os.hpp:27
Mutex does not support operation.
ScopeLock(MutexInterface &mutex)
construct the scope lock
ScopeLock & operator=(const ScopeLock &other)=delete
assignment operator is forbidden
virtual Status release()=0
unlock the mutex return status
Operation was successful.
virtual Status take()=0
lock the mutex return status
virtual MutexHandle * getHandle()=0
return the underlying mutex handle (implementation specific)
void unLock()
unlock the mutex and assert success
~ScopeLock()
unlock the scoped mutex
virtual ~MutexInterface()=default
default virtual destructor
void lock()
lock the mutex and assert success
Deadlock condition detected.
locks a mutex within the current scope
void unlock()
alias for unLock to meet BasicLockable requirements
MutexInterface()=default
default constructor
U8 MutexHandleStorage[FW_MUTEX_HANDLE_MAX_SIZE]
Definition: Os.hpp:12
static MutexInterface * getDelegate(MutexHandleStorage &aligned_new_memory)
provide a pointer to a Mutex delegate object
MutexInterface & operator=(const MutexInterface &other)=delete
assignment operator is forbidden
#define FW_ASSERT(...)
Definition: Assert.hpp:14