Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r57483 - in sandbox/fiber: boost/fiber boost/fiber/detail libs/fiber/examples libs/fiber/src libs/fiber/test
From: oliver.kowalke_at_[hidden]
Date: 2009-11-08 08:15:50


Author: olli
Date: 2009-11-08 08:15:48 EST (Sun, 08 Nov 2009)
New Revision: 57483
URL: http://svn.boost.org/trac/boost/changeset/57483

Log:
- refactoring
- tests enhanced
- new example related to multithreaded env

Added:
   sandbox/fiber/libs/fiber/examples/simple_mt.cpp (contents, props changed)
   sandbox/fiber/libs/fiber/test/test_rrp.cpp (contents, props changed)
   sandbox/fiber/libs/fiber/test/test_utility.cpp (contents, props changed)
Text files modified:
   sandbox/fiber/boost/fiber/detail/fiber_info_posix.hpp | 8 +++-
   sandbox/fiber/boost/fiber/exceptions.hpp | 20 +++++++++--
   sandbox/fiber/boost/fiber/fiber.hpp | 65 ++++++++++-----------------------------
   sandbox/fiber/boost/fiber/policy.hpp | 3 -
   sandbox/fiber/boost/fiber/scheduler.hpp | 51 ++++++++++++++++++++++++-------
   sandbox/fiber/libs/fiber/examples/Jamfile.v2 | 1
   sandbox/fiber/libs/fiber/examples/simple.cpp | 4 +-
   sandbox/fiber/libs/fiber/src/attributes.cpp | 4 +-
   sandbox/fiber/libs/fiber/src/fiber.cpp | 19 +++--------
   sandbox/fiber/libs/fiber/src/fiber_posix.cpp | 8 +---
   sandbox/fiber/libs/fiber/src/rrp.cpp | 7 ++-
   sandbox/fiber/libs/fiber/src/scheduler.cpp | 31 ++++++++----------
   sandbox/fiber/libs/fiber/test/Jamfile.v2 | 2 +
   13 files changed, 112 insertions(+), 111 deletions(-)

Modified: sandbox/fiber/boost/fiber/detail/fiber_info_posix.hpp
==============================================================================
--- sandbox/fiber/boost/fiber/detail/fiber_info_posix.hpp (original)
+++ sandbox/fiber/boost/fiber/detail/fiber_info_posix.hpp 2009-11-08 08:15:48 EST (Sun, 08 Nov 2009)
@@ -36,9 +36,11 @@
         ::ucontext_t uctx;
         shared_array< char > uctx_stack;
 
+ static void convert_thread_to_fiber() {}
+
         fiber_info_base();
 
- fiber_info_base( attributes const& attribs_);
+ fiber_info_base( attributes const&);
 
         virtual ~fiber_info_base() {}
 
@@ -55,10 +57,10 @@
 #else
 
     void add_ref()
- { ++use_count_; }
+ { ++use_count; }
 
     void release()
- { if ( --use_count_ == 0) delete this; }
+ { if ( --use_count == 0) delete this; }
 
 #endif
 };

Modified: sandbox/fiber/boost/fiber/exceptions.hpp
==============================================================================
--- sandbox/fiber/boost/fiber/exceptions.hpp (original)
+++ sandbox/fiber/boost/fiber/exceptions.hpp 2009-11-08 08:15:48 EST (Sun, 08 Nov 2009)
@@ -10,25 +10,37 @@
 #include <stdexcept>
 #include <string>
 
+#include <boost/config/abi_prefix.hpp>
+
 namespace boost {
 namespace fiber {
 
 class fiber_error : public std::runtime_error
 {
 public:
- fiber_error( std::string const& msg)
- : std::runtime_error( msg)
+ fiber_error( std::string const& msg) :
+ std::runtime_error( msg)
+ {}
+};
+
+class invalid_stacksize : public std::runtime_error
+{
+public:
+ invalid_stacksize() :
+ std::runtime_error("invalid stacksize")
         {}
 };
 
 class scheduler_error : public std::runtime_error
 {
 public:
- scheduler_error( std::string const& msg)
- : std::runtime_error( msg)
+ scheduler_error( std::string const& msg) :
+ std::runtime_error( msg)
         {}
 };
 
 }}
 
