|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r57614 - in sandbox/fiber: boost/fiber boost/fiber/detail libs/fiber/src
From: oliver.kowalke_at_[hidden]
Date: 2009-11-12 15:30:09
Author: olli
Date: 2009-11-12 15:30:08 EST (Thu, 12 Nov 2009)
New Revision: 57614
URL: http://svn.boost.org/trac/boost/changeset/57614
Log:
- cancel_fiber() added
Text files modified:
sandbox/fiber/boost/fiber/detail/fiber_state.hpp | 2
sandbox/fiber/boost/fiber/detail/scheduler_impl.hpp | 17 ++++++---
sandbox/fiber/boost/fiber/fiber.hpp | 4 +
sandbox/fiber/boost/fiber/scheduler.hpp | 7 ++-
sandbox/fiber/boost/fiber/utility.hpp | 4 +-
sandbox/fiber/libs/fiber/src/fiber.cpp | 5 ++
sandbox/fiber/libs/fiber/src/fiber_posix.cpp | 2
sandbox/fiber/libs/fiber/src/scheduler.cpp | 8 ++--
sandbox/fiber/libs/fiber/src/scheduler_impl.cpp | 72 +++++++++++++++++++++++++++++----------
9 files changed, 84 insertions(+), 37 deletions(-)
Modified: sandbox/fiber/boost/fiber/detail/fiber_state.hpp
==============================================================================
--- sandbox/fiber/boost/fiber/detail/fiber_state.hpp (original)
+++ sandbox/fiber/boost/fiber/detail/fiber_state.hpp 2009-11-12 15:30:08 EST (Thu, 12 Nov 2009)
@@ -18,7 +18,7 @@
STATE_MASTER = 0X00,
STATE_NOT_STARTED = 0X10,
STATE_RUNNABLE = 0x20,
- STATE_READY = 0x21,
+ STATE_READY = 0x21,
STATE_RUNNING = 0x22,
STATE_NOT_RUNNABLE = 0x40,
STATE_SUSPENDED = 0x41,
Modified: sandbox/fiber/boost/fiber/detail/scheduler_impl.hpp
==============================================================================
--- sandbox/fiber/boost/fiber/detail/scheduler_impl.hpp (original)
+++ sandbox/fiber/boost/fiber/detail/scheduler_impl.hpp 2009-11-12 15:30:08 EST (Thu, 12 Nov 2009)
@@ -8,8 +8,8 @@
#define BOOST_FIBERS_DETAIL_SCHEDULER_IMPL_H
#include <cstddef>
+#include <list>
#include <map>
-#include <queue>
#include <boost/utility.hpp>
@@ -25,11 +25,14 @@
class BOOST_FIBER_DECL scheduler_impl : private noncopyable
{
private:
- fiber master_;
- fiber::id f_id_;
- std::map< fiber::id, fiber > fibers_;
- std::queue< fiber::id > runnable_fibers_;
- std::queue< fiber::id > terminated_fibers_;
+ typedef std::map< fiber::id, fiber > container;
+ typedef std::list< fiber::id > queue;
+
+ fiber master_;
+ fiber::id f_id_;
+ container fibers_;
+ queue runnable_fibers_;
+ queue terminated_fibers_;
public:
scheduler_impl();
@@ -42,6 +45,8 @@
void terminate_active_fiber();
+ void cancel_fiber( fiber::id const&);
+
bool run();
bool empty();
Modified: sandbox/fiber/boost/fiber/fiber.hpp
==============================================================================
--- sandbox/fiber/boost/fiber/fiber.hpp (original)
+++ sandbox/fiber/boost/fiber/fiber.hpp 2009-11-12 15:30:08 EST (Thu, 12 Nov 2009)
@@ -44,6 +44,8 @@
void init_();
+ void switch_to_( fiber &);
+
#ifdef BOOST_HAS_RVALUE_REFS
template< typename Fn >
static detail::fiber_info_base::ptr_t make_info_(
@@ -186,7 +188,7 @@
bool is_alive() const;
- void switch_to( fiber &);
+ void cancel();
};
class fiber::id
Modified: sandbox/fiber/boost/fiber/scheduler.hpp
==============================================================================
--- sandbox/fiber/boost/fiber/scheduler.hpp (original)
+++ sandbox/fiber/boost/fiber/scheduler.hpp 2009-11-12 15:30:08 EST (Thu, 12 Nov 2009)
@@ -41,6 +41,7 @@
friend fiber::id this_fiber::get_id();
friend void this_fiber::yield();
friend void this_fiber::cancel();
+ friend class fiber;
typedef thread_specific_ptr< detail::scheduler_impl > tss_impl_t;
@@ -50,11 +51,11 @@
static fiber::id get_id();
- static void fiber_yield();
+ static void yield_active_fiber();
- static void fiber_terminate();
+ static void terminate_active_fiber();
- static void fiber_failed();
+ static void cancel_fiber( fiber::id const&);
detail::scheduler_impl * access_();
Modified: sandbox/fiber/boost/fiber/utility.hpp
==============================================================================
--- sandbox/fiber/boost/fiber/utility.hpp (original)
+++ sandbox/fiber/boost/fiber/utility.hpp 2009-11-12 15:30:08 EST (Thu, 12 Nov 2009)
@@ -29,11 +29,11 @@
inline
void yield()
-{ fibers::scheduler::fiber_yield(); }
+{ fibers::scheduler::yield_active_fiber(); }
inline
void cancel()
-{ fibers::scheduler::fiber_terminate(); }
+{ fibers::scheduler::terminate_active_fiber(); }
}}
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-12 15:30:08 EST (Thu, 12 Nov 2009)
@@ -10,6 +10,7 @@
#include <boost/fiber/detail/fiber_state.hpp>
#include <boost/fiber/exceptions.hpp>
+#include <boost/fiber/scheduler.hpp>
#include <boost/config/abi_prefix.hpp>
@@ -109,6 +110,10 @@
( info_->state & detail::STATE_NOT_RUNNABLE) == detail::STATE_NOT_RUNNABLE;
}
+void
+fiber::cancel()
+{ scheduler::cancel_fiber( get_id() ); }
+
}}
#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-12 15:30:08 EST (Thu, 12 Nov 2009)
@@ -53,7 +53,7 @@
}
void
-fiber::switch_to( fiber & to)
+fiber::switch_to_( fiber & to)
{
if ( ! info_) throw fiber_moved();
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-12 15:30:08 EST (Thu, 12 Nov 2009)
@@ -30,7 +30,7 @@
}
void
-scheduler::fiber_yield()
+scheduler::yield_active_fiber()
{
detail::scheduler_impl * impl( impl_.get() );
if ( ! impl) throw fiber_error("not a fiber");
@@ -38,7 +38,7 @@
}
void
-scheduler::fiber_terminate()
+scheduler::terminate_active_fiber()
{
detail::scheduler_impl * impl( impl_.get() );
if ( ! impl) throw fiber_error("not a fiber");
@@ -46,11 +46,11 @@
}
void
-scheduler::fiber_failed()
+scheduler::cancel_fiber( fiber::id const& f_id)
{
detail::scheduler_impl * impl( impl_.get() );
if ( ! impl) throw fiber_error("not a fiber");
- impl->terminate_active_fiber();
+ impl->cancel_fiber( f_id);
}
detail::scheduler_impl *
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-12 15:30:08 EST (Thu, 12 Nov 2009)
@@ -23,8 +23,8 @@
scheduler_impl::scheduler_impl() :
master_(
- detail::fiber_info_base::ptr_t(
- new detail::fiber_info_default() ) ),
+ fiber_info_base::ptr_t(
+ new fiber_info_default() ) ),
f_id_(),
fibers_(),
runnable_fibers_(),
@@ -36,12 +36,12 @@
{
if ( ! f) throw fiber_moved();
fiber::id id( f.get_id() );
- BOOST_ASSERT( f.info_->state == detail::STATE_NOT_STARTED);
- f.info_->state = detail::STATE_READY;
+ BOOST_ASSERT( STATE_NOT_STARTED == f.info_->state);
+ f.info_->state = STATE_READY;
std::pair< std::map< fiber::id, fiber >::iterator, bool > result(
fibers_.insert( std::make_pair( id, f) ) );
if ( ! result.second) throw scheduler_error("inserting fiber failed");
- runnable_fibers_.push( result.first->first);
+ runnable_fibers_.push_back( result.first->first);
}
fiber::id
@@ -51,19 +51,53 @@
void
scheduler_impl::yield_active_fiber()
{
- BOOST_ASSERT( fibers_[f_id_].info_->state == detail::STATE_RUNNING);
- fibers_[f_id_].info_->state = detail::STATE_READY;
- runnable_fibers_.push( f_id_);
- fibers_[f_id_].switch_to( master_);
+ BOOST_ASSERT( STATE_RUNNING == fibers_[f_id_].info_->state);
+ fibers_[f_id_].info_->state = STATE_READY;
+ runnable_fibers_.push_back( f_id_);
+ fibers_[f_id_].switch_to_( master_);
}
void
scheduler_impl::terminate_active_fiber()
{
- BOOST_ASSERT( fibers_[f_id_].info_->state == detail::STATE_RUNNING);
- fibers_[f_id_].info_->state = detail::STATE_TERMINATED;
- terminated_fibers_.push( f_id_);
- fibers_[f_id_].switch_to( master_);
+ BOOST_ASSERT( STATE_RUNNING == fibers_[f_id_].info_->state);
+ fibers_[f_id_].info_->state = STATE_TERMINATED;
+ terminated_fibers_.push_back( f_id_);
+ fibers_[f_id_].switch_to_( master_);
+}
+
+void
+scheduler_impl::cancel_fiber( fiber::id const& f_id)
+{
+ container::iterator i = fibers_.find( f_id);
+ if ( i == fibers_.end() ) return;
+ fiber f( i->second);
+ BOOST_ASSERT( f);
+ BOOST_ASSERT( STATE_MASTER != f.info_->state);
+ if ( STATE_TERMINATED == f.info_->state) return;
+ else if ( STATE_NOT_STARTED == f.info_->state)
+ {
+ f.info_->state = STATE_TERMINATED;
+ terminated_fibers_.push_back( f_id);
+ }
+ else if ( STATE_READY == f.info_->state)
+ {
+ f.info_->state = STATE_TERMINATED;
+ runnable_fibers_.remove( f.get_id() );
+ terminated_fibers_.push_back( f_id);
+ }
+ else if ( STATE_RUNNING == f.info_->state)
+ {
+ f.info_->state = STATE_TERMINATED;
+ terminated_fibers_.push_back( f_id);
+ f.switch_to_( master_);
+ }
+ else
+ {
+ // TODO: remove waiting fiber/suspended fiber
+ f.info_->state = STATE_TERMINATED;
+ terminated_fibers_.push_back( f_id);
+ }
}
bool
@@ -73,17 +107,17 @@
if ( ! runnable_fibers_.empty() )
{
f_id_ = runnable_fibers_.front();
- BOOST_ASSERT( fibers_[f_id_].info_->state == detail::STATE_READY);
- fibers_[f_id_].info_->state = detail::STATE_RUNNING;
- master_.switch_to( fibers_[f_id_]);
- runnable_fibers_.pop();
+ BOOST_ASSERT( fibers_[f_id_].info_->state == STATE_READY);
+ fibers_[f_id_].info_->state = STATE_RUNNING;
+ master_.switch_to_( fibers_[f_id_]);
+ runnable_fibers_.pop_front();
result = true;
}
while ( ! terminated_fibers_.empty() )
{
- BOOST_ASSERT( fibers_[terminated_fibers_.front()].info_->state == detail::STATE_TERMINATED);
+ BOOST_ASSERT( STATE_TERMINATED == fibers_[terminated_fibers_.front()].info_->state);
fibers_.erase( terminated_fibers_.front() );
- terminated_fibers_.pop();
+ terminated_fibers_.pop_front();
result = true;
}
return result;
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