F´ Flight Software - C/C++ Documentation  devel
A framework for building embedded system applications to NASA flight quality standards.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Mutex.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title Os/Mutex.cpp
3 // \brief common function implementation for Os::Mutex
4 // ======================================================================
5 #include <Fw/Types/Assert.hpp>
6 #include <Os/Mutex.hpp>
7 
8 namespace Os {
9 
10 Mutex::Mutex() : m_handle_storage(), m_delegate(*MutexInterface::getDelegate(m_handle_storage)) {
11  FW_ASSERT(&this->m_delegate == reinterpret_cast<MutexInterface*>(&this->m_handle_storage[0]));
12 }
13 
15  FW_ASSERT(&this->m_delegate == reinterpret_cast<MutexInterface*>(&this->m_handle_storage[0]));
16  m_delegate.~MutexInterface();
17 }
18 
20  FW_ASSERT(&this->m_delegate == reinterpret_cast<MutexInterface*>(&this->m_handle_storage[0]));
21  return this->m_delegate.getHandle();
22 }
23 
25  FW_ASSERT(&this->m_delegate == reinterpret_cast<MutexInterface*>(&this->m_handle_storage[0]));
26  return this->m_delegate.take();
27 }
28 
30  FW_ASSERT(&this->m_delegate == reinterpret_cast<MutexInterface*>(&this->m_handle_storage[0]));
31  return this->m_delegate.release();
32 }
33 
34 void Mutex::lock() {
35  FW_ASSERT(&this->m_delegate == reinterpret_cast<MutexInterface*>(&this->m_handle_storage[0]));
36  Mutex::Status status = this->take();
37  FW_ASSERT(status == Mutex::Status::OP_OK, status);
38 }
39 
40 void Mutex::unLock() {
41  FW_ASSERT(&this->m_delegate == reinterpret_cast<MutexInterface*>(&this->m_handle_storage[0]));
42  Mutex::Status status = this->release();
43  FW_ASSERT(status == Mutex::Status::OP_OK, status);
44 }
45 
46 ScopeLock::ScopeLock(Mutex& mutex) : m_mutex(mutex) {
47  this->m_mutex.lock();
48 }
49 
51  this->m_mutex.release();
52 }
53 
54 } // namespace Os
#define FW_ASSERT(...)
Definition: Assert.hpp:14
~Mutex() final
Destructor.
Definition: Mutex.cpp:14
MutexHandle * getHandle() override
return the underlying mutex handle (implementation specific)
Definition: Mutex.cpp:19
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
virtual MutexHandle * getHandle()=0
return the underlying mutex handle (implementation specific)
virtual ~MutexInterface()=default
default virtual destructor
virtual Status release()=0
unlock the mutex return status
~ScopeLock()
unlock the scoped mutex
Definition: Mutex.cpp:50
ScopeLock(Mutex &mutex)
construct the scope lock
Definition: Mutex.cpp:46
@ OP_OK
Operation succeeded.
Definition: Os.hpp:26