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.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with SSNES.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "autosave.h"
|
2011-10-06 18:31:39 +00:00
|
|
|
#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;
|
2011-10-06 18:31:39 +00:00
|
|
|
slock_t *lock;
|
2011-02-10 20:16:59 +00:00
|
|
|
|
2011-10-06 18:31:39 +00:00
|
|
|
slock_t *cond_lock;
|
|
|
|
scond_t *cond;
|
|
|
|
sthread_t *thread;
|
2011-02-10 20:16:59 +00:00
|
|
|
|
|
|
|
void *buffer;
|
|
|
|
const void *snes_buffer;
|
|
|
|
const char *path;
|
|
|
|
size_t bufsize;
|
|
|
|
unsigned interval;
|
|
|
|
};
|
|
|
|
|
2011-10-06 18:31:39 +00:00
|
|
|
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;
|
2011-08-23 19:04:00 +00:00
|
|
|
|
|
|
|
bool first_log = true;
|
|
|
|
|
2011-02-10 20:16:59 +00:00
|
|
|
while (!save->quit)
|
|
|
|
{
|
|
|
|
autosave_lock(save);
|
2011-09-17 08:57:55 +00:00
|
|
|
bool differ = memcmp(save->buffer, save->snes_buffer, save->bufsize) != 0;
|
|
|
|
if (differ)
|
|
|
|
memcpy(save->buffer, save->snes_buffer, save->bufsize);
|
2011-02-10 20:16:59 +00:00
|
|
|
autosave_unlock(save);
|
|
|
|
|
2011-09-17 08:57:55 +00:00
|
|
|
if (differ)
|
2011-02-10 20:16:59 +00:00
|
|
|
{
|
2011-09-17 08:57:55 +00:00
|
|
|
// Should probably deal with this more elegantly.
|
|
|
|
FILE *file = fopen(save->path, "wb");
|
|
|
|
if (file)
|
2011-08-23 19:04:00 +00:00
|
|
|
{
|
2011-09-17 08:57:55 +00:00
|
|
|
// 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);
|
2011-09-17 08:57:55 +00:00
|
|
|
first_log = false;
|
|
|
|
}
|
|
|
|
else
|
2012-04-21 21:25:32 +00:00
|
|
|
RARCH_LOG("SRAM changed ... autosaving ...\n");
|
2011-08-23 19:04:00 +00:00
|
|
|
|
2011-09-17 08:57:55 +00:00
|
|
|
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-09-17 08:57:55 +00:00
|
|
|
}
|
2011-02-10 20:16:59 +00:00
|
|
|
}
|
|
|
|
|
2011-10-06 18:31:39 +00:00
|
|
|
slock_lock(save->cond_lock);
|
2011-02-11 18:54:42 +00:00
|
|
|
if (!save->quit)
|
2011-10-06 18:31:39 +00:00
|
|
|
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);
|
|
|
|
handle->snes_buffer = data;
|
|
|
|
|
|
|
|
if (!handle->buffer)
|
|
|
|
{
|
|
|
|
free(handle);
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-09-17 08:57:55 +00:00
|
|
|
memcpy(handle->buffer, handle->snes_buffer, handle->bufsize);
|
2011-02-10 20:16:59 +00:00
|
|
|
|
2011-10-06 18:31:39 +00:00
|
|
|
handle->lock = slock_new();
|
|
|
|
handle->cond_lock = slock_new();
|
|
|
|
handle->cond = scond_new();
|
2011-02-10 20:16:59 +00:00
|
|
|
|
2011-10-06 18:31:39 +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)
|
|
|
|
{
|
2011-10-06 18:31:39 +00:00
|
|
|
slock_lock(handle->lock);
|
2011-02-10 20:16:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void autosave_unlock(autosave_t *handle)
|
|
|
|
{
|
2011-10-06 18:31:39 +00:00
|
|
|
slock_unlock(handle->lock);
|
2011-02-10 20:16:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void autosave_free(autosave_t *handle)
|
|
|
|
{
|
2011-10-06 18:31:39 +00:00
|
|
|
slock_lock(handle->cond_lock);
|
2011-02-10 20:16:59 +00:00
|
|
|
handle->quit = true;
|
2011-10-06 18:31:39 +00:00
|
|
|
slock_unlock(handle->cond_lock);
|
|
|
|
scond_signal(handle->cond);
|
|
|
|
sthread_join(handle->thread);
|
2011-02-10 20:16:59 +00:00
|
|
|
|
2011-10-06 18:31:39 +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]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|