|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r57643 - in sandbox/fiber/libs/fiber: examples src test
From: oliver.kowalke_at_[hidden]
Date: 2009-11-13 17:10:35
Author: olli
Date: 2009-11-13 17:10:35 EST (Fri, 13 Nov 2009)
New Revision: 57643
URL: http://svn.boost.org/trac/boost/changeset/57643
Log:
- unit test and example related to suspend()/resume() of fiber added
Added:
sandbox/fiber/libs/fiber/examples/suspend.cpp (contents, props changed)
sandbox/fiber/libs/fiber/test/test_suspended.cpp (contents, props changed)
Text files modified:
sandbox/fiber/libs/fiber/examples/Jamfile.v2 | 1 +
sandbox/fiber/libs/fiber/src/scheduler_impl.cpp | 1 +
sandbox/fiber/libs/fiber/test/Jamfile.v2 | 1 +
3 files changed, 3 insertions(+), 0 deletions(-)
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-13 17:10:35 EST (Fri, 13 Nov 2009)
@@ -23,6 +23,7 @@
<threading>multi
;
+exe suspend : suspend.cpp ;
exe cancel : cancel.cpp ;
exe simple : simple.cpp ;
exe simple_mt : simple_mt.cpp ;
Added: sandbox/fiber/libs/fiber/examples/suspend.cpp
==============================================================================
--- (empty file)
+++ sandbox/fiber/libs/fiber/examples/suspend.cpp 2009-11-13 17:10:35 EST (Fri, 13 Nov 2009)
@@ -0,0 +1,72 @@
+#include <cstdlib>
+#include <iostream>
+#include <string>
+
+#include <boost/bind.hpp>
+#include <boost/system/system_error.hpp>
+
+#include <boost/fiber.hpp>
+
+int value1 = 0;
+int value2 = 0;
+
+void fn_1()
+{
+ for ( int i = 0; i < 5; ++i)
+ {
+ ++value1;
+ std::cout << "fn_1() increment value1 " << value1 << std::endl;
+ if ( i == 1)
+ {
+ std::cout << "fiber suspends itself " << boost::this_fiber::get_id() << std::endl;
+ boost::this_fiber::suspend();
+ }
+ boost::this_fiber::yield();
+ }
+}
+
+void fn_2( boost::fiber f)
+{
+ for ( int i = 0; i < 5; ++i)
+ {
+ ++value2;
+ std::cout << "fn_2() increment value2 " << value2 << std::endl;
+ boost::this_fiber::yield();
+ }
+ std::cout << "fn_2() resume fiber " << f.get_id() << std::endl;
+ f.resume();
+}
+
+int main()
+{
+ try
+ {
+ boost::fibers::scheduler sched;
+
+ boost::fiber f( fn_1);
+ sched.submit_fiber( f);
+ sched.make_fiber( fn_2, f);
+
+ std::cout << "start" << std::endl;
+ std::cout << "fiber to be suspended " << f.get_id() << std::endl;
+
+ for (;;)
+ {
+ while ( sched.run() );
+ if ( sched.empty() ) break;
+ }
+
+ std::cout << "finish: value1 == " << value1 << ", value2 == " << value2 << std::endl;
+
+ return EXIT_SUCCESS;
+ }
+ catch ( boost::system::system_error const& e)
+ { std::cerr << "system_error: " << e.code().value() << std::endl; }
+ catch ( boost::fibers::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/scheduler_impl.cpp
==============================================================================
--- sandbox/fiber/libs/fiber/src/scheduler_impl.cpp (original)
+++ sandbox/fiber/libs/fiber/src/scheduler_impl.cpp 2009-11-13 17:10:35 EST (Fri, 13 Nov 2009)
@@ -184,6 +184,7 @@
{
case STATE_READY:
case STATE_RUNNING:
+ f.info_->state = STATE_READY;
runnable_fibers_.push_back( f.get_id() );
break;
case STATE_WAITING:
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-13 17:10:35 EST (Fri, 13 Nov 2009)
@@ -29,6 +29,7 @@
[ fiber-test test_scheduler ]
[ fiber-test test_utility ]
[ fiber-test test_cancel ]
+ [ fiber-test test_suspended ]
[ fiber-test test_mutex ]
[ fiber-test test_auto_reset_event ]
[ fiber-test test_manual_reset_event ]
Added: sandbox/fiber/libs/fiber/test/test_suspended.cpp
==============================================================================
--- (empty file)
+++ sandbox/fiber/libs/fiber/test/test_suspended.cpp 2009-11-13 17:10:35 EST (Fri, 13 Nov 2009)
@@ -0,0 +1,121 @@
+
+// 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 fn_1()
+{
+ for ( int i = 0; i < 3; ++i)
+ {
+ ++value1;
+ if ( i == 1) boost::this_fiber::suspend();
+ boost::this_fiber::yield();
+ }
+}
+
+void fn_2( boost::fiber f)
+{
+ for ( int i = 0; i < 3; ++i)
+ {
+ ++value2;
+ boost::this_fiber::yield();
+ }
+ f.resume();
+}
+
+void test_case_1()
+{
+ value1 = 0;
+ value2 = 0;
+
+ boost::fibers::scheduler sched;
+
+ boost::fiber f( fn_1);
+ sched.submit_fiber( f);
+ sched.make_fiber( fn_2, f);
+
+ BOOST_CHECK_EQUAL( 0, value1);
+ BOOST_CHECK_EQUAL( 0, value2);
+
+ BOOST_CHECK( sched.run() );
+ BOOST_CHECK( ! sched.empty() );
+ BOOST_CHECK_EQUAL( std::size_t( 2), 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( 2), sched.size() );
+ BOOST_CHECK_EQUAL( 1, value1);
+ BOOST_CHECK_EQUAL( 1, value2);
+
+ BOOST_CHECK( sched.run() );
+ BOOST_CHECK( ! sched.empty() );
+ BOOST_CHECK_EQUAL( std::size_t( 2), sched.size() );
+ BOOST_CHECK_EQUAL( 2, value1);
+ BOOST_CHECK_EQUAL( 1, value2);
+
+ BOOST_CHECK( sched.run() );
+ BOOST_CHECK( ! sched.empty() );
+ BOOST_CHECK_EQUAL( std::size_t( 2), sched.size() );
+ BOOST_CHECK_EQUAL( 2, value1);
+ BOOST_CHECK_EQUAL( 2, value2);
+
+ BOOST_CHECK( sched.run() );
+ BOOST_CHECK( ! sched.empty() );
+ BOOST_CHECK_EQUAL( std::size_t( 2), sched.size() );
+ BOOST_CHECK_EQUAL( 2, value1);
+ BOOST_CHECK_EQUAL( 3, value2);
+
+ BOOST_CHECK( sched.run() );
+ BOOST_CHECK( ! sched.empty() );
+ BOOST_CHECK_EQUAL( std::size_t( 1), sched.size() );
+ BOOST_CHECK_EQUAL( 2, value1);
+ BOOST_CHECK_EQUAL( 3, value2);
+
+ BOOST_CHECK( sched.run() );
+ BOOST_CHECK( ! sched.empty() );
+ BOOST_CHECK_EQUAL( std::size_t( 1), sched.size() );
+ BOOST_CHECK_EQUAL( 2, value1);
+ BOOST_CHECK_EQUAL( 3, value2);
+
+ BOOST_CHECK( sched.run() );
+ BOOST_CHECK( ! sched.empty() );
+ BOOST_CHECK_EQUAL( std::size_t( 1), sched.size() );
+ BOOST_CHECK_EQUAL( 3, value1);
+ BOOST_CHECK_EQUAL( 3, value2);
+
+ BOOST_CHECK( sched.run() );
+ BOOST_CHECK( sched.empty() );
+ BOOST_CHECK_EQUAL( std::size_t( 0), sched.size() );
+ BOOST_CHECK_EQUAL( 3, value1);
+ BOOST_CHECK_EQUAL( 3, value2);
+
+ BOOST_CHECK( ! sched.run() );
+ BOOST_CHECK( sched.empty() );
+ BOOST_CHECK_EQUAL( std::size_t( 0), sched.size() );
+ BOOST_CHECK_EQUAL( 3, value1);
+ BOOST_CHECK_EQUAL( 3, value2);
+}
+
+boost::unit_test::test_suite * init_unit_test_suite( int, char* [])
+{
+ boost::unit_test::test_suite * test =
+ BOOST_TEST_SUITE("Boost.Fiber: suspend test suite");
+
+ test->add( BOOST_TEST_CASE( & test_case_1) );
+
+ 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