libsaria: Create an idle namespace

This cleans up the idle task code a lot, and reduces the size of
ocarina.bin to less than 1MB (with full debugging).  It probably won't
stay there long...
This commit is contained in:
Bryan Schumaker 2011-10-27 15:07:16 -04:00
parent c8dc6e1ee8
commit 707e7b3427
5 changed files with 57 additions and 90 deletions

View File

@ -8,15 +8,16 @@ class IdleTask
virtual ~IdleTask() = 0;
virtual void run_task() = 0;
int queue();
int queue_front();
void queue();
void queue_front();
};
namespace libsaria
{
int idle_task();
int queue_task(IdleTask *);
int queue_task_front(IdleTask *);
}
namespace idle
{
int run_task();
};
};
#endif /* LIBSARIA_IDLE_H */

View File

@ -1,37 +0,0 @@
#include <libsaria/callback.h>
#include "idle/idle.h"
static Idle idle;
int libsaria::idle_task()
{
return idle.run_task();
}
/*
* This will queue a pre-constructed idle task
* It should be constructed through `new` before calling
* and will be deleted by the idle queue once run.
*
* IDLE_TASK_QUEUED will only be triggered when the queue
* goes from size is increased from 0 to 1. This will tell
* the UI that idle tasks have been queued.
*/
static int queue_task_generic(IdleTask *task, bool front)
{
int queue_size = idle.queue_task(task, front);
if (queue_size == 1)
trigger_callback(IDLE_TASK_QUEUED);
return queue_size;
}
int libsaria::queue_task(IdleTask *task)
{
return queue_task_generic(task, false);
}
int libsaria::queue_task_front(IdleTask *task)
{
return queue_task_generic(task, true);
}

View File

@ -1,31 +1,41 @@
#include <queue>
using namespace std;
#include <libsaria/callback.h>
#include "idle.h"
Idle::Idle()
{
}
static deque<IdleTask *> idle_queue;
Idle::~Idle()
namespace libsaria
{
}
int Idle::run_task()
{
IdleTask *task;
if (idle_queue.size() == 0)
int idle::run_task()
{
IdleTask *task;
if (idle_queue.size() == 0)
return idle_queue.size();
task = idle_queue.front();
idle_queue.pop_front();
task->run_task();
delete task;
return idle_queue.size();
task = idle_queue.front();
idle_queue.pop_front();
task->run_task();
delete task;
return idle_queue.size();
}
}
int Idle::queue_task(IdleTask *task, bool front)
{
if (front == true)
idle_queue.push_front(task);
else
idle_queue.push_back(task);
return idle_queue.size();
}
/*
* IDLE_TASK_QUEUED will only be triggered when the queue
* size is increased from 0 to 1. This will tell the UI
* that idle tasks have been queued.
*/
int idle::queue_task(IdleTask *task, bool front)
{
if (front == true)
idle_queue.push_front(task);
else
idle_queue.push_back(task);
if (idle_queue.size() == 1)
trigger_callback(IDLE_TASK_QUEUED);
return idle_queue.size();
}
};

View File

@ -1,22 +1,14 @@
#ifndef LIBSARIA_IDLE_COMPONENT_H
#define LIBSARIA_IDLE_COMPONENT_H
#include <queue>
using namespace std;
#include <libsaria/idle.h>
class Idle
namespace libsaria
{
private:
deque<IdleTask*> idle_queue;
public:
Idle();
~Idle();
int run_task();
namespace idle
{
int queue_task(IdleTask *, bool);
};
};
#endif /* LIBSARIA_IDLE_COMPONENT_H */

View File

@ -1,20 +1,21 @@
#include <libsaria/idle.h>
#include "idle.h"
IdleTask::IdleTask()
/*
* The IdleTask should be constructed through `new` and not
* allocated on the stack since it will be deleted by the
* idle queue once it has run.
*/
IdleTask::IdleTask() {}
IdleTask::~IdleTask() {}
void IdleTask::queue()
{
libsaria::idle::queue_task(this, false);
}
IdleTask::~IdleTask()
void IdleTask::queue_front()
{
}
int IdleTask::queue()
{
return libsaria::queue_task(this);
}
int IdleTask::queue_front()
{
return libsaria::queue_task_front(this);
libsaria::idle::queue_task(this, true);
}