libcopp  1.1.0
Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | Private Member Functions | Private Attributes | Friends
copp::coroutine_context_container< TALLOC > Class Template Reference

coroutine container contain stack context, stack allocator and runtime fcontext More...

#include <coroutine_context_container.h>

Inheritance diagram for copp::coroutine_context_container< TALLOC >:
Inheritance graph
Collaboration diagram for copp::coroutine_context_container< TALLOC >:
Collaboration graph

Public Types

typedef coroutine_context coroutine_context_type
 
typedef coroutine_context base_type
 
typedef TALLOC allocator_type
 
typedef coroutine_context_container< allocator_typethis_type
 
typedef std::intrusive_ptr< this_typeptr_t
 
typedef coroutine_context::callback_t callback_t
 

Public Member Functions

 ~coroutine_context_container ()
 
const allocator_typeget_allocator () const UTIL_CONFIG_NOEXCEPT
 get stack allocator More...
 
allocator_typeget_allocator () UTIL_CONFIG_NOEXCEPT
 get stack allocator More...
 
size_t use_count () const UTIL_CONFIG_NOEXCEPT
 
int start (void *priv_data=UTIL_CONFIG_NULLPTR)
 start coroutine More...
 
int resume (void *priv_data=UTIL_CONFIG_NULLPTR)
 resume coroutine More...
 
int yield (void **priv_data=UTIL_CONFIG_NULLPTR)
 yield coroutine More...
 
bool set_flags (int flags)
 set all flags to true More...
 
bool unset_flags (int flags)
 set all flags to false More...
 
bool check_flags (int flags) const
 check flags More...
 
int set_runner (const callback_t &runner)
 set runner More...
 
const std::function< int(void *)> & get_runner () const UTIL_CONFIG_NOEXCEPT
 
int get_ret_code () const UTIL_CONFIG_NOEXCEPT
 get runner return code More...
 
bool is_finished () const UTIL_CONFIG_NOEXCEPT
 get runner return code More...
 
void * get_private_buffer () const UTIL_CONFIG_NOEXCEPT
 get private buffer(raw pointer) More...
 
size_t get_private_buffer_size () const UTIL_CONFIG_NOEXCEPT
 get private buffer size More...
 

Static Public Member Functions

static ptr_t create (const callback_t &runner, allocator_type &alloc, size_t stack_sz=0, size_t private_buffer_size=0, size_t coroutine_size=0) UTIL_CONFIG_NOEXCEPT
 create and init coroutine with specify runner and specify stack size More...
 
template<class TRunner >
static ptr_t create (TRunner *runner, allocator_type &alloc, size_t stack_size=0, size_t private_buffer_size=0, size_t coroutine_size=0) UTIL_CONFIG_NOEXCEPT
 
static ptr_t create (int(*fn)(void *), allocator_type &alloc, size_t stack_size=0, size_t private_buffer_size=0, size_t coroutine_size=0) UTIL_CONFIG_NOEXCEPT
 
static ptr_t create (const callback_t &runner, size_t stack_size=0, size_t private_buffer_size=0, size_t coroutine_size=0) UTIL_CONFIG_NOEXCEPT
 
template<class TRunner >
static ptr_t create (TRunner *runner, size_t stack_size=0, size_t private_buffer_size=0, size_t coroutine_size=0) UTIL_CONFIG_NOEXCEPT
 
static ptr_t create (int(*fn)(void *), size_t stack_size=0, size_t private_buffer_size=0, size_t coroutine_size=0) UTIL_CONFIG_NOEXCEPT
 
static int create (coroutine_context *p, callback_t &runner, const stack_context &callee_stack, size_t coroutine_size, size_t private_buffer_size) UTIL_CONFIG_NOEXCEPT
 create coroutine context at stack context callee_ More...
 
template<typename TRunner >
static int create (coroutine_context *p, TRunner *runner, const stack_context &callee_stack, size_t coroutine_size, size_t private_buffer_size) UTIL_CONFIG_NOEXCEPT
 
static size_t align_private_data_size (size_t sz)
 
static size_t align_address_size (size_t sz)
 

Protected Member Functions

void run_and_recv_retcode (void *priv_data)
 coroutine entrance function More...
 

