Skip to content

Commit

Permalink
WorkerThreadPool: Enhance lifetime for more flexibility
Browse files Browse the repository at this point in the history
  • Loading branch information
RandomShaper committed Sep 10, 2024
1 parent d0dc389 commit 2d1dd41
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
5 changes: 4 additions & 1 deletion core/object/worker_thread_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,8 @@ WorkerThreadPool::TaskID WorkerThreadPool::add_native_task(void (*p_func)(void *
}

WorkerThreadPool::TaskID WorkerThreadPool::_add_task(const Callable &p_callable, void (*p_func)(void *), void *p_userdata, BaseTemplateUserdata *p_template_userdata, bool p_high_priority, const String &p_description) {
ERR_FAIL_COND_V_MSG(threads.is_empty(), INVALID_TASK_ID, "Can't add a task because the WorkerThreadPool is either not initialized yet or already terminated.");

task_mutex.lock();
// Get a free task
Task *task = task_allocator.alloc();
Expand Down Expand Up @@ -538,6 +540,7 @@ void WorkerThreadPool::notify_yield_over(TaskID p_task_id) {
}

WorkerThreadPool::GroupID WorkerThreadPool::_add_group_task(const Callable &p_callable, void (*p_func)(void *, uint32_t), void *p_userdata, BaseTemplateUserdata *p_template_userdata, int p_elements, int p_tasks, bool p_high_priority, const String &p_description) {
ERR_FAIL_COND_V_MSG(threads.is_empty(), INVALID_TASK_ID, "Can't add a group task because the WorkerThreadPool is either not initialized yet or already terminated.");
ERR_FAIL_COND_V(p_elements < 0, INVALID_TASK_ID);
if (p_tasks < 0) {
p_tasks = MAX(1u, threads.size());
Expand Down Expand Up @@ -749,5 +752,5 @@ WorkerThreadPool::WorkerThreadPool() {
}

WorkerThreadPool::~WorkerThreadPool() {
finish();
DEV_ASSERT(threads.size() == 0 && "finish() hasn't been called!");
}
8 changes: 1 addition & 7 deletions core/register_core_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,6 @@ static Time *_time = nullptr;
static core_bind::Geometry2D *_geometry_2d = nullptr;
static core_bind::Geometry3D *_geometry_3d = nullptr;

static WorkerThreadPool *worker_thread_pool = nullptr;

extern Mutex _global_mutex;

static GDExtensionManager *gdextension_manager = nullptr;
Expand Down Expand Up @@ -297,8 +295,6 @@ void register_core_types() {
GDREGISTER_NATIVE_STRUCT(AudioFrame, "float left;float right");
GDREGISTER_NATIVE_STRUCT(ScriptLanguageExtensionProfilingInfo, "StringName signature;uint64_t call_count;uint64_t total_time;uint64_t self_time");

worker_thread_pool = memnew(WorkerThreadPool);

OS::get_singleton()->benchmark_end_measure("Core", "Register Types");
}

Expand Down Expand Up @@ -349,7 +345,7 @@ void register_core_singletons() {
Engine::get_singleton()->add_singleton(Engine::Singleton("Time", Time::get_singleton()));
Engine::get_singleton()->add_singleton(Engine::Singleton("GDExtensionManager", GDExtensionManager::get_singleton()));
Engine::get_singleton()->add_singleton(Engine::Singleton("ResourceUID", ResourceUID::get_singleton()));
Engine::get_singleton()->add_singleton(Engine::Singleton("WorkerThreadPool", worker_thread_pool));
Engine::get_singleton()->add_singleton(Engine::Singleton("WorkerThreadPool", WorkerThreadPool::get_singleton()));

OS::get_singleton()->benchmark_end_measure("Core", "Register Singletons");
}
Expand Down Expand Up @@ -382,8 +378,6 @@ void unregister_core_types() {

// Destroy singletons in reverse order to ensure dependencies are not broken.

memdelete(worker_thread_pool);

memdelete(_engine_debugger);
memdelete(_marshalls);
memdelete(_classdb);
Expand Down
18 changes: 18 additions & 0 deletions main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ static Engine *engine = nullptr;
static ProjectSettings *globals = nullptr;
static Input *input = nullptr;
static InputMap *input_map = nullptr;
static WorkerThreadPool *worker_thread_pool = nullptr;
static TranslationServer *translation_server = nullptr;
static Performance *performance = nullptr;
static PackedData *packed_data = nullptr;
Expand Down Expand Up @@ -690,6 +691,8 @@ Error Main::test_setup() {

register_core_settings(); // Here globals are present.

worker_thread_pool = memnew(WorkerThreadPool);

translation_server = memnew(TranslationServer);
tsman = memnew(TextServerManager);

Expand Down Expand Up @@ -800,6 +803,8 @@ void Main::test_cleanup() {
ResourceSaver::remove_custom_savers();
PropertyListHelper::clear_base_helpers();

WorkerThreadPool::get_singleton()->finish();

#ifdef TOOLS_ENABLED
GDExtensionManager::get_singleton()->deinitialize_extensions(GDExtension::INITIALIZATION_LEVEL_EDITOR);
uninitialize_modules(MODULE_INITIALIZATION_LEVEL_EDITOR);
Expand Down Expand Up @@ -841,6 +846,9 @@ void Main::test_cleanup() {
if (physics_server_2d_manager) {
memdelete(physics_server_2d_manager);
}
if (worker_thread_pool) {
memdelete(worker_thread_pool);
}
if (globals) {
memdelete(globals);
}
Expand Down Expand Up @@ -931,6 +939,7 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph

register_core_settings(); //here globals are present

worker_thread_pool = memnew(WorkerThreadPool);
translation_server = memnew(TranslationServer);
performance = memnew(Performance);
GDREGISTER_CLASS(Performance);
Expand Down Expand Up @@ -2620,6 +2629,10 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph
if (translation_server) {
memdelete(translation_server);
}
if (worker_thread_pool) {
worker_thread_pool->finish();
memdelete(worker_thread_pool);
}
if (globals) {
memdelete(globals);
}
Expand Down Expand Up @@ -4501,6 +4514,8 @@ void Main::cleanup(bool p_force) {
ResourceLoader::clear_translation_remaps();
ResourceLoader::clear_path_remaps();

WorkerThreadPool::get_singleton()->finish();

ScriptServer::finish_languages();

// Sync pending commands that may have been queued from a different thread during ScriptServer finalization
Expand Down Expand Up @@ -4591,6 +4606,9 @@ void Main::cleanup(bool p_force) {
if (physics_server_2d_manager) {
memdelete(physics_server_2d_manager);
}
if (worker_thread_pool) {
memdelete(worker_thread_pool);
}
if (globals) {
memdelete(globals);
}
Expand Down

0 comments on commit 2d1dd41

Please sign in to comment.