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