Refactor task_database.c

This commit is contained in:
twinaphex 2015-07-08 22:03:23 +02:00
parent c84ddb53fa
commit 52e2fd1689
5 changed files with 78 additions and 45 deletions

View File

@ -89,7 +89,12 @@ void rarch_main_data_free(void)
data_runloop_t *runloop = rarch_main_data_get_ptr(); data_runloop_t *runloop = rarch_main_data_get_ptr();
rarch_main_data_nbio_uninit(); rarch_main_data_nbio_uninit();
#ifdef HAVE_NETWORKING
rarch_main_data_http_uninit(); rarch_main_data_http_uninit();
#endif
#ifdef HAVE_LIBRETRODB
rarch_main_data_db_uninit();
#endif
if (runloop) if (runloop)
free(runloop); free(runloop);
@ -119,8 +124,7 @@ bool rarch_main_data_active(data_runloop_t *runloop)
bool active = false; bool active = false;
driver_t *driver = driver_get_ptr(); driver_t *driver = driver_get_ptr();
#ifdef HAVE_LIBRETRODB #ifdef HAVE_LIBRETRODB
database_info_handle_t *db = runloop ? runloop->db.handle : NULL; if (rarch_main_data_db_is_active())
if (db && db->status != DATABASE_STATUS_NONE)
active = true; active = true;
#endif #endif
@ -286,7 +290,12 @@ void rarch_main_data_clear_state(void)
return; return;
rarch_main_data_nbio_init(); rarch_main_data_nbio_init();
#ifdef HAVE_NETWORKING
rarch_main_data_http_init(); rarch_main_data_http_init();
#endif
#ifdef HAVE_LIBRETRODB
rarch_main_data_db_init();
#endif
} }

View File

@ -21,9 +21,6 @@
#ifdef HAVE_THREADS #ifdef HAVE_THREADS
#include <rthreads/rthreads.h> #include <rthreads/rthreads.h>
#endif #endif
#ifdef HAVE_LIBRETRODB
#include "database_info.h"
#endif
#include "tasks/tasks.h" #include "tasks/tasks.h"
#ifdef __cplusplus #ifdef __cplusplus
@ -42,33 +39,8 @@ enum runloop_data_type
DATA_TYPE_DB DATA_TYPE_DB
}; };
#ifdef HAVE_LIBRETRODB
typedef struct database_state_handle
{
database_info_list_t *info;
struct string_list *list;
size_t list_index;
size_t entry_index;
uint32_t crc;
uint8_t *buf;
char zip_name[PATH_MAX_LENGTH];
} database_state_handle_t;
typedef struct db_handle
{
database_state_handle_t state;
database_info_handle_t *handle;
msg_queue_t *msg_queue;
unsigned status;
} db_handle_t;
#endif
typedef struct data_runloop typedef struct data_runloop
{ {
#ifdef HAVE_LIBRETRODB
db_handle_t db;
#endif
bool inited; bool inited;
#ifdef HAVE_THREADS #ifdef HAVE_THREADS

View File

@ -16,11 +16,14 @@
#include <compat/strcasestr.h> #include <compat/strcasestr.h>
#include <compat/strl.h> #include <compat/strl.h>
#ifdef HAVE_LIBRETRODB
#include "../database_info.h"
#endif
#include "../dir_list_special.h" #include "../dir_list_special.h"
#include "../file_ops.h" #include "../file_ops.h"
#include "../msg_hash.h" #include "../msg_hash.h"
#include "../general.h" #include "../general.h"
#include "../runloop_data.h"
#include "tasks.h" #include "tasks.h"
#define CB_DB_SCAN_FILE 0x70ce56d2U #define CB_DB_SCAN_FILE 0x70ce56d2U
@ -28,6 +31,27 @@
#define HASH_EXTENSION_ZIP 0x0b88c7d8U #define HASH_EXTENSION_ZIP 0x0b88c7d8U
typedef struct database_state_handle
{
database_info_list_t *info;
struct string_list *list;
size_t list_index;
size_t entry_index;
uint32_t crc;
uint8_t *buf;
char zip_name[PATH_MAX_LENGTH];
} database_state_handle_t;
typedef struct db_handle
{
database_state_handle_t state;
database_info_handle_t *handle;
msg_queue_t *msg_queue;
unsigned status;
} db_handle_t;
static db_handle_t *db_ptr;
#ifdef HAVE_LIBRETRODB #ifdef HAVE_LIBRETRODB
#ifdef HAVE_ZLIB #ifdef HAVE_ZLIB
@ -374,12 +398,11 @@ static void rarch_main_data_db_cleanup_state(database_state_handle_t *db_state)
void rarch_main_data_db_iterate(bool is_thread, void *data) void rarch_main_data_db_iterate(bool is_thread, void *data)
{ {
data_runloop_t *runloop = (data_runloop_t*)data; database_info_handle_t *db = (db_ptr) ? db_ptr->handle : NULL;
database_info_handle_t *db = runloop ? runloop->db.handle : NULL; database_state_handle_t *db_state = (db_ptr) ? &db_ptr->state : NULL;
database_state_handle_t *db_state = runloop ? &runloop->db.state : NULL; const char *name = db ? db->list->elems[db->list_ptr].data : NULL;
const char *name = db ? db->list->elems[db->list_ptr].data : NULL;
if (!db || !runloop) if (!db)
goto do_poll; goto do_poll;
switch (db->status) switch (db->status)
@ -396,7 +419,7 @@ void rarch_main_data_db_iterate(bool is_thread, void *data)
database_info_iterate_start(db, name); database_info_iterate_start(db, name);
break; break;
case DATABASE_STATUS_ITERATE: case DATABASE_STATUS_ITERATE:
if (database_info_iterate(&runloop->db.state, db) == 0) if (database_info_iterate(&db_ptr->state, db) == 0)
{ {
db->status = DATABASE_STATUS_ITERATE_NEXT; db->status = DATABASE_STATUS_ITERATE_NEXT;
db->type = DATABASE_TYPE_ITERATE; db->type = DATABASE_TYPE_ITERATE;
@ -423,9 +446,9 @@ void rarch_main_data_db_iterate(bool is_thread, void *data)
db_state->list = NULL; db_state->list = NULL;
rarch_main_data_db_cleanup_state(db_state); rarch_main_data_db_cleanup_state(db_state);
database_info_free(db); database_info_free(db);
if (runloop->db.handle) if (db_ptr->handle)
free(runloop->db.handle); free(db_ptr->handle);
runloop->db.handle = NULL; db_ptr->handle = NULL;
break; break;
default: default:
case DATABASE_STATUS_NONE: case DATABASE_STATUS_NONE:
@ -435,17 +458,16 @@ void rarch_main_data_db_iterate(bool is_thread, void *data)
return; return;
do_poll: do_poll:
if (database_info_poll(&runloop->db) != -1) if (database_info_poll(db_ptr) != -1)
{ {
if (runloop->db.handle) if (db_ptr->handle)
runloop->db.handle->status = DATABASE_STATUS_ITERATE_BEGIN; db_ptr->handle->status = DATABASE_STATUS_ITERATE_BEGIN;
} }
} }
void *rarch_main_data_db_get_ptr(void) void *rarch_main_data_db_get_ptr(void)
{ {
data_runloop_t *runloop = rarch_main_data_get_ptr(); db_handle_t *db = db_ptr;
db_handle_t *db = runloop ? &runloop->db : NULL;
if (!db) if (!db)
return NULL; return NULL;
return db; return db;
@ -466,4 +488,27 @@ msg_queue_t *rarch_main_data_db_get_msg_queue_ptr(void)
return NULL; return NULL;
return db->msg_queue; return db->msg_queue;
} }
void rarch_main_data_db_uninit(void)
{
if (db_ptr)
free(db_ptr);
db_ptr = NULL;
}
void rarch_main_data_db_init(void)
{
db_ptr = (db_handle_t*)calloc(1, sizeof(*db_ptr));
}
bool rarch_main_data_db_is_active(void)
{
db_handle_t *db = (db_handle_t*)rarch_main_data_db_get_ptr();
database_info_handle_t *dbi = db ? db->handle : NULL;
if (dbi && dbi->status != DATABASE_STATUS_NONE)
return true;
return false;
}
#endif #endif

View File

@ -19,6 +19,7 @@
#include <string/string_list.h> #include <string/string_list.h>
#include <compat/strl.h> #include <compat/strl.h>
#include <file/file_path.h> #include <file/file_path.h>
#include <file/file_extract.h>
#include <net/net_compat.h> #include <net/net_compat.h>
#include "../file_ops.h" #include "../file_ops.h"

View File

@ -84,6 +84,12 @@ void rarch_main_data_db_init_msg_queue(void);
void *rarch_main_data_db_get_ptr(void); void *rarch_main_data_db_get_ptr(void);
msg_queue_t *rarch_main_data_db_get_msg_queue_ptr(void); msg_queue_t *rarch_main_data_db_get_msg_queue_ptr(void);
void rarch_main_data_db_uninit(void);
void rarch_main_data_db_init(void);
bool rarch_main_data_db_is_active(void);
#endif #endif
#ifdef HAVE_OVERLAY #ifdef HAVE_OVERLAY