2014-10-17 09:56:54 -04:00
|
|
|
/**
|
2013-09-01 10:55:13 -04:00
|
|
|
* Copyright 2013 (c) Anna Schumaker.
|
|
|
|
*/
|
2014-06-21 09:37:47 -04:00
|
|
|
#ifndef OCARINA_CORE_IDLE_H
|
|
|
|
#define OCARINA_CORE_IDLE_H
|
2013-09-01 10:55:13 -04:00
|
|
|
|
2014-10-17 09:56:54 -04:00
|
|
|
/**
|
2014-11-08 11:55:22 -05:00
|
|
|
* The idle queue is used to schedule function calls to run at a
|
|
|
|
* later time. It is expected that a higher layer can determine
|
|
|
|
* when the application is idle and call idle::run_task() accordingly.
|
|
|
|
*
|
|
|
|
* The idle layer keeps a count of the number of tasks added to the queue
|
|
|
|
* since the last time the queue was empty. Whenever a task is scheduled,
|
|
|
|
* the "queued" count is incremented by 1. When tasks are run, the "serviced"
|
|
|
|
* count is incremented by 1. These counters are reset to 0 once the queue
|
|
|
|
* is empty.
|
2014-10-17 09:56:54 -04:00
|
|
|
*/
|
2013-09-01 10:55:13 -04:00
|
|
|
namespace idle
|
|
|
|
{
|
|
|
|
|
2014-10-17 09:56:54 -04:00
|
|
|
/**
|
2014-11-08 11:55:22 -05:00
|
|
|
* Base class used by the templated IdleTask class. This lets
|
|
|
|
* us create an idle queue storing IdleBase pointers to get
|
|
|
|
* around potential templating issues.
|
2014-10-17 09:56:54 -04:00
|
|
|
*/
|
2013-09-01 10:55:13 -04:00
|
|
|
class IdleBase {
|
|
|
|
protected:
|
2014-10-17 09:56:54 -04:00
|
|
|
/**
|
2014-11-08 11:55:22 -05:00
|
|
|
* Adds the IdleBase to the idle queue and increments
|
|
|
|
* the "queued" counter by 1.
|
2014-10-17 09:56:54 -04:00
|
|
|
*/
|
2013-09-01 10:55:13 -04:00
|
|
|
void schedule();
|
2014-03-16 13:09:33 -04:00
|
|
|
|
2013-09-01 10:55:13 -04:00
|
|
|
public:
|
2014-11-08 11:55:22 -05:00
|
|
|
IdleBase(); /**< IdleBase constructor. */
|
|
|
|
virtual ~IdleBase(); /**< IdleBase destructor. */
|
|
|
|
virtual void run() = 0; /**< Run the idle task. */
|
2013-09-01 10:55:13 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-10-17 09:56:54 -04:00
|
|
|
/**
|
2014-11-08 11:55:22 -05:00
|
|
|
* Generic IdleTask class. This class is templated to allow
|
|
|
|
* for scheduling functions that take different types of data
|
|
|
|
* parameters.
|
2014-10-17 09:56:54 -04:00
|
|
|
*/
|
2013-09-01 10:55:13 -04:00
|
|
|
template <class T>
|
|
|
|
class IdleTask : public IdleBase {
|
|
|
|
private:
|
2014-11-08 11:55:22 -05:00
|
|
|
void (*_func)(T &); /**< Function to call when run. */
|
|
|
|
T _data; /**< Data to pass to the function. */
|
2014-03-16 13:09:33 -04:00
|
|
|
|
2013-09-01 10:55:13 -04:00
|
|
|
public:
|
2014-10-17 09:56:54 -04:00
|
|
|
/**
|
|
|
|
* IdleTask constructor.
|
2014-11-08 11:55:22 -05:00
|
|
|
*
|
2014-10-17 09:56:54 -04:00
|
|
|
* @param func Function to call when running this task.
|
|
|
|
* @param data Data to pass to the function.
|
|
|
|
*/
|
2013-09-01 10:55:13 -04:00
|
|
|
IdleTask(void (*)(T &), T);
|
2014-10-17 09:56:54 -04:00
|
|
|
|
2014-11-08 11:55:22 -05:00
|
|
|
~IdleTask(); /**< IdleTask destructor. */
|
|
|
|
void run(); /**< Call func(), passing data as the argument. */
|
2013-09-01 10:55:13 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-10-17 09:56:54 -04:00
|
|
|
/**
|
2014-11-08 11:55:22 -05:00
|
|
|
* Creates a new IdleTask and adds it to the queue.
|
|
|
|
*
|
2014-10-17 09:56:54 -04:00
|
|
|
* @param func Function to call when running the new task.
|
|
|
|
* @param data Data to pass to the function.
|
|
|
|
*/
|
2013-09-01 10:55:13 -04:00
|
|
|
template <class T>
|
2014-11-08 11:55:22 -05:00
|
|
|
static inline void schedule(void (*func)(T &), T data)
|
2013-09-01 10:55:13 -04:00
|
|
|
{
|
2014-11-08 11:55:22 -05:00
|
|
|
new IdleTask<T>(func, data);
|
2013-09-01 10:55:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2015-11-10 13:27:32 -05:00
|
|
|
|
2015-11-10 13:56:54 -05:00
|
|
|
/*
|
|
|
|
* Called to run the next task on the idle queue.
|
|
|
|
* Returns true if there are more tasks to run.
|
|
|
|
*/
|
|
|
|
bool idle_run_task();
|
|
|
|
|
2015-11-10 13:27:32 -05:00
|
|
|
/* Called to find the percentage of idle tasks that have been run. */
|
|
|
|
float idle_progress();
|
|
|
|
|
2013-09-01 10:55:13 -04:00
|
|
|
#include "idle.hpp"
|
|
|
|
|
2014-06-21 09:37:47 -04:00
|
|
|
#endif /* OCARINA_CORE_IDLE_H */
|