Mutex.cpp 2.09 KB
Newer Older
1
#include "castor/messages/Mutex.hpp"
2
#include "common/exception/Errnum.hpp"
3
#include "common/exception/Exception.hpp"
4
5
6
7
8
9

//------------------------------------------------------------------------------
//constructor
//------------------------------------------------------------------------------
castor::messages::Mutex::Mutex()  {
  pthread_mutexattr_t attr;
10
  cta::exception::Errnum::throwOnReturnedErrno(
11
12
    pthread_mutexattr_init(&attr),
    "Error from pthread_mutexattr_init in castor::messages::Mutex::Mutex()");
13
  cta::exception::Errnum::throwOnReturnedErrno(
14
15
    pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK),
    "Error from pthread_mutexattr_settype in castor::messages::Mutex::Mutex()");
16
  cta::exception::Errnum::throwOnReturnedErrno(
17
18
19
    pthread_mutex_init(&m_mutex, &attr),
    "Error from pthread_mutex_init in castor::messages::Mutex::Mutex()");
  try {
20
    cta::exception::Errnum::throwOnReturnedErrno(
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
      pthread_mutexattr_destroy(&attr),
      "Error from pthread_mutexattr_destroy in"
      " castor::messages::Mutex::Mutex()");
  } catch (...) {
    pthread_mutex_destroy(&m_mutex);
    throw;
  }
}

//------------------------------------------------------------------------------
// destructor
//------------------------------------------------------------------------------
castor::messages::Mutex::~Mutex() throw() {
  pthread_mutex_destroy(&m_mutex);
}

//------------------------------------------------------------------------------
// lock
//------------------------------------------------------------------------------
void castor::messages::Mutex::lock()  {
41
  cta::exception::Errnum::throwOnReturnedErrno(
42
43
44
45
46
47
48
49
    pthread_mutex_lock(&m_mutex),
    "Error from pthread_mutex_lock in castor::messages::Mutex::lock()");
}

//------------------------------------------------------------------------------
// unlock
//------------------------------------------------------------------------------
void castor::messages::Mutex::unlock()  {
50
  cta::exception::Errnum::throwOnReturnedErrno(
51
52
53
  pthread_mutex_unlock(&m_mutex),
    "Error from pthread_mutex_unlock in castor::messages::Mutex::unlock()");
}