libcopp  2.2.0
Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Private Member Functions | Private Attributes | Friends
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 coroutine_context_container< TALLOC >:
Inheritance graph
Collaboration diagram for coroutine_context_container< TALLOC >:
Collaboration graph

Public Types

using coroutine_context_type = coroutine_context
 
using base_type = coroutine_context
 
using allocator_type = TALLOC
 
using this_type = coroutine_context_container< allocator_type >
 
using ptr_type = LIBCOPP_COPP_NAMESPACE_ID::util::intrusive_ptr< this_type >
 
using callback_type = coroutine_context::callback_type
 
using ptr_t = ptr_type
 
using callback_t = callback_type
 
using status_type = coroutine_context_base::status_type
 
using flag_type = coroutine_context_base::flag_type
 
using status_t = status_type
 
using flag_t = flag_type
 

Public Member Functions

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

Static Public Member Functions

static ptr_type create (callback_type &&runner, allocator_type &alloc, size_t stack_sz=0, size_t private_buffer_size=0, size_t coroutine_size=0) LIBCOPP_MACRO_NOEXCEPT
 create and init coroutine with specify runner and specify stack size More...
 
template<class TRunner >
static ptr_type create (TRunner *runner, allocator_type &alloc, size_t stack_size=0, size_t private_buffer_size=0, size_t coroutine_size=0) LIBCOPP_MACRO_NOEXCEPT
 
static ptr_type create (int(*fn)(void *), allocator_type &alloc, size_t stack_size=0, size_t private_buffer_size=0, size_t coroutine_size=0) LIBCOPP_MACRO_NOEXCEPT
 
static ptr_type create (callback_type &&runner, size_t stack_size=0, size_t private_buffer_size=0, size_t coroutine_size=0) LIBCOPP_MACRO_NOEXCEPT
 
template<class TRunner >
static ptr_type create (TRunner *runner, size_t stack_size=0, size_t private_buffer_size=0, size_t coroutine_size=0) LIBCOPP_MACRO_NOEXCEPT
 
static ptr_type create (int(*fn)(void *), size_t stack_size=0, size_t private_buffer_size=0, size_t coroutine_size=0) LIBCOPP_MACRO_NOEXCEPT
 
static LIBCOPP_COPP_API int create (coroutine_context *p, callback_type &&runner, const stack_context &callee_stack, size_t coroutine_size, size_t private_buffer_size) LIBCOPP_MACRO_NOEXCEPT
 create coroutine context at stack context callee_ More...
 
template<typename TRunner >
static LIBCOPP_COPP_API_HEAD_ONLY int create (coroutine_context *p, TRunner *runner, const stack_context &callee_stack, size_t coroutine_size, size_t private_buffer_size) LIBCOPP_MACRO_NOEXCEPT
 
static UTIL_FORCEINLINE size_t align_private_data_size (size_t sz)
 
static size_t align_address_size (size_t sz)
 
static size_t align_stack_size (size_t sz)
 
static LIBCOPP_COPP_API coroutine_context_baseget_this_coroutine_base () LIBCOPP_MACRO_NOEXCEPT
 get current coroutine More...
 
static LIBCOPP_COPP_API void set_this_coroutine_base (coroutine_context_base *ctx) LIBCOPP_MACRO_NOEXCEPT
 set current coroutine More...
 

Protected Member Functions

UTIL_FORCEINLINE void run_and_recv_retcode (void *priv_data)
 coroutine 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) LIBCOPP_MACRO_NOEXCEPT
 
 coroutine_context_container (allocator_type &&alloc) LIBCOPP_MACRO_NOEXCEPT
 
 coroutine_context_container (const coroutine_context_container &)=delete
 

Private Attributes

allocator_type alloc_
 
LIBCOPP_COPP_NAMESPACE_ID::util::lock::atomic_int_type< size_t > ref_count_
 
int flags_
 
void * priv_data_
 
size_t private_buffer_size_
 
callback_type runner_
 
int runner_ret_code_
 
LIBCOPP_COPP_NAMESPACE_ID::util::lock::atomic_int_type< int > status_
 

Friends

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

Detailed Description

template<typename TALLOC>
class coroutine_context_container< TALLOC >

coroutine container contain stack context, stack allocator and runtime fcontext

Definition at line 26 of file coroutine_context_container.h.

Member Typedef Documentation

◆ allocator_type

template<typename TALLOC >
using coroutine_context_container< TALLOC >::allocator_type = TALLOC

Definition at line 30 of file coroutine_context_container.h.

◆ base_type

template<typename TALLOC >
using coroutine_context_container< TALLOC >::base_type = coroutine_context

Definition at line 29 of file coroutine_context_container.h.

◆ callback_t

template<typename TALLOC >
using coroutine_context_container< TALLOC >::callback_t = callback_type

Definition at line 37 of file coroutine_context_container.h.

◆ callback_type

template<typename TALLOC >
using coroutine_context_container< TALLOC >::callback_type = coroutine_context::callback_type

Definition at line 33 of file coroutine_context_container.h.

◆ coroutine_context_type

template<typename TALLOC >
using coroutine_context_container< TALLOC >::coroutine_context_type = coroutine_context

Definition at line 28 of file coroutine_context_container.h.

◆ flag_t

Definition at line 37 of file coroutine_context.h.

◆ flag_type

Definition at line 31 of file coroutine_context.h.

◆ ptr_t

template<typename TALLOC >
using coroutine_context_container< TALLOC >::ptr_t = ptr_type

Definition at line 36 of file coroutine_context_container.h.

◆ ptr_type

template<typename TALLOC >
using coroutine_context_container< TALLOC >::ptr_type = LIBCOPP_COPP_NAMESPACE_ID::util::intrusive_ptr<this_type>

Definition at line 32 of file coroutine_context_container.h.

◆ status_t

Definition at line 36 of file coroutine_context.h.

◆ status_type

Definition at line 30 of file coroutine_context.h.

◆ this_type

template<typename TALLOC >
using coroutine_context_container< TALLOC >::this_type = coroutine_context_container<allocator_type>

Definition at line 31 of file coroutine_context_container.h.

Constructor & Destructor Documentation

◆ coroutine_context_container() [1/3]

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

Definition at line 42 of file coroutine_context_container.h.

◆ coroutine_context_container() [2/3]

template<typename TALLOC >
coroutine_context_container< TALLOC >::coroutine_context_container ( allocator_type &&  alloc)
inlineprivate

Definition at line 44 of file coroutine_context_container.h.

◆ ~coroutine_context_container()

template<typename TALLOC >
coroutine_context_container< TALLOC >::~coroutine_context_container ( )
inline

Definition at line 48 of file coroutine_context_container.h.

◆ coroutine_context_container() [3/3]

template<typename TALLOC >
coroutine_context_container< TALLOC >::coroutine_context_container ( const coroutine_context_container< TALLOC > &  )
privatedelete

Member Function Documentation

◆ align_address_size()

static size_t coroutine_context_base::align_address_size ( size_t  sz)
inlinestaticinherited

◆ align_private_data_size()

static UTIL_FORCEINLINE size_t coroutine_context_base::align_private_data_size ( size_t  sz)
inlinestaticinherited

◆ align_stack_size()

static size_t coroutine_context_base::align_stack_size ( size_t  sz)
inlinestaticinherited

Definition at line 227 of file coroutine_context_base.h.

References COROUTINE_CONTEXT_STACK_ALIGN_UNIT_SIZE.

◆ check_flags()

LIBCOPP_COPP_API bool coroutine_context_base::check_flags ( int  flags) const
inherited

check flags

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

Definition at line 98 of file coroutine_context.cpp.

Referenced by this_coroutine::get_coroutine(), and coroutine_context::start().

◆ create() [1/8]

