F´ Flight Software - C/C++ Documentation  NASA-v1.6.0
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 #include <Os/Mutex.hpp>
2 #include <pthread.h>
3 #include <Fw/Types/Assert.hpp>
4 #include <new>
5 
6 namespace Os {
7 
8  Mutex::Mutex() {
9  pthread_mutex_t* handle = new(std::nothrow) pthread_mutex_t;
10  FW_ASSERT(handle != nullptr);
11 
12  // set attributes
13  pthread_mutexattr_t attr;
14  pthread_mutexattr_init(&attr);
15 
16  NATIVE_INT_TYPE stat;
17  // set to error checking
18 // stat = pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_ERRORCHECK);
19 // FW_ASSERT(stat == 0,stat);
20 
21  // set to normal mutex type
22  stat = pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_NORMAL);
23  FW_ASSERT(stat == 0,stat);
24 
25  // set to check for priority inheritance
26  stat = pthread_mutexattr_setprotocol(&attr,PTHREAD_PRIO_INHERIT);
27  FW_ASSERT(stat == 0,stat);
28 
29  stat = pthread_mutex_init(handle,&attr);
30  FW_ASSERT(stat == 0,stat);
31 
32  this->m_handle = reinterpret_cast<POINTER_CAST>(handle);
33  }
34 
35  Mutex::~Mutex() {
36  NATIVE_INT_TYPE stat = pthread_mutex_destroy(reinterpret_cast<pthread_mutex_t*>(this->m_handle));
37  FW_ASSERT(stat == 0,stat);
38  delete reinterpret_cast<pthread_mutex_t*>(this->m_handle);
39  }
40 
41  void Mutex::lock() {
42  NATIVE_INT_TYPE stat = pthread_mutex_lock(reinterpret_cast<pthread_mutex_t*>(this->m_handle));
43  FW_ASSERT(stat == 0,stat);
44  }
45 
46  void Mutex::unLock() {
47  NATIVE_INT_TYPE stat = pthread_mutex_unlock(reinterpret_cast<pthread_mutex_t*>(this->m_handle));
48  FW_ASSERT(stat == 0,stat);
49  }
50 
51 }
Os
Definition: File.cpp:7
Os::Mutex::Mutex
Mutex()
Constructor. Mutex is unlocked when created.
Definition: Mutex.cpp:7
Assert.hpp
Os::Mutex::lock
void lock()
lock the mutex
Definition: Mutex.cpp:12
FW_ASSERT
#define FW_ASSERT(...)
Definition: Assert.hpp:8
Os::Mutex::~Mutex
virtual ~Mutex()
Destructor.
Definition: Mutex.cpp:11
Os::Mutex::unLock
void unLock()
unlock the mutex
Definition: Mutex.cpp:13
Mutex.hpp
NATIVE_INT_TYPE
int NATIVE_INT_TYPE
native integer type declaration
Definition: BasicTypes.hpp:29