F´ Flight Software - C/C++ Documentation  devel
A framework for building embedded system applications to NASA flight quality standards.
Mutex.hpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title Os/Mutex.hpp
3 // \brief common definitions for Os::Mutex
4 // ======================================================================
5 #ifndef Os_Mutex_hpp
6 #define Os_Mutex_hpp
7 
8 #include <FpConfig.hpp>
9 #include <Os/Os.hpp>
10 
11 namespace Os {
12 
13 struct MutexHandle {};
14 
16  public:
17  enum Status {
22  };
23 
25  MutexInterface() = default;
26 
28  virtual ~MutexInterface() = default;
29 
31  MutexInterface(const MutexInterface& other) = delete;
32 
34  MutexInterface(const MutexInterface* other) = delete;
35 
37  MutexInterface& operator=(const MutexInterface& other) = delete;
38 
41  virtual MutexHandle* getHandle() = 0;
42 
44  static MutexInterface* getDelegate(MutexHandleStorage& aligned_new_memory);
45 
46  virtual Status take() = 0;
47  virtual Status release() = 0;
48 };
49 
50 class Mutex final : public MutexInterface {
51  public:
52  Mutex();
53  ~Mutex() final;
54 
57  MutexHandle* getHandle() override;
58 
59  Status take() override;
60  Status release() override;
61  void lock();
62  void unLock();
63  void unlock() { this->unLock(); }
64 
65  private:
66  // This section is used to store the implementation-defined mutex handle. To Os::Mutex and fprime, this type is
67  // opaque and thus normal allocation cannot be done. Instead, we allow the implementor to store then handle in
68  // the byte-array here and set `handle` to that address for storage.
69  //
70  alignas(FW_HANDLE_ALIGNMENT) MutexHandleStorage m_handle_storage;
71  MutexInterface& m_delegate;
72 };
79 class ScopeLock {
80  public:
85  explicit ScopeLock(Mutex& mutex);
86 
88  ~ScopeLock();
89 
91  ScopeLock(const ScopeLock& other) = delete;
92 
94  ScopeLock(const ScopeLock* other) = delete;
95 
97  ScopeLock& operator=(const ScopeLock& other) = delete;
98 
99  private:
100  Mutex& m_mutex;
101 };
102 } // namespace Os
103 
104 #endif
#define FW_HANDLE_ALIGNMENT
Alignment of handle storage.
Definition: FpConfig.h:440
C++-compatible configuration header for fprime configuration.
U8 MutexHandleStorage[FW_MUTEX_HANDLE_MAX_SIZE]
Definition: Os.hpp:12
~Mutex() final
Destructor.
Definition: Mutex.cpp:14
MutexHandle * getHandle() override
return the underlying mutex handle (implementation specific)
Definition: Mutex.cpp:19
void unlock()
alias for unLock to meet BasicLockable requirements
Definition: Mutex.hpp:63
void unLock()
unlock the mutex and assert success
Definition: Mutex.cpp:40
void lock()
lock the mutex and assert success
Definition: Mutex.cpp:34
Mutex()
Constructor. Mutex is unlocked when created.
Definition: Mutex.cpp:10
Status take() override
lock the mutex and get return status
Definition: Mutex.cpp:24
Status release() override
unlock the mutex and get return status
Definition: Mutex.cpp:29
virtual Status take()=0
lock the mutex return status
MutexInterface(const MutexInterface *other)=delete
copy constructor is forbidden
virtual MutexHandle * getHandle()=0
return the underlying mutex handle (implementation specific)
MutexInterface()=default
default constructor
MutexInterface(const MutexInterface &other)=delete
copy constructor is forbidden
MutexInterface & operator=(const MutexInterface &other)=delete
assignment operator is forbidden
virtual ~MutexInterface()=default
default virtual destructor
@ OP_OK
Operation was successful.
Definition: Mutex.hpp:18
@ ERROR_BUSY
Mutex is busy.
Definition: Mutex.hpp:19
@ ERROR_DEADLOCK
Deadlock condition detected.
Definition: Mutex.hpp:20
@ ERROR_OTHER
All other errors.
Definition: Mutex.hpp:21
virtual Status release()=0
unlock the mutex return status
static MutexInterface * getDelegate(MutexHandleStorage &aligned_new_memory)
provide a pointer to a Mutex delegate object
locks a mutex within the current scope
Definition: Mutex.hpp:79
ScopeLock(const ScopeLock *other)=delete
copy constructor is forbidden
ScopeLock(const ScopeLock &other)=delete
copy constructor is forbidden
~ScopeLock()
unlock the scoped mutex
Definition: Mutex.cpp:50
ScopeLock & operator=(const ScopeLock &other)=delete
assignment operator is forbidden
ScopeLock(Mutex &mutex)
construct the scope lock
Definition: Mutex.cpp:46