Files
RetroArch/runahead.c
davey 6259694cbf core signatures: run-ahead loads its copy only if it matches the running core
With a trust store, run-ahead gets its second instance back. The copy
task hashes the copy and compares it with the running core's hash:
from its check, or from its file, if that is still the file loaded. It
loads the copy from the file it hashed. A copy that doesn't match, or
won't load, is deleted and run-ahead keeps one instance.

A staged load's take now grants the stage's own quick pass even when a
probe's quick pass of another core took its place while the worker
opened the core, so that core keeps its record and run-ahead its
source.
2026-11-16 07:12:21 +01:00

2320 lines
77 KiB
C

/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
* Copyright (C) 2011-2023 - Daniel De Matteis
* Copyright (C) 2018-2023 - Dan Weiss
* Copyright (C) 2022-2023 - Neil Fore
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* 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.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* 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 RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdint.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0500 || defined(_XBOX)
#ifndef LEGACY_WIN32
#define LEGACY_WIN32
#endif
#endif
#if defined(HAVE_DYNAMIC) && !defined(_WIN32) && !defined(ANDROID)
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#endif
#include <encodings/utf.h>
#include <string/stdstring.h>
#include <streams/file_stream.h>
#include <queues/task_queue.h>
#include <time/rtime.h>
#include <compat/strl.h>
#include "tasks/tasks_internal.h"
#include "configuration.h"
#include "content.h"
#include "core.h"
#include "dynamic.h"
#include "driver.h"
#include "audio/audio_driver.h"
#include "gfx/video_driver.h"
#include "paths.h"
#include "runloop.h"
#include "verbosity.h"
#ifdef HAVE_CORE_LOAD_CHECK
#include "core_load_check.h"
#endif
/* ===== BEGIN runahead dense input cache =====
* The last value each input tuple returned, for the dirty comparison
* on every core poll. The common tuples - a plain joypad's sixteen
* buttons and its mask, an analog device's two sticks and sixteen
* buttons - live in a fixed table indexed by (port, index, id): a
* load, no search. Anything else - a subclassed device, a keyboard,
* a pointer - goes to the list the frontend kept, found by a walk.
* A tuple never set reads 0 from either, as it always did. */
typedef struct runahead_dense_cache
{
int16_t joypad[MAX_USERS][17]; /* ids 0..15; 16 is the mask */
int16_t analog[MAX_USERS][3][16]; /* index 0..2, id 0..15 */
} runahead_dense_cache_t;
/* Where a tuple lives in the dense cache, or NULL for the list. */
static int16_t *runahead_dense_slot(runahead_dense_cache_t *c,
unsigned port, unsigned device, unsigned index, unsigned id)
{
if (!c || port >= MAX_USERS)
return NULL;
if (device == RETRO_DEVICE_JOYPAD && index == 0)
{
if (id < 16)
return &c->joypad[port][id];
if (id == RETRO_DEVICE_ID_JOYPAD_MASK)
return &c->joypad[port][16];
return NULL;
}
if (device == RETRO_DEVICE_ANALOG && index < 3 && id < 16)
return &c->analog[port][index][id];
return NULL;
}
/* ===== END runahead dense input cache ===== */
/* ===== BEGIN runahead input list =====
* Tuples the dense cache has no slot for, one flat array; each
* tuple's ids are sized by the highest id written to it. */
static runahead_dense_cache_t *runahead_dense;
/* Last value of every id of one (port, device, index)
* tuple; ids past state_size read 0 */
typedef struct input_list_element
{
int16_t *state;
unsigned port;
unsigned device;
unsigned index;
unsigned state_size;
} input_list_element;
typedef struct runahead_input_list
{
input_list_element *elems;
unsigned size;
unsigned cap;
} runahead_input_list_t;
static runahead_input_list_t runahead_input_list;
/* The list entry of a (port, device, index) tuple, or NULL */
static input_list_element *runahead_input_list_find(
const runahead_input_list_t *list,
unsigned port, unsigned device, unsigned index)
{
unsigned i;
for (i = 0; i < list->size; i++)
{
input_list_element *element = &list->elems[i];
if ( (element->port == port)
&& (element->device == device)
&& (element->index == index))
return element;
}
return NULL;
}
static void runahead_input_list_free(runahead_input_list_t *list)
{
unsigned i;
for (i = 0; i < list->size; i++)
free(list->elems[i].state);
free(list->elems);
list->elems = NULL;
list->size = 0;
list->cap = 0;
}
/* Grows the state of @element so that @id indexes it;
* the new ids read 0 */
static bool runahead_input_element_expand(
input_list_element *element, unsigned id)
{
int16_t *state;
unsigned new_size = element->state_size ? element->state_size : 32;
while (id >= new_size)
new_size *= 2;
if (!(state = (int16_t*)realloc(element->state,
new_size * sizeof(int16_t))))
return false;
memset(&state[element->state_size], 0,
(new_size - element->state_size) * sizeof(int16_t));
element->state = state;
element->state_size = new_size;
return true;
}
static void runahead_input_state_set_last(
unsigned port, unsigned device,
unsigned index, unsigned id, int16_t value)
{
runahead_input_list_t *list = &runahead_input_list;
input_list_element *element;
int16_t *slot = runahead_dense_slot(runahead_dense, port, device, index, id);
if (slot)
{
*slot = value;
return;
}
if (!(element = runahead_input_list_find(list, port, device, index)))
{
if (list->size == list->cap)
{
unsigned new_cap = list->cap ? list->cap * 2 : 8;
input_list_element *elems = (input_list_element*)realloc(
list->elems, new_cap * sizeof(*elems));
if (!elems)
return;
list->elems = elems;
list->cap = new_cap;
}
element = &list->elems[list->size++];
element->state = NULL;
element->port = port;
element->device = device;
element->index = index;
element->state_size = 0;
}
if ( id >= element->state_size
&& !runahead_input_element_expand(element, id))
return;
element->state[id] = value;
}
static int16_t input_state_get_last(unsigned port,
unsigned device, unsigned index, unsigned id)
{
const input_list_element *element;
const int16_t *slot = runahead_dense_slot(runahead_dense, port, device, index, id);
if (slot)
return *slot;
element = runahead_input_list_find(
&runahead_input_list, port, device, index);
if (element && id < element->state_size)
return element->state[id];
return 0;
}
/* ===== END runahead input list ===== */
static void free_retro_ctx_load_content_info(struct
retro_ctx_load_content_info *dest)
{
if (!dest)
return;
string_list_free((struct string_list*)dest->content);
if (dest->info)
free(dest->info);
dest->info = NULL;
dest->content = NULL;
}
static struct retro_game_info* clone_retro_game_info(const
struct retro_game_info *src)
{
struct retro_game_info *dest = (struct retro_game_info*)malloc(
sizeof(struct retro_game_info));
if (!dest)
return NULL;
/* The copy task in flight copies the core binary, never this; the
* data is read only at retro_load_game on the secondary, on the
* main thread, after the task has reported ready. Unloading
* destroys the secondary - which invalidates any in-flight result
* through the copy generation - before retro_unload_game and
* before the content is freed, and a new secondary needs a new
* core_load_game, which replaces this clone. So the alias holds
* for as long as it is read.
*
* content_file_init() guarantees that all
* elements of the source retro_game_info
* struct will persist for the lifetime of
* the core. This means we do not have to
* copy any data; pointer assignment is
* sufficient */
dest->path = src->path;
dest->data = src->data;
dest->size = src->size;
dest->meta = src->meta;
return dest;
}
static struct retro_ctx_load_content_info
*clone_retro_ctx_load_content_info(
const struct retro_ctx_load_content_info *src)
{
struct retro_ctx_load_content_info *dest = NULL;
if (!src || src->special)
return NULL; /* refuse to deal with the Special field */
dest = (struct retro_ctx_load_content_info*)
malloc(sizeof(*dest));
if (!dest)
return NULL;
dest->info = NULL;
dest->content = NULL;
dest->special = NULL;
if (src->info)
dest->info = clone_retro_game_info(src->info);
if (src->content)
dest->content = string_list_clone(src->content);
return dest;
}
void runahead_set_load_content_info(void *data,
const retro_ctx_load_content_info_t *ctx)
{
runloop_state_t *runloop_st = (runloop_state_t*)data;
free_retro_ctx_load_content_info(runloop_st->load_content_info);
free(runloop_st->load_content_info);
runloop_st->load_content_info = clone_retro_ctx_load_content_info(ctx);
}
/* RUNAHEAD - SECONDARY CORE
*
* A secondary instance is a second copy of the core's binary loaded
* beside the first, so it exists only where the core is a dynamic
* library: HAVE_DYNAMIC. A build that can load libraries but links
* its core statically (HAVE_DYLIB without HAVE_DYNAMIC) has no binary
* to copy, and every part of the secondary path is compiled out of it
* together - creation, teardown, deserialize, and the run loop's use
* of it - so run-ahead there is the single-instance method
* throughout. Every gate below is the same test. */
#if defined(HAVE_DYNAMIC)
/* enum runahead_copy_status lives in runloop.h (shared with the
* secondary_core_ensure_exists() callers) */
static void runahead_copy_reset(bool delete_file);
static void strcat_alloc(char **dst, const char *s)
{
char *tmp;
size_t dst_len, s_len, new_len;
if (!dst)
return;
if (!*dst)
{
if (s)
{
dst_len = strlen(s);
tmp = (char*)malloc(dst_len + 1);
if (tmp)
memcpy(tmp, s, dst_len + 1);
*dst = tmp;
}
else
*dst = (char*)calloc(1, 1);
return;
}
if (!s || !*s)
return;
dst_len = strlen(*dst);
s_len = strlen(s);
new_len = dst_len + s_len + 1;
tmp = (char*)realloc(*dst, new_len);
if (!tmp)
return;
*dst = tmp;
strlcpy(tmp + dst_len, s, s_len + 1);
}
void runahead_secondary_core_destroy(void *data)
{
runloop_state_t *runloop_st = (runloop_state_t*)data;
/* Nothing calls into the copy once it is closed */
runloop_st->secondary_key_event = NULL;
/* Drop any unconsumed async copy result (deleting its temp
* file) and tell an in-flight copy task to discard its result;
* this runs regardless of whether a secondary instance was
* ever actually created. */
runahead_copy_reset(true);
if (!runloop_st->secondary_lib_handle)
return;
/* unload game from core */
if (runloop_st->secondary_core.retro_unload_game)
runloop_st->secondary_core.retro_unload_game();
runloop_st->core_poll_type_override = POLL_TYPE_OVERRIDE_DONTCARE;
/* deinit */
if (runloop_st->secondary_core.retro_deinit)
runloop_st->secondary_core.retro_deinit();
memset(&runloop_st->secondary_core, 0, sizeof(struct retro_core_t));
dylib_close(runloop_st->secondary_lib_handle);
runloop_st->secondary_lib_handle = NULL;
/* Delete the on-disk copy of the secondary core and free the
* path string. The NULL check guards both: filestream_delete
* is currently NULL-safe but the explicit guard here also
* documents the intent and protects against future changes
* to the VFS layer's NULL handling. */
if (runloop_st->secondary_library_path)
{
filestream_delete(runloop_st->secondary_library_path);
free(runloop_st->secondary_library_path);
runloop_st->secondary_library_path = NULL;
}
}
static char *get_tmpdir_alloc(const char *override_dir)
{
char *path = NULL;
#ifdef _WIN32
#ifdef LEGACY_WIN32
DWORD plen = GetTempPath(0, NULL) + 1;
if (!(path = (char*)malloc(plen * sizeof(char))))
return NULL;
path[plen - 1] = 0;
GetTempPath(plen, path);
#else
DWORD plen = GetTempPathW(0, NULL) + 1;
wchar_t *wide_str = (wchar_t*)malloc(plen * sizeof(wchar_t));
if (!wide_str)
return NULL;
wide_str[plen - 1] = 0;
GetTempPathW(plen, wide_str);
path = utf16_to_utf8_string_alloc(wide_str);
free(wide_str);
#endif
#else
const char *src = NULL;
#if defined ANDROID
src = override_dir;
#else
{
char *tmpdir = getenv("TMPDIR");
if (tmpdir)
src = tmpdir;
else
src = "/tmp";
}
#endif
if (src)
{
size_t _len = strlen(src);
if (_len != 0)
path = strldup(src, _len + 1);
}
else
path = (char*)calloc(1,1);
#endif
return path;
}
/* ===== BEGIN runahead tmp dir check =====
* Whether the copy may go into @dir. On Windows and Android the temp
* dir is the user's own. Elsewhere it is a shared one like /tmp, where
* another user could have made @dir first, then planted a link to be
* overwritten or swapped the copied core before it is loaded. So @dir
* must be a real directory we own that no one else could have written
* to: a link left in it from then would be followed by the copy, which
* no change of mode now would undo. Group-writable is ours alone when
* the group is our own, as a user private group is. Readable by others
* is closed off. */
static bool runahead_tmp_dir_private(const char *dir)
{
#if !defined(_WIN32) && !defined(ANDROID)
struct stat st;
if ( lstat(dir, &st) != 0
|| !S_ISDIR(st.st_mode)
|| st.st_uid != geteuid()
|| (st.st_mode & S_IWOTH)
|| ((st.st_mode & S_IWGRP) && st.st_gid != getegid())
|| ((st.st_mode & 077) && chmod(dir, 0700) != 0))
{
RARCH_WARN("[Run-Ahead] \"%s\" is not private to this user; "
"not copying the core there.\n", dir);
return false;
}
#else
(void)dir;
#endif
return true;
}
/* ===== END runahead tmp dir check ===== */
/* ===== BEGIN runahead core-copy fragment =====
* Everything between the BEGIN/END markers depends only on
* libretro-common (filestream, task_queue, path) plus libc and the
* static helpers above, so a test harness can extract and execute
* it verbatim.
*
* Duplicating the core binary for the secondary instance is pure
* file IO sized by the core (MAME and friends run hundreds of
* megabytes), so it must not run synchronously on the thread that
* drives frames. Neither should opening the copy: mapping the image,
* resolving its imports and running its constructors are the
* system loader's work, sized by the core again, and the copy's
* path is its own, so no other handle can share the instance. The
* copy and the open run as a task; while it is in flight,
* secondary_core_create() reports 'pending' and run-ahead falls
* back to the single-instance savestate method for those frames -
* identical output, no stall - then upgrades to the secondary
* instance when the copy completes. All slot state below is
* touched only from the main thread (the task callback runs on the
* main thread); the worker touches only its own task state. */
/* Bytes offered to one step of the VFS copy. On the kernel paths
* (copy_file_range, clonefile) this is a request the
* kernel satisfies without the bytes entering user space; elsewhere
* it bounds the buffered loop. What ends a handler pass is the
* shared per-frame I/O window, not this. */
#define RUNAHEAD_COPY_STEP_BYTES ((int64_t)4 * 1024 * 1024)
/* Result slot, published by the task callback (main thread) */
static char *runahead_copy_slot_path = NULL;
static char *runahead_copy_slot_src = NULL; /* identity of the copy */
static dylib_t runahead_copy_slot_lib = NULL; /* the copy, opened */
static bool runahead_copy_slot_done = false;
static bool runahead_copy_slot_failed = false;
/* Bumped by runahead_copy_reset(); a task publishes its result only
* if its recorded generation still matches, otherwise it discards
* it (deleting the temp file). This covers every teardown window,
* including the race where the worker has already finished but the
* main-thread callback has not yet run - a plain 'in flight?' check
* misses that window. All reads/writes happen on the main thread
* (the handler never touches it). */
static unsigned runahead_copy_generation = 0;
/* True from a successful task push until that task's main-thread
* callback has run. This must NOT be derived from
* task_queue_find(): the threaded queue has a window where the
* worker has finished (task no longer in the running list) but the
* callback has not yet executed - a find-based check reports 'not
* in flight' there, and a concurrent poll would push a second copy
* task racing the first onto the same destination path. The flag
* transitions strictly on the main thread (push / callback), so no
* such window exists. */
static bool runahead_copy_task_pending = false;
typedef struct runahead_copy_handle
{
char *src_path;
char *dir_libretro;
char *out_path; /* produced temp file on success */
/* In-flight VFS copy, advanced one window's worth per handler
* pass. NULL before the first pass has opened one and after the
* copy has been closed. */
struct retro_vfs_copy_handle *copy;
char *copy_dst; /* destination the open copy is writing to */
dylib_t lib; /* out_path opened by the task; NULL if it
* could not be, and the caller opens it */
#ifdef HAVE_CORE_LOAD_CHECK
core_load_match_t *match; /* the copy against the running core */
struct core_load_source src;
bool check; /* the copy loads only once it matches */
bool mismatch;
#endif
bool started; /* the open was attempted (once, on pass one) */
bool failed;
unsigned generation;
} runahead_copy_handle_t;
/* Copies a core through the VFS copy, which uses the platform's own
* copy primitive where there is one: copy_file_range on Linux,
* clonefile on APFS (constant time, no bytes move at all). Nothing
* is buffered in user space on those paths, so a 40MB core costs no
* transfer buffer; elsewhere the VFS uses one bounded buffer of its
* own. A failed or abandoned copy leaves no
* partial file behind.
*
* Advances an in-flight copy by one share of the shared per-frame
* I/O window and returns; it never runs the copy to completion and
* never sleeps. The handler is re-entered next tick to continue.
*
* @return 1 done, 0 still running, -1 failed. */
static int runahead_copy_step(struct retro_vfs_copy_handle *copy)
{
nbio_budget_t b;
int st = RETRO_VFS_COPY_RUNNING;
/* The window is shared with the file-transfer spine and the
* content scanner, so a copy running alongside them costs the
* frame the window once rather than a slice each. */
task_nbio_slice_open(&b);
/* do/while: the floor guarantees one step even when the window is
* already spent, or a busy queue would never advance the copy. */
do
{
st = filestream_copy_step(copy, RUNAHEAD_COPY_STEP_BYTES, NULL, NULL);
} while ( st == RETRO_VFS_COPY_RUNNING
&& task_nbio_slice_within_budget(&b, 0, 0));
task_nbio_slice_close(&b);
if (st == RETRO_VFS_COPY_DONE)
return 1;
if (st == RETRO_VFS_COPY_RUNNING)
return 0;
return -1;
}
/* Opens a VFS copy to a randomly named destination under @tmp_path,
* used when the plain basename is taken (another instance holds it).
* Only begin() runs here: no bytes move, so this cannot block.
* On success *temp_dll_path owns the chosen path and the returned
* handle must be stepped to completion by the caller. */
static struct retro_vfs_copy_handle *copy_begin_with_random_name(
char **temp_dll_path,
const char *tmp_path, const char *src_dll_path)
{
int i;
struct retro_vfs_copy_handle *copy = NULL;
char number_buf[32];
const char *prefix = "tmp";
char *ext = NULL;
time_t time_value = time(NULL);
/* The generator's state, advanced for every candidate: unsigned,
* so the wrap is defined. */
uint32_t lcg = (uint32_t)time_value;
const char *src = path_get_extension(*temp_dll_path);
if (src)
{
size_t _len = strlen(src);
if (_len != 0)
ext = strldup(src, _len + 1);
}
else
ext = (char*)calloc(1,1);
if (ext)
{
size_t ext_len = strlen(ext);
if (ext_len > 0)
{
strcat_alloc(&ext, ".");
memmove(ext + 1, ext, ext_len);
ext[0] = '.';
ext_len++;
}
}
/* Try up to 30 'random' filenames before giving up */
for (i = 0; i < 30; i++)
{
unsigned number;
lcg = lcg * 214013u + 2531011u;
number = (lcg >> 14) % 100000u;
snprintf(number_buf, sizeof(number_buf), "%05u", number);
if (*temp_dll_path)
free(*temp_dll_path);
*temp_dll_path = NULL;
strcat_alloc(temp_dll_path, tmp_path);
strcat_alloc(temp_dll_path, PATH_DEFAULT_SLASH());
strcat_alloc(temp_dll_path, prefix);
strcat_alloc(temp_dll_path, number_buf);
strcat_alloc(temp_dll_path, ext);
if ((copy = filestream_copy_begin(src_dll_path, *temp_dll_path,
RETRO_VFS_COPY_OVERWRITE)))
break;
}
if (ext)
free(ext);
ext = NULL;
return copy;
}
/* Worker: builds the temp path and performs the chunked copy into
* the task's own state. No shared state is touched here. */
/* Pass one: pick the destination and open the copy (no bytes move).
* Leaves h->copy set on success, h->failed set on failure. */
static void runahead_copy_task_begin(runahead_copy_handle_t *h)
{
char tmp_path[PATH_MAX_LENGTH];
char *tmpdir = NULL;
char *dst = NULL;
const char *core_base = path_basename_nocompression(h->src_path);
h->started = true;
h->failed = true;
if ( !core_base || !*core_base
|| !(tmpdir = get_tmpdir_alloc(h->dir_libretro)))
return;
fill_pathname_join_special(tmp_path, tmpdir, "retroarch_temp",
sizeof(tmp_path));
if (path_mkdir(tmp_path) && runahead_tmp_dir_private(tmp_path))
{
strcat_alloc(&dst, tmp_path);
strcat_alloc(&dst, PATH_DEFAULT_SLASH());
strcat_alloc(&dst, core_base);
if ((h->copy = filestream_copy_begin(h->src_path, dst,
RETRO_VFS_COPY_OVERWRITE)))
{
h->copy_dst = dst;
dst = NULL;
h->failed = false;
}
/* Basename taken (another instance is running this core):
* fall back to a random name under the same directory. */
else if ((h->copy = copy_begin_with_random_name(&dst, tmp_path,
h->src_path)))
{
h->copy_dst = dst;
dst = NULL;
h->failed = false;
}
}
if (dst)
free(dst);
free(tmpdir);
}
/* Advances the copy by one share of the shared per-frame I/O window
* and returns. The task is re-entered next tick until the copy
* finishes, so a 40MB core never holds the handler for more than a
* window - and on a threaded task queue it never holds the frame at
* all. */
static void runahead_copy_task_handler(retro_task_t *task)
{
runahead_copy_handle_t *h;
int st;
if (!task)
return;
if (!(h = (runahead_copy_handle_t*)task->state))
{
task_set_flags(task, RETRO_TASK_FLG_FINISHED, true);
return;
}
if (!h->started)
{
runahead_copy_task_begin(h);
if (!h->copy)
{
task_set_flags(task, RETRO_TASK_FLG_FINISHED, true);
return;
}
/* Opening is this pass's work; the first bytes move next tick. */
return;
}
#ifdef HAVE_CORE_LOAD_CHECK
if (h->match)
{
nbio_budget_t budget;
st = 1;
task_nbio_slice_open(&budget);
while (st > 0 && task_nbio_slice_within_budget(&budget, 0, 0))
st = core_load_match_step(h->match);
task_nbio_slice_close(&budget);
if (st > 0)
return;
if (st == 0)
h->lib = core_load_match_load(h->match);
/* No second instance from a copy that doesn't match, or won't
* load: the main thread never opens it by path */
if (!h->lib)
{
h->mismatch = (st != 0);
h->failed = true;
}
core_load_match_free(h->match);
h->match = NULL;
task_set_flags(task, RETRO_TASK_FLG_FINISHED, true);
return;
}
#endif
if (!h->copy)
{
task_set_flags(task, RETRO_TASK_FLG_FINISHED, true);
return;
}
if (!(st = runahead_copy_step(h->copy)))
return; /* still running: come back next tick */
/* close() removes a partial destination when the copy did not
* finish, so a failure needs no cleanup of its own. */
filestream_copy_close(h->copy);
h->copy = NULL;
if (st > 0)
{
h->out_path = h->copy_dst;
h->copy_dst = NULL;
h->failed = false;
/* On the worker under the threaded queue: the frame loop goes
* on while the loader maps the copy. Unchecked, a copy that
* will not open is still published; the main thread's own open
* of it then reports why. */
#ifdef HAVE_CORE_LOAD_CHECK
if (h->check)
{
/* Hashed over the next ticks, then loaded from the file hashed */
if ((h->match = core_load_match_new(h->out_path, h->src_path,
&h->src)))
return;
h->mismatch = true;
h->failed = true;
}
else
#endif
h->lib = runloop_core_dylib_open(h->out_path, true);
}
else
{
free(h->copy_dst);
h->copy_dst = NULL;
h->failed = true;
}
task_set_flags(task, RETRO_TASK_FLG_FINISHED, true);
}
/* Main thread: publish the result to the slot - unless teardown
* abandoned the copy in the meantime, in which case discard it. */
static void runahead_copy_task_cb(retro_task_t *task,
void *task_data, void *user_data, const char *err)
{
runahead_copy_handle_t *h = NULL;
if (!task)
return;
if (!(h = (runahead_copy_handle_t*)task->state))
return;
/* The pipeline is free again in every case below - including
* the discard path: that task is done. */
runahead_copy_task_pending = false;
#ifdef HAVE_CORE_LOAD_CHECK
if (h->failed && h->out_path)
{
if (h->mismatch)
RARCH_WARN("[Run-Ahead] The core's copy doesn't match the running core: one instance.\n");
else
RARCH_WARN("[Run-Ahead] The core's copy won't load: one instance.\n");
filestream_delete(h->out_path);
free(h->out_path);
h->out_path = NULL;
}
#endif
if (h->generation != runahead_copy_generation)
{
/* Teardown happened while this copy was running (or after it
* finished but before this callback ran): discard. Closed
* first: a library still open cannot be deleted on Windows. */
if (h->lib)
{
dylib_close(h->lib);
h->lib = NULL;
}
if (h->out_path)
filestream_delete(h->out_path);
return;
}
runahead_copy_slot_path = h->out_path;
h->out_path = NULL;
runahead_copy_slot_lib = h->lib;
h->lib = NULL;
runahead_copy_slot_src = h->src_path;
h->src_path = NULL;
runahead_copy_slot_failed = h->failed;
runahead_copy_slot_done = true;
}
static void runahead_copy_task_free(retro_task_t *task)
{
runahead_copy_handle_t *h = NULL;
if (!task)
return;
if ((h = (runahead_copy_handle_t*)task->state))
{
#ifdef HAVE_CORE_LOAD_CHECK
core_load_match_free(h->match);
#endif
/* A task cancelled mid-copy still has one open: closing an
* unfinished copy removes the partial destination. */
if (h->copy)
filestream_copy_close(h->copy);
if (h->lib)
dylib_close(h->lib);
if (h->copy_dst)
free(h->copy_dst);
if (h->src_path)
free(h->src_path);
if (h->dir_libretro)
free(h->dir_libretro);
if (h->out_path)
free(h->out_path);
free(h);
}
task->state = NULL;
}
static bool runahead_copy_in_flight(void)
{
return runahead_copy_task_pending;
}
/* Main thread. Resets the result slot; if a copy is in flight, the
* callback is told to discard its result. delete_file also removes
* an already-published (but unconsumed) temp file. */
static void runahead_copy_reset(bool delete_file)
{
if (runahead_copy_slot_lib)
{
dylib_close(runahead_copy_slot_lib);
runahead_copy_slot_lib = NULL;
}
if (runahead_copy_slot_path)
{
if (delete_file)
filestream_delete(runahead_copy_slot_path);
free(runahead_copy_slot_path);
runahead_copy_slot_path = NULL;
}
if (runahead_copy_slot_src)
{
free(runahead_copy_slot_src);
runahead_copy_slot_src = NULL;
}
runahead_copy_slot_done = false;
runahead_copy_slot_failed = false;
/* Invalidate any copy that is in flight or awaiting its
* callback; see runahead_copy_generation. Note that
* runahead_copy_task_pending is deliberately NOT cleared here:
* a superseded task still occupies the pipeline until its
* callback runs, which keeps poll from starting an overlapping
* copy to the same destination path. */
runahead_copy_generation++;
}
/* Main thread. Polls / advances the async copy:
* - no result and no task -> push the task, report PENDING
* - task in flight -> PENDING
* - finished with failure -> UNAVAILABLE (slot reset, so a later
* attempt starts a fresh copy)
* - finished ok -> READY; ownership of the temp path is
* transferred to *out_path, and of its
* open handle (NULL if the task could not
* open it) to *out_lib */
static enum runahead_copy_status runahead_copy_poll(
const char *core_path, const char *dir_libretro,
char **out_path, dylib_t *out_lib)
{
/* A published result for a different core binary is stale
* (e.g. core switched without an intervening teardown, or any
* path we have not anticipated): drop it and start over. */
if ( runahead_copy_slot_done
&& !string_is_equal(runahead_copy_slot_src ? runahead_copy_slot_src : "",
core_path ? core_path : ""))
runahead_copy_reset(true);
if (!runahead_copy_slot_done)
{
if (!runahead_copy_in_flight())
{
retro_task_t *task = NULL;
runahead_copy_handle_t *h = NULL;
task = task_init();
h = (runahead_copy_handle_t*)calloc(1, sizeof(*h));
if (!task || !h)
{
if (task)
free(task);
if (h)
free(h);
return RUNAHEAD_COPY_UNAVAILABLE;
}
h->src_path = strdup(core_path);
h->dir_libretro = dir_libretro ? strdup(dir_libretro) : NULL;
h->generation = runahead_copy_generation;
#ifdef HAVE_CORE_LOAD_CHECK
switch (core_load_check_copy_source(core_path, &h->src))
{
case CORE_LOAD_COPY_NONE:
free(h->src_path);
free(h->dir_libretro);
free(h);
free(task);
return RUNAHEAD_COPY_UNAVAILABLE;
case CORE_LOAD_COPY_CHECK:
h->check = true;
break;
default:
break;
}
#endif
task->handler = runahead_copy_task_handler;
task->state = h;
task->title = NULL;
task->callback = runahead_copy_task_cb;
task->cleanup = runahead_copy_task_free;
task->flags |= RETRO_TASK_FLG_MUTE;
runahead_copy_task_pending = true;
task_queue_push(task);
}
return RUNAHEAD_COPY_PENDING;
}
if (runahead_copy_slot_failed)
{
runahead_copy_reset(false);
return RUNAHEAD_COPY_UNAVAILABLE;
}
*out_path = runahead_copy_slot_path;
runahead_copy_slot_path = NULL;
*out_lib = runahead_copy_slot_lib;
runahead_copy_slot_lib = NULL;
runahead_copy_reset(false);
return RUNAHEAD_COPY_READY;
/* note: the generation bump in the reset above also invalidates
* any concurrent task, which cannot exist here (poll is the only
* pusher and the slot was occupied) - harmless */
}
/* ===== END runahead core-copy fragment ===== */
/* What run-ahead's second instance may not register. The frontend
* holds one of each, keeps a pointer into the instance that declared
* it - a callback, a table or string in its image, its RAM - and the
* second instance is a copy of the running core, which has declared
* all of them already: taking the copy's would hand the running core's
* callbacks and memory to the copy, and leave them dangling once the
* copy is closed.
*
* Returns 1 to answer the declaration as accepted without storing it,
* 0 to refuse it (a hardware context is the running core's alone, so
* a hardware core has no second instance), -1 to pass it on. */
#define RUNAHEAD_ENV_BASE(cmd) ((cmd) & ~RETRO_ENVIRONMENT_EXPERIMENTAL)
static int runahead_secondary_env_filter(unsigned cmd)
{
switch (RUNAHEAD_ENV_BASE(cmd))
{
case RUNAHEAD_ENV_BASE(RETRO_ENVIRONMENT_SET_HW_RENDER):
case RUNAHEAD_ENV_BASE(RETRO_ENVIRONMENT_SET_HW_RENDER_CONTEXT_NEGOTIATION_INTERFACE):
case RUNAHEAD_ENV_BASE(RETRO_ENVIRONMENT_SET_HW_SHARED_CONTEXT):
return 0;
case RUNAHEAD_ENV_BASE(RETRO_ENVIRONMENT_SET_DISK_CONTROL_INTERFACE):
case RUNAHEAD_ENV_BASE(RETRO_ENVIRONMENT_SET_DISK_CONTROL_EXT_INTERFACE):
case RUNAHEAD_ENV_BASE(RETRO_ENVIRONMENT_SET_AUDIO_CALLBACK):
case RUNAHEAD_ENV_BASE(RETRO_ENVIRONMENT_SET_FRAME_TIME_CALLBACK):
case RUNAHEAD_ENV_BASE(RETRO_ENVIRONMENT_SET_AUDIO_BUFFER_STATUS_CALLBACK):
case RUNAHEAD_ENV_BASE(RETRO_ENVIRONMENT_SET_CORE_OPTIONS_UPDATE_DISPLAY_CALLBACK):
case RUNAHEAD_ENV_BASE(RETRO_ENVIRONMENT_SET_MEMORY_MAPS):
case RUNAHEAD_ENV_BASE(RETRO_ENVIRONMENT_SET_CONTROLLER_INFO):
case RUNAHEAD_ENV_BASE(RETRO_ENVIRONMENT_SET_SUBSYSTEM_INFO):
case RUNAHEAD_ENV_BASE(RETRO_ENVIRONMENT_SET_INPUT_DESCRIPTORS):
case RUNAHEAD_ENV_BASE(RETRO_ENVIRONMENT_SET_NETPACKET_INTERFACE):
return 1;
default:
break;
}
return -1;
}
#undef RUNAHEAD_ENV_BASE
static bool runloop_environment_secondary_core_hook(
unsigned cmd, void *data)
{
runloop_state_t *runloop_st = runloop_state_get_ptr();
int filtered = runahead_secondary_env_filter(cmd);
bool result;
if (filtered >= 0)
return filtered != 0;
/* The running core keeps its keyboard callback; the copy's has a
* slot of its own, emptied when the copy is closed */
if ( (cmd & ~RETRO_ENVIRONMENT_EXPERIMENTAL)
== RETRO_ENVIRONMENT_SET_KEYBOARD_CALLBACK)
{
const struct retro_keyboard_callback *info =
(const struct retro_keyboard_callback*)data;
runloop_st->secondary_key_event = info ? info->callback : NULL;
return true;
}
result = runloop_environment_cb(cmd, data);
if (runloop_st->flags & RUNLOOP_FLAG_HAS_VARIABLE_UPDATE)
{
if (cmd == RETRO_ENVIRONMENT_GET_VARIABLE_UPDATE)
{
bool *bool_p = (bool*)data;
*bool_p = true;
runloop_st->flags &= ~RUNLOOP_FLAG_HAS_VARIABLE_UPDATE;
return true;
}
else if (cmd == RETRO_ENVIRONMENT_GET_VARIABLE)
runloop_st->flags &= ~RUNLOOP_FLAG_HAS_VARIABLE_UPDATE;
}
return result;
}
void runahead_clear_controller_port_map(void *data)
{
int i;
runloop_state_t *runloop_st = (runloop_state_t*)data;
for (i = 0; i < MAX_USERS; i++)
runloop_st->port_map[i] = -1;
}
static enum runahead_copy_status secondary_core_create(
runloop_state_t *runloop_st,
const char *path_directory_libretro, unsigned num_active_users)
{
enum runahead_copy_status copy_status;
char *copied_path = NULL;
dylib_t copied_lib = NULL;
const enum rarch_core_type
last_core_type = runloop_st->last_core_type;
rarch_system_info_t *sys_info = &runloop_st->system;
uint8_t flags = content_get_flags();
if ( (last_core_type != CORE_TYPE_PLAIN)
|| (!runloop_st->load_content_info)
|| ( runloop_st->load_content_info->special))
return RUNAHEAD_COPY_UNAVAILABLE;
/* The core binary is duplicated by a task; until the copy
* completes this reports PENDING and the caller runs the
* single-instance fallback for the frame - no stall. */
copy_status = runahead_copy_poll(
path_get(RARCH_PATH_CORE), path_directory_libretro,
&copied_path, &copied_lib);
if (copy_status != RUNAHEAD_COPY_READY)
return copy_status;
if (runloop_st->secondary_library_path)
free(runloop_st->secondary_library_path);
runloop_st->secondary_library_path = copied_path;
/* Opened by the task; runloop_init_libretro_symbols takes it as
* it is and opens the path itself only when this is NULL. */
runloop_st->secondary_lib_handle = copied_lib;
/* Load Core */
if (!runloop_init_libretro_symbols(runloop_st,
CORE_TYPE_PLAIN, &runloop_st->secondary_core,
runloop_st->secondary_library_path,
&runloop_st->secondary_lib_handle))
return false;
runloop_st->secondary_core.flags |= RETRO_CORE_FLAG_SYMBOLS_INITED;
runloop_st->secondary_core.retro_set_environment(
runloop_environment_secondary_core_hook);
runloop_st->flags |= RUNLOOP_FLAG_HAS_VARIABLE_UPDATE;
runloop_st->secondary_core.retro_init();
if (flags & CONTENT_ST_FLAG_IS_INITED)
runloop_st->secondary_core.flags |= RETRO_CORE_FLAG_INITED;
else
runloop_st->secondary_core.flags &= ~RETRO_CORE_FLAG_INITED;
/* Load Content */
/* disabled due to crashes */
if ( (!runloop_st->load_content_info)
|| (runloop_st->load_content_info->special))
return false;
if ( ( runloop_st->load_content_info->content->size > 0)
&& runloop_st->load_content_info->content->elems[0].data)
{
if (!runloop_st->secondary_core.retro_load_game(
runloop_st->load_content_info->info))
{
runloop_st->secondary_core.flags &= ~RETRO_CORE_FLAG_GAME_LOADED;
goto error;
}
runloop_st->secondary_core.flags |= RETRO_CORE_FLAG_GAME_LOADED;
}
else if (flags & CONTENT_ST_FLAG_CORE_DOES_NOT_NEED_CONTENT)
{
if (!runloop_st->secondary_core.retro_load_game(NULL))
{
runloop_st->secondary_core.flags &= ~RETRO_CORE_FLAG_GAME_LOADED;
goto error;
}
runloop_st->secondary_core.flags |= RETRO_CORE_FLAG_GAME_LOADED;
}
else
runloop_st->secondary_core.flags &= ~RETRO_CORE_FLAG_GAME_LOADED;
if (!(runloop_st->secondary_core.flags & RETRO_CORE_FLAG_INITED))
goto error;
core_set_default_callbacks(&runloop_st->secondary_callbacks);
runloop_st->secondary_core.retro_set_video_refresh(
runloop_st->secondary_callbacks.frame_cb);
runloop_st->secondary_core.retro_set_audio_sample(
runloop_st->secondary_callbacks.sample_cb);
runloop_st->secondary_core.retro_set_audio_sample_batch(
runloop_st->secondary_callbacks.sample_batch_cb);
runloop_st->secondary_core.retro_set_input_state(
runloop_st->secondary_callbacks.state_cb);
runloop_st->secondary_core.retro_set_input_poll(
runloop_st->secondary_callbacks.poll_cb);
if (sys_info)
{
ssize_t port;
for (port = 0; port < MAX_USERS; port++)
{
if (port < (ssize_t)sys_info->ports.size)
{
unsigned device = (port < (ssize_t)num_active_users)
? runloop_st->port_map[port]
: RETRO_DEVICE_NONE;
runloop_st->secondary_core.retro_set_controller_port_device(
(unsigned)port, device);
}
}
}
#if defined(HAVE_DYNAMIC)
runahead_clear_controller_port_map(runloop_st);
#endif
return RUNAHEAD_COPY_READY;
error:
runahead_secondary_core_destroy(runloop_st);
return RUNAHEAD_COPY_UNAVAILABLE;
}
#if defined(HAVE_DYNAMIC)
enum runahead_copy_status secondary_core_ensure_exists(void *data,
settings_t *settings)
{
runloop_state_t *runloop_st = (runloop_state_t*)data;
const char *path_directory_libretro = settings->paths.directory_libretro;
unsigned input_max_users = settings->uints.input_max_users;
if (!runloop_st->secondary_lib_handle)
return secondary_core_create(runloop_st, path_directory_libretro,
input_max_users);
return RUNAHEAD_COPY_READY;
}
#endif
#if defined(HAVE_DYNAMIC)
static bool secondary_core_deserialize(runloop_state_t *runloop_st,
settings_t *settings, const void *data, size_t len)
{
bool ret = false;
enum runahead_copy_status status =
secondary_core_ensure_exists(runloop_st, settings);
if (status == RUNAHEAD_COPY_READY)
{
runloop_st->flags |= RUNLOOP_FLAG_REQUEST_SPECIAL_SAVESTATE;
ret = runloop_st->secondary_core.retro_unserialize(data, len);
runloop_st->flags &= ~RUNLOOP_FLAG_REQUEST_SPECIAL_SAVESTATE;
}
else if (status == RUNAHEAD_COPY_UNAVAILABLE)
runahead_secondary_core_destroy(runloop_st);
/* PENDING: copy still in flight - report failure quietly and
* leave the task running; do NOT destroy (that would abandon
* the copy and restart it on the next call). */
return ret;
}
#endif
static void secondary_core_input_poll_null(void) { }
static bool secondary_core_run_use_last_input(runloop_state_t *runloop_st)
{
retro_input_poll_t old_poll_function;
retro_input_state_t old_input_function;
{
enum runahead_copy_status status =
secondary_core_ensure_exists(runloop_st, config_get_ptr());
if (status != RUNAHEAD_COPY_READY)
{
if (status == RUNAHEAD_COPY_UNAVAILABLE)
runahead_secondary_core_destroy(runloop_st);
/* PENDING: leave the copy task running */
return false;
}
}
old_poll_function = runloop_st->secondary_callbacks.poll_cb;
old_input_function = runloop_st->secondary_callbacks.state_cb;
runloop_st->secondary_callbacks.poll_cb = secondary_core_input_poll_null;
runloop_st->secondary_callbacks.state_cb = input_state_get_last;
runloop_st->secondary_core.retro_set_input_poll(
runloop_st->secondary_callbacks.poll_cb);
runloop_st->secondary_core.retro_set_input_state(
runloop_st->secondary_callbacks.state_cb);
runloop_st->secondary_core.retro_run();
runloop_st->secondary_callbacks.poll_cb = old_poll_function;
runloop_st->secondary_callbacks.state_cb = old_input_function;
runloop_st->secondary_core.retro_set_input_poll(
runloop_st->secondary_callbacks.poll_cb);
runloop_st->secondary_core.retro_set_input_state(
runloop_st->secondary_callbacks.state_cb);
return true;
}
void runahead_remember_controller_port_device(void *data,
long port, long device)
{
runloop_state_t *runloop_st = (runloop_state_t*)data;
if (port >= 0 && port < MAX_USERS)
runloop_st->port_map[port] = (int)device;
if ( runloop_st->secondary_lib_handle
&& runloop_st->secondary_core.retro_set_controller_port_device)
runloop_st->secondary_core.retro_set_controller_port_device((unsigned)port, (unsigned)device);
}
#else
/* No secondary instance in this build: the callers that would use
* one get 'unavailable' and stay on the single-instance method, and
* the port map they keep for it has nothing to remember. */
void runahead_secondary_core_destroy(void *data) { (void)data; }
enum runahead_copy_status secondary_core_ensure_exists(void *data,
settings_t *settings)
{
(void)data; (void)settings;
return RUNAHEAD_COPY_UNAVAILABLE;
}
void runahead_clear_controller_port_map(void *data) { (void)data; }
void runahead_remember_controller_port_device(void *data,
long port, long device)
{
(void)data; (void)port; (void)device;
}
#endif
static int16_t runahead_input_state_with_logging(unsigned port,
unsigned device, unsigned index, unsigned id)
{
runloop_state_t *runloop_st = runloop_state_get_ptr();
if (runloop_st->input_state_callback_original)
{
int16_t result =
runloop_st->input_state_callback_original(
port, device, index, id);
int16_t last_input =
input_state_get_last(port, device, index, id);
if (result != last_input)
runloop_st->flags |= RUNLOOP_FLAG_INPUT_IS_DIRTY;
/*arbitrary limit of up to 65536 elements in state array*/
if (id < 65536)
runahead_input_state_set_last(port, device, index, id, result);
return result;
}
return 0;
}
static void runahead_reset_hook(void)
{
runloop_state_t *runloop_st = runloop_state_get_ptr();
runloop_st->flags |= RUNLOOP_FLAG_INPUT_IS_DIRTY;
if (runloop_st->retro_reset_callback_original)
runloop_st->retro_reset_callback_original();
}
static bool runahead_unserialize_hook(const void *buf, size_t len)
{
runloop_state_t *runloop_st = runloop_state_get_ptr();
runloop_st->flags |= RUNLOOP_FLAG_INPUT_IS_DIRTY;
if (runloop_st->retro_unserialize_callback_original)
return runloop_st->retro_unserialize_callback_original(buf, len);
return false;
}
static void runahead_add_input_state_hook(runloop_state_t *runloop_st)
{
struct retro_callbacks *cbs = &runloop_st->retro_ctx;
if (!runloop_st->input_state_callback_original)
{
/* Allocated once, here; the polls that follow allocate nothing
* for the common tuples. Without it, everything takes the list. */
if (!runahead_dense)
runahead_dense = (runahead_dense_cache_t*)calloc(1, sizeof(*runahead_dense));
runloop_st->input_state_callback_original = cbs->state_cb;
cbs->state_cb = runahead_input_state_with_logging;
runloop_st->current_core.retro_set_input_state(cbs->state_cb);
}
if (!runloop_st->retro_reset_callback_original)
{
runloop_st->retro_reset_callback_original
= runloop_st->current_core.retro_reset;
runloop_st->current_core.retro_reset = runahead_reset_hook;
}
if (!runloop_st->retro_unserialize_callback_original)
{
runloop_st->retro_unserialize_callback_original = runloop_st->current_core.retro_unserialize;
runloop_st->current_core.retro_unserialize = runahead_unserialize_hook;
}
}
static void runahead_remove_input_state_hook(runloop_state_t *runloop_st)
{
struct retro_callbacks *cbs = &runloop_st->retro_ctx;
if (runloop_st->input_state_callback_original)
{
cbs->state_cb =
runloop_st->input_state_callback_original;
runloop_st->current_core.retro_set_input_state(cbs->state_cb);
runloop_st->input_state_callback_original = NULL;
runahead_input_list_free(&runahead_input_list);
free(runahead_dense);
runahead_dense = NULL;
}
if (runloop_st->retro_reset_callback_original)
{
runloop_st->current_core.retro_reset =
runloop_st->retro_reset_callback_original;
runloop_st->retro_reset_callback_original = NULL;
}
if (runloop_st->retro_unserialize_callback_original)
{
runloop_st->current_core.retro_unserialize =
runloop_st->retro_unserialize_callback_original;
runloop_st->retro_unserialize_callback_original = NULL;
}
}
static bool runahead_savestate_info_init(
runloop_state_t *runloop_st,
size_t save_state_size)
{
retro_ctx_serialize_info_t *info = &runloop_st->runahead_savestate_info;
runloop_st->flags |= RUNLOOP_FLAG_RUNAHEAD_SAVE_STATE_SIZE_KNOWN;
/* Free any previous buffer so callers can safely re-init. The
* matching invariant (data NULL when nothing allocated) lets the
* per-frame save_state/load_state path use the buffer pointer as
* its readiness check. */
free(info->data);
info->data = NULL;
info->data_const = NULL;
info->size = 0;
if (save_state_size == 0)
return false;
info->data = malloc(save_state_size);
if (!info->data)
return false;
info->data_const = info->data;
info->size = save_state_size;
return true;
}
static void runahead_savestate_info_free(runloop_state_t *runloop_st)
{
retro_ctx_serialize_info_t *info = &runloop_st->runahead_savestate_info;
free(info->data);
info->data = NULL;
info->data_const = NULL;
info->size = 0;
}
/* Hooks - Hooks to cleanup, and add dirty input hooks */
static void runahead_remove_hooks(runloop_state_t *runloop_st)
{
if (runloop_st->original_retro_deinit)
{
runloop_st->current_core.retro_deinit =
runloop_st->original_retro_deinit;
runloop_st->original_retro_deinit = NULL;
}
if (runloop_st->original_retro_unload)
{
runloop_st->current_core.retro_unload_game =
runloop_st->original_retro_unload;
runloop_st->original_retro_unload = NULL;
}
runahead_remove_input_state_hook(runloop_st);
}
static void runahead_destroy(runloop_state_t *runloop_st)
{
runahead_savestate_info_free(runloop_st);
runahead_remove_hooks(runloop_st);
runahead_clear_variables(runloop_st);
}
static void runahead_unload_hook(void)
{
runloop_state_t *runloop_st = runloop_state_get_ptr();
runahead_remove_hooks(runloop_st);
runahead_destroy(runloop_st);
runahead_secondary_core_destroy(runloop_st);
if (runloop_st->current_core.retro_unload_game)
runloop_st->current_core.retro_unload_game();
runloop_st->core_poll_type_override = POLL_TYPE_OVERRIDE_DONTCARE;
}
static void runahead_deinit_hook(void)
{
runloop_state_t *runloop_st = runloop_state_get_ptr();
runahead_remove_hooks(runloop_st);
runahead_destroy(runloop_st);
runahead_secondary_core_destroy(runloop_st);
if (runloop_st->current_core.retro_deinit)
runloop_st->current_core.retro_deinit();
}
static void runahead_add_hooks(runloop_state_t *runloop_st)
{
if (!runloop_st->original_retro_deinit)
{
runloop_st->original_retro_deinit =
runloop_st->current_core.retro_deinit;
runloop_st->current_core.retro_deinit = runahead_deinit_hook;
}
if (!runloop_st->original_retro_unload)
{
runloop_st->original_retro_unload = runloop_st->current_core.retro_unload_game;
runloop_st->current_core.retro_unload_game = runahead_unload_hook;
}
runahead_add_input_state_hook(runloop_st);
}
/* Runahead Code */
static void runahead_err(runloop_state_t *runloop_st)
{
runloop_st->flags &= ~RUNLOOP_FLAG_RUNAHEAD_AVAILABLE;
runahead_savestate_info_free(runloop_st);
runahead_remove_hooks(runloop_st);
runloop_st->flags |= RUNLOOP_FLAG_RUNAHEAD_SAVE_STATE_SIZE_KNOWN;
}
static bool runahead_create(runloop_state_t *runloop_st)
{
/* get savestate size and allocate buffer */
video_driver_state_t *video_st = video_state_get_ptr();
size_t info_size = core_serialize_size_special();
if (!runahead_savestate_info_init(runloop_st, info_size))
{
runahead_err(runloop_st);
return false;
}
if (video_st->main_flags & VIDEO_FLAG_ACTIVE)
video_driver_modify_disp_flags(VIDEO_FLAG_RUNAHEAD_IS_ACTIVE, 0);
else
video_driver_modify_disp_flags(0, VIDEO_FLAG_RUNAHEAD_IS_ACTIVE);
runahead_add_hooks(runloop_st);
runloop_st->flags |= RUNLOOP_FLAG_RUNAHEAD_FORCE_INPUT_DIRTY;
return true;
}
static bool runahead_save_state(runloop_state_t *runloop_st)
{
retro_ctx_serialize_info_t *info = &runloop_st->runahead_savestate_info;
if (info->data)
{
if (core_serialize_special(info))
return true;
runahead_err(runloop_st);
}
return false;
}
static bool runahead_load_state(runloop_state_t *runloop_st)
{
retro_ctx_serialize_info_t *info = &runloop_st->runahead_savestate_info;
bool last_dirty = (runloop_st->flags & RUNLOOP_FLAG_INPUT_IS_DIRTY) ? true : false;
bool ret = core_unserialize_special(info);
if (last_dirty)
runloop_st->flags |= RUNLOOP_FLAG_INPUT_IS_DIRTY;
else
runloop_st->flags &= ~RUNLOOP_FLAG_INPUT_IS_DIRTY;
if (!ret)
runahead_err(runloop_st);
return ret;
}
#if defined(HAVE_DYNAMIC)
static bool runahead_load_state_secondary(runloop_state_t *runloop_st, settings_t *settings)
{
retro_ctx_serialize_info_t *info = &runloop_st->runahead_savestate_info;
if (!secondary_core_deserialize(runloop_st, settings,
info->data_const, info->size))
{
runloop_st->flags &= ~RUNLOOP_FLAG_RUNAHEAD_SECONDARY_CORE_AVAILABLE;
runahead_err(runloop_st);
return false;
}
return true;
}
#endif
static void runahead_core_run_use_last_input(runloop_state_t *runloop_st)
{
struct retro_callbacks *cbs = &runloop_st->retro_ctx;
retro_input_poll_t old_poll_function = cbs->poll_cb;
retro_input_state_t old_input_function = cbs->state_cb;
cbs->poll_cb = retro_input_poll_null;
cbs->state_cb = input_state_get_last;
runloop_st->current_core.retro_set_input_poll(cbs->poll_cb);
runloop_st->current_core.retro_set_input_state(cbs->state_cb);
runloop_st->current_core.retro_run();
if (runloop_st->auto_state_load_pending)
runloop_st->auto_state_load_core_ran = true;
audio_driver_frame_end();
cbs->poll_cb = old_poll_function;
cbs->state_cb = old_input_function;
/* The core gets back the poll callback it was given at load, which
* polls only when the poll mode says the core's call is the one
* that does. cbs->poll_cb is not that: it is the frontend's own
* "poll now", input_driver_poll(). Handing it to the core made the
* core's input_poll an unconditional poll from the first frame of
* run-ahead on, so under late or early polling every frame polled
* twice - and went on doing so after run-ahead was switched off,
* until the core was reloaded. Turbo counts polls, so it ran at
* twice its rate. */
runloop_st->current_core.retro_set_input_poll(
runloop_st->input_poll_callback_original);
runloop_st->current_core.retro_set_input_state(cbs->state_cb);
}
bool runahead_run(void *data,
int runahead_count,
bool runahead_hide_warnings,
bool use_secondary)
{
runloop_state_t *runloop_st = (runloop_state_t*)data;
int frame_number = 0;
bool last_frame = false;
bool suspended_frame = false;
bool advanced = false;
#if defined(HAVE_DYNAMIC)
const bool have_dynamic = true;
settings_t *settings = config_get_ptr();
#else
const bool have_dynamic = false;
#endif
#if defined(HAVE_DYNAMIC)
enum runahead_copy_status sec_status = RUNAHEAD_COPY_UNAVAILABLE;
#else
const enum runahead_copy_status sec_status = RUNAHEAD_COPY_UNAVAILABLE;
#endif
video_driver_state_t
*video_st = video_state_get_ptr();
uint64_t frame_count = video_st->frame_count;
audio_driver_state_t
*audio_st = audio_state_get_ptr();
if ( runahead_count <= 0
|| !(runloop_st->flags & RUNLOOP_FLAG_RUNAHEAD_AVAILABLE))
goto force_input_dirty;
if (!(runloop_st->flags & RUNLOOP_FLAG_RUNAHEAD_SAVE_STATE_SIZE_KNOWN))
{
/* Disable runahead if current core reports
* that it has an insufficient savestate
* support level */
if (!core_info_current_supports_runahead())
{
runahead_err(runloop_st);
/* If core is incompatible with runahead,
* log a warning but do not spam OSD messages.
* Runahead menu entries are hidden when using
* incompatible cores, so there is no mechanism
* for users to respond to notifications. In
* addition, auto-disabling runahead is a feature,
* not a cause for 'concern'; OSD warnings should
* be reserved for when a core reports that it is
* runahead-compatible but subsequently fails in
* execution */
RARCH_WARN("[Run-Ahead] %s\n", msg_hash_to_str(MSG_RUNAHEAD_CORE_DOES_NOT_SUPPORT_RUNAHEAD));
goto force_input_dirty;
}
if (!runahead_create(runloop_st))
{
const char *_msg =
msg_hash_to_str(MSG_RUNAHEAD_CORE_DOES_NOT_SUPPORT_SAVESTATES);
if (!runahead_hide_warnings)
runloop_msg_queue_push(_msg, strlen(_msg), 0, 2 * 60, true, NULL,
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
RARCH_WARN("[Run-Ahead] %s\n", _msg);
goto force_input_dirty;
}
}
/* Check for GUI */
/* Hack: If we were in the GUI, force a resync. */
if (frame_count != runloop_st->runahead_last_frame_count + 1)
runloop_st->flags |= RUNLOOP_FLAG_RUNAHEAD_FORCE_INPUT_DIRTY;
runloop_st->runahead_last_frame_count = frame_count;
#if defined(HAVE_DYNAMIC)
if ( use_secondary
&& have_dynamic
&& (runloop_st->flags & RUNLOOP_FLAG_RUNAHEAD_SECONDARY_CORE_AVAILABLE))
{
sec_status = secondary_core_ensure_exists(runloop_st, config_get_ptr());
if (sec_status == RUNAHEAD_COPY_UNAVAILABLE)
{
const char *_msg =
msg_hash_to_str(MSG_RUNAHEAD_FAILED_TO_CREATE_SECONDARY_INSTANCE);
runahead_secondary_core_destroy(runloop_st);
runloop_st->flags &= ~RUNLOOP_FLAG_RUNAHEAD_SECONDARY_CORE_AVAILABLE;
runloop_msg_queue_push(_msg, strlen(_msg), 0, 3 * 60, true, NULL,
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
RARCH_WARN("[Run-Ahead] %s\n", _msg);
goto force_input_dirty;
}
/* PENDING: the copy task is still duplicating the core
* binary. Run the single-instance savestate method for this
* frame (identical output) and retry next frame; the
* secondary instance attaches seamlessly once ready, since
* it is (re)synchronised from a savestate every time it
* runs anyway. */
}
#endif
if ( !use_secondary
|| !have_dynamic
|| !(runloop_st->flags & RUNLOOP_FLAG_RUNAHEAD_SECONDARY_CORE_AVAILABLE)
|| (sec_status != RUNAHEAD_COPY_READY))
{
/* TODO: multiple savestates for higher performance
* when not using secondary core */
for (frame_number = 0; frame_number <= runahead_count; frame_number++)
{
last_frame = frame_number == runahead_count;
suspended_frame = !last_frame;
if (suspended_frame)
{
AUDIO_FLAGS_SET(audio_st, AUDIO_FLAG_SUSPENDED);
video_st->main_flags &= ~VIDEO_FLAG_ACTIVE;
}
if (frame_number == 0)
advanced = core_run();
else
runahead_core_run_use_last_input(runloop_st);
if (suspended_frame)
{
if ((uint32_t)retro_atomic_load_relaxed_int(&video_st->flags) & VIDEO_FLAG_RUNAHEAD_IS_ACTIVE)
video_st->main_flags |= VIDEO_FLAG_ACTIVE;
else
video_st->main_flags &= ~VIDEO_FLAG_ACTIVE;
AUDIO_FLAGS_CLEAR(audio_st, AUDIO_FLAG_SUSPENDED);
}
if (!advanced)
return false;
if (frame_number == 0)
{
if (!runahead_save_state(runloop_st))
{
const char *_msg =
msg_hash_to_str(MSG_RUNAHEAD_FAILED_TO_SAVE_STATE);
runloop_msg_queue_push(_msg, strlen(_msg), 0, 3 * 60, true, NULL,
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
RARCH_WARN("[Run-Ahead] %s\n", _msg);
return advanced;
}
}
if (last_frame)
{
if (!runahead_load_state(runloop_st))
{
const char *_msg = msg_hash_to_str(MSG_RUNAHEAD_FAILED_TO_LOAD_STATE);
runloop_msg_queue_push(_msg, strlen(_msg), 0, 3 * 60, true, NULL,
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
RARCH_WARN("[Run-Ahead] %s\n", _msg);
return advanced;
}
}
}
}
else
{
#if defined(HAVE_DYNAMIC)
/* sec_status == RUNAHEAD_COPY_READY here (checked above) */
/* run main core with video suspended */
video_st->main_flags &= ~VIDEO_FLAG_ACTIVE;
advanced = core_run();
if ((uint32_t)retro_atomic_load_relaxed_int(&video_st->flags) & VIDEO_FLAG_RUNAHEAD_IS_ACTIVE)
video_st->main_flags |= VIDEO_FLAG_ACTIVE;
else
video_st->main_flags &= ~VIDEO_FLAG_ACTIVE;
if (!advanced)
return false;
if ( (runloop_st->flags & RUNLOOP_FLAG_INPUT_IS_DIRTY)
|| (runloop_st->flags & RUNLOOP_FLAG_RUNAHEAD_FORCE_INPUT_DIRTY))
{
runloop_st->flags &= ~RUNLOOP_FLAG_INPUT_IS_DIRTY;
if (!runahead_save_state(runloop_st))
{
const char *_msg = msg_hash_to_str(MSG_RUNAHEAD_FAILED_TO_SAVE_STATE);
runloop_msg_queue_push(_msg, strlen(_msg), 0, 3 * 60, true, NULL,
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
RARCH_WARN("[Run-Ahead] %s\n", _msg);
return advanced;
}
if (!runahead_load_state_secondary(runloop_st, settings))
{
const char *_msg = msg_hash_to_str(MSG_RUNAHEAD_FAILED_TO_LOAD_STATE);
runloop_msg_queue_push(_msg, strlen(_msg), 0, 3 * 60, true, NULL,
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
RARCH_WARN("[Run-Ahead] %s\n", _msg);
return advanced;
}
for (frame_number = 0; frame_number < runahead_count - 1; frame_number++)
{
video_st->main_flags &= ~VIDEO_FLAG_ACTIVE;
AUDIO_FLAGS_SET(audio_st, AUDIO_FLAG_SUSPENDED | AUDIO_FLAG_HARD_DISABLE);
if (secondary_core_run_use_last_input(runloop_st))
runloop_st->flags |= RUNLOOP_FLAG_RUNAHEAD_SECONDARY_CORE_AVAILABLE;
else
runloop_st->flags &= ~RUNLOOP_FLAG_RUNAHEAD_SECONDARY_CORE_AVAILABLE;
AUDIO_FLAGS_CLEAR(audio_st, AUDIO_FLAG_SUSPENDED | AUDIO_FLAG_HARD_DISABLE);
if ((uint32_t)retro_atomic_load_relaxed_int(&video_st->flags) & VIDEO_FLAG_RUNAHEAD_IS_ACTIVE)
video_st->main_flags |= VIDEO_FLAG_ACTIVE;
else
video_st->main_flags &= ~VIDEO_FLAG_ACTIVE;
}
}
AUDIO_FLAGS_SET(audio_st, AUDIO_FLAG_SUSPENDED | AUDIO_FLAG_HARD_DISABLE);
if (secondary_core_run_use_last_input(runloop_st))
runloop_st->flags |= RUNLOOP_FLAG_RUNAHEAD_SECONDARY_CORE_AVAILABLE;
else
runloop_st->flags &= ~RUNLOOP_FLAG_RUNAHEAD_SECONDARY_CORE_AVAILABLE;
AUDIO_FLAGS_CLEAR(audio_st, AUDIO_FLAG_SUSPENDED | AUDIO_FLAG_HARD_DISABLE);
#endif
}
runloop_st->flags &= ~RUNLOOP_FLAG_RUNAHEAD_FORCE_INPUT_DIRTY;
return advanced;
force_input_dirty:
advanced = core_run();
runloop_st->flags |= RUNLOOP_FLAG_RUNAHEAD_FORCE_INPUT_DIRTY;
return advanced;
}
/* Preemptive Frames */
/* ===== BEGIN preempt analog-mask bit =====
* The bit of analog_mask an (index, id) pair asks for: the left and
* right sticks' two axes in bits 0..3, the analog buttons in bits
* 4..19. A pair outside that - an index past the buttons, an id past
* the sixteen buttons or the two axes - has no bit, and is rejected
* rather than folded onto one that exists. */
static bool preempt_analog_mask_bit(unsigned index, unsigned id, unsigned *bit)
{
if (index == RETRO_DEVICE_INDEX_ANALOG_BUTTON)
{
if (id >= 16)
return false;
*bit = 4 + id;
return true;
}
if (index > RETRO_DEVICE_INDEX_ANALOG_RIGHT || id > RETRO_DEVICE_ID_ANALOG_Y)
return false;
*bit = index * 2 + id;
return true;
}
/* ===== END preempt analog-mask bit ===== */
static int16_t preempt_input_state(unsigned port,
unsigned device, unsigned index, unsigned id)
{
runloop_state_t *runloop_st = runloop_state_get_ptr();
preempt_t *preempt = runloop_st->preempt_data;
unsigned device_class = device & RETRO_DEVICE_MASK;
/* A port the state has no slot for is not one this core has: no
* input, and nothing written past the arrays. */
if (port >= MAX_USERS)
return 0;
switch (device_class)
{
case RETRO_DEVICE_ANALOG:
{
/* Add requested inputs to mask */
unsigned bit;
if (preempt_analog_mask_bit(index, id, &bit))
preempt->analog_mask[port] |= (1u << bit);
break;
}
case RETRO_DEVICE_LIGHTGUN:
case RETRO_DEVICE_POINTER:
/* Set pointing device for this port */
preempt->ptr_dev_needed[port] = device_class;
break;
case RETRO_DEVICE_MOUSE:
/* Set pointing device and return stored x,y */
if (id <= RETRO_DEVICE_ID_MOUSE_Y)
{
preempt->ptr_dev_needed[port] = device_class;
if (preempt->ptr_dev_polled[port] == device_class)
return preempt->ptrdev_state[port][id];
}
break;
default:
break;
}
return input_driver_state_wrapper(port, device, index, id);
}
/* ===== BEGIN preempt slab =====
* The frame buffers are one allocation, frames * state_size bytes,
* with buffer[i] at i * state_size from the base; buffer[0] is the
* base and the one to free. The product is checked before it is
* asked for. */
static bool preempt_slab_alloc(void **buffer, unsigned frames, size_t state_size)
{
unsigned i;
uint8_t *base;
if (!frames || !state_size || state_size > ((size_t)-1) / frames)
return false;
if (!(base = (uint8_t*)malloc((size_t)frames * state_size)))
return false;
for (i = 0; i < frames; i++)
buffer[i] = base + (size_t)i * state_size;
return true;
}
/* ===== END preempt slab ===== */
static const char* preempt_allocate(runloop_state_t *runloop_st,
const uint8_t frames)
{
size_t info_size;
preempt_t *preempt = (preempt_t*)calloc(1, sizeof(preempt_t));
if (!(runloop_st->preempt_data = preempt))
return msg_hash_to_str(MSG_PREEMPT_FAILED_TO_ALLOCATE);
info_size = core_serialize_size_special();
if (!info_size)
return msg_hash_to_str(MSG_PREEMPT_CORE_DOES_NOT_SUPPORT_SAVESTATES);
preempt->state_size = info_size;
preempt->frames = frames;
if (!preempt_slab_alloc(preempt->buffer, frames, info_size))
return msg_hash_to_str(MSG_PREEMPT_FAILED_TO_ALLOCATE);
return NULL;
}
/**
* preempt_deinit:
*
* Frees preempt object and unsets overrides.
**/
void preempt_deinit(void *data)
{
runloop_state_t *runloop_st = (runloop_state_t*)data;
preempt_t *preempt = runloop_st->preempt_data;
struct retro_core_t *current_core = &runloop_st->current_core;
if (!preempt)
return;
/* One slab; buffer[0] is its base. */
free(preempt->buffer[0]);
free(preempt);
runloop_st->preempt_data = NULL;
/* Undo overrides */
runloop_st->flags |= (RUNLOOP_FLAG_RUNAHEAD_AVAILABLE
| RUNLOOP_FLAG_RUNAHEAD_SECONDARY_CORE_AVAILABLE);
if (current_core->retro_set_input_poll)
current_core->retro_set_input_poll(runloop_st->input_poll_callback_original);
if (current_core->retro_set_input_state)
current_core->retro_set_input_state(runloop_st->retro_ctx.state_cb);
}
/**
* preempt_init:
*
* @return true on success, false on failure
*
* Allocates savestate buffer and sets overrides for preemptive frames.
**/
bool preempt_init(void *data)
{
runloop_state_t *runloop_st = (runloop_state_t*)data;
settings_t *settings = config_get_ptr();
const char *_msg = NULL;
bool preemptive_frames_enable = settings->bools.preemptive_frames_enable;
unsigned run_ahead_frames = settings->uints.run_ahead_frames;
bool run_ahead_hide_warnings = settings->bools.run_ahead_hide_warnings;
if ( runloop_st->preempt_data
|| !preemptive_frames_enable
|| !run_ahead_frames
|| !(runloop_st->current_core.flags & RETRO_CORE_FLAG_GAME_LOADED))
return false;
/* Check if supported - same requirements as runahead */
if (!core_info_current_supports_runahead())
{
_msg = msg_hash_to_str(MSG_PREEMPT_CORE_DOES_NOT_SUPPORT_PREEMPT);
goto error;
}
/* Set flags to block runahead and request 'same instance' states */
runloop_st->flags &= ~(RUNLOOP_FLAG_RUNAHEAD_AVAILABLE
| RUNLOOP_FLAG_RUNAHEAD_SECONDARY_CORE_AVAILABLE);
/* Run at least one frame before attempting
* retro_serialize_size or retro_serialize */
if (video_state_get_ptr()->frame_count == 0)
{
runloop_st->current_core.retro_run();
if (runloop_st->auto_state_load_pending)
runloop_st->auto_state_load_core_ran = true;
audio_driver_frame_end();
}
/* Allocate - same 'frames' setting as runahead */
if ((_msg = preempt_allocate(runloop_st, run_ahead_frames)))
goto error;
/* Only poll in preempt_run() */
runloop_st->current_core.retro_set_input_poll(retro_input_poll_null);
/* Track requested analog states and pointing device types */
runloop_st->current_core.retro_set_input_state(preempt_input_state);
return true;
error:
preempt_deinit(runloop_st);
if (!run_ahead_hide_warnings)
runloop_msg_queue_push(_msg, strlen(_msg), 0, 2 * 60, true, NULL,
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
RARCH_WARN("[Run-Ahead Preemptive] %s\n", _msg);
return false;
}
static INLINE bool preempt_analog_input_dirty(preempt_t *preempt,
retro_input_state_t state_cb, unsigned port)
{
int16_t state[20] = {0};
uint8_t base, i;
/* axes */
for (i = 0; i < 2; i++)
{
base = i * 2;
if (preempt->analog_mask[port] & (1 << (base )))
state[base ] = state_cb(port, RETRO_DEVICE_ANALOG, i, 0);
if (preempt->analog_mask[port] & (1 << (base + 1)))
state[base + 1] = state_cb(port, RETRO_DEVICE_ANALOG, i, 1);
}
/* buttons */
if (preempt->analog_mask[port] & 0xfff0)
{
for (i = 0; i < RARCH_FIRST_CUSTOM_BIND; i++)
{
if (preempt->analog_mask[port] & (1 << (i + 4)))
state[i + 4] = state_cb(port, RETRO_DEVICE_ANALOG,
RETRO_DEVICE_INDEX_ANALOG_BUTTON, i);
}
}
if (memcmp(preempt->analog_state[port], state, sizeof(state)) == 0)
return false;
memcpy(preempt->analog_state[port], state, sizeof(state));
return true;
}
static INLINE bool preempt_ptr_input_dirty(preempt_t *preempt,
retro_input_state_t state_cb, unsigned device, unsigned port)
{
int16_t state[4] = {0};
unsigned count_id = 0;
unsigned x_id = 0;
unsigned id, max_id;
switch (device)
{
case RETRO_DEVICE_MOUSE:
max_id = RETRO_DEVICE_ID_MOUSE_BUTTON_5;
break;
case RETRO_DEVICE_LIGHTGUN:
x_id = RETRO_DEVICE_ID_LIGHTGUN_SCREEN_X;
max_id = RETRO_DEVICE_ID_LIGHTGUN_DPAD_RIGHT;
break;
case RETRO_DEVICE_POINTER:
max_id = RETRO_DEVICE_ID_POINTER_PRESSED;
count_id = RETRO_DEVICE_ID_POINTER_COUNT;
break;
default:
return false;
}
/* x, y */
state[0] = state_cb(port, device, 0, x_id );
state[1] = state_cb(port, device, 0, x_id + 1);
/* buttons */
for (id = 2; id <= max_id; id++)
state[2] |= state_cb(port, device, 0, id) ? 1 << id : 0;
/* ptr count */
if (count_id)
state[3] = state_cb(port, device, 0, count_id);
if (memcmp(preempt->ptrdev_state[port], state, sizeof(state)) == 0)
return false;
memcpy(preempt->ptrdev_state[port], state, sizeof(state));
return true;
}
static INLINE void preempt_input_poll(preempt_t *preempt,
runloop_state_t *runloop_st, unsigned max_users)
{
size_t p;
retro_input_state_t state_cb = input_driver_state_wrapper;
input_driver_poll();
/* Check for input state changes */
for (p = 0; p < max_users; p++)
{
/* Check full digital joypad */
int16_t joypad_state = (int16_t)(state_cb((unsigned)p, RETRO_DEVICE_JOYPAD,
0, RETRO_DEVICE_ID_JOYPAD_MASK));
if (joypad_state != preempt->joypad_state[p])
{
preempt->joypad_state[p] = joypad_state;
runloop_st->flags |= RUNLOOP_FLAG_INPUT_IS_DIRTY;
}
/* Check requested analogs */
if ( preempt->analog_mask[p]
&& preempt_analog_input_dirty(preempt, state_cb, (unsigned)p))
{
runloop_st->flags |= RUNLOOP_FLAG_INPUT_IS_DIRTY;
preempt->analog_mask[p] = 0;
}
/* Check requested pointing device */
if (preempt->ptr_dev_needed[p])
{
if (preempt_ptr_input_dirty(
preempt, state_cb, preempt->ptr_dev_needed[p], (unsigned)p))
runloop_st->flags |= RUNLOOP_FLAG_INPUT_IS_DIRTY;
preempt->ptr_dev_polled[p] = preempt->ptr_dev_needed[p];
preempt->ptr_dev_needed[p] = RETRO_DEVICE_NONE;
}
}
}
/* macro for preempt_run */
#define PREEMPT_NEXT_PTR(x) ((x + 1) % preempt->frames)
/**
* preempt_run:
* @preempt : pointer to preemptive frames object
*
* Call in place of core_run() for preemptive frames.
**/
bool preempt_run(preempt_t *preempt, void *data)
{
runloop_state_t *runloop_st = (runloop_state_t*)data;
struct retro_core_t *current_core = &runloop_st->current_core;
const char *_msg = NULL;
audio_driver_state_t *audio_st = audio_state_get_ptr();
settings_t *settings = config_get_ptr();
unsigned input_max_users = settings->uints.input_max_users;
bool run_ahead_hide_warnings = settings->bools.run_ahead_hide_warnings;
/* Poll and check for dirty input */
preempt_input_poll(preempt, runloop_st, input_max_users);
runloop_st->flags |= RUNLOOP_FLAG_REQUEST_SPECIAL_SAVESTATE;
if ((runloop_st->flags & RUNLOOP_FLAG_INPUT_IS_DIRTY)
&& preempt->frame_count >= preempt->frames)
{
/* Suspend A/V and run preemptive frames */
AUDIO_FLAGS_SET(audio_st, AUDIO_FLAG_SUSPENDED);
video_state_get_ptr()->main_flags &= ~VIDEO_FLAG_ACTIVE;
if (!current_core->retro_unserialize(
preempt->buffer[preempt->start_ptr], preempt->state_size))
{
_msg = msg_hash_to_str(MSG_PREEMPT_FAILED_TO_LOAD_STATE);
goto error;
}
current_core->retro_run();
if (runloop_st->auto_state_load_pending)
runloop_st->auto_state_load_core_ran = true;
preempt->replay_ptr = PREEMPT_NEXT_PTR(preempt->start_ptr);
while (preempt->replay_ptr != preempt->start_ptr)
{
if (!current_core->retro_serialize(
preempt->buffer[preempt->replay_ptr], preempt->state_size))
{
_msg = msg_hash_to_str(MSG_PREEMPT_FAILED_TO_SAVE_STATE);
goto error;
}
current_core->retro_run();
if (runloop_st->auto_state_load_pending)
runloop_st->auto_state_load_core_ran = true;
preempt->replay_ptr = PREEMPT_NEXT_PTR(preempt->replay_ptr);
}
AUDIO_FLAGS_CLEAR(audio_st, AUDIO_FLAG_SUSPENDED);
video_state_get_ptr()->main_flags |= VIDEO_FLAG_ACTIVE;
}
/* Save current state and set start_ptr to oldest state */
if (!current_core->retro_serialize(
preempt->buffer[preempt->start_ptr], preempt->state_size))
{
_msg = msg_hash_to_str(MSG_PREEMPT_FAILED_TO_SAVE_STATE);
goto error;
}
preempt->start_ptr = PREEMPT_NEXT_PTR(preempt->start_ptr);
runloop_st->flags &= ~(RUNLOOP_FLAG_REQUEST_SPECIAL_SAVESTATE
| RUNLOOP_FLAG_INPUT_IS_DIRTY);
/* Run normal frame */
current_core->retro_run();
if (runloop_st->auto_state_load_pending)
runloop_st->auto_state_load_core_ran = true;
audio_driver_frame_end();
preempt->frame_count++;
return true;
error:
runloop_st->flags &= ~(RUNLOOP_FLAG_REQUEST_SPECIAL_SAVESTATE
| RUNLOOP_FLAG_INPUT_IS_DIRTY);
AUDIO_FLAGS_CLEAR(audio_st, AUDIO_FLAG_SUSPENDED);
video_state_get_ptr()->main_flags |= VIDEO_FLAG_ACTIVE;
preempt_deinit(runloop_st);
if (!run_ahead_hide_warnings)
runloop_msg_queue_push(_msg, strlen(_msg), 0, 2 * 60, true, NULL,
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
RARCH_ERR("[Run-Ahead Preemptive] %s\n", _msg);
return false;
}
void runahead_clear_variables(void *data)
{
runloop_state_t *runloop_st = (runloop_state_t*)data;
runloop_st->flags &= ~RUNLOOP_FLAG_RUNAHEAD_SAVE_STATE_SIZE_KNOWN;
video_driver_modify_disp_flags(VIDEO_FLAG_RUNAHEAD_IS_ACTIVE, 0);
runloop_st->flags |= RUNLOOP_FLAG_RUNAHEAD_AVAILABLE
| RUNLOOP_FLAG_RUNAHEAD_SECONDARY_CORE_AVAILABLE
| RUNLOOP_FLAG_RUNAHEAD_FORCE_INPUT_DIRTY;
runloop_st->runahead_last_frame_count = 0;
}