RWLockImplPosix.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/RWLockImplPosix.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 RWLockImplPosix::RWLockImplPosix()
00021   : m_rwlock(NULL)
00022 {
00023   m_rwlock = new pthread_rwlock_t;
00024   pthread_rwlock_init(m_rwlock, NULL);
00025 }
00026 
00027 RWLockImplPosix::~RWLockImplPosix()
00028 {
00029   if (m_rwlock)
00030   {
00031     pthread_rwlock_destroy(m_rwlock);
00032     delete m_rwlock;
00033     m_rwlock = NULL;
00034   }
00035 }
00036 
00037 bool RWLockImplPosix::readLock()
00038 {
00039   return pthread_rwlock_rdlock(m_rwlock) == 0;
00040 }
00041 
00042 // ReadLock with absolute timeout
00043 bool RWLockImplPosix::readLock(const ::icl_core::TimeStamp& timeout)
00044 {
00045 #ifdef _SYSTEM_DARWIN_
00046   int ret = pthread_rwlock_tryrdlock(m_rwlock);
00047   while ((ret != 0) && ((timeout > icl_core::TimeStamp::now())))
00048   {
00049     // one microsecond
00050     icl_core::os::usleep(1);
00051     ret = pthread_rwlock_tryrdlock(m_rwlock);
00052   }
00053   return (ret == 0);
00054 #else
00055   struct timespec timeout_timespec = timeout.timespec();
00056   int ret = pthread_rwlock_timedrdlock(m_rwlock, &timeout_timespec);
00057   return (ret == 0);
00058 #endif
00059 }
00060 
00061 // ReadLock with relative timeout
00062 bool RWLockImplPosix::readLock(const ::icl_core::TimeSpan& timeout)
00063 {
00064   return readLock(impl::getAbsoluteTimeout(timeout));
00065 }
00066 
00067 bool RWLockImplPosix::tryReadLock()
00068 {
00069   bool ret = pthread_rwlock_tryrdlock(m_rwlock);
00070   return (ret == 0);
00071 }
00072 
00073 bool RWLockImplPosix::writeLock()
00074 {
00075   return pthread_rwlock_wrlock(m_rwlock) == 0;
00076 }
00077 
00078 // WriteLock with absolute timeout
00079 bool RWLockImplPosix::writeLock(const ::icl_core::TimeStamp& timeout)
00080 {
00081 #ifdef _SYSTEM_DARWIN_
00082   int ret = pthread_rwlock_trywrlock(m_rwlock);
00083   while ((ret != 0) && ((timeout > icl_core::TimeStamp::now())))
00084   {
00085     // one microsecond
00086     icl_core::os::usleep(1);
00087     ret = pthread_rwlock_trywrlock(m_rwlock);
00088   }
00089   return (ret == 0);
00090 #else
00091   struct timespec timeout_timespec = timeout.timespec();
00092   bool ret = pthread_rwlock_timedwrlock(m_rwlock, &timeout_timespec);
00093   return (ret == 0);
00094 #endif
00095 }
00096 
00097 // WriteLock with relative timeout
00098 bool RWLockImplPosix::writeLock(const ::icl_core::TimeSpan& timeout)
00099 {
00100   return writeLock(impl::getAbsoluteTimeout(timeout));
00101 }
00102 
00103 bool RWLockImplPosix::tryWriteLock()
00104 {
00105   bool ret = pthread_rwlock_trywrlock(m_rwlock);
00106   return (ret == 0);
00107 }
00108 
00109 void RWLockImplPosix::unlock()
00110 {
00111   pthread_rwlock_unlock(m_rwlock);
00112 }
00113 
00114 }
00115 }


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