Static Protected Member Functions

static void jump_to (fcontext::fcontext_t &to_fctx, stack_context &from_sctx, stack_context &to_sctx, jump_src_data_t &jump_transfer) UTIL_CONFIG_NOEXCEPT
 call platform jump to asm instruction More...
 
static void coroutine_context_callback (::copp::fcontext::transfer_t src_ctx)
 fcontext entrance function More...
 

Protected Attributes

fcontext::fcontext_t caller_
 
fcontext::fcontext_t callee_
 
stack_context callee_stack_
 

Private Member Functions

 coroutine_context_container (const allocator_type &alloc) UTIL_CONFIG_NOEXCEPT
 
 coroutine_context_container (const coroutine_context_container &) UTIL_CONFIG_DELETED_FUNCTION
 

Private Attributes

allocator_type alloc_
 
util::lock::atomic_int_type< size_t > ref_count_
 

Friends

void intrusive_ptr_add_ref (this_type *p)
 
void intrusive_ptr_release (this_type *p)
 

Detailed Description

template<typename TALLOC>
class copp::coroutine_context_container< TALLOC >

coroutine container contain stack context, stack allocator and runtime fcontext

Definition at line 23 of file coroutine_context_container.h.

Member Typedef Documentation

template<typename TALLOC >
typedef TALLOC copp::coroutine_context_container< TALLOC >::allocator_type

Definition at line 27 of file coroutine_context_container.h.

template<typename TALLOC >
typedef coroutine_context copp::coroutine_context_container< TALLOC >::base_type

Definition at line 26 of file coroutine_context_container.h.

template<typename TALLOC >
typedef coroutine_context::callback_t copp::coroutine_context_container< TALLOC >::callback_t

Definition at line 30 of file coroutine_context_container.h.

template<typename TALLOC >
typedef coroutine_context copp::coroutine_context_container< TALLOC >::coroutine_context_type

Definition at line 25 of file coroutine_context_container.h.

template<typename TALLOC >
typedef std::intrusive_ptr<this_type> copp::coroutine_context_container< TALLOC >::ptr_t

Definition at line 29 of file coroutine_context_container.h.

Definition at line 28 of file coroutine_context_container.h.

Constructor & Destructor Documentation

template<typename TALLOC >
copp::coroutine_context_container< TALLOC >::coroutine_context_container ( const allocator_type alloc)
inlineprivate
template<typename TALLOC >
copp::coroutine_context_container< TALLOC >::~coroutine_context_container ( )
inline

Definition at line 42 of file coroutine_context_container.h.

template<typename TALLOC >
copp::coroutine_context_container< TALLOC >::coroutine_context_container ( const coroutine_context_container< TALLOC > &  )
private

Member Function Documentation

static size_t copp::coroutine_context::align_address_size ( size_t  sz)
inlinestaticinherited
static size_t copp::coroutine_context::align_private_data_size ( size_t  sz)
inlinestaticinherited
bool copp::coroutine_context::check_flags ( int  flags) const
inherited

check flags

Parameters
flagsflags to be checked
Returns
true if flags any flags is true

Definition at line 243 of file coroutine_context.cpp.

References copp::coroutine_context::set_runner().

Referenced by cotask::impl::task_impl::this_task().

void copp::coroutine_context::coroutine_context_callback ( ::copp::fcontext::transfer_t  src_ctx)
staticprotectedinherited
template<typename TALLOC >
static ptr_t copp::coroutine_context_container< TALLOC >::create ( const callback_t runner,
allocator_type alloc,
size_t  stack_sz = 0,
size_t  private_buffer_size = 0,
size_t  coroutine_size = 0 
)
inlinestatic

create and init coroutine with specify runner and specify stack size

Parameters
runnerrunner
stack_szstack size
private_buffer_sizeprivate buffer size
coroutine_sizeextend buffer before coroutine
Returns
COPP_EC_SUCCESS or error code

Definition at line 65 of file coroutine_context_container.h.

References copp::coroutine_context::align_address_size(), copp::coroutine_context::align_private_data_size(), COPP_MACRO_STD_MOVE, copp::coroutine_context::create(), copp::stack_traits::default_size(), std::intrusive_ptr< T >::get(), std::intrusive_ptr< T >::reset(), and copp::stack_context::sp.

