MEL
Microthread & Execution library
mel::tasking::GenericProcess Class Reference

A Process constructed from a functor with signature EGenericProcessResult(uint64_t,Process)*. More...

#include <GenericProcess.h>

Inheritance diagram for mel::tasking::GenericProcess:
mel::tasking::Process

Public Types

typedef std::function< EGenericProcessResult(uint64_t, Process *)> GenericCallback
 
enum  EProcessState : uint8_t {
  PREPARED , INITIATED , ASLEEP , PREPARED_TO_DIE ,
  TRYING_TO_KILL , KILLING_WAITING_FOR_SCHEDULED , DEAD
}
 
enum class  ESwitchResult { ESWITCH_OK , ESWITCH_WAKEUP , ESWITCH_ERROR , ESWITCH_KILL }
 Reason why the process returns from context switch. More...
 

Public Member Functions

 GenericProcess ()
 
template<class F >
void setProcessCallback (F &&functor)
 Set callable to be executed on process update. More...
 
const GenericProcess::GenericCallback & getProcessCallback () const
 
template<class F >
void setKillCallback (F &&functor)
 
void setProcessScheduler (ProcessScheduler *const mgr)
 Sets ProcessScheduler which holds this process. Each process can only be scheduled by one ProcessScheduler.
 
void pause ()
 pause execution until wakeup called onPause is called in order children can do custom behaviour
 
void kill (bool force=false)
 Mark this process to be eliminated by the process manager. More...
 
virtual void reset ()
 Sets process in init state.
 
void setPeriod (unsigned int value)
 Set the period for this process. More...
 
EProcessState getState () const
 
bool getDead () const
 
unsigned int getPeriod () const
 get period (milliseconds) More...
 
unsigned int getElapsedTime () const
 
uint64_t getLastUpdateTime () const
 
void resetTime ()
 
ProcessScheduler *const getProcessScheduler () const
 Gets ProcessScheduler which holds this Process.
 
bool getAsleep () const
 
void wakeUp ()
 wakeup an asleep process or an evicted process (that process having called swtich or wait)
 

Static Public Member Functions

static ESwitchResult wait (unsigned int msegs) OPTIMIZE_FLAGS
 
static ESwitchResult switchProcess (bool continueInmediately) OPTIMIZE_FLAGS
 Evict current process and will continue executing from the same point the next time is scheduled. More...
 
template<class F >
static ESwitchResult sleepAndDo (F postSleep)
 Sleep current process and execute given callable. To reactivate to you must use wakeUp. More...
 
template<class F >
static ESwitchResult waitAndDo (unsigned int msegs, F postWait) OPTIMIZE_FLAGS
 Wait for a given time and execute given callable. More...
 
static ESwitchResult sleep () OPTIMIZE_FLAGS
 pause current process. To reactivate you must use wakeUp More...
 

Protected Member Functions

void onInit (uint64_t msegs) override
 
bool onKill () override
 
void onUpdate (uint64_t msegs) override
 
virtual void onPause ()
 Called when a process is paused.
 
virtual void onWakeUp ()
 Called when process is woken up.
 
virtual void killDone ()
 

Detailed Description

A Process constructed from a functor with signature EGenericProcessResult(uint64_t,Process)*.

This process it's executed until given functor returns EGenericProcessResult::KILL.

Member Enumeration Documentation

◆ EProcessState

enum mel::tasking::Process::EProcessState : uint8_t
inherited
Enumerator
PREPARED 

Process created but not executed.

INITIATED 

executing process normally

ASLEEP 

sleeping. Waiting for a wakeup

PREPARED_TO_DIE 

it's going to die, but process manager doesn't discard it yet

TRYING_TO_KILL 

sending kill signal but no accepted yet

KILLING_WAITING_FOR_SCHEDULED 

switched and no shceduled yet

DEAD 

process is out of process manager

◆ ESwitchResult

Reason why the process returns from context switch.

Enumerator
ESWITCH_OK 

return from context switch was ok

ESWITCH_WAKEUP 

return from context switch was because a wakeup

ESWITCH_ERROR 

switch couldn't be done

ESWITCH_KILL 

return from context switch because a kill

Constructor & Destructor Documentation

◆ GenericProcess()

mel::tasking::GenericProcess::GenericProcess ( )

