F´ Flight Software - C/C++ Documentation
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 {
23  };
24 
26  MutexInterface() = default;
27 
29  virtual ~MutexInterface() = default;
30 
32  MutexInterface(const MutexInterface& other) = delete;
33 
35  MutexInterface(const MutexInterface* other) = delete;
36 
38  MutexInterface& operator=(const MutexInterface& other) = delete;
39 
42  virtual MutexHandle* getHandle() = 0;
43 
45  static MutexInterface* getDelegate(MutexHandleStorage& aligned_new_memory);
46 
47  virtual Status take() = 0;
48  virtual Status release() = 0;
49 };
50 
51 class Mutex final : public MutexInterface {
52  public:
53  Mutex();
54  ~Mutex() final;
55 
58  MutexHandle* getHandle() override;
59 
60  Status take() override;
61  Status release() override;
62  void lock();
63  void unLock();
64  void unlock() { this->unLock(); }
65 
66  private:
67  // This section is used to store the implementation-defined mutex handle. To Os::Mutex and fprime, this type is
68  // opaque and thus normal allocation cannot be done. Instead, we allow the implementor to store then handle in
69  // the byte-array here and set `handle` to that address for storage.
70  //
71  alignas(FW_HANDLE_ALIGNMENT) MutexHandleStorage m_handle_storage;
72  MutexInterface& m_delegate;
73 };
80 class ScopeLock {
81  public:
86  explicit ScopeLock(Mutex& mutex);
87 
89  ~ScopeLock();
90 
92  ScopeLock(const ScopeLock& other) = delete;
93 
95  ScopeLock(const ScopeLock* other) = delete;
96 
98  ScopeLock& operator=(const ScopeLock& other) = delete;
99 
100  private:
101  Mutex& m_mutex;
102 };
103 } // namespace Os
104 
105 #endif
Mutex does not support operation.
Definition: Mutex.hpp:21
void unLock()
unlock the mutex and assert success
Definition: Mutex.cpp:40
All other errors.
Definition: Mutex.hpp:22
Status release() override
unlock the mutex and get return status
Definition: Mutex.cpp:29
ScopeLock & operator=(const ScopeLock &other)=delete
assignment operator is forbidden
virtual Status release()=0
unlock the mutex return status
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:64
Operation was successful.
Definition: Mutex.hpp:18
virtual Status take()=0
lock the mutex return status
virtual MutexHandle * getHandle()=0
return the underlying mutex handle (implementation specific)
Status take() override
lock the mutex and get return status
Definition: Mutex.cpp:24
~ScopeLock()
unlock the scoped mutex
Definition: Mutex.cpp:50
virtual ~MutexInterface()=default
default virtual destructor
C++-compatible configuration header for fprime configuration.
Deadlock condition detected.
Definition: Mutex.hpp:20
ScopeLock(Mutex &mutex)
construct the scope lock
Definition: Mutex.cpp:46
locks a mutex within the current scope
Definition: Mutex.hpp:80
~Mutex() final
Destructor.
Definition: Mutex.cpp:14
Mutex()
Constructor. Mutex is unlocked when created.
Definition: Mutex.cpp:10
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
#define FW_HANDLE_ALIGNMENT
Alignment of handle storage.
Definition: FpConfig.h:440
MutexInterface & operator=(const MutexInterface &other)=delete
assignment operator is forbidden
void lock()
lock the mutex and assert success
Definition: Mutex.cpp:34