
#include <queue>
using namespace std;

#include <libsaria/callback.h>
#include "idle.h"

static deque<IdleTask *> idle_queue;

namespace libsaria
{

	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();
	}

	/*
	 * 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();
	}

};
