Skip to content

Commit 2cf16fb

Browse files
committed
Merge pull request #107040 from bruvzg/wtp_ggrid
Add `WorkerThreadPool.get_caller_group_id`
2 parents 3b7547f + b6461a3 commit 2cf16fb

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

core/object/worker_thread_pool.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,15 @@ WorkerThreadPool::TaskID WorkerThreadPool::get_caller_task_id() const {
740740
}
741741
}
742742

743+
WorkerThreadPool::GroupID WorkerThreadPool::get_caller_group_id() const {
744+
int th_index = get_thread_index();
745+
if (th_index != -1 && threads[th_index].current_task && threads[th_index].current_task->group) {
746+
return threads[th_index].current_task->group->self;
747+
} else {
748+
return INVALID_TASK_ID;
749+
}
750+
}
751+
743752
#ifdef THREADS_ENABLED
744753
uint32_t WorkerThreadPool::_thread_enter_unlock_allowance_zone(THREADING_NAMESPACE::unique_lock<THREADING_NAMESPACE::mutex> &p_ulock) {
745754
for (uint32_t i = 0; i < MAX_UNLOCKABLE_LOCKS; i++) {
@@ -856,13 +865,13 @@ void WorkerThreadPool::_bind_methods() {
856865
ClassDB::bind_method(D_METHOD("add_task", "action", "high_priority", "description"), &WorkerThreadPool::add_task, DEFVAL(false), DEFVAL(String()));
857866
ClassDB::bind_method(D_METHOD("is_task_completed", "task_id"), &WorkerThreadPool::is_task_completed);
858867
ClassDB::bind_method(D_METHOD("wait_for_task_completion", "task_id"), &WorkerThreadPool::wait_for_task_completion);
868+
ClassDB::bind_method(D_METHOD("get_caller_task_id"), &WorkerThreadPool::get_caller_task_id);
859869

860870
ClassDB::bind_method(D_METHOD("add_group_task", "action", "elements", "tasks_needed", "high_priority", "description"), &WorkerThreadPool::add_group_task, DEFVAL(-1), DEFVAL(false), DEFVAL(String()));
861871
ClassDB::bind_method(D_METHOD("is_group_task_completed", "group_id"), &WorkerThreadPool::is_group_task_completed);
862872
ClassDB::bind_method(D_METHOD("get_group_processed_element_count", "group_id"), &WorkerThreadPool::get_group_processed_element_count);
863873
ClassDB::bind_method(D_METHOD("wait_for_group_task_completion", "group_id"), &WorkerThreadPool::wait_for_group_task_completion);
864-
865-
ClassDB::bind_method(D_METHOD("get_caller_task_id"), &WorkerThreadPool::get_caller_task_id);
874+
ClassDB::bind_method(D_METHOD("get_caller_group_id"), &WorkerThreadPool::get_caller_group_id);
866875
}
867876

868877
WorkerThreadPool *WorkerThreadPool::get_named_pool(const StringName &p_name) {

core/object/worker_thread_pool.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ class WorkerThreadPool : public Object {
274274
static WorkerThreadPool *get_singleton() { return singleton; }
275275
int get_thread_index() const;
276276
TaskID get_caller_task_id() const;
277+
GroupID get_caller_group_id() const;
277278

278279
#ifdef THREADS_ENABLED
279280
_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()); }

doc/classes/WorkerThreadPool.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@
7272
[b]Warning:[/b] Every task must be waited for completion using [method wait_for_task_completion] or [method wait_for_group_task_completion] at some point so that any allocated resources inside the task can be cleaned up.
7373
</description>
7474
</method>
75+
<method name="get_caller_group_id" qualifiers="const">
76+
<return type="int" />
77+
<description>
78+
Returns the task group ID of the current thread calling this method, or [code]-1[/code] if invalid or the current thread is not part of a task group.
79+
</description>
80+
</method>
7581
<method name="get_caller_task_id" qualifiers="const">
7682
<return type="int" />
7783
<description>

0 commit comments

Comments
 (0)