Skip to content

Commit

Permalink
Merge branch 'dev' into metal
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaacMarovitz authored Jan 4, 2025
2 parents 1eac653 + 5a1c3f8 commit 1a68c76
Show file tree
Hide file tree
Showing 45 changed files with 1,905 additions and 976 deletions.
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@
url = https://github.com/google/googletest.git
[submodule "core/deps/asio"]
path = core/deps/asio
url = https://github.com/chriskohlhoff/asio.git
url = https://github.com/flyinghead/asio.git
15 changes: 14 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@ if(NINTENDO_SWITCH)
if(USE_GLES)
target_compile_definitions(${PROJECT_NAME} PRIVATE GLES)
endif()
# asio
target_compile_definitions(${PROJECT_NAME} PRIVATE
ASIO_DISABLE_LOCAL_SOCKETS
ASIO_DISABLE_SERIAL_PORT
ESHUTDOWN=110
SA_RESTART=0
SA_NOCLDWAIT=0)

elseif(LIBRETRO)
add_library(${PROJECT_NAME} SHARED core/emulator.cpp)
Expand Down Expand Up @@ -849,6 +856,9 @@ if(LIBRETRO)
if(APPLE)
target_sources(${PROJECT_NAME} PRIVATE shell/libretro/oslib_apple.mm)
endif()
if(WIN32)
target_link_libraries(${PROJECT_NAME} PRIVATE mswsock)
endif()
endif()

target_sources(${PROJECT_NAME} PRIVATE
Expand Down Expand Up @@ -1930,7 +1940,10 @@ if(BUILD_TESTING)
tests/src/serialize_test.cpp
tests/src/AicaArmTest.cpp
tests/src/Sh4InterpreterTest.cpp
tests/src/MmuTest.cpp)
tests/src/MmuTest.cpp
tests/src/util/PeriodicThreadTest.cpp
tests/src/util/TsQueueTest.cpp
tests/src/util/WorkerThreadTest.cpp)
endif()

if(NINTENDO_SWITCH)
Expand Down
74 changes: 23 additions & 51 deletions core/achievements/achievements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@
#include <atomic>
#include <utility>
#include <xxhash.h>
#include <thread>
#include <functional>
#include "util/worker_thread.h"
#include "util/periodic_thread.h"

namespace achievements
{
Expand Down Expand Up @@ -74,10 +75,8 @@ class Achievements
std::string getOrDownloadImage(const char *url);
std::pair<std::string, bool> getCachedImage(const char *url);
void diskChange();
void asyncTask(std::function<void()> f);
void startThread();
void stopThread();
void backgroundThread();
void asyncTask(std::function<void()>&& f);
void stopThreads();

static void clientLoginWithTokenCallback(int result, const char *error_message, rc_client_t *client, void *userdata);
static void clientLoginWithPasswordCallback(int result, const char *error_message, rc_client_t *client, void *userdata);
Expand Down Expand Up @@ -112,11 +111,12 @@ class Achievements
std::string cachePath;
std::unordered_map<u64, std::string> cacheMap;
std::mutex cacheMutex;
std::vector<std::function<void()>> tasks;
std::mutex taskMutex;
std::thread taskThread;
cResetEvent resetEvent;
bool threadRunning = false;
WorkerThread taskThread {"RA-background"};

PeriodicThread idleThread { "RA-idle", [this]() {
if (active)
rc_client_idle(rc_client);
}};
};

bool init() {
Expand Down Expand Up @@ -176,6 +176,7 @@ Achievements::Achievements()
EventManager::listen(EmuEvent::Pause, emuEventCallback, this);
EventManager::listen(EmuEvent::Resume, emuEventCallback, this);
EventManager::listen(EmuEvent::DiskChange, emuEventCallback, this);
idleThread.setPeriod(1000);
}

Achievements::~Achievements()
Expand All @@ -188,44 +189,13 @@ Achievements::~Achievements()
term();
}

