libsaria: Remove old idle task stuff
I handle the idle task stuff inside the idle layer, hiding it from everything else. Signed-off-by: Bryan Schumaker <bjschuma@gmail.com>
This commit is contained in:
parent
1ac51775c3
commit
9043183807
|
@ -1,8 +1,6 @@
|
|||
#ifndef LIBSARIA_IDLE_H
|
||||
#define LIBSARIA_IDLE_H
|
||||
|
||||
#include <task.h>
|
||||
|
||||
namespace libsaria
|
||||
{
|
||||
|
||||
|
@ -13,7 +11,6 @@ namespace libsaria
|
|||
int run_task();
|
||||
void schedule(void (*)());
|
||||
void schedule(void (*)(void *), void *);
|
||||
void queue_task(IdleTask *, bool);
|
||||
void cancel_all(void *);
|
||||
float progress();
|
||||
|
||||
|
|
|
@ -1,32 +0,0 @@
|
|||
#ifndef LIBSARIA_IDLE_TASK_H
|
||||
#define LIBSARIA_IDLE_TASK_H
|
||||
|
||||
#include <fs.h>
|
||||
#include <cstddef>
|
||||
|
||||
class IdleTask
|
||||
{
|
||||
public:
|
||||
IdleTask();
|
||||
virtual ~IdleTask() = 0;
|
||||
virtual void run_task() = 0;
|
||||
virtual bool should_cancel(void *);
|
||||
|
||||
void queue();
|
||||
void queue_front();
|
||||
};
|
||||
|
||||
class WriteTask : public IdleTask
|
||||
{
|
||||
private:
|
||||
string filepath;
|
||||
void (*func)(ofstream &, void *);
|
||||
void *data;
|
||||
public:
|
||||
WriteTask(string, void (*)(ofstream &, void *), void *);
|
||||
~WriteTask();
|
||||
bool should_cancel(void *);
|
||||
void run_task();
|
||||
};
|
||||
|
||||
#endif /* LIBSARIA_IDLE_TASK_H */
|
|
@ -1,7 +1,6 @@
|
|||
// Copyright (c) 2012 Bryan Schumaker.
|
||||
#include <fs.h>
|
||||
#include <idle.h>
|
||||
#include <task.h>
|
||||
#include <print.h>
|
||||
|
||||
#include <dirent.h>
|
||||
|
@ -61,6 +60,24 @@ static void read_cb(void *d)
|
|||
delete data;
|
||||
}
|
||||
|
||||
struct WriteData
|
||||
{
|
||||
string filepath;
|
||||
void (*func)(ofstream &, void *);
|
||||
void *data;
|
||||
};
|
||||
|
||||
static void write_cb(void *d)
|
||||
{
|
||||
ofstream stream;
|
||||
struct WriteData *data = (struct WriteData *)d;
|
||||
|
||||
stream.open(data->filepath.c_str());
|
||||
data->func(stream, data->data);
|
||||
stream.close();
|
||||
delete data;
|
||||
}
|
||||
|
||||
namespace libsaria
|
||||
{
|
||||
|
||||
|
@ -140,9 +157,13 @@ namespace libsaria
|
|||
make_dir(appdir.c_str());
|
||||
}
|
||||
|
||||
void app::save(string file, void (*func)(ofstream &, void *), void *data)
|
||||
void app::save(string file, void (*func)(ofstream &, void *), void *d)
|
||||
{
|
||||
new WriteTask(appdir + "/" + file, func, data);
|
||||
struct WriteData *data = new struct WriteData;
|
||||
data->filepath = appdir + "/" + file;
|
||||
data->func = func;
|
||||
data->data = d;
|
||||
idle::schedule(write_cb, data);
|
||||
}
|
||||
|
||||
void app::read(string file, void (*func)(ifstream &))
|
||||
|
|
|
@ -6,11 +6,7 @@
|
|||
#include <queue>
|
||||
using namespace std;
|
||||
|
||||
static deque<IdleTask *> idle_queue;
|
||||
static float queued = 0.0;
|
||||
static float serviced = 0.0;
|
||||
|
||||
class GIdleTask : public IdleTask
|
||||
class IdleTask
|
||||
{
|
||||
private:
|
||||
void (*func)();
|
||||
|
@ -18,28 +14,36 @@ class GIdleTask : public IdleTask
|
|||
void *data;
|
||||
|
||||
public:
|
||||
GIdleTask(void (*)());
|
||||
GIdleTask(void (*)(void *), void *);
|
||||
~GIdleTask();
|
||||
IdleTask(void (*)());
|
||||
IdleTask(void (*)(void *), void *);
|
||||
~IdleTask();
|
||||
void run_task();
|
||||
bool should_cancel(void *);
|
||||
};
|
||||
|
||||
GIdleTask::GIdleTask(void (*f)())
|
||||
static deque<IdleTask *> idle_queue;
|
||||
static float queued = 0.0;
|
||||
static float serviced = 0.0;
|
||||
|
||||
IdleTask::IdleTask(void (*f)())
|
||||
{
|
||||
func = f;
|
||||
func2 = NULL;
|
||||
data = NULL;
|
||||
}
|
||||
|
||||
GIdleTask::GIdleTask(void (*f)(void *), void *d)
|
||||
IdleTask::IdleTask(void (*f)(void *), void *d)
|
||||
{
|
||||
func = NULL;
|
||||
func2 = f;
|
||||
data = d;
|
||||
}
|
||||
|
||||
GIdleTask::~GIdleTask()
|
||||
IdleTask::~IdleTask()
|
||||
{
|
||||
}
|
||||
|
||||
void GIdleTask::run_task()
|
||||
void IdleTask::run_task()
|
||||
{
|
||||
if (func)
|
||||
func();
|
||||
|
@ -47,12 +51,26 @@ void GIdleTask::run_task()
|
|||
func2(data);
|
||||
}
|
||||
|
||||
bool IdleTask::should_cancel(void *d)
|
||||
{
|
||||
if (data && (data == d))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
static void do_task(IdleTask *task)
|
||||
{
|
||||
task->run_task();
|
||||
delete task;
|
||||
}
|
||||
|
||||
static void queue_task(IdleTask *task)
|
||||
{
|
||||
idle_queue.push_front(task);
|
||||
queued += 1.0;
|
||||
libsaria::notify(IDLE_ADD, NULL);
|
||||
}
|
||||
|
||||
namespace libsaria
|
||||
{
|
||||
|
||||
|
@ -70,14 +88,14 @@ namespace libsaria
|
|||
|
||||
void idle::schedule(void (*func)())
|
||||
{
|
||||
GIdleTask *task = new GIdleTask(func);
|
||||
queue_task(task, false);
|
||||
IdleTask *task = new IdleTask(func);
|
||||
queue_task(task);
|
||||
}
|
||||
|
||||
void idle::schedule(void (*func)(void *), void *data)
|
||||
{
|
||||
GIdleTask *task = new GIdleTask(func, data);
|
||||
queue_task(task, false);
|
||||
IdleTask *task = new IdleTask(func, data);
|
||||
queue_task(task);
|
||||
}
|
||||
|
||||
int idle::run_task()
|
||||
|
@ -96,21 +114,6 @@ namespace libsaria
|
|||
return 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.
|
||||
*/
|
||||
void idle::queue_task(IdleTask *task, bool front)
|
||||
{
|
||||
if (front == true)
|
||||
idle_queue.push_front(task);
|
||||
else
|
||||
idle_queue.push_back(task);
|
||||
queued += 1.0;
|
||||
notify(IDLE_ADD, NULL);
|
||||
}
|
||||
|
||||
void idle::cancel_all(void *data)
|
||||
{
|
||||
deque<IdleTask *>::iterator it;
|
||||
|
|
|
@ -1,31 +0,0 @@
|
|||
// Copyright (c) 2012 Bryan Schumaker.
|
||||
#include <task.h>
|
||||
|
||||
#include <fstream>
|
||||
using namespace std;
|
||||
|
||||
WriteTask::WriteTask(string file, void (*fn)(ofstream &, void *), void *d)
|
||||
{
|
||||
filepath = file;
|
||||
func = fn;
|
||||
data = d;
|
||||
|
||||
queue();
|
||||
}
|
||||
|
||||
WriteTask::~WriteTask()
|
||||
{
|
||||
}
|
||||
|
||||
void WriteTask::run_task()
|
||||
{
|
||||
ofstream stream;
|
||||
stream.open(filepath.c_str());
|
||||
func(stream, data);
|
||||
stream.close();
|
||||
}
|
||||
|
||||
bool WriteTask::should_cancel(void *d)
|
||||
{
|
||||
return d == data;
|
||||
}
|
|
@ -1,25 +0,0 @@
|
|||
// Copyright (c) 2011 Bryan Schumaker.
|
||||
#include <idle.h>
|
||||
|
||||
/*
|
||||
* 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);
|
||||
}
|
||||
|
||||
void IdleTask::queue_front()
|
||||
{
|
||||
libsaria::idle::queue_task(this, true);
|
||||
}
|
||||
|
||||
bool IdleTask::should_cancel(void *data)
|
||||
{
|
||||
return false;
|
||||
}
|
|
@ -10,34 +10,13 @@
|
|||
|
||||
static unsigned int MAX_SCAN = 20;
|
||||
|
||||
/*
|
||||
* Scan the library path and remove files that don't exist anymore
|
||||
*/
|
||||
class ValidateTask : public IdleTask
|
||||
{
|
||||
private:
|
||||
struct libsaria::library::Path *path;
|
||||
|
||||
public:
|
||||
ValidateTask(struct libsaria::library::Path *);
|
||||
~ValidateTask();
|
||||
void run_task();
|
||||
};
|
||||
|
||||
ValidateTask::ValidateTask(struct libsaria::library::Path *p)
|
||||
{
|
||||
path = p;
|
||||
}
|
||||
|
||||
ValidateTask::~ValidateTask()
|
||||
{
|
||||
}
|
||||
|
||||
void ValidateTask::run_task()
|
||||
static void validate_path(void *d)
|
||||
{
|
||||
struct libsaria::library::Path *path;
|
||||
libsaria::Track *track;
|
||||
unsigned int i = 0;
|
||||
|
||||
path = (struct libsaria::library::Path *)d;
|
||||
while (i < path->tracks.size()) {
|
||||
track = &path->tracks[i];
|
||||
if (!libsaria::exists((*track)[TRACK_FILEPATH])) {
|
||||
|
@ -52,26 +31,14 @@ void ValidateTask::run_task()
|
|||
save_path(path);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Tag each file in file_list and add to the library path
|
||||
*/
|
||||
class ScanTask : public IdleTask
|
||||
{
|
||||
private:
|
||||
struct libsaria::library::Path *path;
|
||||
list<string> file_list;
|
||||
|
||||
public:
|
||||
ScanTask(struct libsaria::library::Path *, list<string> &);
|
||||
~ScanTask();
|
||||
void run_task();
|
||||
struct ScanData {
|
||||
struct libsaria::library::Path *path;
|
||||
list<string> files;
|
||||
};
|
||||
|
||||
ScanTask::ScanTask(struct libsaria::library::Path *p, list<string> &files)
|
||||
static void setup_scan_data(struct ScanData *scan, list<string> &files)
|
||||
{
|
||||
list<string>::iterator it;
|
||||
path = p;
|
||||
|
||||
if (files.size() <= MAX_SCAN)
|
||||
it = files.end();
|
||||
|
@ -81,11 +48,7 @@ ScanTask::ScanTask(struct libsaria::library::Path *p, list<string> &files)
|
|||
it++;
|
||||
}
|
||||
|
||||
file_list.splice(file_list.begin(), files, files.begin(), it);
|
||||
}
|
||||
|
||||
ScanTask::~ScanTask()
|
||||
{
|
||||
scan->files.splice(scan->files.begin(), files, files.begin(), it);
|
||||
}
|
||||
|
||||
static bool find_file(libsaria::library::Path *path, string &file)
|
||||
|
@ -97,11 +60,13 @@ static bool find_file(libsaria::library::Path *path, string &file)
|
|||
return false;
|
||||
}
|
||||
|
||||
void ScanTask::run_task()
|
||||
static void scan_path(void *data)
|
||||
{
|
||||
struct ScanData *scan = (struct ScanData *)data;
|
||||
list<string>::iterator it;
|
||||
libsaria::library::Path *path = scan->path;
|
||||
|
||||
for (it = file_list.begin(); it != file_list.end(); it++) {
|
||||
for (it = scan->files.begin(); it != scan->files.end(); it++) {
|
||||
/*
|
||||
* TODO: What I really should do for this check is build up
|
||||
* a path component tree right before scanning, and then walk
|
||||
|
@ -120,46 +85,28 @@ void ScanTask::run_task()
|
|||
}
|
||||
|
||||
libsaria::notify(PATH_UPDATED, path);
|
||||
delete scan;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Read a directory and create ScanTasks for chunks of files
|
||||
*/
|
||||
class ReaddirTask : public IdleTask
|
||||
{
|
||||
private:
|
||||
struct libsaria::library::Path *path;
|
||||
|
||||
public:
|
||||
ReaddirTask(struct libsaria::library::Path *path);
|
||||
~ReaddirTask();
|
||||
void run_task();
|
||||
};
|
||||
|
||||
ReaddirTask::ReaddirTask(struct libsaria::library::Path *p)
|
||||
{
|
||||
path = p;
|
||||
}
|
||||
|
||||
ReaddirTask::~ReaddirTask()
|
||||
{
|
||||
}
|
||||
|
||||
void ReaddirTask::run_task()
|
||||
static void update_path(void *d)
|
||||
{
|
||||
struct libsaria::library::Path *path;
|
||||
list<string> file_list;
|
||||
list<string>::iterator it;
|
||||
unsigned int i;
|
||||
ScanTask *task;
|
||||
struct ScanData *data;
|
||||
|
||||
path = (struct libsaria::library::Path *)d;
|
||||
libsaria::list_dir(path->path, file_list);
|
||||
println("Found: %d files to scan", file_list.size());
|
||||
|
||||
i = 0;
|
||||
while (file_list.size() > 0) {
|
||||
task = new ScanTask(path, file_list);
|
||||
task->queue();
|
||||
data = new ScanData;
|
||||
data->path = path;
|
||||
setup_scan_data(data, file_list);
|
||||
libsaria::idle::schedule(scan_path, data);
|
||||
if ((i++ % 50) == 0)
|
||||
save_path(path);
|
||||
}
|
||||
|
@ -175,8 +122,6 @@ void do_update_path(struct libsaria::library::Path *path)
|
|||
return;
|
||||
}
|
||||
|
||||
ValidateTask *validate = new ValidateTask(path);
|
||||
ReaddirTask *task = new ReaddirTask(path);
|
||||
validate->queue();
|
||||
task->queue();
|
||||
libsaria::idle::schedule(validate_path, path);
|
||||
libsaria::idle::schedule(update_path, path);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue