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"
|
|
|
|
|
|
|
|
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;
|
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-01-09 01:53:08 +00:00
|
|
|
/**
|
|
|
|
* autosave_lock:
|
|
|
|
* @handle : pointer to autosave object
|
|
|
|
*
|
|
|
|
* Locks autosave.
|
|
|
|
**/
|
|
|
|
static void autosave_lock(autosave_t *handle)
|
2011-02-10 20:16:59 +00:00
|
|
|
{
|
2015-01-09 01:53:08 +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);
|
|
|
|
}
|
2011-08-23 19:04:00 +00:00
|
|
|
|
2015-01-09 01:53:08 +00:00
|
|
|
/**
|
|
|
|
* autosave_thread:
|
|
|
|
* @data : pointer to autosave object
|
|
|
|
*
|
|
|
|
* Callback function for (threaded) autosave.
|
|
|
|
**/
|
|
|
|
static void autosave_thread(void *data)
|
|
|
|
{
|
2011-08-23 19:04:00 +00:00
|
|
|
bool first_log = true;
|
2015-01-09 01:53:08 +00:00
|
|
|
autosave_t *save = (autosave_t*)data;
|
2011-08-23 19:04:00 +00:00
|
|
|
|
2011-02-10 20:16:59 +00:00
|
|
|
while (!save->quit)
|
|
|
|
{
|
2015-09-28 16:44:28 +00:00
|
|
|
bool differ;
|
2015-01-09 01:53:08 +00:00
|
|
|
|
2011-02-10 20:16:59 +00:00
|
|
|
autosave_lock(save);
|
2015-01-09 01:53:08 +00:00
|
|
|
differ = memcmp(save->buffer, save->retro_buffer,
|
2014-09-02 03:10:54 +00:00
|
|
|
save->bufsize) != 0;
|
2011-09-17 08:57:55 +00:00
|
|
|
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);
|
|
|
|
|
2011-09-17 08:57:55 +00:00
|
|
|
if (differ)
|
2011-02-10 20:16:59 +00:00
|
|
|
{
|
2014-08-20 15:18:43 +00:00
|
|
|
/* Should probably deal with this more elegantly. */
|
2011-09-17 08:57:55 +00:00
|
|
|
FILE *file = fopen(save->path, "wb");
|
2015-01-09 01:53:08 +00:00
|
|
|
|
2011-09-17 08:57:55 +00:00
|
|
|
if (file)
|
2011-08-23 19:04:00 +00:00
|
|
|
{
|
2015-01-09 01:53:08 +00:00
|
|
|
bool failed = false;
|
|
|
|
|
2014-08-20 15:18:43 +00:00
|
|
|
/* Avoid spamming down stderr ... */
|
2011-09-17 08:57:55 +00:00
|
|
|
if (first_log)
|
|
|
|
{
|
2014-08-20 15:18:43 +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
|
|
|
|
2014-09-02 03:10:54 +00:00
|
|
|
failed |= fwrite(save->buffer, 1, save->bufsize, file)
|
|
|
|
!= save->bufsize;
|
2011-09-17 08:57:55 +00:00
|
|
|
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);
|
2014-08-20 15:18:43 +00:00
|
|
|
|
2011-02-11 18:54:42 +00:00
|
|
|
if (!save->quit)
|
2014-08-20 15:18:43 +00:00
|
|
|
scond_wait_timeout(save->cond, save->cond_lock,
|
|
|
|
save->interval * 1000000LL);
|
|
|
|
|
2011-10-06 18:31:39 +00:00
|
|
|
slock_unlock(save->cond_lock);
|
2011-02-10 20:16:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-09 01:53:08 +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.
|
|
|
|
**/
|
2014-08-20 15:18:43 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-01-09 01:53:08 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
|
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
|
|
|
|
2015-01-09 01:53:08 +00:00
|
|
|
/**
|
|
|
|
* lock_autosave:
|
|
|
|
*
|
|
|
|
* Lock autosave.
|
|
|
|
**/
|
2011-02-18 22:51:51 +00:00
|
|
|
void lock_autosave(void)
|
|
|
|
{
|
2013-10-22 13:08:17 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-09 01:53:08 +00:00
|
|
|
/**
|
|
|
|
* unlock_autosave:
|
|
|
|
*
|
|
|
|
* Unlocks autosave.
|
|
|
|
**/
|
2011-02-18 22:51:51 +00:00
|
|
|
void unlock_autosave(void)
|
|
|
|
{
|
2013-10-22 13:08:17 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|