void Achievements::asyncTask(std::function<void()> f)
{
{
std::lock_guard<std::mutex> _(taskMutex);
tasks.emplace_back(f);
}
resetEvent.Set();
}

void Achievements::startThread()
{
threadRunning = true;
taskThread = std::thread(&Achievements::backgroundThread, this);
void Achievements::asyncTask(std::function<void()>&& f) {
taskThread.run(std::move(f));
}

void Achievements::stopThread()
{
threadRunning = false;
resetEvent.Set();
if (taskThread.joinable())
taskThread.join();
}

void Achievements::backgroundThread()
{
ThreadName _("RA-background");
while (threadRunning)
{
if (!resetEvent.Wait(1000) && active && paused)
rc_client_idle(rc_client);
std::vector<std::function<void()>> localTasks;
{
std::lock_guard<std::mutex> _(taskMutex);
std::swap(tasks, localTasks);
}
for (auto& f : localTasks)
f();
}
void Achievements::stopThreads() {
taskThread.stop();
idleThread.stop();
}

bool Achievements::init()
Expand All @@ -243,7 +213,6 @@ bool Achievements::init()
//rc_client_set_unofficial_enabled(rc_client, 0);
//rc_client_set_spectator_mode_enabled(rc_client, 0);
loadCache();
startThread();

if (!config::AchievementsUserName.get().empty() && !config::AchievementsToken.get().empty())
{
Expand Down Expand Up @@ -357,7 +326,7 @@ void Achievements::term()
if (rc_client == nullptr)
return;
unloadGame();
stopThread();
stopThreads();
rc_client_destroy(rc_client);
rc_client = nullptr;
}
Expand Down Expand Up @@ -813,15 +782,19 @@ std::string Achievements::getGameHash()
return hash;
}

void Achievements::pauseGame() {
void Achievements::pauseGame()
{
paused = true;
if (active)
idleThread.start();
}

void Achievements::resumeGame()
{
paused = false;
if (config::EnableAchievements && !settings.naomi.slave)
{
idleThread.stop();
loadGame();
if (settings.raHardcoreMode && !config::AchievementsHardcoreMode)
{
Expand Down Expand Up @@ -955,8 +928,7 @@ void Achievements::unloadGame()
paused = false;
EventManager::unlisten(EmuEvent::VBlank, emuEventCallback, this);
// wait for all async tasks before unloading the game
stopThread();
startThread();
stopThreads();
rc_client_unload_game(rc_client);
settings.raHardcoreMode = false;
}
Expand Down
1 change: 1 addition & 0 deletions core/cfg/option.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ Option<bool, false> DiscordPresence("DiscordPresence", true);
#if defined(__ANDROID__) && !defined(LIBRETRO)
Option<bool, false> UseSafFilePicker("UseSafFilePicker", true);
#endif
OptionString LogServer("LogServer", "", "log");

// Profiler
Option<bool> ProfilerEnabled("Profiler.Enabled");
Expand Down
1 change: 1 addition & 0 deletions core/cfg/option.h
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,7 @@ extern Option<bool, false> DiscordPresence;
#if defined(__ANDROID__) && !defined(LIBRETRO)
extern Option<bool, false> UseSafFilePicker;
#endif
extern OptionString LogServer;

// Profiling
extern Option<bool> ProfilerEnabled;
Expand Down
2 changes: 1 addition & 1 deletion core/deps/miniupnpc/src/minissdpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ receiveDevicesFromMiniSSDPD(int s, int * error)
#ifdef DEBUG
printf(" usnsize=%u\n", usnsize);
#endif /* DEBUG */
tmp = (struct UPNPDev *)malloc(sizeof(struct UPNPDev)+urlsize+stsize+usnsize);
tmp = (struct UPNPDev *)malloc(sizeof(struct UPNPDev)+urlsize+stsize+usnsize+3);
if(tmp == NULL) {
if (error)
*error = MINISSDPC_MEMORY_ERROR;
Expand Down
11 changes: 11 additions & 0 deletions core/deps/picotcp/include/arch/pico_msvc.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <windows.h>
#pragma pack(pop)

#define PICO_SUPPORT_THREADING

#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
#define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64
#else
Expand Down Expand Up @@ -54,6 +56,15 @@ static inline void PICO_IDLE(void)

#define alloca _alloca

#ifdef PICO_SUPPORT_THREADING
#define PICO_SUPPORT_MUTEX
/* mutex implementations */
extern void* pico_mutex_init(void);
extern void pico_mutex_lock(void* mux);
extern void pico_mutex_unlock(void* mux);
extern void pico_mutex_deinit(void* mux);
#endif

#endif /* PICO_SUPPORT_MSVC */


2 changes: 1 addition & 1 deletion core/deps/picotcp/include/pico_stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "pico_frame.h"
#include "pico_constants.h"

#define PICO_MAX_TIMERS 20
#define PICO_MAX_TIMERS 50

#define PICO_ETH_MRU (1514u)
#define PICO_IP_MRU (1500u)
Expand Down
1 change: 0 additions & 1 deletion core/deps/picotcp/modules/pico_socket_tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@ static int socket_tcp_do_deliver(struct pico_socket *s, struct pico_frame *f)
return 0;
}

dbg("TCP SOCKET> Not s.\n");
return -1;
}

Expand Down
1 change: 0 additions & 1 deletion core/hw/naomi/naomi_roms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,6 @@ const Game Games[] =
{ "opr-23968.ic20", 0x2000002, 0x800000, 0x0000000, InterleavedWord },
{ "opr-23969.ic21s", 0x3000000, 0x800000, 0x0000000, InterleavedWord },
{ "opr-23970.ic22", 0x3000002, 0x800000, 0x0000000, InterleavedWord },
{ NULL, 0, 0 },
}
},
// Soreike! Anpanman Popcorn Koujou 2 (Rev C)
Expand Down
30 changes: 12 additions & 18 deletions core/hw/pvr/ta_ctx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,24 +101,23 @@ void FinishRender(TA_context* ctx)
}

