00001 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*- 00002 //---------------------------------------------------------------------- 00008 //---------------------------------------------------------------------- 00009 #include "SemaphoreImplLxrt38.h" 00010 00011 #include <rtai_posix.h> 00012 00013 #include "Common.h" 00014 00015 namespace icl_core { 00016 namespace thread { 00017 00018 SemaphoreImplLxrt38::SemaphoreImplLxrt38(size_t initial_value, int type) 00019 : m_semaphore(NULL) 00020 { 00021 m_semaphore = rt_typed_sem_init(size_t(this), initial_value, type | PRIO_Q); 00022 } 00023 00024 SemaphoreImplLxrt38::~SemaphoreImplLxrt38() 00025 { 00026 if (m_semaphore != NULL) 00027 { 00028 rt_sem_delete(m_semaphore); 00029 m_semaphore = NULL; 00030 } 00031 } 00032 00033 void SemaphoreImplLxrt38::post() 00034 { 00035 rt_sem_signal(m_semaphore); 00036 } 00037 00038 bool SemaphoreImplLxrt38::tryWait() 00039 { 00040 int res = rt_sem_wait_if(m_semaphore); 00041 return (res > 0 && res < SEM_TIMOUT); 00042 } 00043 00044 bool SemaphoreImplLxrt38::wait() 00045 { 00046 int res = rt_sem_wait(m_semaphore); 00047 return (res < SEM_TIMOUT); 00048 } 00049 00050 bool SemaphoreImplLxrt38::wait(const icl_core::TimeSpan& timeout) 00051 { 00052 return wait(impl::getAbsoluteTimeout(timeout)); 00053 } 00054 00055 bool SemaphoreImplLxrt38::wait(const icl_core::TimeStamp& timeout) 00056 { 00057 struct timespec timeout_spec = timeout.systemTimespec(); 00058 int res = rt_sem_wait_until(m_semaphore, timespec2count(&timeout_spec)); 00059 return (res < SEM_TIMOUT); 00060 } 00061 00062 } 00063 }