Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r56801 - in sandbox/task: boost/task libs/task/src libs/task/test
From: oliver.kowalke_at_[hidden]
Date: 2009-10-13 16:10:24


Author: olli
Date: 2009-10-13 16:10:22 EDT (Tue, 13 Oct 2009)
New Revision: 56801
URL: http://svn.boost.org/trac/boost/changeset/56801

Log:
- test for spin-event objects + semaphore added

Added:
   sandbox/task/libs/task/test/test_semaphore.cpp (contents, props changed)
   sandbox/task/libs/task/test/test_spin_auto_reset_event.cpp (contents, props changed)
   sandbox/task/libs/task/test/test_spin_count_down_event.cpp (contents, props changed)
   sandbox/task/libs/task/test/test_spin_manual_reset_event.cpp (contents, props changed)
Text files modified:
   sandbox/task/boost/task/future.hpp | 1 +
   sandbox/task/boost/task/spin_condition.hpp | 2 ++
   sandbox/task/boost/task/spin_lock.hpp | 2 ++
   sandbox/task/boost/task/spin_mutex.hpp | 2 ++
   sandbox/task/libs/task/src/semaphore_posix.cpp | 5 +++++
   sandbox/task/libs/task/src/spin_auto_reset_event.cpp | 8 +++++++-
   sandbox/task/libs/task/src/spin_condition.cpp | 6 ++++++
   sandbox/task/libs/task/src/spin_count_down_event.cpp | 4 ++++
   sandbox/task/libs/task/src/spin_manual_reset_event.cpp | 4 ++++
   sandbox/task/libs/task/src/spin_mutex.cpp | 8 ++++++--
   sandbox/task/libs/task/test/Jamfile.v2 | 4 ++++
   11 files changed, 43 insertions(+), 3 deletions(-)

Modified: sandbox/task/boost/task/future.hpp
==============================================================================
--- sandbox/task/boost/task/future.hpp (original)
+++ sandbox/task/boost/task/future.hpp 2009-10-13 16:10:22 EDT (Tue, 13 Oct 2009)
@@ -7,6 +7,7 @@
 #ifndef BOOST_FUTURE_HPP
 #define BOOST_FUTURE_HPP
 #include <stdexcept>
+#include <vector>
 #include <boost/exception.hpp>
 #include <boost/thread.hpp>
 #include <boost/thread/detail/move.hpp>

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-13 16:10:22 EDT (Tue, 13 Oct 2009)
@@ -3,6 +3,8 @@
 // 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)
+//
+// based on boost::interprocess::sync::interprocess_condition
 
 #ifndef BOOST_TASK_SPIN_CONDITION_H
 #define BOOST_TASK_SPIN_CONDITION_H

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-13 16:10:22 EDT (Tue, 13 Oct 2009)
@@ -3,6 +3,8 @@
 // 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)
+//
+// based on boost::interprocess::sync::scoped_lock
 
 #ifndef BOOST_TASK_SPIN_LOCK_H
 #define BOOST_TASK_SPIN_LOCK_H

Modified: sandbox/task/boost/task/spin_mutex.hpp
==============================================================================
--- sandbox/task/boost/task/spin_mutex.hpp (original)
+++ sandbox/task/boost/task/spin_mutex.hpp 2009-10-13 16:10:22 EDT (Tue, 13 Oct 2009)
@@ -3,6 +3,8 @@
 // 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)
+//
+// based on boost::interprocess::sync::interprocess_mutex
 
 #ifndef BOOST_TASK_SPIN_MUTEX_H
 #define BOOST_TASK_SPIN_MUTEX_H

Modified: sandbox/task/libs/task/src/semaphore_posix.cpp
==============================================================================
--- sandbox/task/libs/task/src/semaphore_posix.cpp (original)
+++ sandbox/task/libs/task/src/semaphore_posix.cpp 2009-10-13 16:10:22 EDT (Tue, 13 Oct 2009)
@@ -10,6 +10,7 @@
 
 #include <boost/system/error_code.hpp>
 #include <boost/system/system_error.hpp>
+#include <boost/thread.hpp>
 
 #include "boost/task/exceptions.hpp"
 #include "boost/task/utility.hpp"
