LLFIO v2.00
Loading...
Searching...
No Matches
llfio_v2_xxx::byte_io_multiplexer::awaitable< T > Struct Template Referencefinal

A convenience coroutine awaitable type returned by .co_read(), .co_write() and .co_barrier(). Blocks execution if no i/o multiplexer has been set on this handle! More...

#include "byte_io_multiplexer.hpp"

Inheritance diagram for llfio_v2_xxx::byte_io_multiplexer::awaitable< T >:
llfio_v2_xxx::byte_io_multiplexer::io_operation_state_visitor

Public Types

using result_type = T
 The result type of this awaitable.
 
using buffers_type = typename result_type::value_type
 The buffers type of this awaitable.
 

Public Member Functions

constexpr awaitable ()
 Default constructor.
 
 awaitable (result_type &&res) noexcept
 Constructs an immediately finished awaitable.
 
 awaitable (const awaitable &)=delete
 
 awaitable (awaitable &&o) noexcept
 Move construction, terminates the process if the i/o is in progress.
 
awaitableoperator= (const awaitable &)=delete
 
awaitableoperator= (awaitable &&o) noexcept
 
 ~awaitable ()
 Destructor, blocks if the i/o is in progress.
 
bool await_ready () noexcept
 True if the i/o state is finished. Begins the i/o if it is not initiated yet.
 
result_type await_resume ()
 Returns the result of the i/o.
 

Public Attributes

byte _state_storage [_state_storage_bytes]
 
io_operation_state_state {nullptr}
 

Static Public Attributes

static constexpr size_t _state_storage_bytes = _awaitable_size - sizeof(void *) - sizeof(io_operation_state *)
 

Protected Types

using lock_guard = io_operation_state::lock_guard
 

Protected Member Functions

void set_state (io_operation_state *state) noexcept
 
virtual void read_finished (lock_guard &g, io_operation_state_type) override
 Called when an i/o has finished, and its state can now be destroyed.
 
virtual void write_or_barrier_finished (lock_guard &g, io_operation_state_type) override
 Called when an i/o has finished, and its state can now be destroyed.
 
virtual void read_initiated (lock_guard &, io_operation_state_type)
 Called when an i/o has been initiated, and is now being processed asynchronously.
 
virtual bool read_completed (lock_guard &, io_operation_state_type, io_result< buffers_type > &&)
 Called when an i/o has completed, and its result is available. Return true if you consume the result.
 
virtual void write_initiated (lock_guard &, io_operation_state_type)
 Called when an i/o has been initiated, and is now being processed asynchronously.
 
virtual bool write_completed (lock_guard &, io_operation_state_type, io_result< const_buffers_type > &&)
 Called when an i/o has completed, and its result is available. Return true if you consume the result.
 
virtual void barrier_initiated (lock_guard &, io_operation_state_type)
 Called when an i/o has been initiated, and is now being processed asynchronously.
 
virtual bool barrier_completed (lock_guard &, io_operation_state_type, io_result< const_buffers_type > &&)
 Called when an i/o has completed, and its result is available. Return true if you consume the result.
 

Friends

class byte_io_handle
 
class byte_socket_handle
 
class listening_byte_socket_handle
 

Detailed Description

template<class T>
struct llfio_v2_xxx::byte_io_multiplexer::awaitable< T >

A convenience coroutine awaitable type returned by .co_read(), .co_write() and .co_barrier(). Blocks execution if no i/o multiplexer has been set on this handle!

Upon first .await_ready(), the awaitable initiates the i/o. If the i/o completes immediately, the awaitable is immediately ready and no coroutine suspension occurs.

If the i/o does not complete immediately, the coroutine is suspended. To cause resumption of execution, you will need to pump the associated i/o multiplexer for completions using byte_io_multiplexer::check_for_any_completed_io().

Constructor & Destructor Documentation

◆ awaitable() [1/3]

template<class T >
constexpr llfio_v2_xxx::byte_io_multiplexer::awaitable< T >::awaitable ( )
inlineconstexpr

Default constructor.

1224{}

◆ awaitable() [2/3]

template<class T >
llfio_v2_xxx::byte_io_multiplexer::awaitable< T >::awaitable ( result_type &&  res)
inlineexplicitnoexcept

Constructs an immediately finished awaitable.

1227 {
1228 _state = new(_state_storage) _unsynchronised_io_operation_state(std::move(res));
1229 _state->visitor = this;
1230 }
io_operation_state_visitor * visitor
The state visitor supplied when the operation was initialised.
Definition byte_io_multiplexer.hpp:597

◆ awaitable() [3/3]