constructor

Member Function Documentation

◆ getDead()

bool mel::tasking::Process::getDead ( ) const
inlineinherited

check if process is dead

◆ getElapsedTime()

unsigned int mel::tasking::Process::getElapsedTime ( ) const
inherited

returns time elapsed during this iteration

◆ getLastUpdateTime()

uint64_t mel::tasking::Process::getLastUpdateTime ( ) const
inlineinherited
Returns
time when task was executed in last iteration, when onUpdate is called:
Note
context switches or waits inside code doesn't modify update time.

◆ getPeriod()

unsigned int mel::tasking::Process::getPeriod ( ) const
inlineinherited

get period (milliseconds)

check if process is going to die

◆ kill()

void mel::tasking::Process::kill ( bool  force = false)
inherited

Mark this process to be eliminated by the process manager.

Internally, kill calls virtual function Process::onKill, which returns true if kill can be acomplished or not. In case of true, then process is put in PREPARED_TO_DIE state, so the scheduler can remove it. In case of false, process is put in a TRYING_TO_KILL state so scheduler will continue to trying to kill it in next iterations. You can know if a process is die checking getDead

Remarks
if process is not already inited (state == PREPARED) then onKill is not called
Parameters
[in]forceif true, then killing is without regarding previous explained process, so Process go inmediately to PREPARED_TO_DIE
Remarks
not multithread safe, kill should be done in the owner thread context (se abordar� en el futuro)

◆ killDone()

virtual void mel::tasking::Process::killDone ( )
inlineprotectedvirtualinherited

called when kill is definitively done

◆ onInit()

void mel::tasking::GenericProcess::onInit ( uint64_t  msecs)
overrideprotectedvirtual

called when process inits

Parameters
msecsmilliseconds

Reimplemented from mel::tasking::Process.

◆ onKill()

bool mel::tasking::GenericProcess::onKill ( )
overrideprotectedvirtual

overridden from Process

Reimplemented from mel::tasking::Process.

◆ onUpdate()

void mel::tasking::GenericProcess::onUpdate ( uint64_t  msegs)
overrideprotectedvirtual

overridden from Process

Parameters
msegsmsegs
Remarks
If callback method return false. Process is killed.

Implements mel::tasking::Process.

◆ setPeriod()

void mel::tasking::Process::setPeriod ( unsigned int  value)
inlineinherited

Set the period for this process.

Parameters
valuethe new period (in msecs)

◆ setProcessCallback()

template<class F >
void mel::tasking::GenericProcess::setProcessCallback ( F &&  functor)

Set callable to be executed on process update.

Parameters
[in]functorA functor with signature EGenericProcessResult(uint64_t,Process*)

◆ sleep()

static ESwitchResult mel::tasking::Process::sleep ( )
staticinherited

pause current process. To reactivate you must use wakeUp

Returns
true if process received kill signal
See also
switchProcess for comments

◆ sleepAndDo()

template<class F >
Process::ESwitchResult mel::tasking::Process::sleepAndDo ( postSleep)
staticinherited

Sleep current process and execute given callable. To reactivate to you must use wakeUp.

Parameters
[in]postSleepfunctor (signature void(void) ) to execute just in the moment when Process go to sleep
Returns
resulting state of these operation
See also
sleep
Remarks
not multithread-safe

◆ switchProcess()

static ESwitchResult mel::tasking::Process::switchProcess ( bool  continueInmediately)
staticinherited

Evict current process and will continue executing from the same point the next time is scheduled.

this function is a static one because, in the same way threads work, is the currently executing process which can be evicted in any point of the execution call stack.

Parameters
continueInmediatelyif true, the process will continue executing in the next shceduler cycle, so without respecting the period.

◆ wait()

static ESwitchResult mel::tasking::Process::wait ( unsigned int  msegs)
staticinherited
Returns
true if process received kill signal
See also
switchProcess for comments

◆ waitAndDo()

template<class F >
Process::ESwitchResult mel::tasking::Process::waitAndDo ( unsigned int  msegs,
postWait 
)
staticinherited

Wait for a given time and execute given callable.

Parameters
[in]postWaitfunctor (signature <void,void>) to execute just in the moment when Process go to sleep
Returns
resulting state of the operation
See also
sleep
Remarks
not multithread-safe

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