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 "SemaphoreImplPosix.h" 00023 00024 #include <errno.h> 00025 00026 #include "Common.h" 00027 00028 namespace icl_core { 00029 namespace thread { 00030 00031 SemaphoreImplPosix::SemaphoreImplPosix(size_t initial_value) 00032 : m_semaphore(0) 00033 { 00034 m_semaphore = new sem_t; 00035 sem_init(m_semaphore, PTHREAD_PROCESS_PRIVATE, initial_value); 00036 } 00037 00038 SemaphoreImplPosix::~SemaphoreImplPosix() 00039 { 00040 if (m_semaphore) 00041 { 00042 sem_destroy(m_semaphore); 00043 delete m_semaphore; 00044 m_semaphore = 0; 00045 } 00046 } 00047 00048 void SemaphoreImplPosix::post() 00049 { 00050 sem_post(m_semaphore); 00051 } 00052 00053 bool SemaphoreImplPosix::tryWait() 00054 { 00055 int res = sem_trywait(m_semaphore); 00056 return (res == 0); 00057 } 00058 00059 bool SemaphoreImplPosix::wait() 00060 { 00061 int res = sem_wait(m_semaphore); 00062 return (res == 0); 00063 } 00064 00065 bool SemaphoreImplPosix::wait(const icl_core::TimeSpan& timeout) 00066 { 00067 return wait(impl::getAbsoluteTimeout(timeout)); 00068 } 00069 00070 bool SemaphoreImplPosix::wait(const icl_core::TimeStamp& timeout) 00071 { 00072 struct timespec timeout_spec = timeout.timespec(); 00073 int res = sem_timedwait(m_semaphore, &timeout_spec); 00074 return (res == 0); 00075 } 00076 00077 } 00078 }