forked from rosebudai/OpenRCT2-Web
Handle single-threaded wasm job execution paths
This commit is contained in:
4 files changed
+98
-37
No files matched your search
+40
-27
@@ -23,6 +23,7 @@
|
||||
#include <openrct2/command_line/CommandLine.hpp>
|
||||
#include <openrct2/platform/Platform.h>
|
||||
#include <openrct2/ui/UiContext.h>
|
||||
#include <exception>
|
||||
|
||||
#ifdef __EMSCRIPTEN__
|
||||
#include <emscripten.h>
|
||||
@@ -53,41 +54,53 @@ int main(int argc, const char** argv)
|
||||
Module.canvas.addEventListener("contextmenu", function(e) { e.preventDefault(); });
|
||||
});
|
||||
#endif
|
||||
int32_t rc = EXIT_SUCCESS;
|
||||
int runGame = CommandLineRun(argv, argc);
|
||||
RegisterBitmapReader();
|
||||
if (runGame == EXITCODE_CONTINUE)
|
||||
try
|
||||
{
|
||||
std::unique_ptr<IContext> context;
|
||||
if (gOpenRCT2Headless)
|
||||
int32_t rc = EXIT_SUCCESS;
|
||||
int runGame = CommandLineRun(argv, argc);
|
||||
RegisterBitmapReader();
|
||||
if (runGame == EXITCODE_CONTINUE)
|
||||
{
|
||||
// Run OpenRCT2 with a plain context
|
||||
context = CreateContext();
|
||||
std::unique_ptr<IContext> context;
|
||||
if (gOpenRCT2Headless)
|
||||
{
|
||||
// Run OpenRCT2 with a plain context
|
||||
context = CreateContext();
|
||||
}
|
||||
else
|
||||
{
|
||||
// Run OpenRCT2 with a UI context
|
||||
auto env = CreatePlatformEnvironment();
|
||||
std::unique_ptr<IAudioContext> audioContext;
|
||||
try
|
||||
{
|
||||
audioContext = CreateAudioContext();
|
||||
}
|
||||
catch (const SDLException& e)
|
||||
{
|
||||
LOG_WARNING("Failed to create audio context. Using dummy audio context. Error message was: %s", e.what());
|
||||
audioContext = CreateDummyAudioContext();
|
||||
}
|
||||
auto uiContext = CreateUiContext(*env);
|
||||
context = CreateContext(std::move(env), std::move(audioContext), std::move(uiContext));
|
||||
}
|
||||
rc = context->RunOpenRCT2(argc, argv);
|
||||
}
|
||||
else
|
||||
else if (runGame == EXITCODE_FAIL)
|
||||
{
|
||||
// Run OpenRCT2 with a UI context
|
||||
auto env = CreatePlatformEnvironment();
|
||||
std::unique_ptr<IAudioContext> audioContext;
|
||||
try
|
||||
{
|
||||
audioContext = CreateAudioContext();
|
||||
}
|
||||
catch (const SDLException& e)
|
||||
{
|
||||
LOG_WARNING("Failed to create audio context. Using dummy audio context. Error message was: %s", e.what());
|
||||
audioContext = CreateDummyAudioContext();
|
||||
}
|
||||
auto uiContext = CreateUiContext(*env);
|
||||
context = CreateContext(std::move(env), std::move(audioContext), std::move(uiContext));
|
||||
rc = EXIT_FAILURE;
|
||||
}
|
||||
rc = context->RunOpenRCT2(argc, argv);
|
||||
return rc;
|
||||
}
|
||||
else if (runGame == EXITCODE_FAIL)
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
rc = EXIT_FAILURE;
|
||||
LOG_ERROR("Unhandled exception in OpenRCT2 main: %s", e.what());
|
||||
}
|
||||
return rc;
|
||||
catch (...)
|
||||
{
|
||||
LOG_ERROR("Unhandled non-std exception in OpenRCT2 main.");
|
||||
}
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
#ifdef __ANDROID__
|
||||
|
||||
@@ -1228,6 +1228,13 @@ namespace OpenRCT2
|
||||
{
|
||||
if (!_versionCheckFuture.valid())
|
||||
{
|
||||
#if defined(__EMSCRIPTEN__) && !defined(__EMSCRIPTEN_PTHREADS__)
|
||||
_newVersionInfo = GetLatestVersion();
|
||||
if (!String::startsWith(gVersionInfoTag, _newVersionInfo.tag))
|
||||
{
|
||||
_hasNewVersionInfo = true;
|
||||
}
|
||||
#else
|
||||
_versionCheckFuture = std::async(std::launch::async, [this] {
|
||||
_newVersionInfo = GetLatestVersion();
|
||||
if (!String::startsWith(gVersionInfoTag, _newVersionInfo.tag))
|
||||
@@ -1235,6 +1242,7 @@ namespace OpenRCT2
|
||||
_hasNewVersionInfo = true;
|
||||
}
|
||||
});
|
||||
#endif
|
||||
}
|
||||
|
||||
if (!gOpenRCT2Headless)
|
||||
|
||||
@@ -24,6 +24,12 @@
|
||||
|
||||
namespace OpenRCT2
|
||||
{
|
||||
#if defined(__EMSCRIPTEN__) && !defined(__EMSCRIPTEN_PTHREADS__)
|
||||
constexpr bool kBackgroundWorkerSingleThreaded = true;
|
||||
#else
|
||||
constexpr bool kBackgroundWorkerSingleThreaded = false;
|
||||
#endif
|
||||
|
||||
namespace Detail
|
||||
{
|
||||
template<typename TFn, typename TWantsToken>
|
||||
@@ -178,15 +184,18 @@ namespace OpenRCT2
|
||||
public:
|
||||
BackgroundWorker()
|
||||
{
|
||||
const auto threadsAvailable = std::max(std::thread::hardware_concurrency(), 1u);
|
||||
|
||||
// NOTE: We don't want to use all available threads, this is for background work only.
|
||||
// Adjust the number of threads if needed.
|
||||
const auto numThreads = std::min(threadsAvailable, 2u);
|
||||
|
||||
for (auto i = 0u; i < numThreads; ++i)
|
||||
if constexpr (!kBackgroundWorkerSingleThreaded)
|
||||
{
|
||||
_workThreads.emplace_back([this] { processJobs(); });
|
||||
const auto threadsAvailable = std::max(std::thread::hardware_concurrency(), 1u);
|
||||
|
||||
// NOTE: We don't want to use all available threads, this is for background work only.
|
||||
// Adjust the number of threads if needed.
|
||||
const auto numThreads = std::min(threadsAvailable, 2u);
|
||||
|
||||
for (auto i = 0u; i < numThreads; ++i)
|
||||
{
|
||||
_workThreads.emplace_back([this] { processJobs(); });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -242,9 +251,19 @@ namespace OpenRCT2
|
||||
{
|
||||
std::lock_guard lock(_mtx);
|
||||
_jobs.push_back(job);
|
||||
_pending.push_back(job);
|
||||
if constexpr (!kBackgroundWorkerSingleThreaded)
|
||||
{
|
||||
_pending.push_back(job);
|
||||
}
|
||||
}
|
||||
if constexpr (kBackgroundWorkerSingleThreaded)
|
||||
{
|
||||
job->run();
|
||||
}
|
||||
else
|
||||
{
|
||||
_cv.notify_one();
|
||||
}
|
||||
_cv.notify_one();
|
||||
|
||||
return Job(job);
|
||||
}
|
||||
|
||||
@@ -11,6 +11,12 @@
|
||||
|
||||
#include <cassert>
|
||||
|
||||
#if defined(__EMSCRIPTEN__) && !defined(__EMSCRIPTEN_PTHREADS__)
|
||||
static constexpr bool kJobPoolSingleThreaded = true;
|
||||
#else
|
||||
static constexpr bool kJobPoolSingleThreaded = false;
|
||||
#endif
|
||||
|
||||
JobPool::TaskData::TaskData(std::function<void()> workFn, std::function<void()> completionFn)
|
||||
: WorkFn(std::move(workFn))
|
||||
, CompletionFn(std::move(completionFn))
|
||||
@@ -19,6 +25,11 @@ JobPool::TaskData::TaskData(std::function<void()> workFn, std::function<void()>
|
||||
|
||||
JobPool::JobPool(size_t maxThreads)
|
||||
{
|
||||
if constexpr (kJobPoolSingleThreaded)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
maxThreads = std::min<size_t>(maxThreads, std::max(1u, std::thread::hardware_concurrency()));
|
||||
for (size_t n = 0; n < maxThreads; n++)
|
||||
{
|
||||
@@ -43,6 +54,16 @@ JobPool::~JobPool()
|
||||
|
||||
void JobPool::AddTask(std::function<void()> workFn, std::function<void()> completionFn)
|
||||
{
|
||||
if constexpr (kJobPoolSingleThreaded)
|
||||
{
|
||||
workFn();
|
||||
if (completionFn)
|
||||
{
|
||||
completionFn();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
{
|
||||
std::lock_guard lock(_mutex);
|
||||
_pending.emplace_back(workFn, completionFn);
|
||||
|
||||
Reference in new issue
Block a user