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 2014 FZI Forschungszentrum Informatik, Karlsruhe, Germany 00012 // 00013 // -- END LICENSE BLOCK ------------------------------------------------ 00014 00015 //---------------------------------------------------------------------- 00021 //---------------------------------------------------------------------- 00022 #include "SemaphoreImplDarwin.h" 00023 00024 #include <mach/mach_init.h> 00025 #include <mach/task.h> 00026 00027 #include "Common.h" 00028 00029 namespace icl_core { 00030 namespace thread { 00031 00032 SemaphoreImplDarwin::SemaphoreImplDarwin(size_t initial_value) 00033 :m_semaphore(0) 00034 { 00035 semaphore_create(mach_task_self(), &m_semaphore, SYNC_POLICY_FIFO, initial_value); 00036 } 00037 00038 SemaphoreImplDarwin::~SemaphoreImplDarwin() 00039 { 00040 semaphore_destroy(mach_task_self(), m_semaphore); 00041 m_semaphore = 0; 00042 } 00043 00044 void SemaphoreImplDarwin::post() 00045 { 00046 semaphore_signal(m_semaphore); 00047 } 00048 00049 bool SemaphoreImplDarwin::tryWait() 00050 { 00051 return wait(icl_core::TimeSpan(0, 0)); 00052 } 00053 00054 bool SemaphoreImplDarwin::wait() 00055 { 00056 kern_return_t res = semaphore_wait(m_semaphore); 00057 return (res == KERN_SUCCESS); 00058 } 00059 00060 bool SemaphoreImplDarwin::wait(const icl_core::TimeSpan& timeout) 00061 { 00062 mach_timespec_t timeout_spec = timeout.machTimespec(); 00063 kern_return_t res = semaphore_timedwait(m_semaphore, timeout_spec); 00064 return (res == KERN_SUCCESS); 00065 } 00066 00067 bool SemaphoreImplDarwin::wait(const icl_core::TimeStamp& timeout) 00068 { 00069 return wait(impl::getRelativeTimeout(timeout)); 00070 } 00071 00072 } 00073 }