Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r85846 - in trunk/boost/sync/detail: . event
From: tim_at_[hidden]
Date: 2013-09-23 04:03:14


Author: timblechmann
Date: 2013-09-23 04:03:14 EDT (Mon, 23 Sep 2013)
New Revision: 85846
URL: http://svn.boost.org/trac/boost/changeset/85846

Log:
sync: use std::atomic if available

Added:
   trunk/boost/sync/detail/atomic.hpp (contents, props changed)
Text files modified:
   trunk/boost/sync/detail/atomic.hpp | 54 ++++++++++++++++++++++++++++++++++++++++
   trunk/boost/sync/detail/event/event_futex.hpp | 10 ++++++
   trunk/boost/sync/detail/event/event_mach.hpp | 6 +++
   3 files changed, 68 insertions(+), 2 deletions(-)

Added: trunk/boost/sync/detail/atomic.hpp
==============================================================================
--- /dev/null 00:00:00 1970 (empty, because file is newly added)
+++ trunk/boost/sync/detail/atomic.hpp 2013-09-23 04:03:14 EDT (Mon, 23 Sep 2013) (r85846)
@@ -0,0 +1,54 @@
+// atomic.hpp: pull in std::atomic or boost::atomic
+//
+// 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_ATOMIC_HPP
+#define BOOST_SYNC_DETAIL_ATOMIC_HPP
+
+#ifdef BOOST_SYNC_USE_STD_ATOMIC
+#include <atomic>
+#else
+#include <boost/atomic.hpp>
+#endif
+
+namespace boost {
+namespace sync {
+namespace detail {
+
+#ifdef BOOST_SYNC_USE_STD_ATOMIC
+
+using std::atomic;
+using std::atomic_int;
+using std::atomic_bool;
+
+using std::memory_order_acquire;
+using std::memory_order_acq_rel;
+using std::memory_order_release;
+using std::memory_order_consume;
+using std::memory_order_relaxed;
+using std::memory_order_seq_cst;
+
+#else
+
+using boost::atomic;
+using boost::atomic_int;
+using boost::atomic_bool;
+
+using boost::memory_order_acquire;
+using boost::memory_order_release;
+using boost::memory_order_acq_rel;
+using boost::memory_order_consume;
+using boost::memory_order_relaxed;
+using boost::memory_order_seq_cst;
+
+#endif
+
+}
+}
+}
+
+#endif // BOOST_SYNC_DETAIL_ATOMIC_HPP

Modified: trunk/boost/sync/detail/event/event_futex.hpp
==============================================================================
--- trunk/boost/sync/detail/event/event_futex.hpp Mon Sep 23 03:41:33 2013 (r85845)
+++ trunk/boost/sync/detail/event/event_futex.hpp 2013-09-23 04:03:14 EDT (Mon, 23 Sep 2013) (r85846)
@@ -19,6 +19,9 @@
 #include <boost/assert.hpp>
 #include <boost/atomic.hpp>
 #include <boost/sync/detail/config.hpp>
+#include <boost/sync/detail/atomic.hpp>
+#include <boost/sync/detail/pause.hpp>
+
 #include <boost/sync/detail/header.hpp>
 
 namespace boost {
@@ -37,12 +40,14 @@
 
     void post() BOOST_NOEXCEPT
     {
+ using namespace boost::sync::detail; // for memory_order
         if (m_auto_reset) {
             int32_t old_state = m_state.load();
             if (old_state >= 0) {
                 for(;;) {
                     if (m_state.compare_exchange_weak(old_state, old_state - 1))
                         break;
+ detail::pause();
                 }
                 futex(&m_state, FUTEX_WAKE_PRIVATE, std::numeric_limits<int>::max() ); // wake all threads
             }
@@ -56,6 +61,7 @@
 
     void wait() BOOST_NOEXCEPT
     {
+ using namespace boost::sync::detail; // for memory_order
         if (m_auto_reset) {
             int32_t old_state = m_state.fetch_add(1) + 1;
 
@@ -101,6 +107,7 @@
 
     bool try_wait()
     {
+ using namespace boost::sync::detail; // for memory_order
         if (m_auto_reset) {
             int32_t old_state = m_state.load();
 
@@ -145,6 +152,7 @@
 private:
     bool do_wait_for(const struct timespec & timeout)
     {
+ using namespace boost::sync::detail; // for memory_order
         if (m_auto_reset) {
             int32_t old_state = m_state.fetch_add(1) + 1;
 
@@ -203,7 +211,7 @@
 
     const bool m_auto_reset;
 
- atomic<int32_t> m_state;
+ detail::atomic<int32_t> m_state;
 };
 
 }

Modified: trunk/boost/sync/detail/event/event_mach.hpp
==============================================================================
--- trunk/boost/sync/detail/event/event_mach.hpp Mon Sep 23 03:41:33 2013 (r85845)
+++ trunk/boost/sync/detail/event/event_mach.hpp 2013-09-23 04:03:14 EDT (Mon, 23 Sep 2013) (r85846)
@@ -17,6 +17,7 @@
 #include <boost/cstdint.hpp>
 
 #include <boost/sync/detail/config.hpp>
+#include <boost/sync/detail/atomic.hpp>
 #include <boost/sync/detail/pause.hpp>
 #include <boost/sync/exceptions/resource_error.hpp>
 
@@ -54,6 +55,7 @@
 
     void post() BOOST_NOEXCEPT
     {
+ using namespace boost::sync::detail; // for memory_order
         if (m_auto_reset) {
             int32_t old_state = m_state.load(memory_order_acquire);
             if (old_state >= 0) {
@@ -84,6 +86,7 @@
 
     void wait() BOOST_NOEXCEPT
     {
+ using namespace boost::sync::detail; // for memory_order
         if (m_auto_reset) {
             m_state.fetch_add(1, memory_order_acquire);
 
@@ -123,6 +126,7 @@
 private:
     bool do_try_wait_until (const mach_timespec_t & timeout)
     {
+ using namespace boost::sync::detail; // for memory_order
         if (m_auto_reset) {
             m_state.fetch_add(1, memory_order_acquire);
 
@@ -147,7 +151,7 @@
 
     const bool m_auto_reset;
     semaphore_t m_sem;
- atomic<int32_t> m_state;
+ detail::atomic<int32_t> m_state;
 };
 
 }


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