mutex_pos.cpp
Go to the documentation of this file.
1 
8 /*****************************************************************************
9  ** Platform Check
10  *****************************************************************************/
11 
12 #include <ecl/config/ecl.hpp>
13 #if defined(ECL_IS_POSIX)
14 
15 /*****************************************************************************
16  ** Includes
17  *****************************************************************************/
18 
19 #include <errno.h>
21 #include "../../include/ecl/threads/mutex.hpp"
22 
23 /*****************************************************************************
24  ** Namespaces
25  *****************************************************************************/
26 
27 namespace ecl {
28 
29 /*****************************************************************************
30  * Mutex Class Methods
31  *****************************************************************************/
32 
33 Mutex::Mutex(const bool locked) ecl_assert_throw_decl(StandardException) :
34  number_locks(0)
35 {
36 
37  pthread_mutexattr_t attr;
38  int result;
39 
40  result = pthread_mutexattr_init(&attr);
41  ecl_assert_throw(result == 0, threads::throwMutexAttrException(LOC,result));
42 
43  #if defined(NDEBUG) || defined(ECL_NDEBUG)
44  result = pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_NORMAL);
45  #else
46  result = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
47  #endif
48  ecl_assert_throw(result == 0, threads::throwMutexAttrException(LOC,result));
49 
50  if (result == 0) {
51  result = pthread_mutex_init(&mutex, &attr);
52  }
53  ecl_assert_throw(result == 0, threads::throwMutexInitException(LOC,result));
54  result = pthread_mutexattr_destroy(&attr);
55  ecl_assert_throw(result == 0, threads::throwMutexAttrException(LOC,result));
56 
57  if (locked) {
58  this->lock();
59  }
60 }
61 ;
62 
63 Mutex::~Mutex()
64 {
65  pthread_mutex_destroy(&mutex);
66  // Spank! Destructor exceptions are bad.
67  // ecl_assert_throw( result == 0, threads::throwMutexDestroyException(LOC,result));
68 }
69 
70 void Mutex::lock() ecl_assert_throw_decl(StandardException)
71 {
72  ++number_locks;
73  int result = pthread_mutex_lock(&mutex);
74  ecl_assert_throw(result == 0, threads::throwMutexLockException(LOC,result));
75 }
76 ;
77 
78 bool Mutex::trylock(Duration &duration) ecl_assert_throw_decl(StandardException)
79 {
80  #if defined(_POSIX_TIMEOUTS) && (_POSIX_TIMEOUTS - 200112L) >= 0L
81  timespec timeout;
82  timeout.tv_sec = duration.sec();
83  timeout.tv_nsec = duration.nsec();
84  int result = pthread_mutex_timedlock(&mutex, &timeout);
85  if (result == ETIMEDOUT) {
86  return false;
87  }
88  ecl_assert_throw(result == 0, threads::throwMutexTimedLockException(LOC,result));
89  ++number_locks;
90  #else
91  return trylock(); // fallback option
92  #endif
93  return true;
94 }
95 ;
96 
97 bool Mutex::trylock() ecl_assert_throw_decl(StandardException)
98 {
99  int result = pthread_mutex_trylock(&mutex);
100 
101  // result will typically be EBUSY if already locked, so filter it from the assert check.
102  if (result == EBUSY) {
103  return false;
104  }
105 
106  ecl_assert_throw(result == 0, threads::throwMutexLockException(LOC,result));
107 
108  // If we made it here, it means the attempt locked the mutex.
109  ++number_locks;
110  return true;
111 }
112 ;
113 
114 void Mutex::unlock() ecl_assert_throw_decl(StandardException)
115 {
116  --number_locks;
117  int result = pthread_mutex_unlock(&mutex);
118  ecl_assert_throw(result == 0, threads::throwMutexUnLockException(LOC,result));
119 }
120 ;
121 
122 }
123 ;
124 // namespace ecl
125 
126 #endif /* ECL_IS_POSIX */
Embedded control libraries.
#define ecl_assert_throw(expression, exception)
#define ecl_assert_throw_decl(exception)
TimeStamp Duration
ecl::Mutex mutex


ecl_threads
Author(s): Daniel Stonier
autogenerated on Mon Jun 10 2019 13:08:44