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