Debian Codegraph @

tests/core/threads/test_worker_thread_pool.h

raw symbols SCIP
1 /**************************************************************************/
2 /* test_worker_thread_pool.h */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #pragma once
32
33 #include "core/object/worker_thread_pool.h"
34
35 #include "tests/test_macros.h"
36
37 namespace TestWorkerThreadPool {
38
39 static LocalVector<SafeNumeric<int>> counter;
40 static SafeFlag exit;
41
42 static void static_test(void *p_arg) {
43 counter[(uint64_t)p_arg].increment();
44 counter[0].add(2);
45 }
46 static void static_callable_test() {
47 counter[0].sub(2);
48 }
49 TEST_CASE("[WorkerThreadPool] Process threads using individual tasks") {
50 for (int iterations = 0; iterations < 500; iterations++) {
51 const int count = Math::pow(2.0f, Math::random(0.0f, 5.0f));
52 const bool low_priority = Math::rand() % 2;
53
54 LocalVector<WorkerThreadPool::TaskID> tasks1;
55 LocalVector<WorkerThreadPool::TaskID> tasks2;
56 tasks1.resize(count);
57 tasks2.resize(count);
58
59 counter.clear();
60 counter.resize(count);
61 for (int i = 0; i < count; i++) {
62 tasks1[i] = WorkerThreadPool::get_singleton()->add_native_task(static_test, (void *)(uintptr_t)i, low_priority);
63 tasks2[i] = WorkerThreadPool::get_singleton()->add_task(callable_mp_static(static_callable_test), !low_priority);
64 }
65 for (int i = 0; i < count; i++) {
66 WorkerThreadPool::get_singleton()->wait_for_task_completion(tasks1[i]);
67 WorkerThreadPool::get_singleton()->wait_for_task_completion(tasks2[i]);
68 }
69
70 bool all_run_once = true;
71 for (int i = 0; i < count; i++) {
72 //Reduce number of check messages
73 all_run_once &= counter[i].get() == 1;
74 }
75 CHECK(all_run_once);
76 }
77 }
78
79 static void static_group_test(void *p_arg, uint32_t p_index) {
80 counter[p_index].increment();
81 counter[0].add((uintptr_t)p_arg);
82 }
83 static void static_callable_group_test(uint32_t p_index) {
84 counter[p_index].increment();
85 counter[0].sub(2);
86 }
87 TEST_CASE("[WorkerThreadPool] Process elements using group tasks") {
88 for (int iterations = 0; iterations < 500; iterations++) {
89 const int count = Math::pow(2.0f, Math::random(0.0f, 5.0f));
90 const int tasks = Math::pow(2.0f, Math::random(0.0f, 5.0f));
91 const bool low_priority = Math::rand() % 2;
92
93 counter.clear();
94 counter.resize(count);
95 WorkerThreadPool::GroupID group1 = WorkerThreadPool::get_singleton()->add_native_group_task(static_group_test, (void *)2, count, tasks, !low_priority);
96 WorkerThreadPool::GroupID group2 = WorkerThreadPool::get_singleton()->add_group_task(callable_mp_static(static_callable_group_test), count, tasks, low_priority);
97 WorkerThreadPool::get_singleton()->wait_for_group_task_completion(group1);
98 WorkerThreadPool::get_singleton()->wait_for_group_task_completion(group2);
99
100 bool all_run_once = true;
101 for (int i = 0; i < count; i++) {
102 //Reduce number of check messages
103 all_run_once &= counter[i].get() == 2;
104 }
105 CHECK(all_run_once);
106 }
107 }
108
109 static void static_test_daemon(void *p_arg) {
110 while (!exit.is_set()) {
111 counter[0].add(1);
112 WorkerThreadPool::get_singleton()->yield();
113 }
114 }
115
116 static void static_busy_task(void *p_arg) {
117 while (!exit.is_set()) {
118 OS::get_singleton()->delay_usec(1);
119 }
120 }
121
122 static void static_legit_task(void *p_arg) {
123 *((bool *)p_arg) = counter[0].get() > 0;
124 counter[1].add(1);
125 }
126
127 TEST_CASE("[WorkerThreadPool] Run a yielding daemon as the only hope for other tasks to run") {
128 exit.clear();
129 counter.clear();
130 counter.resize(2);
131
132 WorkerThreadPool::TaskID daemon_task_id = WorkerThreadPool::get_singleton()->add_native_task(static_test_daemon, nullptr, true);
133
134 int num_threads = WorkerThreadPool::get_singleton()->get_thread_count();
135
136 // Keep all the other threads busy.
137 LocalVector<WorkerThreadPool::TaskID> task_ids;
138 for (int i = 0; i < num_threads - 1; i++) {
139 task_ids.push_back(WorkerThreadPool::get_singleton()->add_native_task(static_busy_task, nullptr, true));
140 }
141
142 LocalVector<WorkerThreadPool::TaskID> legit_task_ids;
143 LocalVector<bool> legit_task_needed_yield;
144 int legit_tasks_count = num_threads * 4;
145 legit_task_needed_yield.resize(legit_tasks_count);
146 for (int i = 0; i < legit_tasks_count; i++) {
147 legit_task_needed_yield[i] = false;
148 task_ids.push_back(WorkerThreadPool::get_singleton()->add_native_task(static_legit_task, &legit_task_needed_yield[i], i >= legit_tasks_count / 2));
149 }
150
151 while (counter[1].get() != legit_tasks_count) {
152 OS::get_singleton()->delay_usec(1);
153 }
154
155 exit.set();
156 for (uint32_t i = 0; i < task_ids.size(); i++) {
157 WorkerThreadPool::get_singleton()->wait_for_task_completion(task_ids[i]);
158 }
159 WorkerThreadPool::get_singleton()->notify_yield_over(daemon_task_id);
160 WorkerThreadPool::get_singleton()->wait_for_task_completion(daemon_task_id);
161
162 CHECK_MESSAGE(counter[0].get() > 0, "Daemon task should have looped at least once.");
163 CHECK_MESSAGE(counter[1].get() == legit_tasks_count, "All legit tasks should have been able to run.");
164
165 bool all_needed_yield = true;
166 for (int i = 0; i < legit_tasks_count; i++) {
167 if (!legit_task_needed_yield[i]) {
168 all_needed_yield = false;
169 break;
170 }
171 }
172 CHECK_MESSAGE(all_needed_yield, "All legit tasks should have needed the daemon yielding to run.");
173 }
174
175 } // namespace TestWorkerThreadPool