|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r51786 - in sandbox/threadpool: boost/tp boost/tp/detail libs/tp/examples libs/tp/src
From: oliver.kowalke_at_[hidden]
Date: 2009-03-15 12:06:40
Author: olli
Date: 2009-03-15 12:06:39 EDT (Sun, 15 Mar 2009)
New Revision: 51786
URL: http://svn.boost.org/trac/boost/changeset/51786
Log:
* example for launc_in_pool()
* atomic.hpp enhanced
Added:
sandbox/threadpool/libs/tp/examples/launch.cpp (contents, props changed)
Text files modified:
sandbox/threadpool/boost/tp/detail/atomic.hpp | 120 ++++++++++++++++++++++++++++++++++------
sandbox/threadpool/boost/tp/lockfree_channel.hpp | 21 +++----
sandbox/threadpool/libs/tp/src/guard.cpp | 4
3 files changed, 113 insertions(+), 32 deletions(-)
Modified: sandbox/threadpool/boost/tp/detail/atomic.hpp
==============================================================================
--- sandbox/threadpool/boost/tp/detail/atomic.hpp (original)
+++ sandbox/threadpool/boost/tp/detail/atomic.hpp 2009-03-15 12:06:39 EDT (Sun, 15 Mar 2009)
@@ -5,28 +5,112 @@
#ifndef BOOST_TP_DETAIL_ATOMIC_H
#define BOOST_TP_DETAIL_ATOMIC_H
-#include <boost/assert.hpp>
-#include <boost/interprocess/detail/atomic.hpp>
-#include <boost/utility.hpp>
+#include <boost/config.hpp>
namespace boost { namespace tp {
namespace detail
{
-template< typename A, typename C >
-bool atomic_compare_exchange( volatile A * object, C * expected, C desired)
-{ return __sync_bool_compare_and_swap( object, * expected, desired); }
-
-template< typename A, typename C >
-void atomic_store( volatile A * object, C desired)
-{ __sync_lock_test_and_set ( object, desired); }
-
-template< typename A, typename M >
-A atomic_fetch_add( volatile A * object, M operand)
-{ return __sync_fetch_and_add( object, operand); }
-
-template< typename A, typename M >
-A atomic_fetch_sub( volatile A * object, M operand)
-{ return __sync_fetch_and_sub( object, operand); }
+#if (defined BOOST_WINDOWS) && !(defined BOOST_DISABLE_WIN32)
+
+#include <Windows.h>
+
+template< typename T >
+bool atomic_compare_exchange_ptr( volatile T * object, T expected, T desired)
+{ return InterlockedCompareExchangePointer( object, expected, desired); }
+
+void atomic_write_32( volatile unsigned int * object, unsigned int desired)
+{ InterlockedExchange( object, desired); }
+
+template< typename T >
+void atomic_write_ptr( volatile T * object, T desired)
+{ InterlockedExchangePointer( object, desired); }
+
+inline
+unsigned int atomic_inc_32( volatile unsigned int * object)
+{ return InterlockedIncrement( object); }
+
+inline
+unsigned int atomic_dec_32( volatile unsigned int * object)
+{ return InterlockedDecrement( object); }
+
+#elif defined(__hpux)
+
+#include <atomic.h>
+
+template< typename T >
+bool atomic_compare_exchange_ptr( volatile T * object, T expected, T desired)
+{ return ::atomic_cas( object, expected, & desired) == object; }
+
+inline
+void atomic_write_32( volatile unsigned int * object, unsigned int desired)
+{ * object = desired; }
+
+template< typename T >
+void atomic_write_ptr( volatile T * object, T * desired)
+{ * object = desired; }
+
+inline
+unsigned int atomic_inc_32( volatile unsigned int * object)
+{ return ::atomic_inc( object); }
+
+inline
+unsigned int atomic_dec_32( volatile unsigned int * object)
+{ return ::atomic_dec( object); }
+
+#elif (defined(sun) || defined(__sun))
+
+#include <atomic.h>
+
+template< typename T >
+bool atomic_compare_exchange_ptr( volatile T * object, T expected, T desired)
+{ return ::atomic_cas_ptr( object, expected, & desired) == object; }
+
+inline
+void atomic_write_32( volatile unsigned int * object, unsigned int desired)
+{
+ unsigned int desired_( desired);
+ ::atomic_swap_32( object, desired_);
+}
+
+template< typename T >
+void atomic_write_ptr( volatile T * object, T * desired)
+{ * object = desired; }
+
+inline
+unsigned int atomic_inc_32( volatile unsigned int * object)
+{ return ::atomic_inc_32( object); }
+
+inline
+unsigned int atomic_dec_32( volatile unsigned int * object)
+{ return ::atomic_dec_32( object); }
+
+#elif defined(__GNUC__) && ( __GNUC__ * 100 + __GNUC_MINOR__ >= 401 )
+
+template< typename T >
+bool atomic_compare_exchange_ptr( volatile T * object, T expected, T desired)
+{ return __sync_bool_compare_and_swap( object, expected, desired); }
+
+inline
+void atomic_write_32( volatile unsigned int * object, unsigned int desired)
+{ * object = desired; }
+
+template< typename T >
+void atomic_write_ptr( volatile T * object, T desired)
+{ * object = desired; }
+
+inline
+unsigned int atomic_inc_32( volatile unsigned int * object)
+{ return __sync_fetch_and_add( object, 1); }
+
+inline
+unsigned int atomic_dec_32( volatile unsigned int * object)
+{ return __sync_fetch_and_sub( object, 1); }
+
+#else
+
+#error No atomic operations available for this platform!
+
+#endif
} } }
#endif // BOOST_TP_DETAIL_ATOMIC_H
Modified: sandbox/threadpool/boost/tp/lockfree_channel.hpp
==============================================================================
--- sandbox/threadpool/boost/tp/lockfree_channel.hpp (original)
+++ sandbox/threadpool/boost/tp/lockfree_channel.hpp 2009-03-15 12:06:39 EDT (Sun, 15 Mar 2009)
@@ -1,9 +1,6 @@
#ifndef BOOST_TP_LOCKFREE_CHANNEL_H
#define BOOST_TP_LOCKFREE_CHANNEL_H
-// based on the lock-free algorithm of
-// Edya Ladan-Mozes and Nir Shavit
-
#include <vector>
#include <boost/assert.hpp>
@@ -70,8 +67,8 @@
pointer_t( pointer_t const& rhs)
: ptr( 0), tag( 0)
{
- detail::atomic_store( & ptr, rhs.ptr);
- detail::atomic_store( & tag, rhs.tag);
+ detail::atomic_write_ptr( & ptr, rhs.ptr);
+ detail::atomic_write_32( & tag, rhs.tag);
}
bool operator==( pointer_t const& rhs)
@@ -103,9 +100,9 @@
bool compare_exchange_( pointer_t & dest, pointer_t & cmp, pointer_t & value)
{
- if ( detail::atomic_compare_exchange( & dest.ptr, & cmp.ptr, value.ptr) )
+ if ( detail::atomic_compare_exchange_ptr( & dest.ptr, cmp.ptr, value.ptr) )
{
- detail::atomic_store( & dest.tag, value.tag);
+ detail::atomic_write_32( & dest.tag, value.tag);
return true;
}
@@ -128,10 +125,10 @@
}
void increment_size_()
- { detail::atomic_fetch_add( & size_, 1); }
+ { detail::atomic_inc_32( & size_); }
void decrement_size_()
- { detail::atomic_fetch_sub( & size_, 1); }
+ { detail::atomic_dec_32( & size_); }
public:
lockfree_channel()
@@ -250,12 +247,12 @@
{ return state_ == channel_deactive_now; }
void activate()
- { detail::atomic_store( & state_, channel_active); }
+ { detail::atomic_write_32( ( unsigned int *) & state_, channel_active); }
void deactivate()
{
if ( active() )
- detail::atomic_store( & state_, channel_deactive);
+ detail::atomic_write_32( ( unsigned int *) & state_, channel_deactive);
BOOST_ASSERT( deactive() );
}
@@ -263,7 +260,7 @@
void deactivate_now()
{
if ( active() )
- detail::atomic_store( & state_, channel_deactive_now);
+ detail::atomic_write_32( ( unsigned int *) & state_, channel_deactive_now);
BOOST_ASSERT( deactive_now() );
}
Added: sandbox/threadpool/libs/tp/examples/launch.cpp
==============================================================================
--- (empty file)
+++ sandbox/threadpool/libs/tp/examples/launch.cpp 2009-03-15 12:06:39 EDT (Sun, 15 Mar 2009)
@@ -0,0 +1,50 @@
+#include <iostream>
+#include <cstdlib>
+#include <stdexcept>
+
+#include <boost/bind.hpp>
+
+#include "boost/tp.hpp"
+
+namespace tp = boost::tp;
+
+inline
+int fibonacci_fn( int n)
+{
+ if ( n == 0) return 0;
+ if ( n == 1) return 1;
+ int k1( 1), k2( 0);
+ for ( int i( 2); i <= n; ++i)
+ {
+ boost::this_thread::interruption_point();
+ int tmp( k1);
+ k1 = k1 + k2;
+ k2 = tmp;
+ }
+ boost::this_thread::interruption_point();
+ return k1;
+}
+
+int main( int argc, char *argv[])
+{
+ try
+ {
+ tp::pool<
+ tp::unbounded_channel< tp::fifo >
+ > pool( tp::poolsize( 1) );
+ tp::task< int > t(
+ tp::launch_in_pool(
+ boost::bind(
+ fibonacci_fn,
+ 10) ) );
+ std::cout << t.get() << std::endl;
+
+ return EXIT_SUCCESS;
+ }
+ catch ( std::exception const& e)
+ { std::cerr << "exception: " << e.what() << std::endl; }
+ catch ( ... )
+ { std::cerr << "unhandled" << std::endl; }
+
+ return EXIT_FAILURE;
+}
Modified: sandbox/threadpool/libs/tp/src/guard.cpp
==============================================================================
--- sandbox/threadpool/libs/tp/src/guard.cpp (original)
+++ sandbox/threadpool/libs/tp/src/guard.cpp 2009-03-15 12:06:39 EDT (Sun, 15 Mar 2009)
@@ -11,12 +11,12 @@
: active_worker_( active_worker)
{
BOOST_ASSERT( active_worker_ >= 0);
- atomic_fetch_add( & active_worker_, 1);
+ atomic_inc_32( & active_worker_);
}
guard::~guard()
{
- atomic_fetch_sub( & active_worker_, 1);
+ atomic_dec_32( & active_worker_);
BOOST_ASSERT( active_worker_ >= 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