Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r58315 - sandbox/fiber/libs/fiber/src/spin
From: oliver.kowalke_at_[hidden]
Date: 2009-12-12 04:57:31


Author: olli
Date: 2009-12-12 04:57:30 EST (Sat, 12 Dec 2009)
New Revision: 58315
URL: http://svn.boost.org/trac/boost/changeset/58315

Log:
added

Added:
   sandbox/fiber/libs/fiber/src/spin/
   sandbox/fiber/libs/fiber/src/spin/auto_reset_event.cpp (contents, props changed)
   sandbox/fiber/libs/fiber/src/spin/barrier.cpp (contents, props changed)
   sandbox/fiber/libs/fiber/src/spin/condition.cpp (contents, props changed)
   sandbox/fiber/libs/fiber/src/spin/count_down_event.cpp (contents, props changed)
   sandbox/fiber/libs/fiber/src/spin/manual_reset_event.cpp (contents, props changed)
   sandbox/fiber/libs/fiber/src/spin/mutex.cpp (contents, props changed)

Added: sandbox/fiber/libs/fiber/src/spin/auto_reset_event.cpp
==============================================================================
--- (empty file)
+++ sandbox/fiber/libs/fiber/src/spin/auto_reset_event.cpp 2009-12-12 04:57:30 EST (Sat, 12 Dec 2009)
@@ -0,0 +1,49 @@
+
+// Copyright Oliver Kowalke 2009.
+// 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)
+
+#include "boost/fiber/spin/auto_reset_event.hpp"
+
+#include <boost/fiber/detail/atomic.hpp>
+#include <boost/fiber/utility.hpp>
+
+namespace boost {
+namespace fibers {
+namespace spin {
+
+auto_reset_event::auto_reset_event( bool isset) :
+ state_(
+ isset ?
+ static_cast< uint32_t >( SET) :
+ static_cast< uint32_t >( RESET) )
+{}
+
+void
+auto_reset_event::set()
+{ detail::atomic_exchange( & state_, static_cast< uint32_t >( SET) ); }
+
+void
+auto_reset_event::wait()
+{
+ uint32_t expected = static_cast< uint32_t >( SET);
+ while ( ! detail::atomic_compare_exchange_strong(
+ & state_, & expected,
+ static_cast< uint32_t >( RESET) ) )
+ {
+ this_fiber::yield();
+ expected = static_cast< uint32_t >( SET);
+ }
+}
+
+bool
+auto_reset_event::try_wait()
+{
+ uint32_t expected = static_cast< uint32_t >( SET);
+ return detail::atomic_compare_exchange_strong(
+ & state_, & expected,
+ static_cast< uint32_t >( RESET) );
+}
+
+}}}

Added: sandbox/fiber/libs/fiber/src/spin/barrier.cpp
==============================================================================
--- (empty file)
+++ sandbox/fiber/libs/fiber/src/spin/barrier.cpp 2009-12-12 04:57:30 EST (Sat, 12 Dec 2009)
@@ -0,0 +1,43 @@
+
+// Copyright Oliver Kowalke 2009.
+// 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)
+
+#include "boost/fiber/spin/barrier.hpp"
+
+#include <stdexcept>
+
+namespace boost {
+namespace fibers {
+namespace spin {
+
+barrier::barrier( uint32_t initial) :
+ initial_( initial),
+ current_( initial_),
+ cycle_( true),
+ mtx_(),
+ cond_()
+{ if ( initial == 0) throw std::invalid_argument("invalid barrier count"); }
+
+bool
+barrier::wait()
+{
+ mutex::scoped_lock lk( mtx_);
+ bool cycle( cycle_);
+ if ( 0 == --current_)
+ {
+ cycle_ = ! cycle_;
+ current_ = initial_;
+ cond_.notify_all();
+ return true;
+ }
+ else
+ {
+ while ( cycle == cycle_)
+ cond_.wait( lk);
+ }
+ return false;
+}
+
+}}}

Added: sandbox/fiber/libs/fiber/src/spin/condition.cpp
==============================================================================
--- (empty file)
+++ sandbox/fiber/libs/fiber/src/spin/condition.cpp 2009-12-12 04:57:30 EST (Sat, 12 Dec 2009)
@@ -0,0 +1,45 @@
+
+// Copyright Oliver Kowalke 2009.
+// 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)
+
+#include "boost/fiber/spin/condition.hpp"
+
+namespace boost {
+namespace fibers {
+namespace spin {
+
+void
+condition::notify_( uint32_t cmd)
+{
+ enter_mtx_.lock();
+
+ if ( 0 == detail::atomic_load( & waiters_) )
+ {
+ enter_mtx_.unlock();
+ return;
+ }
+
+ uint32_t expected = static_cast< uint32_t >( SLEEPING);
+ while ( ! detail::atomic_compare_exchange_strong(
+ & cmd_, & expected, cmd) )
+ this_fiber::yield();
+}
+
+condition::condition() :
+ cmd_( static_cast< uint32_t >( SLEEPING) ),
+ waiters_( 0),
+ enter_mtx_(),
+ check_mtx_()
+{}
+
+void
+condition::notify_one()
+{ notify_( static_cast< uint32_t >( NOTIFY_ONE) ); }
+
+void
+condition::notify_all()
+{ notify_( static_cast< uint32_t >( NOTIFY_ALL) ); }
+
+}}}

