|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r85850 - trunk/boost/sync/detail/semaphore
From: tim_at_[hidden]
Date: 2013-09-23 05:27:55
Author: timblechmann
Date: 2013-09-23 05:27:55 EDT (Mon, 23 Sep 2013)
New Revision: 85850
URL: http://svn.boost.org/trac/boost/changeset/85850
Log:
sync: semaphores - wrap mach semaphores
Added:
trunk/boost/sync/detail/semaphore/semaphore_mach.hpp (contents, props changed)
Added: trunk/boost/sync/detail/semaphore/semaphore_mach.hpp
==============================================================================
--- /dev/null 00:00:00 1970 (empty, because file is newly added)
+++ trunk/boost/sync/detail/semaphore/semaphore_mach.hpp 2013-09-23 05:27:55 EDT (Mon, 23 Sep 2013) (r85850)
@@ -0,0 +1,116 @@
+// semaphore.hpp, mach semaphores
+//
+// Copyright (C) 2013 Tim Blechmann
+//
+// Distributed under the Boost Software License, Version 1.0. (See
+// accompanying file LICENSE_1_0.txt or copy at
+// http://www.boost.org/LICENSE_1_0.txt)
+
+#ifndef BOOST_SYNC_DETAIL_SEMAPHORE_SEMAPHORE_MACH_HPP_INCLUDED_
+#define BOOST_SYNC_DETAIL_SEMAPHORE_SEMAPHORE_MACH_HPP_INCLUDED_
+
+#include <cstddef>
+
+#include <boost/throw_exception.hpp>
+#include <boost/sync/detail/config.hpp>
+#include <boost/sync/detail/system_error.hpp>
+#include <boost/sync/exceptions/resource_error.hpp>
+
+#ifdef BOOST_SYNC_USES_CHRONO
+#include <boost/chrono/system_clocks.hpp>
+#include <boost/chrono/ceil.hpp>
+#endif
+
+#include <mach/task.h>
+#include <mach/semaphore.h>
+#include <mach/mach_traps.h>
+#include <mach/mach_init.h>
+
+#include <boost/sync/detail/header.hpp>
+
+#ifdef BOOST_HAS_PRAGMA_ONCE
+#pragma once
+#endif
+
+namespace boost {
+
+namespace sync {
+
+BOOST_SYNC_DETAIL_OPEN_ABI_NAMESPACE {
+
+class semaphore
+{
+ BOOST_DELETED_FUNCTION(semaphore(semaphore const&))
+ BOOST_DELETED_FUNCTION(semaphore& operator=(semaphore const&))
+
+public:
+ explicit semaphore(unsigned int i = 0)
+ {
+ kern_return_t result = semaphore_create(mach_task_self(), &m_sem, SYNC_POLICY_FIFO, 0);
+ if (result != KERN_SUCCESS)
+ BOOST_THROW_EXCEPTION(resource_error(sync::detail::system_namespace::errc::not_enough_memory, "boost::sync::semaphore constructor failed in semaphore_create"));
+ }
+
+ ~semaphore() BOOST_NOEXCEPT
+ {
+ kern_return_t result = semaphore_destroy(mach_task_self(), m_sem);
+ BOOST_VERIFY(result == KERN_SUCCESS);
+ }
+
+ void post() BOOST_NOEXCEPT
+ {
+ kern_return_t result = semaphore_signal(m_sem);
+ BOOST_VERIFY(result == KERN_SUCCESS);
+ }
+
+ void wait() BOOST_NOEXCEPT
+ {
+ kern_return_t result = semaphore_wait(m_sem);
+ BOOST_VERIFY(result == KERN_SUCCESS);
+ }
+
+ bool try_wait(void) BOOST_NOEXCEPT
+ {
+ const mach_timespec_t mach_wait_time = { 0, 0 };
+ return do_try_wait_for( mach_wait_time );
+ }
+
+ template <class Rep, class Period>
+ bool try_wait_for(const chrono::duration<Rep, Period> & duration) BOOST_NOEXCEPT
+ {
+ chrono::seconds seconds = chrono::duration_cast<chrono::seconds>(duration);
+ chrono::nanoseconds nanoseconds = chrono::duration_cast<chrono::nanoseconds>(duration) - seconds;
+
+ const mach_timespec_t mach_wait_time = { static_cast<unsigned int>(seconds.count()), static_cast<clock_res_t>(nanoseconds.count()) };
+ return do_try_wait_for(mach_wait_time);
+ }
+
+ template <class Clock, class Duration>
+ bool try_wait_until(const chrono::time_point<Clock, Duration> & timeout ) BOOST_NOEXCEPT
+ {
+ return try_wait_for( timeout - Clock::now() );
+ }
+
+private:
+ bool do_try_wait_for(const mach_timespec_t & wait_time) BOOST_NOEXCEPT
+ {
+ kern_return_t result = semaphore_timedwait(m_sem, wait_time);
+ if (result == KERN_SUCCESS)
+ return true;
+ else
+ return false;
+ }
+
+private:
+ semaphore_t m_sem;
+};
+
+} // namespace posix
+
+} // namespace sync
+
+} // namespace boost
+
+#include <boost/sync/detail/footer.hpp>
+
+#endif // BOOST_SYNC_DETAIL_SEMAPHORE_SEMAPHORE_MACH_HPP_INCLUDED_
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk