|
@@ -64,8 +64,10 @@ void process_array_thread(void *ud) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+// p_num_threads is the number of logical CPU cores to use (0 = use all logical CPU cores available).
|
|
|
+// Negative values subtract from the total number of logical CPU cores available.
|
|
|
template <class C, class M, class U>
|
|
|
-void thread_process_array(uint32_t p_elements, C *p_instance, M p_method, U p_userdata) {
|
|
|
+void thread_process_array(uint32_t p_elements, C *p_instance, M p_method, U p_userdata, int p_num_threads = 0) {
|
|
|
ThreadArrayProcessData<C, U> data;
|
|
|
data.method = p_method;
|
|
|
data.instance = p_instance;
|
|
@@ -74,7 +76,13 @@ void thread_process_array(uint32_t p_elements, C *p_instance, M p_method, U p_us
|
|
|
data.elements = p_elements;
|
|
|
data.process(0); //process first, let threads increment for next
|
|
|
|
|
|
- int thread_count = OS::get_singleton()->get_processor_count();
|
|
|
+ int thread_count;
|
|
|
+ if (p_num_threads <= 0) {
|
|
|
+ thread_count = MAX(1, OS::get_singleton()->get_processor_count() + p_num_threads);
|
|
|
+ } else {
|
|
|
+ thread_count = p_num_threads;
|
|
|
+ }
|
|
|
+
|
|
|
Thread *threads = memnew_arr(Thread, thread_count);
|
|
|
|
|
|
for (int i = 0; i < thread_count; i++) {
|
|
@@ -89,8 +97,9 @@ void thread_process_array(uint32_t p_elements, C *p_instance, M p_method, U p_us
|
|
|
|
|
|
#else
|
|
|
|
|
|
+// p_num_threads is intentionally unused when threads are disabled.
|
|
|
template <class C, class M, class U>
|
|
|
-void thread_process_array(uint32_t p_elements, C *p_instance, M p_method, U p_userdata) {
|
|
|
+void thread_process_array(uint32_t p_elements, C *p_instance, M p_method, U p_userdata, int p_num_threads = 0) {
|
|
|
ThreadArrayProcessData<C, U> data;
|
|
|
data.method = p_method;
|
|
|
data.instance = p_instance;
|