00001 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*- 00002 //---------------------------------------------------------------------- 00009 //---------------------------------------------------------------------- 00010 #include "icl_core_thread/RWLockImplLxrt33.h" 00011 00012 #include <icl_core/internal_raw_debug.h> 00013 #include <icl_core/os_lxrt.h> 00014 00015 #include "icl_core_thread/Common.h" 00016 00017 #undef STRICT_LXRT_CHECKS 00018 00019 00020 namespace icl_core { 00021 namespace thread { 00022 00023 RWLockImplLxrt33::RWLockImplLxrt33() 00024 : m_rwlock(NULL) 00025 { 00026 #ifdef STRICT_LXRT_CHECKS 00027 if (!icl_core::os::isThisLxrtTask()) 00028 { 00029 PRINTF("RWLockImplLxrt33::RWLockImplLxrt33: Called from a Linux task!\n"); 00030 return; 00031 } 00032 #endif 00033 m_rwlock = new pthread_rwlock_t; 00034 pthread_rwlock_init_rt(m_rwlock, NULL); 00035 } 00036 00037 RWLockImplLxrt33::~RWLockImplLxrt33() 00038 { 00039 #ifdef STRICT_LXRT_CHECKS 00040 if (!icl_core::os::isThisLxrtTask()) 00041 { 00042 PRINTF("RWLockImplLxrt33::~RWLockImplLxrt33: Called from a Linux task!\n"); 00043 return; 00044 } 00045 #endif 00046 if (m_rwlock) 00047 { 00048 pthread_rwlock_destroy_rt(m_rwlock); 00049 delete m_rwlock; 00050 m_rwlock = NULL; 00051 } 00052 } 00053 00054 bool RWLockImplLxrt33::readLock() 00055 { 00056 #ifdef STRICT_LXRT_CHECKS 00057 if (!icl_core::os::isThisLxrtTask()) 00058 { 00059 PRINTF("RWLockImplLxrt33::readLock: Called from a Linux task!\n"); 00060 return false; 00061 } 00062 #endif 00063 return pthread_rwlock_rdlock_rt(m_rwlock) == 0; 00064 } 00065 00066 bool RWLockImplLxrt33::readLock(const icl_core::TimeStamp& timeout) 00067 { 00068 #ifdef STRICT_LXRT_CHECKS 00069 if (!icl_core::os::isThisLxrtTask()) 00070 { 00071 PRINTF("RWLockImplLxrt33::readLock: Called from a Linux task!\n"); 00072 return false; 00073 } 00074 #endif 00075 struct timespec timeout_absolute_timespec = timeout.systemTimespec(); 00076 int ret = pthread_rwlock_timedrdlock_rt(m_rwlock, &timeout_absolute_timespec); 00077 return (ret == 0); 00078 } 00079 00080 bool RWLockImplLxrt33::readLock(const icl_core::TimeSpan& timeout) 00081 { 00082 #ifdef STRICT_LXRT_CHECKS 00083 if (!icl_core::os::isThisLxrtTask()) 00084 { 00085 PRINTF("RWLockImplLxrt33::readLock: Called from a Linux task!\n"); 00086 return false; 00087 } 00088 #endif 00089 return readLock(impl::getAbsoluteTimeout(timeout)); 00090 } 00091 00092 bool RWLockImplLxrt33::tryReadLock() 00093 { 00094 #ifdef STRICT_LXRT_CHECKS 00095 if (!icl_core::os::isThisLxrtTask()) 00096 { 00097 PRINTF("RWLockImplLxrt33::tryReadLock: Called from a Linux task!\n"); 00098 return false; 00099 } 00100 #endif 00101 int ret = pthread_rwlock_tryrdlock_rt(m_rwlock); 00102 return (ret == 0); 00103 } 00104 00105 bool RWLockImplLxrt33::writeLock() 00106 { 00107 #ifdef STRICT_LXRT_CHECKS 00108 if (!icl_core::os::isThisLxrtTask()) 00109 { 00110 PRINTF("RWLockImplLxrt33::writeLock: Called from a Linux task!\n"); 00111 return false; 00112 } 00113 #endif 00114 return pthread_rwlock_wrlock_rt(m_rwlock) == 0; 00115 } 00116 00117 bool RWLockImplLxrt33::writeLock(const icl_core::TimeStamp& timeout) 00118 { 00119 #ifdef STRICT_LXRT_CHECKS 00120 if (!icl_core::os::isThisLxrtTask()) 00121 { 00122 PRINTF("RWLockImplLxrt33::writeLock: Called from a Linux task!\n"); 00123 return false; 00124 } 00125 #endif 00126 struct timespec timeout_absolute_timespec = timeout.systemTimespec(); 00127 int ret = pthread_rwlock_timedwrlock_rt(m_rwlock, &timeout_absolute_timespec); 00128 return (ret == 0); 00129 } 00130 00131 bool RWLockImplLxrt33::writeLock(const icl_core::TimeSpan& timeout) 00132 { 00133 #ifdef STRICT_LXRT_CHECKS 00134 if (!icl_core::os::isThisLxrtTask()) 00135 { 00136 PRINTF("RWLockImplLxrt33::writeLock: Called from a Linux task!\n"); 00137 return false; 00138 } 00139 #endif 00140 return writeLock(impl::getAbsoluteTimeout(timeout)); 00141 } 00142 00143 bool RWLockImplLxrt33::tryWriteLock() 00144 { 00145 #ifdef STRICT_LXRT_CHECKS 00146 if (!icl_core::os::isThisLxrtTask()) 00147 { 00148 PRINTF("RWLockImplLxrt33::tryWriteLock: Called from a Linux task!\n"); 00149 return false; 00150 } 00151 #endif 00152 // ATTENTION: Calling pthread_rwlock_trywrlock_rt() while another 00153 // thread holds a read lock seems to be buggy in RTAI 3.3, so the 00154 // following does NOT work: 00155 // int ret = pthread_rwlock_trywrlock_rt(rwlock); 00156 // return (ret == 0); 00157 // Therefore we call WriteLock() with a very short timeout! 00158 static icl_core::TimeSpan try_write_lock_timeout(0, 1); 00159 return writeLock(try_write_lock_timeout); 00160 } 00161 00162 void RWLockImplLxrt33::unlock() 00163 { 00164 #ifdef STRICT_LXRT_CHECKS 00165 if (!icl_core::os::isThisLxrtTask()) 00166 { 00167 PRINTF("RWLockImplLxrt33::unlock: Called from a Linux task!\n"); 00168 return; 00169 } 00170 #endif 00171 pthread_rwlock_unlock_rt(m_rwlock); 00172 } 00173 00174 } 00175 }