core/idle: Convert file to C

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-11-10 14:18:11 -05:00
parent 284e3466f4
commit ab837b1a18
8 changed files with 19 additions and 14 deletions

View File

@ -1,4 +1,4 @@
/**
/*
* Copyright 2013 (c) Anna Schumaker.
*/
#include <core/idle.h>
@ -18,7 +18,7 @@ static float serviced = 0.0;
void idle_schedule(void (*func)(void *), void *data)
{
struct idle_task *task = new idle_task;
struct idle_task *task = g_malloc(sizeof(struct idle_task));
task->idle_func = func;
task->idle_data = data;
@ -30,10 +30,10 @@ bool idle_run_task()
{
struct idle_task *task;
if (g_queue_get_length(&idle_queue) > 0) {
task = (struct idle_task *)g_queue_pop_head(&idle_queue);
if (!g_queue_is_empty(&idle_queue)) {
task = g_queue_pop_head(&idle_queue);
task->idle_func(task->idle_data);
delete task;
g_free(task);
serviced++;
}

View File

@ -1,7 +1,9 @@
/**
* Copyright 2013 (c) Anna Schumaker.
*/
extern "C" {
#include <core/idle.h>
}
#include <core/library.h>
#include <core/print.h>

View File

@ -3,8 +3,8 @@
*/
extern "C" {
#include <core/database.h>
}
#include <core/idle.h>
}
#include <core/index.h>
#include <core/library.h>
#include <core/playlist.h>

View File

@ -1,4 +1,4 @@
/**
/*
* Copyright 2013 (c) Anna Schumaker.
*
* The idle queue is used to schedule function calls to run at a
@ -13,6 +13,7 @@
*/
#ifndef OCARINA_CORE_IDLE_H
#define OCARINA_CORE_IDLE_H
#include <stdbool.h>
/* Called to schedule a function to run later. */
void idle_schedule(void (*)(void *), void *);

View File

@ -16,6 +16,7 @@
static inline gchar *stos(const char *s) { return g_strdup(s); }
static inline gchar *utos(unsigned int u) { return g_strdup_printf("%u", u); }
static inline gchar *itos(int i) { return g_strdup_printf("%i", i); }
static inline gchar *ftos(float f) { return g_strdup_printf("%f", f); }
static inline gchar *btos(bool b) { return g_strdup(b ? "true" : "false"); }
static inline gchar *ptos(void *p) { return g_strdup_printf("%p", p); }
@ -26,6 +27,7 @@ static inline gchar *ptos(void *p) { return g_strdup_printf("%p", p); }
bool: btos, \
unsigned int: utos, \
int: itos, \
float:ftos, \
void *:ptos \
) (x))
#endif /* __cplusplus */

View File

@ -22,7 +22,7 @@ res += [ CoreTest("set", "set.c") ]
res += [ CoreTest("database", "database.c") ]
res += [ CoreTest("index", "index.c") ]
res += [ CoreTest("filter", "filter.c") ]
res += [ CoreTest("idle", "idle.cpp") ]
res += [ CoreTest("idle", "idle.c") ]
res += SConscript("tags/Sconscript")
res += [ CoreTest("queue", "queue.cpp") ]

View File

@ -4,7 +4,7 @@
*/
#include <core/idle.h>
#include <core/string.h>
#include "test.h"
#include <tests/test.h>
static unsigned int cur = -1;
static bool func_passed = false;
@ -22,19 +22,19 @@ static void test_idle_queue(unsigned int n)
cur = -1;
test_equal(idle_progress(), (float)1.0);
test_equal(idle_run_task(), false);
test_equal(idle_run_task(), (bool)false);
for (unsigned int i = 0; i < n; i++)
idle_schedule(inc_cur, GINT_TO_POINTER(i));
test_equal(idle_progress(), (float)0.0);
for (unsigned int i = 0; i < (n - 1); i++) {
test_loop_equal(idle_run_task(), true, i);
test_loop_equal(idle_run_task(), (bool)true, i);
test_loop_equal(idle_progress(), ((i + 1) / (float)n), i);
test_loop_equal(func_passed, true, i);
test_loop_equal(func_passed, (bool)true, i);
} test_loop_passed();
test_equal(idle_run_task(), false);
test_equal(idle_run_task(), (bool)false);
test_equal(idle_progress(), (float)1.0);
}

View File

@ -3,8 +3,8 @@
*/
extern "C" {
#include <core/filter.h>
}
#include <core/idle.h>
}
#include <core/library.h>
#include <core/tags/tags.h>
#include "test.h"