RetroArch/autosave.c

289 lines
6.5 KiB
C
Raw Normal View History

2012-04-21 21:13:50 +00:00
/* RetroArch - A frontend for libretro.
2014-01-01 00:50:59 +00:00
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
2016-01-10 03:06:50 +00:00
* Copyright (C) 2011-2016 - Daniel De Matteis
2011-02-10 20:16:59 +00:00
*
2012-04-21 21:13:50 +00:00
* RetroArch is free software: you can redistribute it and/or modify it under the terms
2011-02-10 20:16:59 +00:00
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
2012-04-21 21:13:50 +00:00
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
2011-02-10 20:16:59 +00:00
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
2012-04-21 21:31:57 +00:00
* You should have received a copy of the GNU General Public License along with RetroArch.
2011-02-10 20:16:59 +00:00
* If not, see <http://www.gnu.org/licenses/>.
*/
2015-09-04 19:11:00 +00:00
#include <stdio.h>
2011-02-10 20:16:59 +00:00
#include <stdlib.h>
#include <string.h>
2015-09-04 19:11:00 +00:00
2015-09-05 17:51:55 +00:00
#include <boolean.h>
#include <string/string_list.h>
2015-09-04 19:11:00 +00:00
#include <rthreads/rthreads.h>
2015-11-30 18:23:13 +00:00
#include "autosave.h"
#include "configuration.h"
#include "msg_hash.h"
#include "runloop.h"
#include "libretro_version_1.h"
2015-11-23 11:03:38 +00:00
#include "verbosity.h"
2011-02-10 20:16:59 +00:00
2015-11-30 18:23:13 +00:00
/* Autosave support. */
struct autosave_st
{
autosave_t **list;
unsigned num;
};
2011-02-10 20:16:59 +00:00
struct autosave
{
volatile bool quit;
slock_t *lock;
2011-02-10 20:16:59 +00:00
slock_t *cond_lock;
scond_t *cond;
sthread_t *thread;
2011-02-10 20:16:59 +00:00
void *buffer;
2012-07-07 15:19:32 +00:00
const void *retro_buffer;
2011-02-10 20:16:59 +00:00
const char *path;
size_t bufsize;
unsigned interval;
};
2015-11-30 18:23:13 +00:00
static struct autosave_st autosave_state;
/**
* autosave_lock:
* @handle : pointer to autosave object
*
* Locks autosave.
**/
static void autosave_lock(autosave_t *handle)
2011-02-10 20:16:59 +00:00
{
slock_lock(handle->lock);
}
/**
* autosave_unlock:
* @handle : pointer to autosave object
*
* Unlocks autosave.
**/
static void autosave_unlock(autosave_t *handle)
{
slock_unlock(handle->lock);
}
/**
* autosave_thread:
* @data : pointer to autosave object
*
* Callback function for (threaded) autosave.
**/
static void autosave_thread(void *data)
{
bool first_log = true;
autosave_t *save = (autosave_t*)data;
2011-02-10 20:16:59 +00:00
while (!save->quit)
{
2015-09-28 16:44:28 +00:00
bool differ;
2011-02-10 20:16:59 +00:00
autosave_lock(save);
differ = memcmp(save->buffer, save->retro_buffer,
2014-09-02 03:10:54 +00:00
save->bufsize) != 0;
if (differ)
2012-07-07 15:19:32 +00:00
memcpy(save->buffer, save->retro_buffer, save->bufsize);
2011-02-10 20:16:59 +00:00
autosave_unlock(save);
if (differ)
2011-02-10 20:16:59 +00:00
{
/* Should probably deal with this more elegantly. */
FILE *file = fopen(save->path, "wb");
if (file)
{
bool failed = false;
/* Avoid spamming down stderr ... */
if (first_log)
{
RARCH_LOG("Autosaving SRAM to \"%s\", will continue to check every %u seconds ...\n",
save->path, save->interval);
first_log = false;
}
else
2012-04-21 21:25:32 +00:00
RARCH_LOG("SRAM changed ... autosaving ...\n");
2014-09-02 03:10:54 +00:00
failed |= fwrite(save->buffer, 1, save->bufsize, file)
!= save->bufsize;
failed |= fflush(file) != 0;
failed |= fclose(file) != 0;
if (failed)
2012-04-21 21:25:32 +00:00
RARCH_WARN("Failed to autosave SRAM. Disk might be full.\n");
}
2011-02-10 20:16:59 +00:00
}
slock_lock(save->cond_lock);
if (!save->quit)
scond_wait_timeout(save->cond, save->cond_lock,
save->interval * 1000000LL);
slock_unlock(save->cond_lock);
2011-02-10 20:16:59 +00:00
}
}
/**
* autosave_new:
* @path : path to autosave file
* @data : pointer to buffer
* @size : size of @data buffer
* @interval : interval at which saves should be performed.
*
* Create and initialize autosave object.
*
* Returns: pointer to new autosave_t object if successful, otherwise
* NULL.
**/
autosave_t *autosave_new(const char *path, const void *data, size_t size,
unsigned interval)
2011-02-10 20:16:59 +00:00
{
2011-12-24 12:46:12 +00:00
autosave_t *handle = (autosave_t*)calloc(1, sizeof(*handle));
2011-02-10 20:16:59 +00:00
if (!handle)
return NULL;
2015-06-22 19:23:13 +00:00
handle->bufsize = size;
handle->interval = interval;
handle->path = path;
handle->buffer = malloc(size);
2012-07-07 15:19:32 +00:00
handle->retro_buffer = data;
2011-02-10 20:16:59 +00:00
if (!handle->buffer)
{
free(handle);
return NULL;
}
2012-07-07 15:19:32 +00:00
memcpy(handle->buffer, handle->retro_buffer, handle->bufsize);
2011-02-10 20:16:59 +00:00
2015-06-22 19:23:13 +00:00
handle->lock = slock_new();
handle->cond_lock = slock_new();
handle->cond = scond_new();
2011-02-10 20:16:59 +00:00
2015-06-22 19:23:13 +00:00
handle->thread = sthread_create(autosave_thread, handle);
2011-02-10 20:16:59 +00:00
return handle;
}
/**
* autosave_free:
* @handle : pointer to autosave object
*
* Frees autosave object.
**/
2011-02-10 20:16:59 +00:00
void autosave_free(autosave_t *handle)
{
2014-04-04 12:58:42 +00:00
if (!handle)
return;
slock_lock(handle->cond_lock);
2011-02-10 20:16:59 +00:00
handle->quit = true;
slock_unlock(handle->cond_lock);
scond_signal(handle->cond);
sthread_join(handle->thread);
2011-02-10 20:16:59 +00:00
slock_free(handle->lock);
slock_free(handle->cond_lock);
scond_free(handle->cond);
2011-02-10 20:16:59 +00:00
free(handle->buffer);
free(handle);
}
2011-02-18 22:51:51 +00:00
/**
* lock_autosave:
*
* Lock autosave.
**/
2011-02-18 22:51:51 +00:00
void lock_autosave(void)
{
unsigned i;
2015-03-21 03:43:18 +00:00
2015-11-30 18:23:13 +00:00
for (i = 0; i < autosave_state.num; i++)
2011-02-18 22:51:51 +00:00
{
2015-11-30 18:23:13 +00:00
if (autosave_state.list[i])
autosave_lock(autosave_state.list[i]);
2011-02-18 22:51:51 +00:00
}
}
/**
* unlock_autosave:
*
* Unlocks autosave.
**/
2011-02-18 22:51:51 +00:00
void unlock_autosave(void)
{
unsigned i;
2015-03-21 03:43:18 +00:00
2015-11-30 18:23:13 +00:00
for (i = 0; i < autosave_state.num; i++)
{
if (autosave_state.list[i])
autosave_unlock(autosave_state.list[i]);
}
}
void autosave_event_init(void)
{
unsigned i;
settings_t *settings = config_get_ptr();
global_t *global = global_get_ptr();
if (settings->autosave_interval < 1 || !global->savefiles)
return;
if (!(autosave_state.list = (autosave_t**)calloc(global->savefiles->size,
sizeof(*autosave_state.list))))
return;
autosave_state.num = global->savefiles->size;
for (i = 0; i < global->savefiles->size; i++)
2011-02-18 22:51:51 +00:00
{
retro_ctx_memory_info_t mem_info;
2015-11-30 18:23:13 +00:00
const char *path = global->savefiles->elems[i].data;
unsigned type = global->savefiles->elems[i].attr.i;
mem_info.id = type;
core_ctl(CORE_CTL_RETRO_GET_MEMORY, &mem_info);
if (mem_info.size <= 0)
2015-11-30 18:23:13 +00:00
continue;
autosave_state.list[i] = autosave_new(path,
mem_info.data,
mem_info.size,
2015-11-30 18:23:13 +00:00
settings->autosave_interval);
if (!autosave_state.list[i])
RARCH_WARN("%s\n", msg_hash_to_str(MSG_AUTOSAVE_FAILED));
2011-02-18 22:51:51 +00:00
}
}
2015-11-30 18:23:13 +00:00
void autosave_event_deinit(void)
{
unsigned i;
for (i = 0; i < autosave_state.num; i++)
autosave_free(autosave_state.list[i]);
if (autosave_state.list)
free(autosave_state.list);
2011-02-18 22:51:51 +00:00
2015-11-30 18:23:13 +00:00
autosave_state.list = NULL;
autosave_state.num = 0;
}