Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
boost::fibers::asio::round_robin Class Reference

#include <round_robin.hpp>

Inheritance diagram for boost::fibers::asio::round_robin:
Collaboration diagram for boost::fibers::asio::round_robin:

Classes

struct  service
 

Public Member Functions

 round_robin (boost::asio::io_service &io_svc)
 
void awakened (context *ctx) noexcept
 
contextpick_next () noexcept
 
bool has_ready_fibers () const noexcept
 
void suspend_until (std::chrono::steady_clock::time_point const &abs_time) noexcept
 
void notify () noexcept
 

Detailed Description

Definition at line 35 of file round_robin.hpp.

Constructor & Destructor Documentation

◆ round_robin()

boost::fibers::asio::round_robin::round_robin ( boost::asio::io_service & io_svc)
inline

Definition at line 89 of file round_robin.hpp.

89 :
90 io_svc_( io_svc),
91 suspend_timer_( io_svc_) {
92 // We use add_service() very deliberately. This will throw
93 // service_already_exists if you pass the same io_service instance to
94 // more than one round_robin instance.
95 boost::asio::add_service( io_svc_, new service( io_svc_));
96 }

Member Function Documentation

◆ awakened()

void boost::fibers::asio::round_robin::awakened ( context * ctx)
inlinenoexcept

Definition at line 99 of file round_robin.hpp.

99 {
100 BOOST_ASSERT( nullptr != ctx);
101 ctx->ready_link( rqueue_); /*< fiber, enqueue on ready queue >*/
102 }

◆ has_ready_fibers()

bool boost::fibers::asio::round_robin::has_ready_fibers ( ) const
inlinenoexcept

Definition at line 117 of file round_robin.hpp.

117 {
118 return ! rqueue_.empty();
119 }

◆ notify()

void boost::fibers::asio::round_robin::notify ( )
inlinenoexcept

Definition at line 156 of file round_robin.hpp.

156 {
157 // Something has happened that should wake one or more fibers BEFORE
158 // suspend_timer_ expires. Reset the timer to cause it to fire
159 // immediately, causing the run_one() call to return. In theory we
160 // could use cancel() because we don't care whether suspend_timer_'s
161 // handler is called with operation_aborted or success. However --
162 // cancel() doesn't change the expiration time, and we use
163 // suspend_timer_'s expiration time to decide whether it's already
164 // set. If suspend_until() set some specific wake time, then notify()
165 // canceled it, then suspend_until() was called again with the same
166 // wake time, it would match suspend_timer_'s expiration time and we'd
167 // refrain from setting the timer. So instead of simply calling
168 // cancel(), reset the timer, which cancels the pending sleep AND sets
169 // a new expiration time. This will cause us to spin the loop twice --
170 // once for the operation_aborted handler, once for timer expiration
171 // -- but that shouldn't be a big problem.
172 suspend_timer_.expires_at( std::chrono::steady_clock::now() );
173 }

◆ pick_next()

context * boost::fibers::asio::round_robin::pick_next ( )
inlinenoexcept

Definition at line 104 of file round_robin.hpp.

104 {
105 context * ctx( nullptr);
106 if ( ! rqueue_.empty() ) { /*<
107 pop an item from the ready queue
108 >*/
109 ctx = & rqueue_.front();
110 rqueue_.pop_front();
111 BOOST_ASSERT( nullptr != ctx);
112 BOOST_ASSERT( context::active() != ctx);
113 }
114 return ctx;
115 }

◆ suspend_until()

void boost::fibers::asio::round_robin::suspend_until ( std::chrono::steady_clock::time_point const & abs_time)
inlinenoexcept

Definition at line 122 of file round_robin.hpp.

122 {
123 // Set a timer so at least one handler will eventually fire, causing
124 // run_one() to eventually return. Set a timer even if abs_time ==
125 // time_point::max() so the timer can be canceled by our notify()
126 // method -- which calls the handler.
127 if ( suspend_timer_.expires_at() != abs_time) {
128 // Each expires_at(time_point) call cancels any previous pending
129 // call. We could inadvertently spin like this:
130 // dispatcher calls suspend_until() with earliest wake time
131 // suspend_until() sets suspend_timer_
132 // lambda loop calls run_one()
133 // some other asio handler runs before timer expires
134 // run_one() returns to lambda loop
135 // lambda loop yields to dispatcher
136 // dispatcher finds no ready fibers
137 // dispatcher calls suspend_until() with SAME wake time
138 // suspend_until() sets suspend_timer_ to same time, canceling
139 // previous async_wait()
140 // lambda loop calls run_one()
141 // asio calls suspend_timer_ handler with operation_aborted
142 // run_one() returns to lambda loop... etc. etc.
143 // So only actually set the timer when we're passed a DIFFERENT
144 // abs_time value.
145 suspend_timer_.expires_at( abs_time);
146 // It really doesn't matter what the suspend_timer_ handler does,
147 // or even whether it's called because the timer ran out or was
148 // canceled. The whole point is to cause the run_one() call to
149 // return. So just pass a no-op lambda with proper signature.
150 suspend_timer_.async_wait([](boost::system::error_code const&){});
151 }
152 }

The documentation for this class was generated from the following file: