2022-07-18 12:09:19 +02:00
|
|
|
/**************************************************************************/
|
|
|
|
/* worker_thread_pool.h */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
|
|
|
|
|
|
|
#ifndef WORKER_THREAD_POOL_H
|
|
|
|
#define WORKER_THREAD_POOL_H
|
|
|
|
|
WorkerThreadPool: Overhaul scheduling and synchronization
This commits rewrites the sync logic in a way that the
`use_system_threads_for_low_priority_tasks` setting, which was added due to
the lack of a cross-platform wait-for-multiple-objects functionality, can be
removed (it's as if it was effectively hardcoded to `false`).
With the new implementation, we have the best of both worlds: threads don't
have to poll, plus no bespoke threads are used.
In addition, regarding deadlock prevention, since not every possible case of
wait-deadlock could be avoided, this commits removes the current best-effort
avoidance mechanisms and keeps only a simple, pessimistic way of detection.
It turns out that the only current user of deadlock prevention, ResourceLoader,
works fine with it and so every possible situation in resource loading is now
properly handled, with no possibilities of deadlocking. There's a comment in
the code with further details.
Lastly, a potential for load tasks never being awaited/disposed is cleared.
2024-01-05 17:39:26 +01:00
|
|
|
#include "core/os/condition_variable.h"
|
2022-07-18 12:09:19 +02:00
|
|
|
#include "core/os/memory.h"
|
|
|
|
#include "core/os/os.h"
|
|
|
|
#include "core/os/semaphore.h"
|
|
|
|
#include "core/os/thread.h"
|
|
|
|
#include "core/templates/local_vector.h"
|
|
|
|
#include "core/templates/paged_allocator.h"
|
|
|
|
#include "core/templates/rid.h"
|
|
|
|
#include "core/templates/safe_refcount.h"
|
|
|
|
|
|
|
|
class WorkerThreadPool : public Object {
|
|
|
|
GDCLASS(WorkerThreadPool, Object)
|
|
|
|
public:
|
|
|
|
enum {
|
|
|
|
INVALID_TASK_ID = -1
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef int64_t TaskID;
|
|
|
|
typedef int64_t GroupID;
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct Task;
|
|
|
|
|
2022-07-23 19:12:41 +02:00
|
|
|
struct BaseTemplateUserdata {
|
|
|
|
virtual void callback() {}
|
|
|
|
virtual void callback_indexed(uint32_t p_index) {}
|
|
|
|
virtual ~BaseTemplateUserdata() {}
|
|
|
|
};
|
|
|
|
|
2022-07-18 12:09:19 +02:00
|
|
|
struct Group {
|
WorkerThreadPool: Overhaul scheduling and synchronization
This commits rewrites the sync logic in a way that the
`use_system_threads_for_low_priority_tasks` setting, which was added due to
the lack of a cross-platform wait-for-multiple-objects functionality, can be
removed (it's as if it was effectively hardcoded to `false`).
With the new implementation, we have the best of both worlds: threads don't
have to poll, plus no bespoke threads are used.
In addition, regarding deadlock prevention, since not every possible case of
wait-deadlock could be avoided, this commits removes the current best-effort
avoidance mechanisms and keeps only a simple, pessimistic way of detection.
It turns out that the only current user of deadlock prevention, ResourceLoader,
works fine with it and so every possible situation in resource loading is now
properly handled, with no possibilities of deadlocking. There's a comment in
the code with further details.
Lastly, a potential for load tasks never being awaited/disposed is cleared.
2024-01-05 17:39:26 +01:00
|
|
|
GroupID self = -1;
|
2022-07-18 12:09:19 +02:00
|
|
|
SafeNumeric<uint32_t> index;
|
2022-07-23 19:12:41 +02:00
|
|
|
SafeNumeric<uint32_t> completed_index;
|
2022-07-18 12:09:19 +02:00
|
|
|
uint32_t max = 0;
|
|
|
|
Semaphore done_semaphore;
|
|
|
|
SafeFlag completed;
|
|
|
|
SafeNumeric<uint32_t> finished;
|
|
|
|
uint32_t tasks_used = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Task {
|
WorkerThreadPool: Overhaul scheduling and synchronization
This commits rewrites the sync logic in a way that the
`use_system_threads_for_low_priority_tasks` setting, which was added due to
the lack of a cross-platform wait-for-multiple-objects functionality, can be
removed (it's as if it was effectively hardcoded to `false`).
With the new implementation, we have the best of both worlds: threads don't
have to poll, plus no bespoke threads are used.
In addition, regarding deadlock prevention, since not every possible case of
wait-deadlock could be avoided, this commits removes the current best-effort
avoidance mechanisms and keeps only a simple, pessimistic way of detection.
It turns out that the only current user of deadlock prevention, ResourceLoader,
works fine with it and so every possible situation in resource loading is now
properly handled, with no possibilities of deadlocking. There's a comment in
the code with further details.
Lastly, a potential for load tasks never being awaited/disposed is cleared.
2024-01-05 17:39:26 +01:00
|
|
|
TaskID self = -1;
|
2022-07-18 12:09:19 +02:00
|
|
|
Callable callable;
|
|
|
|
void (*native_func)(void *) = nullptr;
|
|
|
|
void (*native_group_func)(void *, uint32_t) = nullptr;
|
|
|
|
void *native_func_userdata = nullptr;
|
|
|
|
String description;
|
WorkerThreadPool: Overhaul scheduling and synchronization
This commits rewrites the sync logic in a way that the
`use_system_threads_for_low_priority_tasks` setting, which was added due to
the lack of a cross-platform wait-for-multiple-objects functionality, can be
removed (it's as if it was effectively hardcoded to `false`).
With the new implementation, we have the best of both worlds: threads don't
have to poll, plus no bespoke threads are used.
In addition, regarding deadlock prevention, since not every possible case of
wait-deadlock could be avoided, this commits removes the current best-effort
avoidance mechanisms and keeps only a simple, pessimistic way of detection.
It turns out that the only current user of deadlock prevention, ResourceLoader,
works fine with it and so every possible situation in resource loading is now
properly handled, with no possibilities of deadlocking. There's a comment in
the code with further details.
Lastly, a potential for load tasks never being awaited/disposed is cleared.
2024-01-05 17:39:26 +01:00
|
|
|
Semaphore done_semaphore; // For user threads awaiting.
|
2024-04-18 19:05:44 +02:00
|
|
|
bool completed : 1;
|
|
|
|
bool pending_notify_yield_over : 1;
|
2022-07-18 12:09:19 +02:00
|
|
|
Group *group = nullptr;
|
|
|
|
SelfList<Task> task_elem;
|
WorkerThreadPool: Overhaul scheduling and synchronization
This commits rewrites the sync logic in a way that the
`use_system_threads_for_low_priority_tasks` setting, which was added due to
the lack of a cross-platform wait-for-multiple-objects functionality, can be
removed (it's as if it was effectively hardcoded to `false`).
With the new implementation, we have the best of both worlds: threads don't
have to poll, plus no bespoke threads are used.
In addition, regarding deadlock prevention, since not every possible case of
wait-deadlock could be avoided, this commits removes the current best-effort
avoidance mechanisms and keeps only a simple, pessimistic way of detection.
It turns out that the only current user of deadlock prevention, ResourceLoader,
works fine with it and so every possible situation in resource loading is now
properly handled, with no possibilities of deadlocking. There's a comment in
the code with further details.
Lastly, a potential for load tasks never being awaited/disposed is cleared.
2024-01-05 17:39:26 +01:00
|
|
|
uint32_t waiting_pool = 0;
|
|
|
|
uint32_t waiting_user = 0;
|
2022-07-18 12:09:19 +02:00
|
|
|
bool low_priority = false;
|
2022-07-23 19:12:41 +02:00
|
|
|
BaseTemplateUserdata *template_userdata = nullptr;
|
2023-05-17 00:00:45 +02:00
|
|
|
int pool_thread_index = -1;
|
2022-07-23 19:12:41 +02:00
|
|
|
|
|
|
|
void free_template_userdata();
|
2022-07-18 12:09:19 +02:00
|
|
|
Task() :
|
2024-04-18 19:05:44 +02:00
|
|
|
completed(false),
|
|
|
|
pending_notify_yield_over(false),
|
2022-07-18 12:09:19 +02:00
|
|
|
task_elem(this) {}
|
|
|
|
};
|
|
|
|
|
2023-12-28 19:31:28 +01:00
|
|
|
static const uint32_t TASKS_PAGE_SIZE = 1024;
|
|
|
|
static const uint32_t GROUPS_PAGE_SIZE = 256;
|
|
|
|
|
|
|
|
PagedAllocator<Task, false, TASKS_PAGE_SIZE> task_allocator;
|
|
|
|
PagedAllocator<Group, false, GROUPS_PAGE_SIZE> group_allocator;
|
2022-07-18 12:09:19 +02:00
|
|
|
|
|
|
|
SelfList<Task>::List low_priority_task_queue;
|
|
|
|
SelfList<Task>::List task_queue;
|
|
|
|
|
WorkerThreadPool: Overhaul scheduling and synchronization
This commits rewrites the sync logic in a way that the
`use_system_threads_for_low_priority_tasks` setting, which was added due to
the lack of a cross-platform wait-for-multiple-objects functionality, can be
removed (it's as if it was effectively hardcoded to `false`).
With the new implementation, we have the best of both worlds: threads don't
have to poll, plus no bespoke threads are used.
In addition, regarding deadlock prevention, since not every possible case of
wait-deadlock could be avoided, this commits removes the current best-effort
avoidance mechanisms and keeps only a simple, pessimistic way of detection.
It turns out that the only current user of deadlock prevention, ResourceLoader,
works fine with it and so every possible situation in resource loading is now
properly handled, with no possibilities of deadlocking. There's a comment in
the code with further details.
Lastly, a potential for load tasks never being awaited/disposed is cleared.
2024-01-05 17:39:26 +01:00
|
|
|
BinaryMutex task_mutex;
|
2022-07-18 12:09:19 +02:00
|
|
|
|
|
|
|
struct ThreadData {
|
2024-04-09 17:26:45 +02:00
|
|
|
static Task *const YIELDING; // Too bad constexpr doesn't work here.
|
|
|
|
|
WorkerThreadPool: Overhaul scheduling and synchronization
This commits rewrites the sync logic in a way that the
`use_system_threads_for_low_priority_tasks` setting, which was added due to
the lack of a cross-platform wait-for-multiple-objects functionality, can be
removed (it's as if it was effectively hardcoded to `false`).
With the new implementation, we have the best of both worlds: threads don't
have to poll, plus no bespoke threads are used.
In addition, regarding deadlock prevention, since not every possible case of
wait-deadlock could be avoided, this commits removes the current best-effort
avoidance mechanisms and keeps only a simple, pessimistic way of detection.
It turns out that the only current user of deadlock prevention, ResourceLoader,
works fine with it and so every possible situation in resource loading is now
properly handled, with no possibilities of deadlocking. There's a comment in
the code with further details.
Lastly, a potential for load tasks never being awaited/disposed is cleared.
2024-01-05 17:39:26 +01:00
|
|
|
uint32_t index = 0;
|
2022-07-18 12:09:19 +02:00
|
|
|
Thread thread;
|
2024-04-09 17:26:45 +02:00
|
|
|
bool signaled : 1;
|
|
|
|
bool yield_is_over : 1;
|
2024-09-16 11:51:57 +02:00
|
|
|
bool pre_exited_languages : 1;
|
|
|
|
bool exited_languages : 1;
|
WorkerThreadPool: Overhaul scheduling and synchronization
This commits rewrites the sync logic in a way that the
`use_system_threads_for_low_priority_tasks` setting, which was added due to
the lack of a cross-platform wait-for-multiple-objects functionality, can be
removed (it's as if it was effectively hardcoded to `false`).
With the new implementation, we have the best of both worlds: threads don't
have to poll, plus no bespoke threads are used.
In addition, regarding deadlock prevention, since not every possible case of
wait-deadlock could be avoided, this commits removes the current best-effort
avoidance mechanisms and keeps only a simple, pessimistic way of detection.
It turns out that the only current user of deadlock prevention, ResourceLoader,
works fine with it and so every possible situation in resource loading is now
properly handled, with no possibilities of deadlocking. There's a comment in
the code with further details.
Lastly, a potential for load tasks never being awaited/disposed is cleared.
2024-01-05 17:39:26 +01:00
|
|
|
Task *current_task = nullptr;
|
2024-04-09 17:26:45 +02:00
|
|
|
Task *awaited_task = nullptr; // Null if not awaiting the condition variable, or special value (YIELDING).
|
WorkerThreadPool: Overhaul scheduling and synchronization
This commits rewrites the sync logic in a way that the
`use_system_threads_for_low_priority_tasks` setting, which was added due to
the lack of a cross-platform wait-for-multiple-objects functionality, can be
removed (it's as if it was effectively hardcoded to `false`).
With the new implementation, we have the best of both worlds: threads don't
have to poll, plus no bespoke threads are used.
In addition, regarding deadlock prevention, since not every possible case of
wait-deadlock could be avoided, this commits removes the current best-effort
avoidance mechanisms and keeps only a simple, pessimistic way of detection.
It turns out that the only current user of deadlock prevention, ResourceLoader,
works fine with it and so every possible situation in resource loading is now
properly handled, with no possibilities of deadlocking. There's a comment in
the code with further details.
Lastly, a potential for load tasks never being awaited/disposed is cleared.
2024-01-05 17:39:26 +01:00
|
|
|
ConditionVariable cond_var;
|
2024-04-09 17:26:45 +02:00
|
|
|
|
|
|
|
ThreadData() :
|
|
|
|
signaled(false),
|
2024-09-16 11:51:57 +02:00
|
|
|
yield_is_over(false),
|
|
|
|
pre_exited_languages(false),
|
|
|
|
exited_languages(false) {}
|
2022-07-18 12:09:19 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
TightLocalVector<ThreadData> threads;
|
2024-09-13 14:20:11 +02:00
|
|
|
enum Runlevel {
|
|
|
|
RUNLEVEL_NORMAL,
|
2024-09-16 11:51:57 +02:00
|
|
|
RUNLEVEL_PRE_EXIT_LANGUAGES, // Block adding new tasks
|
|
|
|
RUNLEVEL_EXIT_LANGUAGES, // All threads detach from scripting threads.
|
2024-09-13 14:20:11 +02:00
|
|
|
RUNLEVEL_EXIT,
|
|
|
|
} runlevel = RUNLEVEL_NORMAL;
|
2024-09-16 11:51:57 +02:00
|
|
|
union { // Cleared on every runlevel change.
|
|
|
|
struct {
|
|
|
|
uint32_t num_idle_threads;
|
|
|
|
} pre_exit_languages;
|
|
|
|
struct {
|
|
|
|
uint32_t num_exited_threads;
|
|
|
|
} exit_languages;
|
|
|
|
} runlevel_data;
|
|
|
|
ConditionVariable control_cond_var;
|
2022-07-18 12:09:19 +02:00
|
|
|
|
|
|
|
HashMap<Thread::ID, int> thread_ids;
|
2023-12-28 19:31:28 +01:00
|
|
|
HashMap<
|
|
|
|
TaskID,
|
|
|
|
Task *,
|
|
|
|
HashMapHasherDefault,
|
|
|
|
HashMapComparatorDefault<TaskID>,
|
|
|
|
PagedAllocator<HashMapElement<TaskID, Task *>, false, TASKS_PAGE_SIZE>>
|
|
|
|
tasks;
|
|
|
|
HashMap<
|
|
|
|
GroupID,
|
|
|
|
Group *,
|
|
|
|
HashMapHasherDefault,
|
|
|
|
HashMapComparatorDefault<GroupID>,
|
|
|
|
PagedAllocator<HashMapElement<GroupID, Group *>, false, GROUPS_PAGE_SIZE>>
|
|
|
|
groups;
|
2022-07-18 12:09:19 +02:00
|
|
|
|
|
|
|
uint32_t max_low_priority_threads = 0;
|
2023-05-11 12:24:59 +02:00
|
|
|
uint32_t low_priority_threads_used = 0;
|
WorkerThreadPool: Overhaul scheduling and synchronization
This commits rewrites the sync logic in a way that the
`use_system_threads_for_low_priority_tasks` setting, which was added due to
the lack of a cross-platform wait-for-multiple-objects functionality, can be
removed (it's as if it was effectively hardcoded to `false`).
With the new implementation, we have the best of both worlds: threads don't
have to poll, plus no bespoke threads are used.
In addition, regarding deadlock prevention, since not every possible case of
wait-deadlock could be avoided, this commits removes the current best-effort
avoidance mechanisms and keeps only a simple, pessimistic way of detection.
It turns out that the only current user of deadlock prevention, ResourceLoader,
works fine with it and so every possible situation in resource loading is now
properly handled, with no possibilities of deadlocking. There's a comment in
the code with further details.
Lastly, a potential for load tasks never being awaited/disposed is cleared.
2024-01-05 17:39:26 +01:00
|
|
|
uint32_t notify_index = 0; // For rotating across threads, no help distributing load.
|
2022-07-18 12:09:19 +02:00
|
|
|
|
|
|
|
uint64_t last_task = 1;
|
|
|
|
|
|
|
|
static void _thread_function(void *p_user);
|
|
|
|
|
|
|
|
void _process_task(Task *task);
|
|
|
|
|
2024-09-16 11:51:57 +02:00
|
|
|
void _post_tasks(Task **p_tasks, uint32_t p_count, bool p_high_priority, MutexLock<BinaryMutex> &p_lock);
|
WorkerThreadPool: Overhaul scheduling and synchronization
This commits rewrites the sync logic in a way that the
`use_system_threads_for_low_priority_tasks` setting, which was added due to
the lack of a cross-platform wait-for-multiple-objects functionality, can be
removed (it's as if it was effectively hardcoded to `false`).
With the new implementation, we have the best of both worlds: threads don't
have to poll, plus no bespoke threads are used.
In addition, regarding deadlock prevention, since not every possible case of
wait-deadlock could be avoided, this commits removes the current best-effort
avoidance mechanisms and keeps only a simple, pessimistic way of detection.
It turns out that the only current user of deadlock prevention, ResourceLoader,
works fine with it and so every possible situation in resource loading is now
properly handled, with no possibilities of deadlocking. There's a comment in
the code with further details.
Lastly, a potential for load tasks never being awaited/disposed is cleared.
2024-01-05 17:39:26 +01:00
|
|
|
void _notify_threads(const ThreadData *p_current_thread_data, uint32_t p_process_count, uint32_t p_promote_count);
|
2022-07-18 12:09:19 +02:00
|
|
|
|
2023-05-17 00:00:45 +02:00
|
|
|
bool _try_promote_low_priority_task();
|
|
|
|
|
2022-07-18 12:09:19 +02:00
|
|
|
static WorkerThreadPool *singleton;
|
|
|
|
|
2024-06-19 08:09:59 +02:00
|
|
|
#ifdef THREADS_ENABLED
|
2024-07-18 14:54:58 +02:00
|
|
|
static const uint32_t MAX_UNLOCKABLE_LOCKS = 2;
|
|
|
|
struct UnlockableLocks {
|
|
|
|
THREADING_NAMESPACE::unique_lock<THREADING_NAMESPACE::mutex> *ulock = nullptr;
|
|
|
|
uint32_t rc = 0;
|
|
|
|
};
|
|
|
|
static thread_local UnlockableLocks unlockable_locks[MAX_UNLOCKABLE_LOCKS];
|
2024-06-19 08:09:59 +02:00
|
|
|
#endif
|
2023-12-29 01:27:17 +01:00
|
|
|
|
2022-07-23 19:12:41 +02:00
|
|
|
TaskID _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);
|
|
|
|
GroupID _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);
|
|
|
|
|
|
|
|
template <typename C, typename M, typename U>
|
|
|
|
struct TaskUserData : public BaseTemplateUserdata {
|
|
|
|
C *instance;
|
|
|
|
M method;
|
|
|
|
U userdata;
|
|
|
|
virtual void callback() override {
|
|
|
|
(instance->*method)(userdata);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename C, typename M, typename U>
|
|
|
|
struct GroupUserData : public BaseTemplateUserdata {
|
|
|
|
C *instance;
|
|
|
|
M method;
|
|
|
|
U userdata;
|
|
|
|
virtual void callback_indexed(uint32_t p_index) override {
|
|
|
|
(instance->*method)(p_index, userdata);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-04-09 17:26:45 +02:00
|
|
|
void _wait_collaboratively(ThreadData *p_caller_pool_thread, Task *p_task);
|
|
|
|
|
2024-09-13 14:20:11 +02:00
|
|
|
void _switch_runlevel(Runlevel p_runlevel);
|
2024-09-16 11:51:57 +02:00
|
|
|
bool _handle_runlevel(ThreadData *p_thread_data, MutexLock<BinaryMutex> &p_lock);
|
2024-09-13 14:20:11 +02:00
|
|
|
|
2024-06-19 08:09:59 +02:00
|
|
|
#ifdef THREADS_ENABLED
|
2024-07-18 14:54:58 +02:00
|
|
|
static uint32_t _thread_enter_unlock_allowance_zone(THREADING_NAMESPACE::unique_lock<THREADING_NAMESPACE::mutex> &p_ulock);
|
2024-06-19 08:09:59 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
void _lock_unlockable_mutexes();
|
|
|
|
void _unlock_unlockable_mutexes();
|
|
|
|
|
2022-07-18 12:09:19 +02:00
|
|
|
protected:
|
|
|
|
static void _bind_methods();
|
|
|
|
|
|
|
|
public:
|
2022-07-23 19:12:41 +02:00
|
|
|
template <typename C, typename M, typename U>
|
|
|
|
TaskID add_template_task(C *p_instance, M p_method, U p_userdata, bool p_high_priority = false, const String &p_description = String()) {
|
|
|
|
typedef TaskUserData<C, M, U> TUD;
|
|
|
|
TUD *ud = memnew(TUD);
|
|
|
|
ud->instance = p_instance;
|
|
|
|
ud->method = p_method;
|
|
|
|
ud->userdata = p_userdata;
|
|
|
|
return _add_task(Callable(), nullptr, nullptr, ud, p_high_priority, p_description);
|
|
|
|
}
|
2022-07-18 12:09:19 +02:00
|
|
|
TaskID add_native_task(void (*p_func)(void *), void *p_userdata, bool p_high_priority = false, const String &p_description = String());
|
|
|
|
TaskID add_task(const Callable &p_action, bool p_high_priority = false, const String &p_description = String());
|
|
|
|
|
|
|
|
bool is_task_completed(TaskID p_task_id) const;
|
2023-05-17 00:00:45 +02:00
|
|
|
Error wait_for_task_completion(TaskID p_task_id);
|
2022-07-18 12:09:19 +02:00
|
|
|
|
2024-04-09 17:26:45 +02:00
|
|
|
void yield();
|
|
|
|
void notify_yield_over(TaskID p_task_id);
|
|
|
|
|
2022-07-23 19:12:41 +02:00
|
|
|
template <typename C, typename M, typename U>
|
|
|
|
GroupID add_template_group_task(C *p_instance, M p_method, U p_userdata, int p_elements, int p_tasks = -1, bool p_high_priority = false, const String &p_description = String()) {
|
2023-02-01 10:49:05 +01:00
|
|
|
typedef GroupUserData<C, M, U> GroupUD;
|
|
|
|
GroupUD *ud = memnew(GroupUD);
|
2022-07-23 19:12:41 +02:00
|
|
|
ud->instance = p_instance;
|
|
|
|
ud->method = p_method;
|
|
|
|
ud->userdata = p_userdata;
|
|
|
|
return _add_group_task(Callable(), nullptr, nullptr, ud, p_elements, p_tasks, p_high_priority, p_description);
|
|
|
|
}
|
2022-07-18 12:09:19 +02:00
|
|
|
GroupID add_native_group_task(void (*p_func)(void *, uint32_t), void *p_userdata, int p_elements, int p_tasks = -1, bool p_high_priority = false, const String &p_description = String());
|
|
|
|
GroupID add_group_task(const Callable &p_action, int p_elements, int p_tasks = -1, bool p_high_priority = false, const String &p_description = String());
|
2022-07-23 19:12:41 +02:00
|
|
|
uint32_t get_group_processed_element_count(GroupID p_group) const;
|
2022-07-18 12:09:19 +02:00
|
|
|
bool is_group_task_completed(GroupID p_group) const;
|
|
|
|
void wait_for_group_task_completion(GroupID p_group);
|
|
|
|
|
|
|
|
_FORCE_INLINE_ int get_thread_count() const { return threads.size(); }
|
|
|
|
|
|
|
|
static WorkerThreadPool *get_singleton() { return singleton; }
|
2023-05-23 11:05:32 +02:00
|
|
|
static int get_thread_index();
|
2024-07-10 12:53:14 +02:00
|
|
|
static TaskID get_caller_task_id();
|
2023-05-23 11:05:32 +02:00
|
|
|
|
2024-06-19 08:09:59 +02:00
|
|
|
#ifdef THREADS_ENABLED
|
2024-07-18 14:54:58 +02:00
|
|
|
_ALWAYS_INLINE_ static uint32_t thread_enter_unlock_allowance_zone(const MutexLock<BinaryMutex> &p_lock) { return _thread_enter_unlock_allowance_zone(p_lock._get_lock()); }
|
|
|
|
template <int Tag>
|
|
|
|
_ALWAYS_INLINE_ static uint32_t thread_enter_unlock_allowance_zone(const SafeBinaryMutex<Tag> &p_mutex) { return _thread_enter_unlock_allowance_zone(p_mutex._get_lock()); }
|
2024-06-19 08:09:59 +02:00
|
|
|
static void thread_exit_unlock_allowance_zone(uint32_t p_zone_id);
|
|
|
|
#else
|
2024-07-18 14:54:58 +02:00
|
|
|
static uint32_t thread_enter_unlock_allowance_zone(const MutexLock<BinaryMutex> &p_lock) { return UINT32_MAX; }
|
|
|
|
template <int Tag>
|
|
|
|
static uint32_t thread_enter_unlock_allowance_zone(const SafeBinaryMutex<Tag> &p_mutex) { return UINT32_MAX; }
|
2024-06-19 08:09:59 +02:00
|
|
|
static void thread_exit_unlock_allowance_zone(uint32_t p_zone_id) {}
|
|
|
|
#endif
|
2023-12-29 01:27:17 +01:00
|
|
|
|
WorkerThreadPool: Overhaul scheduling and synchronization
This commits rewrites the sync logic in a way that the
`use_system_threads_for_low_priority_tasks` setting, which was added due to
the lack of a cross-platform wait-for-multiple-objects functionality, can be
removed (it's as if it was effectively hardcoded to `false`).
With the new implementation, we have the best of both worlds: threads don't
have to poll, plus no bespoke threads are used.
In addition, regarding deadlock prevention, since not every possible case of
wait-deadlock could be avoided, this commits removes the current best-effort
avoidance mechanisms and keeps only a simple, pessimistic way of detection.
It turns out that the only current user of deadlock prevention, ResourceLoader,
works fine with it and so every possible situation in resource loading is now
properly handled, with no possibilities of deadlocking. There's a comment in
the code with further details.
Lastly, a potential for load tasks never being awaited/disposed is cleared.
2024-01-05 17:39:26 +01:00
|
|
|
void init(int p_thread_count = -1, float p_low_priority_task_ratio = 0.3);
|
2024-09-16 11:51:57 +02:00
|
|
|
void exit_languages_threads();
|
2022-07-18 12:09:19 +02:00
|
|
|
void finish();
|
|
|
|
WorkerThreadPool();
|
|
|
|
~WorkerThreadPool();
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // WORKER_THREAD_POOL_H
|