F´ Flight Software - C/C++ Documentation  devel
A framework for building embedded system applications to NASA flight quality standards.
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 }
#define FW_ASSERT(...)
Definition: Assert.hpp:14
PlatformPointerCastType POINTER_CAST
Definition: BasicTypes.h:53
PlatformIntType NATIVE_INT_TYPE
Definition: BasicTypes.h:51
virtual ~Mutex()
Destructor.
Definition: Mutex.cpp:11
void unLock()
unlock the mutex
Definition: Mutex.cpp:13
void lock()
lock the mutex
Definition: Mutex.cpp:12
Mutex()
Constructor. Mutex is unlocked when created.
Definition: Mutex.cpp:7
Definition: File.cpp:6