gem5  v20.1.0.0
Classes | Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
m5::Coroutine< Arg, Ret > Class Template Reference

This template defines a Coroutine wrapper type with a Boost-like interface. More...

#include <coroutine.hh>

Inheritance diagram for m5::Coroutine< Arg, Ret >:
Fiber

Classes

class  CallerType
 CallerType: A reference to an object of this class will be passed to the coroutine task. More...
 
struct  Empty
 

Public Member Functions

 Coroutine (std::function< void(CallerType &)> f, bool run_coroutine=true)
 Coroutine constructor. More...
 
virtual ~Coroutine ()
 
template<typename T = Arg>
Coroutineoperator() (typename std::enable_if< !std::is_same< T, void >::value, T >::type param)
 Coroutine interface. More...
 
template<typename T = Arg>
std::enable_if< std::is_same< T, void >::value, Coroutine >::typeoperator() ()
 operator() is the way we can jump inside the coroutine. More...
 
template<typename T = Ret>
std::enable_if<!std::is_same< T, void >::value, T >::type get ()
 get() is the way we can extrapolate return values (yielded) from the coroutine. More...
 
 operator bool () const
 Check if coroutine is still running. More...
 
 Coroutine ()=delete
 
 Coroutine (const Coroutine &rhs)=delete
 
Coroutineoperator= (const Coroutine &rhs)=delete
 
- Public Member Functions inherited from Fiber
virtual ~Fiber ()
 
void run ()
 Start executing the fiber represented by this object. More...
 
bool finished () const
 Returns whether the "main" function of this fiber has finished. More...
 
bool started () const
 Returns whether the "main" function of this fiber has started. More...
 
 Fiber (size_t stack_size=DefaultStackSize)
 
 Fiber (Fiber *link, size_t stack_size=DefaultStackSize)
 

Private Types

using ArgChannel = typename std::conditional< std::is_same< Arg, void >::value, Empty, std::stack< Arg > >::type
 
using RetChannel = typename std::conditional< std::is_same< Ret, void >::value, Empty, std::stack< Ret > >::type
 

Private Member Functions

void main () override
 Overriding base (Fiber) main. More...
 
void call ()
 

Private Attributes

ArgChannel argsChannel
 Arguments for the coroutine. More...
 
std::function< void(CallerType &)> task
 Coroutine task. More...
 
CallerType caller
 Coroutine caller. More...
 

Additional Inherited Members

- Static Public Member Functions inherited from Fiber
static FibercurrentFiber ()
 Get a pointer to the current running Fiber. More...
 
static FiberprimaryFiber ()
 Get a pointer to the primary Fiber. More...
 
- Static Public Attributes inherited from Fiber
const static size_t DefaultStackSize = 0x50000
 
- Protected Member Functions inherited from Fiber
void setStarted ()
 

Detailed Description

template<typename Arg, typename Ret>
class m5::Coroutine< Arg, Ret >

This template defines a Coroutine wrapper type with a Boost-like interface.

It is built on top of the gem5 fiber class. The two template parameters (Arg and Ret) are the coroutine argument and coroutine return types which are passed between the coroutine and the caller via operator() and get() method. This implementation doesn't support passing multiple values, so a tuple must be used in that scenario.

Most methods are templatized since it is relevant to distinguish the cases where one or both of the template parameters are void

Definition at line 62 of file coroutine.hh.

Member Typedef Documentation

◆ ArgChannel

template<typename Arg , typename Ret >
using m5::Coroutine< Arg, Ret >::ArgChannel = typename std::conditional< std::is_same<Arg, void>::value, Empty, std::stack<Arg> >::type
private

Definition at line 70 of file coroutine.hh.

◆ RetChannel

template<typename Arg , typename Ret >
using m5::Coroutine< Arg, Ret >::RetChannel = typename std::conditional< std::is_same<Ret, void>::value, Empty, std::stack<Ret> >::type
private

Definition at line 73 of file coroutine.hh.

Member Function Documentation

◆ call()

template<typename Arg , typename Ret >
void m5::Coroutine< Arg, Ret >::call ( )
inlineprivate

◆ main()

template<typename Arg , typename Ret >
void m5::Coroutine< Arg, Ret >::main ( )
inlineoverrideprivatevirtual

Overriding base (Fiber) main.

This method will be automatically called by the Fiber running engine and it is a simple wrapper for the task that the coroutine is supposed to run.

Implements Fiber.

Definition at line 275 of file coroutine.hh.

References m5::Coroutine< Arg, Ret >::caller, and m5::Coroutine< Arg, Ret >::task.

Member Data Documentation

◆ argsChannel

template<typename Arg , typename Ret >
ArgChannel m5::Coroutine< Arg, Ret >::argsChannel
private

Arguments for the coroutine.

Definition at line 286 of file coroutine.hh.

Referenced by m5::Coroutine< Arg, Ret >::CallerType::get(), and m5::Coroutine< Arg, Ret >::operator()().

◆ caller

template<typename Arg , typename Ret >
CallerType m5::Coroutine< Arg, Ret >::caller
private

◆ task

template<typename Arg , typename Ret >
std::function<void(CallerType&)> m5::Coroutine< Arg, Ret >::task
private

Coroutine task.

Definition at line 289 of file coroutine.hh.

Referenced by m5::Coroutine< Arg, Ret >::main().


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

Generated on Wed Sep 30 2020 14:03:06 for gem5 by doxygen 1.8.17