+#include <boost/config/abi_suffix.hpp>
+
 #endif // BOOST_FIBER_EXCEPTIONS_H

Modified: sandbox/fiber/boost/fiber/fiber.hpp
==============================================================================
--- sandbox/fiber/boost/fiber/fiber.hpp (original)
+++ sandbox/fiber/boost/fiber/fiber.hpp 2009-11-08 08:15:48 EST (Sun, 08 Nov 2009)
@@ -30,16 +30,12 @@
 private:
         friend void trampoline( fiber *);
 
- detail::fiber_info_base::ptr_t info;
+ detail::fiber_info_base::ptr_t info_;
 
         fiber( fiber &);
         fiber & operator=( fiber const&);
 
- void init();
-
- template< typename Fn >
- static inline detail::fiber_info_base::ptr_t make_info( Fn fn, attributes const& attribs)
- { return detail::fiber_info_base::ptr_t( new detail::fiber_info< Fn >( fn, attribs) ); }
+ void init_();
 
         struct dummy;
 
@@ -51,36 +47,9 @@
         fiber();
 
         template< typename Fn >
- explicit fiber( Fn fn, attributes const& attribs) :
- info( make_info( fn, attribs) )
- { init(); }
-
-#ifndef BOOST_FIBER_MAX_ARITY
-#define BOOST_FIBER_MAX_ARITY 10
-#endif
-
-#define BOOST_FIBER_ARG(z, n, unused) \
- BOOST_PP_CAT(A, n) BOOST_PP_CAT(a, n)
-#define BOOST_ENUM_FIBER_ARGS(n) BOOST_PP_ENUM(n, BOOST_FIBER_ARG, ~)
-
-#define BOOST_FIBER_CTOR(z, n, unused) \
- template< \
- typename Fn, \
- BOOST_PP_ENUM_PARAMS(n, typename A) \
- > \
- fiber( Fn fn, attributes const& attribs, BOOST_ENUM_FIBER_ARGS(n)) : \
- info( \
- make_info( \
- boost::bind( boost::type< void >(), fn, BOOST_PP_ENUM_PARAMS(n, a) ), \
- attribs) ) \
- { init(); }
-
-BOOST_PP_REPEAT_FROM_TO( 1, BOOST_FIBER_MAX_ARITY, BOOST_FIBER_CTOR, ~)
-
-#undef BOOST_FIBER_CTOR
-#undef BOOST_FIBER_MAX_ARITY
-
- ~fiber();
+ explicit fiber( Fn fn, attributes const& attrs) :
+ info_( new detail::fiber_info< Fn >( fn, attrs) )
+ { init_(); }
 
         void swap( fiber & other);
 
@@ -97,41 +66,41 @@
 private:
         friend class fiber;
 
- detail::fiber_info_base::ptr_t info;
+ detail::fiber_info_base::ptr_t info_;
 
- explicit id( detail::fiber_info_base::ptr_t info_) :
- info( info_)
+ explicit id( detail::fiber_info_base::ptr_t info) :
+ info_( info)
         {}
 
 public:
         id() :
- info()
+ info_()
         {}
 
         bool operator==( id const& other) const
- { return info == other.info; }
+ { return info_ == other.info_; }
 
         bool operator!=( id const& other) const
- { return info != other.info; }
+ { return info_ != other.info_; }
         
         bool operator<( id const& other) const
- { return info < other.info; }
+ { return info_ < other.info_; }
         
         bool operator>( id const& other) const
- { return other.info < info; }
+ { return other.info_ < info_; }
         
         bool operator<=( id const& other) const
- { return !( other.info < info); }
+ { return !( other.info_ < info_); }
         
         bool operator>=( id const& other) const
- { return ! ( info < other.info); }
+ { return ! ( info_ < other.info_); }
 
         template< typename charT, class traitsT >
         friend std::basic_ostream< charT, traitsT > &
         operator<<( std::basic_ostream< charT, traitsT > & os, id const& other)
         {
- if ( other.info)
- return os << other.info;
+ if ( other.info_)
+ return os << other.info_;
                 else
                         return os << "{not-a-fiber}";
         }

Modified: sandbox/fiber/boost/fiber/policy.hpp
==============================================================================
--- sandbox/fiber/boost/fiber/policy.hpp (original)
+++ sandbox/fiber/boost/fiber/policy.hpp 2009-11-08 08:15:48 EST (Sun, 08 Nov 2009)
@@ -10,7 +10,6 @@
 #include <cstddef>
 #include <memory>
 
-#include <boost/fiber/detail/config.hpp>
 #include <boost/fiber/fiber.hpp>
 
 #include <boost/config/abi_prefix.hpp>
@@ -18,7 +17,7 @@
 namespace boost {
 namespace fiber {
 
-struct BOOST_FIBER_DECL policy
+struct policy
 {
         virtual ~policy() {}
 

Modified: sandbox/fiber/boost/fiber/scheduler.hpp
==============================================================================
--- sandbox/fiber/boost/fiber/scheduler.hpp (original)
+++ sandbox/fiber/boost/fiber/scheduler.hpp 2009-11-08 08:15:48 EST (Sun, 08 Nov 2009)
@@ -42,9 +42,9 @@
         friend fiber::id this_fiber::get_id();
         friend void this_fiber::yield();
 
- typedef thread_specific_ptr< policy > policy_ptr_t;
+ typedef thread_specific_ptr< policy > tss_policy_t;
 
- static policy_ptr_t policy_;
+ static tss_policy_t impl_;
 
         static bool runs_as_fiber();
 
@@ -57,10 +57,17 @@
         policy * access_();
 
 public:
- scheduler();
+ template< typename Fn >
+ void make_fiber( Fn fn)
+ {
+ attributes attrs;
+ access_()->add_fiber(
+ std::auto_ptr< fiber >(
+ new fiber( fn, attrs) ) );
+ }
 
         template< typename Fn >
- void make_fiber( Fn fn, attributes attrs = attributes() )
+ void make_fiber( attributes attrs, Fn fn)
         {
                 access_()->add_fiber(
                         std::auto_ptr< fiber >(
@@ -80,14 +87,34 @@
                 typename Fn, \
                 BOOST_PP_ENUM_PARAMS(n, typename A) \
> \
- void make_fiber( Fn fn, attributes const& attribs, BOOST_ENUM_FIBER_ARGS(n)) \
- { \
- access_()->add_fiber( \
- std::auto_ptr< fiber >( \
- new fiber( \
- boost::bind( \
- boost::type< void >(), fn, BOOST_PP_ENUM_PARAMS(n, a) ), \
- attribs) ) ); \
+ void make_fiber( Fn fn, BOOST_ENUM_FIBER_ARGS(n)) \
+ { \
+ attributes attrs; \
+ access_()->add_fiber( \
+ std::auto_ptr< fiber >( \
+ new fiber( \
+ boost::bind( \
+ boost::type< void >(), fn, BOOST_PP_ENUM_PARAMS(n, a) ),\
+ attrs) ) ); \
+ }
+
+BOOST_PP_REPEAT_FROM_TO( 1, BOOST_FIBER_MAX_ARITY, BOOST_FIBER_MAKE_FIBER_FUNCTION, ~)
+
+#undef BOOST_FIBER_MAKE_FIBER_FUNCTION
+
+#define BOOST_FIBER_MAKE_FIBER_FUNCTION(z, n, unused) \
+ template< \
+ typename Fn, \
+ BOOST_PP_ENUM_PARAMS(n, typename A) \
+ > \
+ void make_fiber( attributes const& attrs, Fn fn, BOOST_ENUM_FIBER_ARGS(n)) \
+ { \
+ access_()->add_fiber( \
+ std::auto_ptr< fiber >( \
+ new fiber( \
+ boost::bind( \
+ boost::type< void >(), fn, BOOST_PP_ENUM_PARAMS(n, a) ),\
+ attrs) ) ); \
         }
 
 BOOST_PP_REPEAT_FROM_TO( 1, BOOST_FIBER_MAX_ARITY, BOOST_FIBER_MAKE_FIBER_FUNCTION, ~)

Modified: sandbox/fiber/libs/fiber/examples/Jamfile.v2
==============================================================================
--- sandbox/fiber/libs/fiber/examples/Jamfile.v2 (original)
+++ sandbox/fiber/libs/fiber/examples/Jamfile.v2 2009-11-08 08:15:48 EST (Sun, 08 Nov 2009)
@@ -24,3 +24,4 @@
     ;
 
 exe simple : simple.cpp ;
+exe simple_mt : simple_mt.cpp ;

Modified: sandbox/fiber/libs/fiber/examples/simple.cpp
==============================================================================
--- sandbox/fiber/libs/fiber/examples/simple.cpp (original)
+++ sandbox/fiber/libs/fiber/examples/simple.cpp 2009-11-08 08:15:48 EST (Sun, 08 Nov 2009)
@@ -23,8 +23,8 @@
         {
                 boost::fiber::scheduler sched;
 
- sched.make_fiber( & f, boost::fiber::attributes(), "abc", 5);
- sched.make_fiber( & f, boost::fiber::attributes(), "xyz", 7);
+ sched.make_fiber( & f, "abc", 5);
+ sched.make_fiber( & f, "xyz", 7);
 
                 std::cout << "start" << std::endl;
 

Added: sandbox/fiber/libs/fiber/examples/simple_mt.cpp
==============================================================================
--- (empty file)
+++ sandbox/fiber/libs/fiber/examples/simple_mt.cpp 2009-11-08 08:15:48 EST (Sun, 08 Nov 2009)
@@ -0,0 +1,80 @@
+#include <cstdlib>
+#include <iostream>
+#include <sstream>
+#include <string>
+
+#include <boost/bind.hpp>
+#include <boost/ref.hpp>
+#include <boost/system/system_error.hpp>
+#include <boost/thread.hpp>
+
+#include <boost/fiber.hpp>
+
+void f( std::string const& str, int n)
+{
+ for ( int i = 0; i < n; ++i)
+ {
+ std::ostringstream os1;
+ std::ostringstream os2;
+ os1 << boost::this_thread::get_id();
+ os2 << boost::this_fiber::get_id();
+ fprintf( stderr, "(thread: %s, fiber: %s) %d: %s\n", os1.str().c_str(), os2.str().c_str(), i, str.c_str() );
+ boost::this_fiber::yield();
+ }
+}
+
+void run_thread(
+ boost::barrier & b,
+ boost::fiber::scheduler & sched,
+ std::string const& msg, int n)
+{
+ std::ostringstream os;
+ os << boost::this_thread::get_id();
+ fprintf( stderr, "start (thread: %s)\n", os.str().c_str() );
+ sched.make_fiber( & f, msg, n);
+
+ b.wait();
+ for (;;)
+ {
+ while ( sched.run() );
+ if ( sched.empty() ) break;
+ }
+
+ fprintf( stderr, "finish (thread: %s)\n", os.str().c_str() );
+}
+
+int main()
+{
+ try
+ {
+ boost::fiber::scheduler sched;
+
+ std::cout << "start" << std::endl;
+
+ boost::barrier b( 2);
+ boost::thread th1(
+ run_thread,
+ boost::ref( b),
+ boost::ref( sched), "abc", 3);
+ boost::thread th2(
+ run_thread,
+ boost::ref( b),
+ boost::ref( sched), "xyz", 4);
+
+ th1.join();
+ th2.join();
+
+ std::cout << "finish" << std::endl;
+
+ return EXIT_SUCCESS;
+ }
+ catch ( boost::system::system_error const& e)
+ { std::cerr << "system_error: " << e.code().value() << std::endl; }
+ catch ( boost::fiber::scheduler_error const& e)
+ { std::cerr << "scheduler_error: " << e.what() << std::endl; }
+ catch ( std::exception const& e)
+ { std::cerr << "exception: " << e.what() << std::endl; }
+ catch (...)
+ { std::cerr << "unhandled exception" << std::endl; }
+ return EXIT_FAILURE;
+}

Modified: sandbox/fiber/libs/fiber/src/attributes.cpp
==============================================================================
--- sandbox/fiber/libs/fiber/src/attributes.cpp (original)
+++ sandbox/fiber/libs/fiber/src/attributes.cpp 2009-11-08 08:15:48 EST (Sun, 08 Nov 2009)
@@ -6,7 +6,7 @@
 
 #include <boost/fiber/attributes.hpp>
 
-#include <stdexcept>
+#include <boost/fiber/exceptions.hpp>
 
 #include <boost/config/abi_prefix.hpp>
 
@@ -22,7 +22,7 @@
 attributes::stack_size( std::size_t value)
 {
         if ( value < 1)
- throw std::invalid_argument("invalid stacksize");
+ throw invalid_stacksize();
         stacksize_ = value;
 }
 

Modified: sandbox/fiber/libs/fiber/src/fiber.cpp
==============================================================================
--- sandbox/fiber/libs/fiber/src/fiber.cpp (original)
+++ sandbox/fiber/libs/fiber/src/fiber.cpp 2009-11-08 08:15:48 EST (Sun, 08 Nov 2009)
@@ -6,8 +6,6 @@
 
 #include <boost/fiber/fiber.hpp>
 
-#include <algorithm>
-
 #include <boost/assert.hpp>
 
 #include <boost/fiber/scheduler.hpp>
@@ -19,22 +17,19 @@
 
 void
 fiber::convert_thread_to_fiber()
-{}
+{ detail::fiber_info_base::convert_thread_to_fiber(); }
 
 fiber::fiber() :
- info( new detail::fiber_info_default() )
-{}
-
-fiber::~fiber()
+ info_( new detail::fiber_info_default() )
 {}
 
 void
 fiber::swap( fiber & other)
-{ info.swap( other.info); }
+{ info_.swap( other.info_); }
 
 fiber::id
 fiber::get_id() const
-{ return fiber::id( info); }
+{ return fiber::id( info_); }
 
 bool
 fiber::operator==( fiber const& other) const
@@ -47,13 +42,11 @@
 void trampoline( fiber * self)
 {
         BOOST_ASSERT( self);
- BOOST_ASSERT( self->info);
- self->info->run();
+ BOOST_ASSERT( self->info_);
+ self->info_->run();
          scheduler::exit();
 }
 
 }}
 
 #include <boost/config/abi_suffix.hpp>
-
-

Modified: sandbox/fiber/libs/fiber/src/fiber_posix.cpp
==============================================================================
--- sandbox/fiber/libs/fiber/src/fiber_posix.cpp (original)
+++ sandbox/fiber/libs/fiber/src/fiber_posix.cpp 2009-11-08 08:15:48 EST (Sun, 08 Nov 2009)
@@ -22,14 +22,14 @@
 namespace fiber {
 
 void
-fiber::init()
+fiber::init_()
 {
         typedef void fn_type( fiber *);
         typedef void ( * st_fn)();
         fn_type * fn_ptr( trampoline);
 
         ::makecontext(
- & info->uctx,
+ & info_->uctx,
                 ( st_fn)( fn_ptr),
                 1,
                 this);
@@ -38,7 +38,7 @@
 void
 fiber::switch_to( fiber & to)
 {
- if ( ::swapcontext( & info->uctx, & to.info->uctx) != 0)
+ if ( ::swapcontext( & info_->uctx, & to.info_->uctx) != 0)
                 throw system::system_error(
                         system::error_code(
                                 errno,
@@ -48,5 +48,3 @@
 }}
 
 #include <boost/config/abi_suffix.hpp>
-
-

Modified: sandbox/fiber/libs/fiber/src/rrp.cpp
==============================================================================
--- sandbox/fiber/libs/fiber/src/rrp.cpp (original)
+++ sandbox/fiber/libs/fiber/src/rrp.cpp 2009-11-08 08:15:48 EST (Sun, 08 Nov 2009)
@@ -54,20 +54,21 @@
 bool
 rrp::run()
 {
+ bool result( false);
         if ( ! runnable_fibers_.empty() )
         {
                 f_id_ = runnable_fibers_.front();
                 master_.switch_to( fibers_[f_id_]);
                 runnable_fibers_.pop();
- return true;
+ result = true;
         }
         if ( ! dead_fibers_.empty() )
         {
                 fibers_.erase( dead_fibers_.front() );
                 dead_fibers_.pop();
- return true;
+ result = true;
         }
- return false;
+ return result;
 }
 
 bool

Modified: sandbox/fiber/libs/fiber/src/scheduler.cpp
==============================================================================
--- sandbox/fiber/libs/fiber/src/scheduler.cpp (original)
+++ sandbox/fiber/libs/fiber/src/scheduler.cpp 2009-11-08 08:15:48 EST (Sun, 08 Nov 2009)
@@ -14,48 +14,45 @@
 namespace boost {
 namespace fiber {
 
-scheduler::policy_ptr_t scheduler::policy_;
+scheduler::tss_policy_t scheduler::impl_;
 
 bool
 scheduler::runs_as_fiber()
-{ return policy_.get() != 0; }
+{ return impl_.get() != 0; }
 
 fiber::id
 scheduler::get_id()
 {
- policy * po( policy_.get() );
- if ( ! po) throw fiber_error("not a fiber");
- return po->active_fiber();
+ policy * impl( impl_.get() );
+ if ( ! impl) throw fiber_error("not a fiber");
+ return impl->active_fiber();
 }
 
 void
 scheduler::yield()
 {
- policy * po( policy_.get() );
- if ( ! po) throw fiber_error("not a fiber");
- po->yield_active_fiber();
+ policy * impl( impl_.get() );
+ if ( ! impl) throw fiber_error("not a fiber");
+ impl->yield_active_fiber();
 }
 
 void
 scheduler::exit()
 {
- policy * po( policy_.get() );
- if ( ! po) throw fiber_error("not a fiber");
- po->exit_active_fiber();
+ policy * impl( impl_.get() );
+ if ( ! impl) throw fiber_error("not a fiber");
+ impl->exit_active_fiber();
 }
 
-scheduler::scheduler()
-{}
-
 policy *
 scheduler::access_()
 {
- if ( ! policy_.get() )
+ if ( ! impl_.get() )
         {
                 fiber::convert_thread_to_fiber();
- policy_.reset( new rrp() );
+ impl_.reset( new rrp() );
         }
- return policy_.get();
+ return impl_.get();
 }
 
 bool

Modified: sandbox/fiber/libs/fiber/test/Jamfile.v2
==============================================================================
--- sandbox/fiber/libs/fiber/test/Jamfile.v2 (original)
+++ sandbox/fiber/libs/fiber/test/Jamfile.v2 2009-11-08 08:15:48 EST (Sun, 08 Nov 2009)
@@ -26,4 +26,6 @@
 
 test-suite fiber :
     [ fiber-test test_fiber ]
+ [ fiber-test test_rrp ]
+ [ fiber-test test_utility ]
     ;

Added: sandbox/fiber/libs/fiber/test/test_rrp.cpp
==============================================================================
--- (empty file)
+++ sandbox/fiber/libs/fiber/test/test_rrp.cpp 2009-11-08 08:15:48 EST (Sun, 08 Nov 2009)
@@ -0,0 +1,84 @@
+
+// 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 <sstream>
+#include <string>
+
+#include <boost/test/unit_test.hpp>
+#include <boost/utility.hpp>
+
+#include <boost/fiber.hpp>
+
+int value1 = 0;
+int value2 = 0;
+
+void zero_args_fn() {}
+
+void value1_fn()
+{ value1 = 1; }
+
+void value2_fn()
+{ value2 = 1; }
+
+// check empty() + size()
+void test_case_1()
+{
+ boost::fiber::scheduler sched;
+ BOOST_CHECK( sched.empty() );
+ BOOST_CHECK_EQUAL( std::size_t( 0), sched.size() );
+ BOOST_CHECK( ! sched.run() );
+
+ sched.make_fiber( zero_args_fn);
+ BOOST_CHECK( ! sched.empty() );
+ BOOST_CHECK_EQUAL( std::size_t( 1), sched.size() );
+
+ sched.make_fiber( zero_args_fn);
+ BOOST_CHECK( ! sched.empty() );
+ BOOST_CHECK_EQUAL( std::size_t( 2), sched.size() );
+
+ BOOST_CHECK( sched.run() );
+ BOOST_CHECK( ! sched.empty() );
+ BOOST_CHECK_EQUAL( std::size_t( 1), sched.size() );
+
+ BOOST_CHECK( sched.run() );
+ BOOST_CHECK( sched.empty() );
+ BOOST_CHECK_EQUAL( std::size_t( 0), sched.size() );
+}
+
+void test_case_2()
+{
+ boost::fiber::scheduler sched;
+
+ sched.make_fiber( value1_fn);
+ sched.make_fiber( value2_fn);
+ BOOST_CHECK( ! sched.empty() );
+ BOOST_CHECK_EQUAL( std::size_t( 2), sched.size() );
+ BOOST_CHECK_EQUAL( 0, value1);
+ BOOST_CHECK_EQUAL( 0, value2);
+
+ BOOST_CHECK( sched.run() );
+ BOOST_CHECK( ! sched.empty() );
+ BOOST_CHECK_EQUAL( std::size_t( 1), sched.size() );
+ BOOST_CHECK_EQUAL( 1, value1);
+ BOOST_CHECK_EQUAL( 0, value2);
+
+ BOOST_CHECK( sched.run() );
+ BOOST_CHECK( sched.empty() );
+ BOOST_CHECK_EQUAL( std::size_t( 0), sched.size() );
+ BOOST_CHECK_EQUAL( 1, value1);
+ BOOST_CHECK_EQUAL( 1, value2);
+}
+
+boost::unit_test::test_suite * init_unit_test_suite( int, char* [])
+{
+ boost::unit_test::test_suite * test =
+ BOOST_TEST_SUITE("Boost.Fiber: rrp test suite");
+
+ test->add( BOOST_TEST_CASE( & test_case_1) );
+ test->add( BOOST_TEST_CASE( & test_case_2) );
+
+ return test;
+}

Added: sandbox/fiber/libs/fiber/test/test_utility.cpp
==============================================================================
--- (empty file)
+++ sandbox/fiber/libs/fiber/test/test_utility.cpp 2009-11-08 08:15:48 EST (Sun, 08 Nov 2009)
@@ -0,0 +1,91 @@
+
+// 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 <sstream>
+#include <string>
+
+#include <boost/test/unit_test.hpp>
+#include <boost/utility.hpp>
+
+#include <boost/fiber.hpp>
+
+bool runs_as_fiber = false;
+std::string id;
+int value = 0;
+
+void runs_as_fn()
+{ runs_as_fiber = boost::this_fiber::runs_as_fiber(); }
+
+void get_id_fn()
+{
+ std::ostringstream os;
+ os << boost::this_fiber::get_id();
+ id = os.str();
+}
+
+void yield_fn( int n)
+{
+ for ( int i = 0; i < n; ++i)
+ {
+ ++value;
+ boost::this_fiber::yield();
+ }
+}
+
+void test_case_1()
+{
+ boost::fiber::scheduler sched;
+ sched.make_fiber( runs_as_fn);
+
+ BOOST_CHECK( sched.run() );
+
+ BOOST_CHECK( runs_as_fiber);
+}
+
+void test_case_2()
+{
+ boost::fiber::scheduler sched;
+ sched.make_fiber( get_id_fn);
+
+ BOOST_CHECK( sched.run() );
+
+ BOOST_CHECK( ! id.empty() );
+ BOOST_CHECK( std::string("{not-a-fiber}") != id);
+}
+
+void test_case_3()
+{
+ boost::fiber::scheduler sched;
+ sched.make_fiber( yield_fn, 3);
+ BOOST_CHECK_EQUAL( 0, value);
+
+ BOOST_CHECK( sched.run() );
+ BOOST_CHECK_EQUAL( 1, value);
+
+ BOOST_CHECK( sched.run() );
+ BOOST_CHECK_EQUAL( 2, value);
+
+ BOOST_CHECK( sched.run() );
+ BOOST_CHECK_EQUAL( 3, value);
+
+ BOOST_CHECK( sched.run() );
+ BOOST_CHECK_EQUAL( 3, value);
+
+ BOOST_CHECK( ! sched.run() );
+ BOOST_CHECK_EQUAL( 3, value);
+}
+
+boost::unit_test::test_suite * init_unit_test_suite( int, char* [])
+{
+ boost::unit_test::test_suite * test =
+ BOOST_TEST_SUITE("Boost.Fiber: utility test suite");
+
+ test->add( BOOST_TEST_CASE( & test_case_1) );
+ test->add( BOOST_TEST_CASE( & test_case_2) );
+ test->add( BOOST_TEST_CASE( & test_case_3) );
+
+ 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