00001 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*- 00002 //---------------------------------------------------------------------- 00008 //---------------------------------------------------------------------- 00009 #include "SemaphoreImplPosix.h" 00010 00011 #include <errno.h> 00012 00013 #include "Common.h" 00014 00015 namespace icl_core { 00016 namespace thread { 00017 00018 SemaphoreImplPosix::SemaphoreImplPosix(size_t initial_value) 00019 : m_semaphore(0) 00020 { 00021 m_semaphore = new sem_t; 00022 sem_init(m_semaphore, PTHREAD_PROCESS_PRIVATE, initial_value); 00023 } 00024 00025 SemaphoreImplPosix::~SemaphoreImplPosix() 00026 { 00027 if (m_semaphore) 00028 { 00029 sem_destroy(m_semaphore); 00030 delete m_semaphore; 00031 m_semaphore = 0; 00032 } 00033 } 00034 00035 void SemaphoreImplPosix::post() 00036 { 00037 sem_post(m_semaphore); 00038 } 00039 00040 bool SemaphoreImplPosix::tryWait() 00041 { 00042 int res = sem_trywait(m_semaphore); 00043 return (res == 0); 00044 } 00045 00046 bool SemaphoreImplPosix::wait() 00047 { 00048 int res = sem_wait(m_semaphore); 00049 return (res == 0); 00050 } 00051 00052 bool SemaphoreImplPosix::wait(const icl_core::TimeSpan& timeout) 00053 { 00054 return wait(impl::getAbsoluteTimeout(timeout)); 00055 } 00056 00057 bool SemaphoreImplPosix::wait(const icl_core::TimeStamp& timeout) 00058 { 00059 struct timespec timeout_spec = timeout.timespec(); 00060 int res = sem_timedwait(m_semaphore, &timeout_spec); 00061 return (res == 0); 00062 } 00063 00064 } 00065 }