@@ -41,7 +42,11 @@
         if ( this_task::runs_in_pool() )
         {
                 while ( ! try_wait() )
+ {
+ this_thread::interruption_point();
                         this_task::block();
+ this_thread::interruption_point();
+ }
         }
         else
         {

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-13 16:10:22 EDT (Tue, 13 Oct 2009)
@@ -33,10 +33,13 @@
                         & state_, & expected,
                         static_cast< uint32_t >( RESET) ) )
         {
+ this_thread::interruption_point();
                 if ( this_task::runs_in_pool() )
                         this_task::block();
                 else
- this_thread::yield();
+ this_thread::yield();
+ this_thread::interruption_point();
+ expected = static_cast< uint32_t >( SET);
         }
 }
 
@@ -50,12 +53,15 @@
                         & state_, & expected,
                         static_cast< uint32_t >( RESET) ) )
         {
+ this_thread::interruption_point();
                 if ( this_task::runs_in_pool() )
                         this_task::block();
                 else
                         this_thread::yield();
+ this_thread::interruption_point();
         
                 if ( get_system_time() >= abs_time) return false;
+ expected = static_cast< uint32_t >( SET);
         }
 
         return true;

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-13 16:10:22 EDT (Tue, 13 Oct 2009)
@@ -32,10 +32,12 @@
         while ( ! detail::atomic_compare_exchange_strong(
                                 & cmd_, & expected, cmd) )
         {
+ this_thread::interruption_point();
                 if ( this_task::runs_in_pool() )
                         this_task::block();
                 else
                         this_thread::yield();
+ this_thread::interruption_point();
         }
 }
 
@@ -54,10 +56,12 @@
         {
                 while ( static_cast< uint32_t >( SLEEPING) == detail::atomic_load( & cmd_) )
                 {
+ this_thread::interruption_point();
                         if ( this_task::runs_in_pool() )
                                 this_task::block();
                         else
                                 this_thread::yield();
+ this_thread::interruption_point();
                 }
 
                 spin_lock< spin_mutex > lk( check_mtx_);
@@ -112,10 +116,12 @@
         {
                 while ( static_cast< uint32_t >( SLEEPING) == detail::atomic_load( & cmd_) )
                 {
+ this_thread::interruption_point();
                         if ( this_task::runs_in_pool() )
                                 this_task::block();
                         else
                                 this_thread::yield();
+ this_thread::interruption_point();
 
                         if ( get_system_time() >= abs_time)
                         {

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-13 16:10:22 EDT (Tue, 13 Oct 2009)
@@ -52,10 +52,12 @@
 {
         while ( 0 != detail::atomic_load( & current_) )
         {
+ this_thread::interruption_point();
                 if ( this_task::runs_in_pool() )
                         this_task::block();
                 else
                         this_thread::yield();
+ this_thread::interruption_point();
         }
 }
 
@@ -66,10 +68,12 @@
 
         while ( 0 < detail::atomic_load( & current_) )
         {
+ this_thread::interruption_point();
                 if ( this_task::runs_in_pool() )
                         this_task::block();
                 else
                         this_thread::yield();
+ this_thread::interruption_point();
 
                 if ( get_system_time() >= abs_time) return false;
         }

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-13 16:10:22 EDT (Tue, 13 Oct 2009)
@@ -60,10 +60,12 @@
 
         while ( static_cast< uint32_t >( RESET) == detail::atomic_load( & state_) )
         {
+ this_thread::interruption_point();
                 if ( this_task::runs_in_pool() )
                         this_task::block();
                 else
                         this_thread::yield();
+ this_thread::interruption_point();
         }
 
         if ( 1 == detail::atomic_fetch_sub( & waiters_, 1) )
@@ -77,10 +79,12 @@
 
         while ( static_cast< uint32_t >( RESET) == detail::atomic_load( & state_) )
         {
+ this_thread::interruption_point();
                 if ( this_task::runs_in_pool() )
                         this_task::block();
                 else
                         this_thread::yield();
+ this_thread::interruption_point();
         
                 if ( get_system_time() >= abs_time) return false;
         }

Modified: sandbox/task/libs/task/src/spin_mutex.cpp
==============================================================================
--- sandbox/task/libs/task/src/spin_mutex.cpp (original)
+++ sandbox/task/libs/task/src/spin_mutex.cpp 2009-10-13 16:10:22 EDT (Tue, 13 Oct 2009)
@@ -28,10 +28,12 @@
                         break;
                 else
                 {
+ this_thread::interruption_point();
                         if ( this_task::runs_in_pool() )
                                 this_task::block();
                         else
                                 this_thread::yield();
+ this_thread::interruption_point();
                 }
         }
 }
@@ -48,8 +50,8 @@
 {
         if ( abs_time.is_infinity() )
         {
- lock();
- return true;
+ lock();
+ return true;
         }
 
         if ( get_system_time() >= abs_time)
@@ -62,10 +64,12 @@
                 if ( get_system_time() >= abs_time)
                         return false;
 
+ this_thread::interruption_point();
                 if ( this_task::runs_in_pool() )
                         this_task::block();
                 else
                         this_thread::yield();
+ this_thread::interruption_point();
         }
 
         return true;

Modified: sandbox/task/libs/task/test/Jamfile.v2
==============================================================================
--- sandbox/task/libs/task/test/Jamfile.v2 (original)
+++ sandbox/task/libs/task/test/Jamfile.v2 2009-10-13 16:10:22 EDT (Tue, 13 Oct 2009)
@@ -34,4 +34,8 @@
     [ task-test test_bounded_twolock_pool ]
     [ task-test test_bounded_onelock_pool ]
     [ task-test test_as_sub_task ]
+ [ task-test test_semaphore ]
+ [ task-test test_spin_count_down_event ]
+ [ task-test test_spin_auto_reset_event ]
+ [ task-test test_spin_manual_reset_event ]
     ;

Added: sandbox/task/libs/task/test/test_semaphore.cpp
==============================================================================
--- (empty file)
+++ sandbox/task/libs/task/test/test_semaphore.cpp 2009-10-13 16:10:22 EDT (Tue, 13 Oct 2009)
@@ -0,0 +1,184 @@
+
+// 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 <cstdlib>
+#include <iostream>
+#include <map>
+#include <stdexcept>
+#include <vector>
+
+#include <boost/bind.hpp>
+#include <boost/date_time/posix_time/posix_time.hpp>
+#include <boost/function.hpp>
+#include <boost/ref.hpp>
+#include <boost/test/unit_test.hpp>
+#include <boost/thread.hpp>
+#include <boost/utility.hpp>
+
+#include <boost/task.hpp>
+
+#include "test_functions.hpp"
+
+namespace pt = boost::posix_time;
+namespace tsk = boost::task;
+
+namespace {
+uint32_t wait_fn( uint32_t n, tsk::semaphore & sem)
+{
+ sem.wait();
+ return n;
+}
+}
+
+class test_semaphore
+{
+public:
+ // check wait in new thread
+ void test_case_1()
+ {
+ uint32_t n = 3;
+ tsk::semaphore sem( 0);
+
+ tsk::handle< uint32_t > h1(
+ tsk::async(
+ tsk::make_task(
+ wait_fn,
+ n, boost::ref( sem) ),
+ tsk::new_thread() ) );
+ tsk::handle< uint32_t > h2(
+ tsk::async(
+ tsk::make_task(
+ wait_fn,
+ n, boost::ref( sem) ),
+ tsk::new_thread() ) );
+
+ boost::this_thread::sleep( pt::millisec( 250) );
+ BOOST_CHECK( ! h1.is_ready() );
+ BOOST_CHECK( ! h2.is_ready() );
+
+ sem.post();
+
+ boost::this_thread::sleep( pt::millisec( 250) );
+ BOOST_CHECK( h1.is_ready() || h2.is_ready() );
+ if ( h1.is_ready() )
+ {
+ BOOST_CHECK_EQUAL( h1.get(), n);
+ BOOST_CHECK( ! h2.is_ready() );
+
+ sem.post();
+
+ boost::this_thread::sleep( pt::millisec( 250) );
+ BOOST_CHECK( h2.is_ready() );
+ BOOST_CHECK_EQUAL( h2.get(), n);
+ }
+ else
+ {
+ BOOST_CHECK( h2.is_ready() );
+ BOOST_CHECK_EQUAL( h2.get(), n);
+ BOOST_CHECK( ! h1.is_ready() );
+
+ sem.post();
+
+ boost::this_thread::sleep( pt::millisec( 250) );
+ BOOST_CHECK( h1.is_ready() );
+ BOOST_CHECK_EQUAL( h1.get(), n);
+
+ }
+ }
+
+ // check wait in pool
+ void test_case_2()
+ {
+ tsk::static_pool<
+ tsk::unbounded_onelock_fifo
+ > pool( tsk::poolsize( 3) );
+
+ uint32_t n = 3;
+ tsk::semaphore sem( 0);
+
+ tsk::handle< uint32_t > h1(
+ tsk::async(
+ tsk::make_task(
+ wait_fn,
+ n, boost::ref( sem) ),
+ pool) );
+ tsk::handle< uint32_t > h2(
+ tsk::async(
+ tsk::make_task(
+ wait_fn,
+ n, boost::ref( sem) ),
+ pool) );
+ boost::this_thread::sleep( pt::millisec( 250) );
+ BOOST_CHECK( ! h1.is_ready() );
+ BOOST_CHECK( ! h2.is_ready() );
+
+ sem.post();
+
+ boost::this_thread::sleep( pt::millisec( 250) );
+ BOOST_CHECK( h1.is_ready() || h2.is_ready() );
+ if ( h1.is_ready() )
+ {
+ BOOST_CHECK_EQUAL( h1.get(), n);
+ BOOST_CHECK( ! h2.is_ready() );
+
+ sem.post();
+
+ boost::this_thread::sleep( pt::millisec( 250) );
+ BOOST_CHECK( h2.is_ready() );
+ BOOST_CHECK_EQUAL( h2.get(), n);
+ }
+ else
+ {
+ BOOST_CHECK( h2.is_ready() );
+ BOOST_CHECK_EQUAL( h2.get(), n);
+ BOOST_CHECK( ! h1.is_ready() );
+
+ sem.post();
+
+ boost::this_thread::sleep( pt::millisec( 250) );
+ BOOST_CHECK( h1.is_ready() );
+ BOOST_CHECK_EQUAL( h1.get(), n);
+
+ }
+ }
+
+ void test_case_3()
+ {
+ uint32_t n = 3;
+ tsk::semaphore sem( 2);
+
+ tsk::handle< uint32_t > h1(
+ tsk::async(
+ tsk::make_task(
+ wait_fn,
+ n, boost::ref( sem) ),
+ tsk::new_thread() ) );
+ tsk::handle< uint32_t > h2(
+ tsk::async(
+ tsk::make_task(
+ wait_fn,
+ n, boost::ref( sem) ),
+ tsk::new_thread() ) );
+
+ boost::this_thread::sleep( pt::millisec( 250) );
+ BOOST_CHECK( h1.is_ready() );
+ BOOST_CHECK( h2.is_ready() );
+ BOOST_CHECK_EQUAL( h1.get(), n);
+ BOOST_CHECK_EQUAL( h2.get(), n);
+ }
+};
+
+boost::unit_test::test_suite * init_unit_test_suite( int, char* [])
+{
+ boost::unit_test::test_suite * test( BOOST_TEST_SUITE("Boost.Task: test suite") );
+
+ boost::shared_ptr< test_semaphore > instance( new test_semaphore() );
+ test->add( BOOST_CLASS_TEST_CASE( & test_semaphore::test_case_1, instance) );
+ test->add( BOOST_CLASS_TEST_CASE( & test_semaphore::test_case_2, instance) );
+ test->add( BOOST_CLASS_TEST_CASE( & test_semaphore::test_case_3, instance) );
+
+ return test;
+}

Added: sandbox/task/libs/task/test/test_spin_auto_reset_event.cpp
==============================================================================
--- (empty file)
+++ sandbox/task/libs/task/test/test_spin_auto_reset_event.cpp 2009-10-13 16:10:22 EDT (Tue, 13 Oct 2009)
@@ -0,0 +1,205 @@
+
+// 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 <cstdlib>
+#include <iostream>
+#include <map>
+#include <stdexcept>
+#include <vector>
+
+#include <boost/bind.hpp>
+#include <boost/date_time/posix_time/posix_time.hpp>
+#include <boost/function.hpp>
+#include <boost/ref.hpp>
+#include <boost/test/unit_test.hpp>
+#include <boost/thread.hpp>
+#include <boost/utility.hpp>
+
+#include <boost/task.hpp>
+
+#include "test_functions.hpp"
+
+namespace pt = boost::posix_time;
+namespace tsk = boost::task;
+
+namespace {
+uint32_t wait_fn( uint32_t n, tsk::spin_auto_reset_event & ev)
+{
+ ev.wait();
+ return n;
+}
+}
+
+class test_spin_auto_reset_event
+{
+public:
+ // check wait in new thread
+ void test_case_1()
+ {
+ uint32_t n = 3;
+ tsk::spin_auto_reset_event ev;
+
+ tsk::handle< uint32_t > h1(
+ tsk::async(
+ tsk::make_task(
+ wait_fn,
+ n, boost::ref( ev) ),
+ tsk::new_thread() ) );
+ tsk::handle< uint32_t > h2(
+ tsk::async(
+ tsk::make_task(
+ wait_fn,
+ n, boost::ref( ev) ),
+ tsk::new_thread() ) );
+
+ boost::this_thread::sleep( pt::millisec( 250) );
+ BOOST_CHECK( ! h1.is_ready() );
+ BOOST_CHECK( ! h2.is_ready() );
+
+ ev.set();
+
+ boost::this_thread::sleep( pt::millisec( 250) );
+ BOOST_CHECK( h1.is_ready() || h2.is_ready() );
+ if ( h1.is_ready() )
+ {
+ BOOST_CHECK_EQUAL( h1.get(), n);
+ BOOST_CHECK( ! h2.is_ready() );
+
+ ev.set();
+
+ boost::this_thread::sleep( pt::millisec( 250) );
+ BOOST_CHECK( h2.is_ready() );
+ BOOST_CHECK_EQUAL( h2.get(), n);
+ }
+ else
+ {
+ BOOST_CHECK( h2.is_ready() );
+ BOOST_CHECK_EQUAL( h2.get(), n);
+ BOOST_CHECK( ! h1.is_ready() );
+
+ ev.set();
+
+ boost::this_thread::sleep( pt::millisec( 250) );
+ BOOST_CHECK( h1.is_ready() );
+ BOOST_CHECK_EQUAL( h1.get(), n);
+
+ }
+ }
+
+ // check wait in pool
+ void test_case_2()
+ {
+ tsk::static_pool<
+ tsk::unbounded_onelock_fifo
+ > pool( tsk::poolsize( 3) );
+
+ uint32_t n = 3;
+ tsk::spin_auto_reset_event ev;
+
+ tsk::handle< uint32_t > h1(
+ tsk::async(
+ tsk::make_task(
+ wait_fn,
+ n, boost::ref( ev) ),
+ pool) );
+ tsk::handle< uint32_t > h2(
+ tsk::async(
+ tsk::make_task(
+ wait_fn,
+ n, boost::ref( ev) ),
+ pool) );
+ boost::this_thread::sleep( pt::millisec( 250) );
+ BOOST_CHECK( ! h1.is_ready() );
+ BOOST_CHECK( ! h2.is_ready() );
+
+ ev.set();
+
+ boost::this_thread::sleep( pt::millisec( 250) );
+ BOOST_CHECK( h1.is_ready() || h2.is_ready() );
+ if ( h1.is_ready() )
+ {
+ BOOST_CHECK_EQUAL( h1.get(), n);
+ BOOST_CHECK( ! h2.is_ready() );
+
+ ev.set();
+
+ boost::this_thread::sleep( pt::millisec( 250) );
+ BOOST_CHECK( h2.is_ready() );
+ BOOST_CHECK_EQUAL( h2.get(), n);
+ }
+ else
+ {
+ BOOST_CHECK( h2.is_ready() );
+ BOOST_CHECK_EQUAL( h2.get(), n);
+ BOOST_CHECK( ! h1.is_ready() );
+
+ ev.set();
+
+ boost::this_thread::sleep( pt::millisec( 250) );
+ BOOST_CHECK( h1.is_ready() );
+ BOOST_CHECK_EQUAL( h1.get(), n);
+
+ }
+ }
+
+ void test_case_3()
+ {
+ uint32_t n = 3;
+ tsk::spin_auto_reset_event ev( true);
+
+ tsk::handle< uint32_t > h1(
+ tsk::async(
+ tsk::make_task(
+ wait_fn,
+ n, boost::ref( ev) ),
+ tsk::new_thread() ) );
+ tsk::handle< uint32_t > h2(
+ tsk::async(
+ tsk::make_task(
+ wait_fn,
+ n, boost::ref( ev) ),
+ tsk::new_thread() ) );
+
+ boost::this_thread::sleep( pt::millisec( 250) );
+ BOOST_CHECK( h1.is_ready() || h2.is_ready() );
+ if ( h1.is_ready() )
+ {
+ BOOST_CHECK_EQUAL( h1.get(), n);
+ BOOST_CHECK( ! h2.is_ready() );
+
+ ev.set();
+
+ boost::this_thread::sleep( pt::millisec( 250) );
+ BOOST_CHECK( h2.is_ready() );
+ BOOST_CHECK_EQUAL( h2.get(), n);
+ }
+ else
+ {
+ BOOST_CHECK( h2.is_ready() );
+ BOOST_CHECK_EQUAL( h2.get(), n);
+ BOOST_CHECK( ! h1.is_ready() );
+
+ ev.set();
+
+ boost::this_thread::sleep( pt::millisec( 250) );
+ BOOST_CHECK( h1.is_ready() );
+ BOOST_CHECK_EQUAL( h1.get(), n);
+
+ }
+ }
+};
+
+boost::unit_test::test_suite * init_unit_test_suite( int, char* [])
+{
+ boost::unit_test::test_suite * test( BOOST_TEST_SUITE("Boost.Task: test suite") );
+
+ boost::shared_ptr< test_spin_auto_reset_event > instance( new test_spin_auto_reset_event() );
+ test->add( BOOST_CLASS_TEST_CASE( & test_spin_auto_reset_event::test_case_1, instance) );
+ test->add( BOOST_CLASS_TEST_CASE( & test_spin_auto_reset_event::test_case_2, instance) );
+ test->add( BOOST_CLASS_TEST_CASE( & test_spin_auto_reset_event::test_case_3, instance) );
+
+ return test;
+}

Added: sandbox/task/libs/task/test/test_spin_count_down_event.cpp
==============================================================================
--- (empty file)
+++ sandbox/task/libs/task/test/test_spin_count_down_event.cpp 2009-10-13 16:10:22 EDT (Tue, 13 Oct 2009)
@@ -0,0 +1,129 @@
+
+// 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 <cstdlib>
+#include <iostream>
+#include <map>
+#include <stdexcept>
+#include <vector>
+
+#include <boost/bind.hpp>
+#include <boost/date_time/posix_time/posix_time.hpp>
+#include <boost/function.hpp>
+#include <boost/ref.hpp>
+#include <boost/test/unit_test.hpp>
+#include <boost/thread.hpp>
+#include <boost/utility.hpp>
+
+#include <boost/task.hpp>
+
+#include "test_functions.hpp"
+
+namespace pt = boost::posix_time;
+namespace tsk = boost::task;
+
+namespace {
+uint32_t wait_fn( uint32_t n, tsk::spin_count_down_event & ev)
+{
+ ev.wait();
+ return n;
+}
+}
+
+class test_spin_count_down_event
+{
+public:
+ // check initial + current
+ void test_case_1()
+ {
+ uint32_t n = 3;
+ tsk::spin_count_down_event ev( n);
+ BOOST_CHECK_EQUAL( ev.initial(), n);
+ BOOST_CHECK_EQUAL( ev.current(), n);
+
+ ev.set();
+ BOOST_CHECK_EQUAL( ev.initial(), n);
+ BOOST_CHECK_EQUAL( ev.current(), static_cast< uint32_t >( 2) );
+
+ ev.set();
+ BOOST_CHECK_EQUAL( ev.initial(), n);
+ BOOST_CHECK_EQUAL( ev.current(), static_cast< uint32_t >( 1) );
+
+ ev.set();
+ BOOST_CHECK_EQUAL( ev.initial(), n);
+ BOOST_CHECK_EQUAL( ev.current(), static_cast< uint32_t >( 0) );
+
+ ev.set();
+ BOOST_CHECK_EQUAL( ev.initial(), n);
+ BOOST_CHECK_EQUAL( ev.current(), static_cast< uint32_t >( 0) );
+ }
+
+ // check wait in new thread
+ void test_case_2()
+ {
+ uint32_t n = 3;
+ tsk::spin_count_down_event ev( n);
+ BOOST_CHECK_EQUAL( ev.initial(), n);
+ BOOST_CHECK_EQUAL( ev.current(), n);
+
+ tsk::handle< uint32_t > h(
+ tsk::async(
+ tsk::make_task(
+ wait_fn,
+ n, boost::ref( ev) ),
+ tsk::new_thread() ) );
+ BOOST_CHECK( ! h.is_ready() );
+ for ( uint32_t i = 0; i < n; ++i)
+ {
+ ev.set();
+ BOOST_CHECK( ! h.is_ready() );
+ }
+ BOOST_CHECK_EQUAL( ev.initial(), n);
+ BOOST_CHECK_EQUAL( ev.current(), static_cast< uint32_t >( 0) );
+ BOOST_CHECK_EQUAL( h.get(), n);
+ }
+
+ // check wait in pool
+ void test_case_3()
+ {
+ tsk::static_pool<
+ tsk::unbounded_onelock_fifo
+ > pool( tsk::poolsize( 3) );
+
+ uint32_t n = 3;
+ tsk::spin_count_down_event ev( n);
+ BOOST_CHECK_EQUAL( ev.initial(), n);
+ BOOST_CHECK_EQUAL( ev.current(), n);
+
+ tsk::handle< uint32_t > h(
+ tsk::async(
+ tsk::make_task(
+ wait_fn,
+ n, boost::ref( ev) ),
+ pool) );
+ BOOST_CHECK( ! h.is_ready() );
+ for ( uint32_t i = 0; i < n; ++i)
+ {
+ ev.set();
+ BOOST_CHECK( ! h.is_ready() );
+ }
+ BOOST_CHECK_EQUAL( ev.initial(), n);
+ BOOST_CHECK_EQUAL( ev.current(), static_cast< uint32_t >( 0) );
+ BOOST_CHECK_EQUAL( h.get(), n);
+ }
+};
+
+boost::unit_test::test_suite * init_unit_test_suite( int, char* [])
+{
+ boost::unit_test::test_suite * test( BOOST_TEST_SUITE("Boost.Task: test suite") );
+
+ boost::shared_ptr< test_spin_count_down_event > instance( new test_spin_count_down_event() );
+ test->add( BOOST_CLASS_TEST_CASE( & test_spin_count_down_event::test_case_1, instance) );
+ test->add( BOOST_CLASS_TEST_CASE( & test_spin_count_down_event::test_case_2, instance) );
+ test->add( BOOST_CLASS_TEST_CASE( & test_spin_count_down_event::test_case_3, instance) );
+
+ return test;
+}

Added: sandbox/task/libs/task/test/test_spin_manual_reset_event.cpp
==============================================================================
--- (empty file)
+++ sandbox/task/libs/task/test/test_spin_manual_reset_event.cpp 2009-10-13 16:10:22 EDT (Tue, 13 Oct 2009)
@@ -0,0 +1,224 @@
+
+// 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 <cstdlib>
+#include <iostream>
+#include <map>
+#include <stdexcept>
+#include <vector>
+
+#include <boost/bind.hpp>
+#include <boost/date_time/posix_time/posix_time.hpp>
+#include <boost/function.hpp>
+#include <boost/ref.hpp>
+#include <boost/test/unit_test.hpp>
+#include <boost/thread.hpp>
+#include <boost/utility.hpp>
+
+#include <boost/task.hpp>
+
+#include "test_functions.hpp"
+
+namespace pt = boost::posix_time;
+namespace tsk = boost::task;
+
+namespace {
+uint32_t wait_fn( uint32_t n, tsk::spin_manual_reset_event & ev)
+{
+ ev.wait();
+ return n;
+}
+}
+
+class test_spin_manual_reset_event
+{
+public:
+ // check wait in new thread
+ void test_case_1()
+ {
+ uint32_t n = 3;
+ tsk::spin_manual_reset_event ev;
+
+ tsk::handle< uint32_t > h1(
+ tsk::async(
+ tsk::make_task(
+ wait_fn,
+ n, boost::ref( ev) ),
+ tsk::new_thread() ) );
+ tsk::handle< uint32_t > h2(
+ tsk::async(
+ tsk::make_task(
+ wait_fn,
+ n, boost::ref( ev) ),
+ tsk::new_thread() ) );
+
+ boost::this_thread::sleep( pt::millisec( 250) );
+ BOOST_CHECK( ! h1.is_ready() );
+ BOOST_CHECK( ! h2.is_ready() );
+
+ ev.set();
+
+ boost::this_thread::sleep( pt::millisec( 250) );
+ BOOST_CHECK( h1.is_ready() );
+ BOOST_CHECK( h2.is_ready() );
+ BOOST_CHECK_EQUAL( h1.get(), n);
+ BOOST_CHECK_EQUAL( h2.get(), n);
+
+ ev.reset();
+
+ tsk::handle< uint32_t > h3(
+ tsk::async(
+ tsk::make_task(
+ wait_fn,
+ n, boost::ref( ev) ),
+ tsk::new_thread() ) );
+ tsk::handle< uint32_t > h4(
+ tsk::async(
+ tsk::make_task(
+ wait_fn,
+ n, boost::ref( ev) ),
+ tsk::new_thread() ) );
+
+ boost::this_thread::sleep( pt::millisec( 250) );
+ BOOST_CHECK( ! h3.is_ready() );
+ BOOST_CHECK( ! h4.is_ready() );
+
+ ev.set();
+
+ boost::this_thread::sleep( pt::millisec( 250) );
+ BOOST_CHECK( h3.is_ready() );
+ BOOST_CHECK( h4.is_ready() );
+ BOOST_CHECK_EQUAL( h3.get(), n);
+ BOOST_CHECK_EQUAL( h4.get(), n);
+ }
+
+ // check wait in pool
+ void test_case_2()
+ {
+ tsk::static_pool<
+ tsk::unbounded_onelock_fifo
+ > pool( tsk::poolsize( 3) );
+
+ uint32_t n = 3;
+ tsk::spin_manual_reset_event ev;
+
+ tsk::handle< uint32_t > h1(
+ tsk::async(
+ tsk::make_task(
+ wait_fn,
+ n, boost::ref( ev) ),
+ pool) );
+ tsk::handle< uint32_t > h2(
+ tsk::async(
+ tsk::make_task(
+ wait_fn,
+ n, boost::ref( ev) ),
+ pool) );
+
+ boost::this_thread::sleep( pt::millisec( 250) );
+ BOOST_CHECK( ! h1.is_ready() );
+ BOOST_CHECK( ! h2.is_ready() );
+
+ ev.set();
+
+ boost::this_thread::sleep( pt::millisec( 250) );
+ BOOST_CHECK( h1.is_ready() );
+ BOOST_CHECK( h2.is_ready() );
+ BOOST_CHECK_EQUAL( h1.get(), n);
+ BOOST_CHECK_EQUAL( h2.get(), n);
+
+ ev.reset();
+
+ tsk::handle< uint32_t > h3(
+ tsk::async(
+ tsk::make_task(
+ wait_fn,
+ n, boost::ref( ev) ),
+ tsk::new_thread() ) );
+ tsk::handle< uint32_t > h4(
+ tsk::async(
+ tsk::make_task(
+ wait_fn,
+ n, boost::ref( ev) ),
+ tsk::new_thread() ) );
+
+ boost::this_thread::sleep( pt::millisec( 250) );
+ BOOST_CHECK( ! h3.is_ready() );
+ BOOST_CHECK( ! h4.is_ready() );
+
+ ev.set();
+
+ boost::this_thread::sleep( pt::millisec( 250) );
+ BOOST_CHECK( h3.is_ready() );
+ BOOST_CHECK( h4.is_ready() );
+ BOOST_CHECK_EQUAL( h3.get(), n);
+ BOOST_CHECK_EQUAL( h4.get(), n);
+ }
+
+ void test_case_3()
+ {
+ uint32_t n = 3;
+ tsk::spin_manual_reset_event ev( true);
+
+ tsk::handle< uint32_t > h1(
+ tsk::async(
+ tsk::make_task(
+ wait_fn,
+ n, boost::ref( ev) ),
+ tsk::new_thread() ) );
+ tsk::handle< uint32_t > h2(
+ tsk::async(
+ tsk::make_task(
+ wait_fn,
+ n, boost::ref( ev) ),
+ tsk::new_thread() ) );
+
+ boost::this_thread::sleep( pt::millisec( 250) );
+ BOOST_CHECK( h1.is_ready() );
+ BOOST_CHECK( h2.is_ready() );
+ BOOST_CHECK_EQUAL( h1.get(), n);
+ BOOST_CHECK_EQUAL( h2.get(), n);
+
+ ev.reset();
+
+ tsk::handle< uint32_t > h3(
+ tsk::async(
+ tsk::make_task(
+ wait_fn,
+ n, boost::ref( ev) ),
+ tsk::new_thread() ) );
+ tsk::handle< uint32_t > h4(
+ tsk::async(
+ tsk::make_task(
+ wait_fn,
+ n, boost::ref( ev) ),
+ tsk::new_thread() ) );
+
+ boost::this_thread::sleep( pt::millisec( 250) );
+ BOOST_CHECK( ! h3.is_ready() );
+ BOOST_CHECK( ! h4.is_ready() );
+
+ ev.set();
+
+ boost::this_thread::sleep( pt::millisec( 250) );
+ BOOST_CHECK( h3.is_ready() );
+ BOOST_CHECK( h4.is_ready() );
+ BOOST_CHECK_EQUAL( h3.get(), n);
+ BOOST_CHECK_EQUAL( h4.get(), n);
+ }
+};
+
+boost::unit_test::test_suite * init_unit_test_suite( int, char* [])
+{
+ boost::unit_test::test_suite * test( BOOST_TEST_SUITE("Boost.Task: test suite") );
+
+ boost::shared_ptr< test_spin_manual_reset_event > instance( new test_spin_manual_reset_event() );
+ test->add( BOOST_CLASS_TEST_CASE( & test_spin_manual_reset_event::test_case_1, instance) );
+ test->add( BOOST_CLASS_TEST_CASE( & test_spin_manual_reset_event::test_case_2, instance) );
+ test->add( BOOST_CLASS_TEST_CASE( & test_spin_manual_reset_event::test_case_3, instance) );
+
+ return test;
+}


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