template<class T >
llfio_v2_xxx::byte_io_multiplexer::awaitable< T >::awaitable ( awaitable< T > &&  o)
inlinenoexcept

Move construction, terminates the process if the i/o is in progress.

1234 : io_operation_state_visitor(std::move(o))
1235#if LLFIO_ENABLE_COROUTINES
1236 , _coro(o._coro)
1237#endif
1238 {
1239 if(o._state != nullptr && !is_initialised(o._state->current_state()) && !is_finished(o._state->current_state()))
1240 {
1241 abort(); // attempt to relocate an awaitable currently in use
1242 }
1243 _state = o._state->relocate_to(_state_storage);
1244 o._state->~io_operation_state();
1245 o._state = nullptr;
1246 _state->visitor = this;
1247#if LLFIO_ENABLE_COROUTINES
1248 o._coro = {};
1249#endif
1250 }
constexpr bool is_initialised(io_operation_state_type s) noexcept
True if the i/o operation state is initialised.
Definition byte_io_multiplexer.hpp:68
constexpr bool is_finished(io_operation_state_type s) noexcept
True if the i/o operation state is finished.
Definition byte_io_multiplexer.hpp:131
virtual io_operation_state * relocate_to(byte *to) noexcept=0
Relocate the state to new storage, clearing the original state. Terminates the process if the state i...

◆ ~awaitable()

template<class T >
llfio_v2_xxx::byte_io_multiplexer::awaitable< T >::~awaitable ( )
inline

Destructor, blocks if the i/o is in progress.

554{
555 if(_state != nullptr)
556 {
557 auto state = _state->current_state();
558 if(state != io_operation_state_type::unknown && !is_initialised(state) && !is_finished(state))
559 {
560#if LLFIO_ENABLE_COROUTINES
561 // Resumption of the coroutine at this point causes recursion into this destructor, so prevent that
562 _coro = {};
563#endif
564 state = _state->h->multiplexer()->check_io_operation(_state);
565 if(!is_completed(state))
566 {
567 // Cancel the i/o
568 (void) _state->h->multiplexer()->cancel_io_operation(_state);
569 }
570 while(!is_finished(state))
571 {
572 // Block forever until I finish
573 (void) _state->h->multiplexer()->check_for_any_completed_io({});
574 state = _state->current_state();
575 }
576 }
577 _state->~io_operation_state();
578 }
579}
byte_io_multiplexer * multiplexer() const noexcept
The i/o multiplexer this handle will use to multiplex i/o. If this returns null, then this handle has...
Definition byte_io_handle.hpp:105
virtual result< check_for_any_completed_io_statistics > check_for_any_completed_io(deadline d=std::chrono::seconds(0), size_t max_completions=(size_t) -1) noexcept=0
Checks all i/o initiated on this i/o multiplexer to see which have completed, trying without guarante...
virtual result< io_operation_state_type > cancel_io_operation(io_operation_state *op, deadline d={}) noexcept=0
Cancel an initiated i/o, returning its current state if successful.
virtual io_operation_state_type check_io_operation(io_operation_state *op) noexcept
Asks the system for the current state of the i/o, returning its current state.
Definition byte_io_multiplexer.hpp:1460
constexpr bool is_completed(io_operation_state_type s) noexcept
True if the i/o operation state is completed.
Definition byte_io_multiplexer.hpp:110
virtual io_operation_state_type current_state() const noexcept=0
Used to retrieve the current state of the i/o operation.
byte_io_handle * h
The i/o handle the i/o operation is upon.
Definition byte_io_multiplexer.hpp:595

Member Function Documentation

◆ await_ready()

template<class T >
bool llfio_v2_xxx::byte_io_multiplexer::awaitable< T >::await_ready ( )
inlinenoexcept

True if the i/o state is finished. Begins the i/o if it is not initiated yet.

543{
544 auto state = _state->current_state();
545 if(is_initialised(state))
546 {
547 // Begin the i/o
548 state = _state->h->multiplexer()->init_io_operation(_state);
549 // std::cout << "Coroutine " << _state << " begins i/o, state on return was " << (int) state << std::endl;
550 }
551 return is_finished(state);
552}
virtual io_operation_state_type init_io_operation(io_operation_state *state) noexcept=0
Initiates the i/o in a previously constructed state. Note that you should always call ....

◆ await_resume()

template<class T >
result_type llfio_v2_xxx::byte_io_multiplexer::awaitable< T >::await_resume ( )
inline

Returns the result of the i/o.

1270 {
1271 // std::cout << "Coroutine " << _state << " fetches result after resumption" << std::endl;
1272 return _result_type_from_io_operation_state(_state, (buffers_type *) nullptr);
1273 }
typename result_type::value_type buffers_type
The buffers type of this awaitable.
Definition byte_io_multiplexer.hpp:1221

