mutex_pos.cpp
Go to the documentation of this file.
00001 
00008 /*****************************************************************************
00009  ** Platform Check
00010  *****************************************************************************/
00011 
00012 #include <ecl/config/ecl.hpp>
00013 #if defined(ECL_IS_POSIX)
00014 
00015 /*****************************************************************************
00016  ** Includes
00017  *****************************************************************************/
00018 
00019 #include <errno.h>
00020 #include <ecl/exceptions/standard_exception.hpp>
00021 #include "../../include/ecl/threads/mutex.hpp"
00022 
00023 /*****************************************************************************
00024  ** Namespaces
00025  *****************************************************************************/
00026 
00027 namespace ecl {
00028 
00029 /*****************************************************************************
00030  * Mutex Class Methods
00031  *****************************************************************************/
00032 
00033 Mutex::Mutex(const bool locked) ecl_assert_throw_decl(StandardException) :
00034     number_locks(0)
00035 {
00036 
00037   pthread_mutexattr_t attr;
00038   int result;
00039 
00040   result = pthread_mutexattr_init(&attr);
00041   ecl_assert_throw(result == 0, threads::throwMutexAttrException(LOC,result));
00042 
00043   #if defined(NDEBUG) || defined(ECL_NDEBUG)
00044     result = pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_NORMAL);
00045   #else
00046     result = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
00047   #endif
00048   ecl_assert_throw(result == 0, threads::throwMutexAttrException(LOC,result));
00049 
00050   if (result == 0) {
00051     result = pthread_mutex_init(&mutex, &attr);
00052   }
00053   ecl_assert_throw(result == 0, threads::throwMutexInitException(LOC,result));
00054   result = pthread_mutexattr_destroy(&attr);
00055   ecl_assert_throw(result == 0, threads::throwMutexAttrException(LOC,result));
00056 
00057   if (locked) {
00058     this->lock();
00059   }
00060 }
00061 ;
00062 
00063 Mutex::~Mutex()
00064 {
00065   pthread_mutex_destroy(&mutex);
00066   // Spank! Destructor exceptions are bad.
00067   // ecl_assert_throw( result == 0, threads::throwMutexDestroyException(LOC,result));
00068 }
00069 
00070 void Mutex::lock() ecl_assert_throw_decl(StandardException)
00071 {
00072   ++number_locks;
00073   int result = pthread_mutex_lock(&mutex);
00074   ecl_assert_throw(result == 0, threads::throwMutexLockException(LOC,result));
00075 }
00076 ;
00077 
00078 bool Mutex::trylock(Duration &duration) ecl_assert_throw_decl(StandardException)
00079 {
00080   #if defined(_POSIX_TIMEOUTS) && (_POSIX_TIMEOUTS - 200112L) >= 0L
00081     timespec timeout;
00082     timeout.tv_sec = duration.sec();
00083     timeout.tv_nsec = duration.nsec();
00084     int result = pthread_mutex_timedlock(&mutex, &timeout);
00085     if (result == ETIMEDOUT) {
00086       return false;
00087     }
00088     ecl_assert_throw(result == 0, threads::throwMutexTimedLockException(LOC,result));
00089     ++number_locks;
00090   #else
00091     return trylock(); // fallback option
00092   #endif
00093   return true;
00094 }
00095 ;
00096 
00097 bool Mutex::trylock() ecl_assert_throw_decl(StandardException)
00098 {
00099   int result = pthread_mutex_trylock(&mutex);
00100 
00101   // result will typically be EBUSY if already locked, so filter it from the assert check.
00102   if (result == EBUSY) {
00103     return false;
00104   }
00105 
00106   ecl_assert_throw(result == 0, threads::throwMutexLockException(LOC,result));
00107 
00108   // If we made it here, it means the attempt locked the mutex.
00109   ++number_locks;
00110   return true;
00111 }
00112 ;
00113 
00114 void Mutex::unlock() ecl_assert_throw_decl(StandardException)
00115 {
00116   --number_locks;
00117   int result = pthread_mutex_unlock(&mutex);
00118   ecl_assert_throw(result == 0, threads::throwMutexUnLockException(LOC,result));
00119 }
00120 ;
00121 
00122 }
00123 ;
00124 // namespace ecl
00125 
00126 #endif /* ECL_IS_POSIX */


ecl_threads
Author(s): Daniel Stonier
autogenerated on Mon Jul 3 2017 02:22:01