template<typename TALLOC >
static ptr_type coroutine_context_container< TALLOC >::create ( callback_type &&  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 71 of file coroutine_context_container.h.

References coroutine_context_base::align_address_size(), coroutine_context_base::align_private_data_size(), coroutine_context::create(), stack_traits::default_size(), stack_context::reset(), and stack_context::sp.

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

◆ create() [2/8]

template<typename TALLOC >
static ptr_type coroutine_context_container< TALLOC >::create ( callback_type &&  runner,
size_t  stack_size = 0,
size_t  private_buffer_size = 0,
size_t  coroutine_size = 0 
)
inlinestatic

◆ create() [3/8]

LIBCOPP_COPP_API int coroutine_context::create ( coroutine_context p,
callback_type &&  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 284 of file coroutine_context.cpp.

References COPP_EC_ARGS_ERROR, COPP_EC_FCONTEXT_MAKE_FAILED, COPP_EC_SUCCESS, fcontext::copp_make_fcontext_v2(), and libcopp_internal_api_set::coroutine_context_callback().

Referenced by coroutine_context_container< TALLOC >::create(), and coroutine_context::create().

◆ create() [4/8]

template<typename TRunner >
static LIBCOPP_COPP_API_HEAD_ONLY int 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 92 of file coroutine_context.h.

References coroutine_context::create().

◆ create() [5/8]

template<typename TALLOC >
static ptr_type 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

◆ create() [6/8]

template<typename TALLOC >
static ptr_type 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

◆ create() [7/8]

template<typename TALLOC >
template<class TRunner >
static ptr_type 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

◆ create() [8/8]

template<typename TALLOC >
template<class TRunner >
static ptr_type coroutine_context_container< TALLOC >::create ( TRunner *  runner,
size_t  stack_size = 0,
size_t  private_buffer_size = 0,
size_t  coroutine_size = 0 
)
inlinestatic

◆ get_allocator() [1/2]

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

get stack allocator

Returns
stack allocator

Definition at line 54 of file coroutine_context_container.h.

References coroutine_context_container< TALLOC >::alloc_.

◆ get_allocator() [2/2]

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

get stack allocator

Returns
stack allocator

Definition at line 60 of file coroutine_context_container.h.

References coroutine_context_container< TALLOC >::alloc_.

◆ get_private_buffer()

UTIL_FORCEINLINE void* coroutine_context_base::get_private_buffer ( ) const
inlineinherited

get private buffer(raw pointer)

Definition at line 195 of file coroutine_context_base.h.

References coroutine_context_base::priv_data_.

◆ get_private_buffer_size()

UTIL_FORCEINLINE size_t coroutine_context_base::get_private_buffer_size ( ) const
inlineinherited

get private buffer size

Definition at line 200 of file coroutine_context_base.h.

References coroutine_context_base::private_buffer_size_.

◆ get_ret_code()

UTIL_FORCEINLINE int coroutine_context_base::get_ret_code ( ) const
inlineinherited

get runner return code

Returns

Definition at line 184 of file coroutine_context_base.h.

References coroutine_context_base::runner_ret_code_.

◆ get_runner()

UTIL_FORCEINLINE const std::function<int(void *)>& coroutine_context_base::get_runner ( ) const
inlineinherited

get runner of this coroutine context (const)

Returns
nullptr of pointer of runner

Definition at line 178 of file coroutine_context_base.h.

References coroutine_context_base::runner_.

◆ get_this_coroutine_base()

LIBCOPP_COPP_API coroutine_context_base * coroutine_context_base::get_this_coroutine_base ( )
staticinherited

get current coroutine

See also
detail::coroutine_context_base
Returns
pointer of current coroutine, if not in coroutine, return nullptr

Definition at line 123 of file coroutine_context.cpp.

References detail::get_this_coroutine_context().

◆ is_finished()

LIBCOPP_COPP_API bool coroutine_context_base::is_finished ( ) const
inherited

get runner return code

Returns
true if coroutine has run and finished

Definition at line 118 of file coroutine_context.cpp.

References coroutine_context_base::status_type::EN_CRS_FINISHED, util::lock::memory_order_acquire, and coroutine_context_base::status_.

◆ resume()

LIBCOPP_COPP_API int coroutine_context::resume ( void *  priv_data = nullptr)
inherited

resume coroutine

Parameters
priv_dataprivate data, will be passed to runner operator() or return to yield
Exceptions
ifexception is enabled, it will throw all unhandled exception after resumed
Returns
COPP_EC_SUCCESS or error code

Definition at line 415 of file coroutine_context.cpp.

References coroutine_context::start().

◆ run_and_recv_retcode()

UTIL_FORCEINLINE void coroutine_context_base::run_and_recv_retcode ( void *  priv_data)
inlineprotectedinherited

◆ set_flags()

LIBCOPP_COPP_API bool coroutine_context_base::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 80 of file coroutine_context.cpp.

◆ set_runner()

LIBCOPP_COPP_API int coroutine_context_base::set_runner ( callback_type &&  runner)
inherited

◆ set_this_coroutine_base()

LIBCOPP_COPP_API void coroutine_context_base::set_this_coroutine_base ( coroutine_context_base ctx)
staticinherited

set current coroutine

See also
detail::coroutine_context_base
Parameters
ctxpointer of current coroutine, if not in coroutine, set nullptr

Definition at line 127 of file coroutine_context.cpp.

References detail::set_this_coroutine_context().

◆ start()

LIBCOPP_COPP_API int coroutine_context::start ( void *  priv_data = nullptr)
inherited

◆ unset_flags()

LIBCOPP_COPP_API bool coroutine_context_base::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 89 of file coroutine_context.cpp.

◆ use_count()

template<typename TALLOC >
size_t coroutine_context_container< TALLOC >::use_count ( ) const
inline

◆ yield()

LIBCOPP_COPP_API int coroutine_context::yield ( void **  priv_data = nullptr)
inherited

Friends And Related Function Documentation

◆ intrusive_ptr_add_ref

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

Definition at line 162 of file coroutine_context_container.h.

◆ intrusive_ptr_release

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

Definition at line 170 of file coroutine_context_container.h.

Field Documentation

◆ alloc_

template<typename TALLOC >
allocator_type coroutine_context_container< TALLOC >::alloc_
private

◆ callee_

fcontext::fcontext_t coroutine_context::callee_
protectedinherited

◆ callee_stack_

stack_context coroutine_context::callee_stack_
protectedinherited

callee runtime context

Definition at line 62 of file coroutine_context.h.

Referenced by coroutine_context::start().

◆ caller_

fcontext::fcontext_t coroutine_context::caller_
protectedinherited

◆ flags_

int coroutine_context_base::flags_
privateinherited

coroutine return code

Definition at line 105 of file coroutine_context_base.h.

Referenced by libcopp_internal_api_set::coroutine_context_callback().

◆ priv_data_

void* coroutine_context_base::priv_data_
privateinherited

coroutine runner

Definition at line 107 of file coroutine_context_base.h.

◆ private_buffer_size_

size_t coroutine_context_base::private_buffer_size_
privateinherited

Definition at line 108 of file coroutine_context_base.h.

◆ ref_count_

template<typename TALLOC >
LIBCOPP_COPP_NAMESPACE_ID::util::lock::atomic_int_type<size_t> coroutine_context_container< TALLOC >::ref_count_
private

stack allocator

Definition at line 195 of file coroutine_context_container.h.

Referenced by coroutine_context_container< TALLOC >::use_count().

◆ runner_

callback_type coroutine_context_base::runner_
privateinherited

flags

Definition at line 106 of file coroutine_context_base.h.

◆ runner_ret_code_

int coroutine_context_base::runner_ret_code_
privateinherited

Definition at line 104 of file coroutine_context_base.h.

◆ status_

LIBCOPP_COPP_NAMESPACE_ID::util::lock::atomic_int_type<int> coroutine_context_base::status_
privateinherited

Definition at line 114 of file coroutine_context_base.h.

Referenced by coroutine_context::start().


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