◆ barrier_completed()

virtual bool llfio_v2_xxx::byte_io_multiplexer::io_operation_state_visitor::barrier_completed ( lock_guard ,
io_operation_state_type  ,
io_result< const_buffers_type > &&   
)
inlinevirtualinherited

Called when an i/o has completed, and its result is available. Return true if you consume the result.

692{ return false; }

◆ barrier_initiated()

virtual void llfio_v2_xxx::byte_io_multiplexer::io_operation_state_visitor::barrier_initiated ( lock_guard ,
io_operation_state_type   
)
inlinevirtualinherited

Called when an i/o has been initiated, and is now being processed asynchronously.

690{}

◆ operator=()

template<class T >
awaitable & llfio_v2_xxx::byte_io_multiplexer::awaitable< T >::operator= ( awaitable< T > &&  o)
inlinenoexcept
1253 {
1254 if(this == &o)
1255 {
1256 return *this;
1257 }
1258 this->~awaitable();
1259 new(this) awaitable(std::move(o));
1260 return *this;
1261 }
constexpr awaitable()
Default constructor.
Definition byte_io_multiplexer.hpp:1224
~awaitable()
Destructor, blocks if the i/o is in progress.
Definition byte_io_handle.hpp:553

◆ read_completed()

virtual bool llfio_v2_xxx::byte_io_multiplexer::io_operation_state_visitor::read_completed ( lock_guard ,
io_operation_state_type  ,
io_result< buffers_type > &&   
)
inlinevirtualinherited

Called when an i/o has completed, and its result is available. Return true if you consume the result.

682{ return false; }

◆ read_finished()

template<class T >
virtual void llfio_v2_xxx::byte_io_multiplexer::awaitable< T >::read_finished ( lock_guard ,
io_operation_state_type   
)
inlineoverrideprotectedvirtual

Called when an i/o has finished, and its state can now be destroyed.

Reimplemented from llfio_v2_xxx::byte_io_multiplexer::io_operation_state_visitor.

1298 {
1299 (void) g;
1300#if LLFIO_ENABLE_COROUTINES
1301 if(_coro)
1302 {
1303 // std::cout << "Coroutine " << _state << " resumes" << std::endl;
1304 auto coro = _coro;
1305 _coro = {};
1306 g.unlock();
1307 coro.resume();
1308 }
1309#endif
1310 }

◆ read_initiated()

virtual void llfio_v2_xxx::byte_io_multiplexer::io_operation_state_visitor::read_initiated ( lock_guard ,
io_operation_state_type   
)
inlinevirtualinherited

Called when an i/o has been initiated, and is now being processed asynchronously.

680{}

◆ set_state()

template<class T >
void llfio_v2_xxx::byte_io_multiplexer::awaitable< T >::set_state ( io_operation_state state)
inlineprotectednoexcept
1212 {
1213 _state = state;
1214 _state->visitor = this;
1215 }

◆ write_completed()

virtual bool llfio_v2_xxx::byte_io_multiplexer::io_operation_state_visitor::write_completed ( lock_guard ,
io_operation_state_type  ,
io_result< const_buffers_type > &&   
)
inlinevirtualinherited

Called when an i/o has completed, and its result is available. Return true if you consume the result.

688{ return false; }

◆ write_initiated()

virtual void llfio_v2_xxx::byte_io_multiplexer::io_operation_state_visitor::write_initiated ( lock_guard ,
io_operation_state_type   
)
inlinevirtualinherited

Called when an i/o has been initiated, and is now being processed asynchronously.

686{}

◆ write_or_barrier_finished()

template<class T >
virtual void llfio_v2_xxx::byte_io_multiplexer::awaitable< T >::write_or_barrier_finished ( lock_guard ,
io_operation_state_type   
)
inlineoverrideprotectedvirtual

Called when an i/o has finished, and its state can now be destroyed.

Reimplemented from llfio_v2_xxx::byte_io_multiplexer::io_operation_state_visitor.

1316 {
1317 (void) g;
1318#if LLFIO_ENABLE_COROUTINES
1319 if(_coro)
1320 {
1321 auto coro = _coro;
1322 _coro = {};
1323 g.unlock();
1324 coro.resume();
1325 }
1326#endif
1327 }

Member Data Documentation

◆ _state

template<class T >
io_operation_state* llfio_v2_xxx::byte_io_multiplexer::awaitable< T >::_state {nullptr}
1206{nullptr};

The documentation for this struct was generated from the following files: