RetroArch/autosave.c

158 lines
4.0 KiB
C
Raw Normal View History

2012-04-21 21:13:50 +00:00
/* RetroArch - A frontend for libretro.
2012-01-08 00:08:18 +00:00
* Copyright (C) 2010-2012 - Hans-Kristian Arntzen
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/>.
*/
#include "autosave.h"
#include "thread.h"
2011-02-10 20:16:59 +00:00
#include <stdlib.h>
2011-12-24 12:46:12 +00:00
#include "boolean.h"
2011-02-10 20:16:59 +00:00
#include <string.h>
#include <stdio.h>
#include "general.h"
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;
};
static void autosave_thread(void *data)
2011-02-10 20:16:59 +00:00
{
2011-12-24 12:46:12 +00:00
autosave_t *save = (autosave_t*)data;
bool first_log = true;
2011-02-10 20:16:59 +00:00
while (!save->quit)
{
autosave_lock(save);
2012-07-07 15:19:32 +00:00
bool differ = memcmp(save->buffer, save->retro_buffer, 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)
{
// Avoid spamming down stderr ... :)
if (first_log)
{
2012-04-21 21:25:32 +00:00
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");
bool failed = false;
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 * 1000);
slock_unlock(save->cond_lock);
2011-02-10 20:16:59 +00:00
}
}
autosave_t *autosave_new(const char *path, const void *data, size_t size, unsigned interval)
{
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;
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
handle->lock = slock_new();
handle->cond_lock = slock_new();
handle->cond = scond_new();
2011-02-10 20:16:59 +00:00
handle->thread = sthread_create(autosave_thread, handle);
2011-02-10 20:16:59 +00:00
return handle;
}
void autosave_lock(autosave_t *handle)
{
slock_lock(handle->lock);
2011-02-10 20:16:59 +00:00
}
void autosave_unlock(autosave_t *handle)
{
slock_unlock(handle->lock);
2011-02-10 20:16:59 +00:00
}
void autosave_free(autosave_t *handle)
{
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
void lock_autosave(void)
{
for (unsigned i = 0; i < sizeof(g_extern.autosave)/sizeof(g_extern.autosave[0]); i++)
{
if (g_extern.autosave[i])
autosave_lock(g_extern.autosave[i]);
}
}
void unlock_autosave(void)
{
for (unsigned i = 0; i < sizeof(g_extern.autosave)/sizeof(g_extern.autosave[0]); i++)
{
if (g_extern.autosave[i])
autosave_unlock(g_extern.autosave[i]);
}
}