Added: sandbox/fiber/libs/fiber/src/spin/count_down_event.cpp
==============================================================================
--- (empty file)
+++ sandbox/fiber/libs/fiber/src/spin/count_down_event.cpp 2009-12-12 04:57:30 EST (Sat, 12 Dec 2009)
@@ -0,0 +1,54 @@
+
+// Copyright Oliver Kowalke 2009.
+// 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)
+
+#include "boost/fiber/spin/count_down_event.hpp"
+
+#include <boost/fiber/detail/atomic.hpp>
+#include <boost/fiber/spin/mutex.hpp>
+#include <boost/fiber/utility.hpp>
+
+namespace boost {
+namespace fibers {
+namespace spin {
+
+count_down_event::count_down_event( uint32_t initial) :
+ initial_( initial),
+ current_( initial_)
+{}
+
+uint32_t
+count_down_event::initial() const
+{ return initial_; }
+
+uint32_t
+count_down_event::current() const
+{ return detail::atomic_load( & current_); }
+
+bool
+count_down_event::is_set() const
+{ return 0 == detail::atomic_load( & current_); }
+
+void
+count_down_event::set()
+{
+ for (;;)
+ {
+ if ( 0 == detail::atomic_load( & current_) )
+ return;
+ uint32_t expected = current_;
+ if ( detail::atomic_compare_exchange_strong( & current_, & expected, expected - 1) )
+ return;
+ }
+}
+
+void
+count_down_event::wait()
+{
+ while ( 0 != detail::atomic_load( & current_) )
+ this_fiber::yield();
+}
+
+}}}

Added: sandbox/fiber/libs/fiber/src/spin/manual_reset_event.cpp
==============================================================================
--- (empty file)
+++ sandbox/fiber/libs/fiber/src/spin/manual_reset_event.cpp 2009-12-12 04:57:30 EST (Sat, 12 Dec 2009)
@@ -0,0 +1,83 @@
+
+// Copyright Oliver Kowalke 2009.
+// 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)
+
+#include "boost/fiber/spin/manual_reset_event.hpp"
+
+#include <boost/assert.hpp>
+
+#include <boost/fiber/detail/atomic.hpp>
+#include <boost/fiber/utility.hpp>
+
+namespace boost {
+namespace fibers {
+namespace spin {
+
+manual_reset_event::manual_reset_event( bool isset) :
+ state_(
+ isset ?
+ static_cast< uint32_t >( SET) :
+ static_cast< uint32_t >( RESET) ),
+ waiters_( 0),
+ enter_mtx_()
+{}
+
+void
+manual_reset_event::set()
+{
+ enter_mtx_.lock();
+
+ uint32_t expected = static_cast< uint32_t >( RESET);
+ if ( ! detail::atomic_compare_exchange_strong(
+ & state_, & expected,
+ static_cast< uint32_t >( SET) ) ||
+ ! detail::atomic_load( & waiters_ ) )
+ enter_mtx_.unlock();
+}
+
+void
+manual_reset_event::reset()
+{
+ mutex::scoped_lock lk( enter_mtx_);
+ BOOST_ASSERT( lk);
+
+ detail::atomic_exchange( & state_,
+ static_cast< uint32_t >( RESET) );
+}
+
+void
+manual_reset_event::wait()
+{
+ {
+ mutex::scoped_lock lk( enter_mtx_);
+ BOOST_ASSERT( lk);
+ detail::atomic_fetch_add( & waiters_, 1);
+ }
+
+ while ( static_cast< uint32_t >( RESET) == detail::atomic_load( & state_) )
+ this_fiber::yield();
+
+ if ( 1 == detail::atomic_fetch_sub( & waiters_, 1) )
+ enter_mtx_.unlock();
+}
+
+bool
+manual_reset_event::try_wait()
+{
+ {
+ mutex::scoped_lock lk( enter_mtx_);
+ BOOST_ASSERT( lk);
+ detail::atomic_fetch_add( & waiters_, 1);
+ }
+
+ bool result = static_cast< uint32_t >( SET) == detail::atomic_load( & state_);
+
+ if ( 1 == detail::atomic_fetch_sub( & waiters_, 1) )
+ enter_mtx_.unlock();
+
+ return result;
+}
+
+}}}

Added: sandbox/fiber/libs/fiber/src/spin/mutex.cpp
==============================================================================
--- (empty file)
+++ sandbox/fiber/libs/fiber/src/spin/mutex.cpp 2009-12-12 04:57:30 EST (Sat, 12 Dec 2009)
@@ -0,0 +1,44 @@
+
+// Copyright Oliver Kowalke 2009.
+// 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)
+
+#include <boost/fiber/spin/mutex.hpp>
+
+#include <boost/fiber/detail/atomic.hpp>
+#include <boost/fiber/utility.hpp>
+
+namespace boost {
+namespace fibers {
+namespace spin {
+
+mutex::mutex() :
+ state_( 0)
+{}
+
+void
+mutex::lock()
+{
+ for (;;)
+ {
+ uint32_t expected = 0;
+ if ( detail::atomic_compare_exchange_strong( & state_, & expected, 1) )
+ break;
+ else
+ this_fiber::yield();
+ }
+}
+
+bool
+mutex::try_lock()
+{
+ uint32_t expected = 0;
+ return detail::atomic_compare_exchange_strong( & state_, & expected, 1);
+}
+
+void
+mutex::unlock()
+{ detail::atomic_exchange( & state_, 0); }
+
+}}}


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