MutexImplPosix.cpp
Go to the documentation of this file.
00001 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
00002 //----------------------------------------------------------------------
00009 //----------------------------------------------------------------------
00010 #include "icl_core_thread/MutexImplPosix.h"
00011 
00012 #include <pthread.h>
00013 #include <icl_core/os_time.h>
00014 
00015 #include "icl_core_thread/Common.h"
00016 
00017 namespace icl_core {
00018 namespace thread {
00019 
00020 MutexImplPosix::MutexImplPosix()
00021   : m_mutex(NULL)
00022 {
00023   m_mutex = new pthread_mutex_t;
00024   pthread_mutexattr_t mutex_attr;
00025   pthread_mutexattr_init(&mutex_attr);
00026   pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE);
00027   pthread_mutex_init(m_mutex, &mutex_attr);
00028   pthread_mutexattr_destroy(&mutex_attr);
00029 }
00030 
00031 MutexImplPosix::~MutexImplPosix()
00032 {
00033   if (m_mutex)
00034   {
00035     pthread_mutex_destroy(m_mutex);
00036     delete m_mutex;
00037     m_mutex = NULL;
00038   }
00039 }
00040 
00041 bool MutexImplPosix::lock()
00042 {
00043   return pthread_mutex_lock(m_mutex) == 0;
00044 }
00045 
00046 bool MutexImplPosix::lock(const ::icl_core::TimeStamp& timeout)
00047 {
00048 #ifdef _SYSTEM_DARWIN_
00049   int ret = pthread_mutex_trylock(m_mutex);
00050   while ((ret != 0) && ((timeout > icl_core::TimeStamp::now())))
00051   {
00052     // one microsecond
00053     icl_core::os::usleep(1);
00054     ret = pthread_mutex_trylock(m_mutex);
00055   }
00056   return ret == 0;
00057 #else
00058   struct timespec timeout_spec = timeout.timespec();
00059   int ret = pthread_mutex_timedlock(m_mutex, &timeout_spec);
00060   return (ret == 0);
00061 #endif
00062 }
00063 
00064 bool MutexImplPosix::lock(const ::icl_core::TimeSpan& timeout)
00065 {
00066   return lock(impl::getAbsoluteTimeout(timeout));
00067 }
00068 
00069 bool MutexImplPosix::tryLock()
00070 {
00071   int ret = pthread_mutex_trylock(m_mutex);
00072   return (ret == 0);
00073 }
00074 
00075 void MutexImplPosix::unlock()
00076 {
00077   pthread_mutex_unlock(m_mutex);
00078 }
00079 
00080 }
00081 }


schunk_svh_driver
Author(s): Georg Heppner
autogenerated on Fri Aug 28 2015 12:59:19