|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r56749 - in sandbox/task: boost boost/task libs/task/build libs/task/examples libs/task/src libs/task/test
From: oliver.kowalke_at_[hidden]
Date: 2009-10-12 14:12:24
Author: olli
Date: 2009-10-12 14:12:22 EDT (Mon, 12 Oct 2009)
New Revision: 56749
URL: http://svn.boost.org/trac/boost/changeset/56749
Log:
- some bugfixes for spin-sny-objects
Text files modified:
sandbox/task/boost/task.hpp | 6 +
sandbox/task/boost/task/spin_auto_reset_event.hpp | 4
sandbox/task/boost/task/spin_condition.hpp | 4
sandbox/task/boost/task/spin_lock.hpp | 3
sandbox/task/boost/task/spin_manual_reset_event.hpp | 2
sandbox/task/libs/task/build/Jamfile.v2 | 4 +
sandbox/task/libs/task/examples/no_deadlock_pool3.cpp | 67 ++----------------
sandbox/task/libs/task/src/spin_auto_reset_event.cpp | 21 ++---
sandbox/task/libs/task/src/spin_condition.cpp | 46 +++++-------
sandbox/task/libs/task/src/spin_count_down_event.cpp | 141 ++++++---------------------------------
sandbox/task/libs/task/src/spin_manual_reset_event.cpp | 19 ++--
sandbox/task/libs/task/test/test_task.cpp | 5 +
12 files changed, 89 insertions(+), 233 deletions(-)
Modified: sandbox/task/boost/task.hpp
==============================================================================
--- sandbox/task/boost/task.hpp (original)
+++ sandbox/task/boost/task.hpp 2009-10-12 14:12:22 EDT (Mon, 12 Oct 2009)
@@ -10,10 +10,10 @@
#include <boost/task/as_sub_task.hpp>
#include <boost/task/async.hpp>
#include <boost/task/bounded_buffer.hpp>
-#include <boost/task/bounded_twolock_fifo.hpp>
#include <boost/task/bounded_onelock_fifo.hpp>
#include <boost/task/bounded_onelock_prio_queue.hpp>
#include <boost/task/bounded_onelock_smart_queue.hpp>
+#include <boost/task/bounded_twolock_fifo.hpp>
#include <boost/task/callable.hpp>
#include <boost/task/context.hpp>
#include <boost/task/exceptions.hpp>
@@ -28,16 +28,18 @@
#include <boost/task/semaphore.hpp>
#include <boost/task/spin_auto_reset_event.hpp>
#include <boost/task/spin_condition.hpp>
+#include <boost/task/spin_count_down_event.hpp>
#include <boost/task/spin_lock.hpp>
+#include <boost/task/spin_manual_reset_event.hpp>
#include <boost/task/spin_mutex.hpp>
#include <boost/task/stacksize.hpp>
#include <boost/task/static_pool.hpp>
#include <boost/task/task.hpp>
#include <boost/task/unbounded_buffer.hpp>
-#include <boost/task/unbounded_twolock_fifo.hpp>
#include <boost/task/unbounded_onelock_fifo.hpp>
#include <boost/task/unbounded_onelock_prio_queue.hpp>
#include <boost/task/unbounded_onelock_smart_queue.hpp>
+#include <boost/task/unbounded_twolock_fifo.hpp>
#include <boost/task/utility.hpp>
#include <boost/task/watermark.hpp>
Modified: sandbox/task/boost/task/spin_auto_reset_event.hpp
==============================================================================
--- sandbox/task/boost/task/spin_auto_reset_event.hpp (original)
+++ sandbox/task/boost/task/spin_auto_reset_event.hpp 2009-10-12 14:12:22 EDT (Mon, 12 Oct 2009)
@@ -23,10 +23,10 @@
SET
};
- volatile state_t state_;
+ volatile uint32_t state_;
public:
- spin_auto_reset_event();
+ spin_auto_reset_event( bool = false);
void set();
Modified: sandbox/task/boost/task/spin_condition.hpp
==============================================================================
--- sandbox/task/boost/task/spin_condition.hpp (original)
+++ sandbox/task/boost/task/spin_condition.hpp 2009-10-12 14:12:22 EDT (Mon, 12 Oct 2009)
@@ -27,14 +27,14 @@
NOTIFY_ALL
};
- volatile command_t cmd_;
+ volatile uint32_t cmd_;
volatile uint32_t waiters_;
spin_mutex enter_mtx_;
spin_mutex check_mtx_;
void wait_( spin_mutex &);
bool wait_( spin_mutex &, system_time const&);
- void notify_( command_t);
+ void notify_( uint32_t);
public:
spin_condition();
Modified: sandbox/task/boost/task/spin_lock.hpp
==============================================================================
--- sandbox/task/boost/task/spin_lock.hpp (original)
+++ sandbox/task/boost/task/spin_lock.hpp 2009-10-12 14:12:22 EDT (Mon, 12 Oct 2009)
@@ -20,6 +20,7 @@
class spin_lock
{
private:
+ typedef spin_lock< Mutex > lock_t;
typedef bool spin_lock::*unspecified_bool_type;
Mutex * mtx_;
@@ -92,7 +93,7 @@
{ return locked_ && mtx_; }
operator unspecified_bool_type() const
- { return locked_ ? & locked_ : 0; }
+ { return locked_ ? & lock_t::locked_ : 0; }
bool operator!() const
{ return ! locked_; }
Modified: sandbox/task/boost/task/spin_manual_reset_event.hpp
==============================================================================
--- sandbox/task/boost/task/spin_manual_reset_event.hpp (original)
+++ sandbox/task/boost/task/spin_manual_reset_event.hpp 2009-10-12 14:12:22 EDT (Mon, 12 Oct 2009)
@@ -25,7 +25,7 @@
SET
};
- volatile state_t state_;
+ volatile uint32_t state_;
volatile uint32_t waiters_;
spin_mutex enter_mtx_;
Modified: sandbox/task/libs/task/build/Jamfile.v2
==============================================================================
--- sandbox/task/libs/task/build/Jamfile.v2 (original)
+++ sandbox/task/libs/task/build/Jamfile.v2 2009-10-12 14:12:22 EDT (Mon, 12 Oct 2009)
@@ -43,6 +43,8 @@
semaphore_windows.cpp
spin_auto_reset_event.cpp
spin_condition.cpp
+ spin_count_down_event.cpp
+ spin_manual_reset_event.cpp
spin_mutex.cpp
stacksize.cpp
watermark.cpp
@@ -63,6 +65,8 @@
semaphore_posix.cpp
spin_auto_reset_event.cpp
spin_condition.cpp
+ spin_count_down_event.cpp
+ spin_manual_reset_event.cpp
spin_mutex.cpp
stacksize.cpp
watermark.cpp
Modified: sandbox/task/libs/task/examples/no_deadlock_pool3.cpp
==============================================================================
--- sandbox/task/libs/task/examples/no_deadlock_pool3.cpp (original)
+++ sandbox/task/libs/task/examples/no_deadlock_pool3.cpp 2009-10-12 14:12:22 EDT (Mon, 12 Oct 2009)
@@ -20,64 +20,13 @@
typedef tsk::static_pool< tsk::unbounded_twolock_fifo > pool_type;
-class event
-{
-private:
- class base : private boost::noncopyable
- {
- private:
- tsk::semaphore sem_;
-
- public:
- base()
- : sem_( 0)
- {}
-
- void set()
- { sem_.post(); }
-
- void wait()
- {
- if ( boost::this_task::runs_in_pool() )
- {
- while ( sem_.value() == 0)
- boost::this_task::block();
- }
- else
- {
- sem_.wait();
- sem_.post();
- }
- }
- };
-
- boost::shared_ptr< base > impl_;
-
-public:
- event()
- : impl_( new base)
- {}
-
- void set()
- { impl_->set(); }
-
- void wait()
- { impl_->wait(); }
-};
-
-void sub_task(
- int i,
- int n,
- event inner_ev)
+void sub_task( int i, int n, tsk::spin_count_down_event & ev)
{
BOOST_ASSERT( boost::this_task::runs_in_pool() );
fprintf( stderr, "t%d running ...\n", i);
- if ( i == n - 1)
- inner_ev.set();
- else
- inner_ev.wait();
+ ev.set();
fprintf( stderr, "t%d finished ...\n", i);
}
@@ -85,13 +34,13 @@
void main_task(
pool_type & pool,
int n,
- event outer_ev)
+ tsk::spin_count_down_event & outer_ev)
{
BOOST_ASSERT( boost::this_task::runs_in_pool() );
fprintf( stderr, "main-task running %d sub-tasks\n", n);
- event inner_ev;
+ tsk::spin_count_down_event inner_ev( n);
for ( int i = 0; i < n; ++i)
tsk::async(
@@ -99,7 +48,7 @@
& sub_task,
i,
n,
- inner_ev),
+ boost::ref( inner_ev) ),
tsk::as_sub_task() );
inner_ev.wait();
@@ -114,17 +63,17 @@
pool_type pool( psize);
int n = 32;
- event outer_ev;
+ tsk::spin_count_down_event ev( 1);
tsk::async(
tsk::make_task(
& main_task,
boost::ref( pool),
n,
- outer_ev),
+ boost::ref( ev) ),
pool);
fprintf( stderr, "main thread: waiting for t0 to finish\n");
- outer_ev.wait();
+ ev.wait();
fprintf( stderr, "main thread: t0 finished\n");
return EXIT_SUCCESS;
Modified: sandbox/task/libs/task/src/spin_auto_reset_event.cpp
==============================================================================
--- sandbox/task/libs/task/src/spin_auto_reset_event.cpp (original)
+++ sandbox/task/libs/task/src/spin_auto_reset_event.cpp 2009-10-12 14:12:22 EDT (Mon, 12 Oct 2009)
@@ -15,24 +15,22 @@
namespace task {
spin_auto_reset_event::spin_auto_reset_event( bool isset)
-: state_( isset ? SET : RESET)
+: state_(
+ isset ?
+ static_cast< uint32_t >( SET) :
+ static_cast< uint32_t >( RESET) )
{}
void
spin_auto_reset_event::set()
-{
- detail::atomic_exchange(
- static_cast< uint32_t volatile* >( & state_),
- static_cast< uint32_t >( SET) );
-}
+{ detail::atomic_exchange( & state_, static_cast< uint32_t >( SET) ); }
void
spin_auto_reset_event::wait()
{
- state_t expected = SET;
+ uint32_t expected = static_cast< uint32_t >( SET);
while ( ! detail::atomic_compare_exchange_strong(
- static_cast< uint32_t volatile* >( & state_),
- static_cast< uint32_t * >( & expected),
+ & state_, & expected,
static_cast< uint32_t >( RESET) ) )
{
if ( this_task::runs_in_pool() )
@@ -47,10 +45,9 @@
{
if ( get_system_time() >= abs_time) return false;
- state_t expected = SET;
+ uint32_t expected = static_cast< uint32_t >( SET);
while ( ! detail::atomic_compare_exchange_strong(
- static_cast< uint32_t volatile* >( & state_),
- static_cast< uint32_t * >( & expected),
+ & state_, & expected,
static_cast< uint32_t >( RESET) ) )
{
if ( this_task::runs_in_pool() )
Modified: sandbox/task/libs/task/src/spin_condition.cpp
==============================================================================
--- sandbox/task/libs/task/src/spin_condition.cpp (original)
+++ sandbox/task/libs/task/src/spin_condition.cpp 2009-10-12 14:12:22 EDT (Mon, 12 Oct 2009)
@@ -18,7 +18,7 @@
namespace task {
void
-spin_condition::notify_( command_t cmd)
+spin_condition::notify_( uint32_t cmd)
{
enter_mtx_.lock();
@@ -28,11 +28,9 @@
return;
}
- command_t expected = SLEEPING;
+ uint32_t expected = static_cast< uint32_t >( SLEEPING);
while ( ! detail::atomic_compare_exchange_strong(
- static_cast< uint32_t volatile* >( & cmd_),
- static_cast< uint32_t * >( & expected),
- cmd) )
+ & cmd_, & expected, cmd) )
{
if ( this_task::runs_in_pool() )
this_task::block();
@@ -54,7 +52,7 @@
bool unlock_enter_mtx = false;
for (;;)
{
- while ( SLEEPING == detail::atomic_load( static_cast< uint32_t volatile* >( & cmd_) ) )
+ while ( static_cast< uint32_t >( SLEEPING) == detail::atomic_load( & cmd_) )
{
if ( this_task::runs_in_pool() )
this_task::block();
@@ -65,14 +63,13 @@
spin_lock< spin_mutex > lk( check_mtx_);
BOOST_ASSERT( lk);
- command_t expected = NOTIFY_ONE;
+ uint32_t expected = static_cast< uint32_t >( NOTIFY_ONE);
detail::atomic_compare_exchange_strong(
- static_cast< uint32_t volatile* >( & cmd_),
- static_cast< uint32_t * >( & expected),
+ & cmd_, & expected,
static_cast< uint32_t >( SLEEPING) );
- if ( SLEEPING == expected)
+ if ( static_cast< uint32_t >( SLEEPING) == expected)
continue;
- else if ( NOTIFY_ONE == expected)
+ else if ( static_cast< uint32_t >( NOTIFY_ONE) == expected)
{
unlock_enter_mtx = true;
detail::atomic_fetch_sub( & waiters_, 1);
@@ -83,10 +80,9 @@
unlock_enter_mtx = 1 == detail::atomic_fetch_sub( & waiters_, 1);
if ( unlock_enter_mtx)
{
- expected = NOTIFY_ALL;
+ expected = static_cast< uint32_t >( NOTIFY_ALL);
detail::atomic_compare_exchange_strong(
- static_cast< uint32_t volatile* >( & cmd_),
- static_cast< uint32_t * >( & expected),
+ & cmd_, & expected,
static_cast< uint32_t >( SLEEPING) );
}
break;
@@ -114,7 +110,7 @@
bool timed_out = false, unlock_enter_mtx = false;
for (;;)
{
- while ( SLEEPING == detail::atomic_load( static_cast< uint32_t volatile* >( & cmd_) ) )
+ while ( static_cast< uint32_t >( SLEEPING) == detail::atomic_load( & cmd_) )
{
if ( this_task::runs_in_pool() )
this_task::block();
@@ -141,14 +137,13 @@
spin_lock< spin_mutex > lk( check_mtx_);
BOOST_ASSERT( lk);
- command_t expected = NOTIFY_ONE;
+ uint32_t expected = static_cast< uint32_t >( NOTIFY_ONE);
detail::atomic_compare_exchange_strong(
- static_cast< uint32_t volatile* >( & cmd_),
- static_cast< uint32_t * >( & expected),
+ & cmd_, & expected,
static_cast< uint32_t >( SLEEPING) );
- if ( SLEEPING == expected)
+ if ( static_cast< uint32_t >( SLEEPING) == expected)
continue;
- else if ( NOTIFY_ONE == expected)
+ else if ( static_cast< uint32_t >( NOTIFY_ONE) == expected)
{
unlock_enter_mtx = true;
detail::atomic_fetch_sub( & waiters_, 1);
@@ -159,10 +154,9 @@
unlock_enter_mtx = 1 == detail::atomic_fetch_sub( & waiters_, 1);
if ( unlock_enter_mtx)
{
- expected = NOTIFY_ALL;
+ expected = static_cast< uint32_t >( NOTIFY_ALL);
detail::atomic_compare_exchange_strong(
- static_cast< uint32_t volatile* >( & cmd_),
- static_cast< uint32_t * >( & expected),
+ & cmd_, & expected,
static_cast< uint32_t >( SLEEPING) );
}
break;
@@ -180,7 +174,7 @@
spin_condition::spin_condition()
:
-cmd_( SLEEPING),
+cmd_( static_cast< uint32_t >( SLEEPING) ),
waiters_( 0),
enter_mtx_(),
check_mtx_()
@@ -188,10 +182,10 @@
void
spin_condition::notify_one()
-{ notify_( NOTIFY_ONE); }
+{ notify_( static_cast< uint32_t >( NOTIFY_ONE) ); }
void
spin_condition::notify_all()
-{ notify_( NOTIFY_ALL); }
+{ notify_( static_cast< uint32_t >( NOTIFY_ALL) ); }
}}
Modified: sandbox/task/libs/task/src/spin_count_down_event.cpp
==============================================================================
--- sandbox/task/libs/task/src/spin_count_down_event.cpp (original)
+++ sandbox/task/libs/task/src/spin_count_down_event.cpp 2009-10-12 14:12:22 EDT (Mon, 12 Oct 2009)
@@ -16,10 +16,10 @@
namespace boost {
namespace task {
-spin_count_down_event::spin_count_down_event( uint32_t intial)
+spin_count_down_event::spin_count_down_event( uint32_t initial)
:
-intial_( intial),
-current_( intial_)
+initial_( initial),
+current_( initial_)
{}
uint32_t
@@ -37,64 +37,26 @@
void
spin_count_down_event::set()
{
- enter_mtx_.lock();
-
- detail::atomic_fetch_sub( & current_, 1);
+ for (;;)
+ {
+ if ( 0 == detail::atomic_load( & current_) )
+ return;
+ uint32_t expected = current_;
+ if ( detail::atomic_compare_exchange_strong( & current_, & expected, expected - 1) )
+ return;
+ }
}
void
spin_count_down_event::wait()
{
+ while ( 0 != detail::atomic_load( & current_) )
{
- spin_lock< spin_mutex > lk( enter_mtx_);
- if ( ! lk) return;
- detail::atomic_fetch_add( & waiters_, 1);
- }
-
- bool unlock_enter_mtx = false;
- for (;;)
- {
- while ( SLEEPING == detail::atomic_load( & cmd_) )
- {
- if ( this_task::runs_in_pool() )
- this_task::block();
- else
- this_thread::yield();
- }
-
- spin_lock< spin_mutex > lk( check_mtx_);
- if ( ! lk)
- {
- unlock_enter_mtx = true;
- break;
- }
-
- uint32_t expected = static_cast< uint32_t >( NOTIFY_ONE);
- detail::atomic_compare_exchange_strong(
- static_cast< uint32_t volatile* >( & cmd_), & expected, SLEEPING);
- if ( SLEEPING == expected)
- continue;
- else if ( NOTIFY_ONE == expected)
- {
- unlock_enter_mtx = true;
- detail::atomic_fetch_sub( & waiters_, 1);
- break;
- }
+ if ( this_task::runs_in_pool() )
+ this_task::block();
else
- {
- unlock_enter_mtx = 1 == detail::atomic_fetch_sub( & waiters_, 1);
- if ( unlock_enter_mtx)
- {
- expected = static_cast< uint32_t >( NOTIFY_ALL);
- detail::atomic_compare_exchange_strong(
- static_cast< uint32_t volatile* >( & cmd_), & expected, SLEEPING);
- }
- break;
- }
+ this_thread::yield();
}
-
- if ( unlock_enter_mtx)
- enter_mtx_.unlock();
}
bool
@@ -102,76 +64,17 @@
{
if ( get_system_time() >= abs_time) return false;
+ while ( 0 < detail::atomic_load( & current_) )
{
- spin_lock< spin_mutex > lk( enter_mtx_, abs_time);
- if ( ! lk) return false;
- detail::atomic_fetch_add( & waiters_, 1);
- }
-
- bool timed_out = false, unlock_enter_mtx = false;
- for (;;)
- {
- while ( SLEEPING == detail::atomic_load( & cmd_) )
- {
- if ( this_task::runs_in_pool() )
- this_task::block();
- else
- this_thread::yield();
-
- if ( get_system_time() >= abs_time)
- {
- timed_out = enter_mtx_.try_lock();
- if ( ! timed_out)
- continue;
- break;
- }
- }
-
- if ( timed_out)
- {
- detail::atomic_fetch_sub( & waiters_, 1);
- unlock_enter_mtx = true;
- break;
- }
+ if ( this_task::runs_in_pool() )
+ this_task::block();
else
- {
- spin_lock< spin_mutex > lk( check_mtx_);
- if ( ! lk)
- {
- timed_out = true;
- unlock_enter_mtx = true;
- break;
- }
-
- uint32_t expected = static_cast< uint32_t >( NOTIFY_ONE);
- detail::atomic_compare_exchange_strong(
- static_cast< uint32_t volatile* >( & cmd_), & expected, SLEEPING);
- if ( SLEEPING == expected)
- continue;
- else if ( NOTIFY_ONE == expected)
- {
- unlock_enter_mtx = true;
- detail::atomic_fetch_sub( & waiters_, 1);
- break;
- }
- else
- {
- unlock_enter_mtx = 1 == detail::atomic_fetch_sub( & waiters_, 1);
- if ( unlock_enter_mtx)
- {
- expected = static_cast< uint32_t >( NOTIFY_ALL);
- detail::atomic_compare_exchange_strong(
- static_cast< uint32_t volatile* >( & cmd_), & expected, SLEEPING);
- }
- break;
- }
- }
- }
+ this_thread::yield();
- if ( unlock_enter_mtx)
- enter_mtx_.unlock();
+ if ( get_system_time() >= abs_time) return false;
+ }
- return ! timed_out;
+ return true;
}
}}
Modified: sandbox/task/libs/task/src/spin_manual_reset_event.cpp
==============================================================================
--- sandbox/task/libs/task/src/spin_manual_reset_event.cpp (original)
+++ sandbox/task/libs/task/src/spin_manual_reset_event.cpp 2009-10-12 14:12:22 EDT (Mon, 12 Oct 2009)
@@ -18,7 +18,10 @@
spin_manual_reset_event::spin_manual_reset_event( bool isset)
:
-state_( isset ? SET : RESET),
+state_(
+ isset ?
+ static_cast< uint32_t >( SET) :
+ static_cast< uint32_t >( RESET) ),
waiters_( 0),
enter_mtx_()
{}
@@ -28,12 +31,11 @@
{
enter_mtx_.lock();
- state_t expected = RESET;
+ uint32_t expected = static_cast< uint32_t >( RESET);
if ( ! detail::atomic_compare_exchange_strong(
- static_cast< uint32_t volatile* >( & state_),
- static_cast< uint32_t * >( & expected),
+ & state_, & expected,
static_cast< uint32_t >( SET) ) ||
- ! detail::atomic_load( static_cast< uint32_t volatile* >( & waiters_) ) )
+ ! detail::atomic_load( & waiters_ ) )
enter_mtx_.unlock();
}
@@ -43,8 +45,7 @@
spin_lock< spin_mutex > lk( enter_mtx_);
BOOST_ASSERT( lk);
- detail::atomic_exchange(
- static_cast< uint32_t volatile* >( & state_),
+ detail::atomic_exchange( & state_,
static_cast< uint32_t >( RESET) );
}
@@ -57,7 +58,7 @@
detail::atomic_fetch_add( & waiters_, 1);
}
- while ( RESET == detail::atomic_load( static_cast< uint32_t volatile* >( & state_) ) )
+ while ( static_cast< uint32_t >( RESET) == detail::atomic_load( & state_) )
{
if ( this_task::runs_in_pool() )
this_task::block();
@@ -74,7 +75,7 @@
{
if ( get_system_time() >= abs_time) return false;
- while ( RESET == detail::atomic_load( & state_) )
+ while ( static_cast< uint32_t >( RESET) == detail::atomic_load( & state_) )
{
if ( this_task::runs_in_pool() )
this_task::block();
Modified: sandbox/task/libs/task/test/test_task.cpp
==============================================================================
--- sandbox/task/libs/task/test/test_task.cpp (original)
+++ sandbox/task/libs/task/test/test_task.cpp 2009-10-12 14:12:22 EDT (Mon, 12 Oct 2009)
@@ -45,10 +45,15 @@
void test_case_2()
{
tsk::task< void > t1;
+ BOOST_CHECK( ! t1);
t1 = tsk::make_task( zero_args_fn);
+ BOOST_CHECK( t1);
tsk::task< int > t2 = tsk::make_task( one_arg_fn, 1);
+ BOOST_CHECK( t2);
tsk::task< int > t3;
+ BOOST_CHECK( ! t3);
t3 = tsk::make_task( two_args_fn, 1, "abc");
+ BOOST_CHECK( t3);
}
// check moved task
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