Referenced by benchmark_round(), CASE_TEST(), copp::coroutine_context_container< TALLOC >::create(), and main().

template<typename TALLOC >
template<class TRunner >
static ptr_t copp::coroutine_context_container< TALLOC >::create ( TRunner *  runner,
allocator_type alloc,
size_t  stack_size = 0,
size_t  private_buffer_size = 0,
size_t  coroutine_size = 0 
)
inlinestatic
template<typename TALLOC >
static ptr_t copp::coroutine_context_container< TALLOC >::create ( int(*)(void *)  fn,
allocator_type alloc,
size_t  stack_size = 0,
size_t  private_buffer_size = 0,
size_t  coroutine_size = 0 
)
inlinestatic
int copp::coroutine_context::create ( coroutine_context p,
callback_t runner,
const stack_context callee_stack,
size_t  coroutine_size,
size_t  private_buffer_size 
)
staticinherited

create coroutine context at stack context callee_

Parameters
runnerrunner
callee_stackstack context
coroutine_sizesize of coroutine object
private_buffer_sizesize of private buffer
Returns
COPP_EC_SUCCESS or error code

Definition at line 76 of file coroutine_context.cpp.

References copp::COPP_EC_ARGS_ERROR, copp::COPP_EC_FCONTEXT_MAKE_FAILED, copp::COPP_EC_SUCCESS, COPP_MACRO_STD_MOVE, copp::fcontext::copp_make_fcontext(), and copp::coroutine_context::coroutine_context_callback().

Referenced by copp::coroutine_context_container< TALLOC >::create().

template<typename TRunner >
static int copp::coroutine_context::create ( coroutine_context p,
TRunner *  runner,
const stack_context callee_stack,
size_t  coroutine_size,
size_t  private_buffer_size 
)
inlinestaticinherited

Definition at line 138 of file coroutine_context.h.

References copp::this_coroutine::yield().

template<typename TALLOC >
static ptr_t copp::coroutine_context_container< TALLOC >::create ( const callback_t runner,
size_t  stack_size = 0,
size_t  private_buffer_size = 0,
size_t  coroutine_size = 0 
)
inlinestatic
template<typename TALLOC >
template<class TRunner >
static ptr_t copp::coroutine_context_container< TALLOC >::create ( TRunner *  runner,
size_t  stack_size = 0,
size_t  private_buffer_size = 0,
size_t  coroutine_size = 0 
)
inlinestatic
template<typename TALLOC >
static ptr_t copp::coroutine_context_container< TALLOC >::create ( int(*)(void *)  fn,
size_t  stack_size = 0,
size_t  private_buffer_size = 0,
size_t  coroutine_size = 0 
)
inlinestatic
template<typename TALLOC >
const allocator_type& copp::coroutine_context_container< TALLOC >::get_allocator ( ) const
inline

get stack allocator

Returns
stack allocator

Definition at line 48 of file coroutine_context_container.h.

References copp::coroutine_context_container< TALLOC >::alloc_.

template<typename TALLOC >
allocator_type& copp::coroutine_context_container< TALLOC >::get_allocator ( )
inline

get stack allocator

Returns
stack allocator

Definition at line 54 of file coroutine_context_container.h.

References copp::coroutine_context_container< TALLOC >::alloc_.

void* copp::coroutine_context::get_private_buffer ( ) const
inlineinherited

get private buffer(raw pointer)

Definition at line 230 of file coroutine_context.h.

Referenced by cotask::impl::task_impl::this_task().

size_t copp::coroutine_context::get_private_buffer_size ( ) const
inlineinherited

get private buffer size

Definition at line 235 of file coroutine_context.h.

int copp::coroutine_context::get_ret_code ( ) const
inlineinherited

get runner return code

Returns

Definition at line 219 of file coroutine_context.h.

const std::function<int(void *)>& copp::coroutine_context::get_runner ( ) const
inlineinherited

get runner of this coroutine context (const)

Returns
NULL of pointer of runner

Definition at line 213 of file coroutine_context.h.

bool copp::coroutine_context::is_finished ( ) const
inherited

get runner return code

