RetroArch/autosave.c

227 lines
5.1 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
2015-01-07 16:46:50 +00:00
* Copyright (C) 2011-2015 - 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>
2015-09-04 19:11:00 +00:00
#include <rthreads/rthreads.h>
2011-02-10 20:16:59 +00:00
#include "general.h"
2015-11-23 11:03:38 +00:00
#include "verbosity.h"
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;
};
/**
* 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
global_t *global = global_get_ptr();
2015-07-27 15:18:10 +00:00
for (i = 0; i < global->autosave.num; i++)
2011-02-18 22:51:51 +00:00
{
2015-07-27 15:18:10 +00:00
if (global->autosave.list[i])
autosave_lock(global->autosave.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
global_t *global = global_get_ptr();
2015-07-27 15:18:10 +00:00
for (i = 0; i < global->autosave.num; i++)
2011-02-18 22:51:51 +00:00
{
2015-07-27 15:18:10 +00:00
if (global->autosave.list[i])
autosave_unlock(global->autosave.list[i]);
2011-02-18 22:51:51 +00:00
}
}