12 #include <ecl/config/ecl.hpp>
13 #if defined(ECL_IS_POSIX)
21 #include "../../include/ecl/threads/mutex.hpp"
33 Mutex::Mutex(
const bool locked) :
37 pthread_mutexattr_t attr;
40 result = pthread_mutexattr_init(&attr);
43 #if defined(NDEBUG) || defined(ECL_NDEBUG)
44 result = pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_NORMAL);
46 result = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
51 result = pthread_mutex_init(&
mutex, &attr);
54 result = pthread_mutexattr_destroy(&attr);
65 pthread_mutex_destroy(&
mutex);
73 int result = pthread_mutex_lock(&
mutex);
78 bool Mutex::trylock(
Duration &duration)
80 #if defined(_POSIX_TIMEOUTS) && (_POSIX_TIMEOUTS - 200112L) >= 0L
82 timeout.tv_sec = duration.sec();
83 timeout.tv_nsec = duration.nsec();
84 int result = pthread_mutex_timedlock(&
mutex, &timeout);
85 if (result == ETIMEDOUT) {
88 ecl_assert_throw(result == 0, threads::throwMutexTimedLockException(LOC,result));
99 int result = pthread_mutex_trylock(&
mutex);
102 if (result == EBUSY) {
106 ecl_assert_throw(result == 0, threads::throwMutexLockException(LOC,result));
117 int result = pthread_mutex_unlock(&
mutex);
118 ecl_assert_throw(result == 0, threads::throwMutexUnLockException(LOC,result));