Returns
true if coroutine has run and finished

Definition at line 264 of file coroutine_context.cpp.

References util::lock::memory_order_acquire.

void copp::coroutine_context::jump_to ( fcontext::fcontext_t to_fctx,
stack_context from_sctx,
stack_context to_sctx,
jump_src_data_t jump_transfer 
)
staticprotectedinherited

call platform jump to asm instruction

Parameters
to_fctxjump to function context
from_sctxjump from stack context(only used for save segment stack)
to_sctxjump to stack context(only used for set segment stack)
jump_transferjump data

save from_co's fcontext and switch status we should use from_co in transfer_t, because it may not jump from jump_transfer.to_co

if we jump sequence is A->B->C->A.resume(), and if this call is A->B, then jump_src->from_co = C, jump_src->to_co = A, jump_transfer.from_co = A, jump_transfer.to_co = B and now we should save the callee of C and set the caller of A = C

if we jump sequence is A->B.yield()->A, and if this call is A->B, then jump_src->from_co = B, jump_src->to_co = NULL, jump_transfer.from_co = A, jump_transfer.to_co = B and now we should save the callee of B and should change the caller of A

Definition at line 269 of file coroutine_context.cpp.

References copp::coroutine_context::callee_, copp::coroutine_context::caller_, copp::fcontext::copp_jump_fcontext(), copp::fcontext::transfer_t::data, copp::fcontext::transfer_t::fctx, copp::coroutine_context::jump_src_data_t::from_co, copp::coroutine_context::jump_src_data_t::priv_data, copp::detail::set_this_coroutine_context(), and copp::coroutine_context::jump_src_data_t::to_co.

int copp::coroutine_context::resume ( void *  priv_data = UTIL_CONFIG_NULLPTR)
inherited

resume coroutine

Parameters
priv_dataprivate data, will be passed to runner operator() or return to yield
Returns
COPP_EC_SUCCESS or error code

Definition at line 182 of file coroutine_context.cpp.

void copp::coroutine_context::run_and_recv_retcode ( void *  priv_data)
inlineprotectedinherited

coroutine entrance function

Definition at line 191 of file coroutine_context.h.

Referenced by copp::coroutine_context::coroutine_context_callback().

bool copp::coroutine_context::set_flags ( int  flags)
inherited

set all flags to true

Parameters
flags(flags & EN_CFT_MASK) must be 0
Returns
true if flags is available, or return false

Definition at line 225 of file coroutine_context.cpp.

int copp::coroutine_context::set_runner ( const callback_t runner)
inherited
int copp::coroutine_context::start ( void *  priv_data = UTIL_CONFIG_NULLPTR)
inherited
bool copp::coroutine_context::unset_flags ( int  flags)
inherited

set all flags to false

Parameters
flags(flags & EN_CFT_MASK) must be 0
Returns
true if flags is available, or return false

Definition at line 234 of file coroutine_context.cpp.

template<typename TALLOC >
size_t copp::coroutine_context_container< TALLOC >::use_count ( ) const
inline
int copp::coroutine_context::yield ( void **  priv_data = UTIL_CONFIG_NULLPTR)
inherited

Friends And Related Function Documentation

template<typename TALLOC >
void intrusive_ptr_add_ref ( this_type p)
friend

Definition at line 169 of file coroutine_context_container.h.

template<typename TALLOC >
void intrusive_ptr_release ( this_type p)
friend

Definition at line 177 of file coroutine_context_container.h.

Field Documentation

template<typename TALLOC >
allocator_type copp::coroutine_context_container< TALLOC >::alloc_
private
fcontext::fcontext_t copp::coroutine_context::callee_
protectedinherited

caller runtime context

Definition at line 105 of file coroutine_context.h.

Referenced by copp::coroutine_context::coroutine_context_callback(), and copp::coroutine_context::jump_to().

stack_context copp::coroutine_context::callee_stack_
protectedinherited

callee runtime context

Definition at line 107 of file coroutine_context.h.

fcontext::fcontext_t copp::coroutine_context::caller_
protectedinherited
template<typename TALLOC >
util::lock::atomic_int_type<size_t> copp::coroutine_context_container< TALLOC >::ref_count_
private

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