00001 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*- 00002 00003 // -- BEGIN LICENSE BLOCK ---------------------------------------------- 00004 // This file is part of FZIs ic_workspace. 00005 // 00006 // This program is free software licensed under the LGPL 00007 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3). 00008 // You can find a copy of this license in LICENSE folder in the top 00009 // directory of the source code. 00010 // 00011 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany 00012 // 00013 // -- END LICENSE BLOCK ------------------------------------------------ 00014 00015 //---------------------------------------------------------------------- 00021 //---------------------------------------------------------------------- 00022 #include "SemaphoreImplLxrt35.h" 00023 00024 #include <errno.h> 00025 00026 #include "Common.h" 00027 00028 #include <iostream> 00029 00030 namespace icl_core { 00031 namespace thread { 00032 00033 SemaphoreImplLxrt35::SemaphoreImplLxrt35(size_t initial_value) 00034 : m_semaphore(NULL) 00035 { 00036 m_semaphore = new sem_t; 00037 sem_init_rt(m_semaphore, PTHREAD_PROCESS_PRIVATE, initial_value); 00038 } 00039 00040 SemaphoreImplLxrt35::~SemaphoreImplLxrt35() 00041 { 00042 if (m_semaphore != NULL) 00043 { 00044 sem_destroy_rt(m_semaphore); 00045 delete m_semaphore; 00046 m_semaphore = 0; 00047 } 00048 } 00049 00050 void SemaphoreImplLxrt35::post() 00051 { 00052 sem_post_rt(m_semaphore); 00053 } 00054 00055 bool SemaphoreImplLxrt35::tryWait() 00056 { 00057 int res = sem_trywait_rt(m_semaphore); 00058 return (res == 0); 00059 } 00060 00061 bool SemaphoreImplLxrt35::wait() 00062 { 00063 int res = sem_wait_rt(m_semaphore); 00064 return (res == 0); 00065 } 00066 00067 bool SemaphoreImplLxrt35::wait(const icl_core::TimeSpan& timeout) 00068 { 00069 return wait(impl::getAbsoluteTimeout(timeout)); 00070 } 00071 00072 bool SemaphoreImplLxrt35::wait(const icl_core::TimeStamp& timeout) 00073 { 00074 struct timespec timeout_spec = timeout.systemTimespec(); 00075 int res = sem_timedwait_rt(m_semaphore, &timeout_spec); 00076 return (res == 0); 00077 } 00078 00079 } 00080 }