static std::mutex mtx_pool;
using Lock = std::lock_guard<std::mutex>;

static std::vector<TA_context*> ctx_pool;
static std::vector<TA_context*> ctx_list;

TA_context *tactx_Alloc()
{
TA_context *ctx = nullptr;

mtx_pool.lock();
if (!ctx_pool.empty())
{
ctx = ctx_pool.back();
ctx_pool.pop_back();
Lock _(mtx_pool);
if (!ctx_pool.empty()) {
ctx = ctx_pool.back();
ctx_pool.pop_back();
}
}
mtx_pool.unlock();

if (ctx == nullptr)
{
if (ctx == nullptr) {
ctx = new TA_context();
ctx->Alloc();
}
Expand All @@ -129,26 +128,22 @@ static void tactx_Recycle(TA_context* ctx)
{
if (ctx->nextContext != nullptr)
tactx_Recycle(ctx->nextContext);
mtx_pool.lock();
if (ctx_pool.size() > 3)
{
Lock _(mtx_pool);
if (ctx_pool.size() > 3) {
delete ctx;
}
else
{
else {
ctx->Reset();
ctx_pool.push_back(ctx);
}
mtx_pool.unlock();
}

static TA_context *tactx_Find(u32 addr, bool allocnew)
{
TA_context *oldCtx = nullptr;
for (TA_context *ctx : ctx_list)
{
if (ctx->Address == addr)
{
if (ctx->Address == addr) {
ctx->lastFrameUsed = FrameCount;
return ctx;
}
Expand Down Expand Up @@ -205,11 +200,10 @@ void tactx_Term()
delete ctx;
ctx_list.clear();

mtx_pool.lock();
Lock _(mtx_pool);
for (TA_context *ctx : ctx_pool)
delete ctx;
ctx_pool.clear();
mtx_pool.unlock();
}

const u32 NULL_CONTEXT = ~0u;
Expand Down
Loading

0 comments on commit 1a68c76

Please sign in to comment.