123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- #ifndef BOOST_FIBERS_ALGO_ALGORITHM_H
- #define BOOST_FIBERS_ALGO_ALGORITHM_H
- #include <atomic>
- #include <chrono>
- #include <cstddef>
- #include <boost/assert.hpp>
- #include <boost/config.hpp>
- #include <boost/intrusive_ptr.hpp>
- #include <boost/fiber/properties.hpp>
- #include <boost/fiber/detail/config.hpp>
- #ifdef BOOST_HAS_ABI_HEADERS
- # include BOOST_ABI_PREFIX
- #endif
- namespace boost {
- namespace fibers {
- class context;
- namespace algo {
- class BOOST_FIBERS_DECL algorithm {
- private:
- std::atomic< std::size_t > use_count_{ 0 };
- public:
- typedef intrusive_ptr< algorithm > ptr_t;
- virtual ~algorithm() = default;
- virtual void awakened( context *) noexcept = 0;
- virtual context * pick_next() noexcept = 0;
- virtual bool has_ready_fibers() const noexcept = 0;
- virtual void suspend_until( std::chrono::steady_clock::time_point const&) noexcept = 0;
- virtual void notify() noexcept = 0;
- #if !defined(BOOST_EMBTC)
-
- friend void intrusive_ptr_add_ref( algorithm * algo) noexcept {
- BOOST_ASSERT( nullptr != algo);
- algo->use_count_.fetch_add( 1, std::memory_order_relaxed);
- }
- friend void intrusive_ptr_release( algorithm * algo) noexcept {
- BOOST_ASSERT( nullptr != algo);
- if ( 1 == algo->use_count_.fetch_sub( 1, std::memory_order_release) ) {
- std::atomic_thread_fence( std::memory_order_acquire);
- delete algo;
- }
- }
-
- #else
-
- friend void intrusive_ptr_add_ref( algorithm * algo) noexcept;
- friend void intrusive_ptr_release( algorithm * algo) noexcept;
-
- #endif
-
- };
- #if defined(BOOST_EMBTC)
- inline void intrusive_ptr_add_ref( algorithm * algo) noexcept {
- BOOST_ASSERT( nullptr != algo);
- algo->use_count_.fetch_add( 1, std::memory_order_relaxed);
- }
- inline void intrusive_ptr_release( algorithm * algo) noexcept {
- BOOST_ASSERT( nullptr != algo);
- if ( 1 == algo->use_count_.fetch_sub( 1, std::memory_order_release) ) {
- std::atomic_thread_fence( std::memory_order_acquire);
- delete algo;
- }
- }
-
- #endif
-
- class BOOST_FIBERS_DECL algorithm_with_properties_base : public algorithm {
- public:
-
- virtual void property_change_( context * ctx, fiber_properties * props) noexcept = 0;
- protected:
- static fiber_properties* get_properties( context * ctx) noexcept;
- static void set_properties( context * ctx, fiber_properties * p) noexcept;
- };
- template< typename PROPS >
- struct algorithm_with_properties : public algorithm_with_properties_base {
- typedef algorithm_with_properties_base super;
-
-
-
-
- void awakened( context * ctx) noexcept final {
- fiber_properties * props = super::get_properties( ctx);
- if ( BOOST_LIKELY( nullptr == props) ) {
-
-
- props = new_properties( ctx);
-
- BOOST_ASSERT_MSG( props, "new_properties() must return non-NULL");
-
- BOOST_ASSERT_MSG( dynamic_cast< PROPS * >( props),
- "new_properties() must return properties class");
- super::set_properties( ctx, props);
- }
-
-
-
- props->set_algorithm( this);
-
- awakened( ctx, properties( ctx) );
- }
-
- virtual void awakened( context *, PROPS &) noexcept = 0;
-
- PROPS & properties( context * ctx) noexcept {
- return static_cast< PROPS & >( * super::get_properties( ctx) );
- }
-
- virtual void property_change( context * , PROPS & ) noexcept {
- }
-
- void property_change_( context * ctx, fiber_properties * props) noexcept final {
- property_change( ctx, * static_cast< PROPS * >( props) );
- }
-
-
-
- virtual fiber_properties * new_properties( context * ctx) {
- return new PROPS( ctx);
- }
- };
- }}}
- #ifdef BOOST_HAS_ABI_HEADERS
- # include BOOST_ABI_SUFFIX
- #endif
- #endif
|