diff options
author | myaaaaaaaaa <103326468+myaaaaaaaaa@users.noreply.github.com> | 2023-02-06 00:23:45 -0500 |
---|---|---|
committer | myaaaaaaaaa <103326468+myaaaaaaaaa@users.noreply.github.com> | 2023-06-09 08:27:32 -0400 |
commit | e28868e30cd478e878d0a8331e17e2cf90039558 (patch) | |
tree | 759c0da9d10b3700ab2c5fcd85b207eb7369b6cf /core | |
parent | 828ec2c5d005b6499c7c4c88beaf81767d05614b (diff) | |
download | redot-engine-e28868e30cd478e878d0a8331e17e2cf90039558.tar.gz |
Implement parallel foreach() for easier multithreading
Diffstat (limited to 'core')
-rw-r--r-- | core/object/worker_thread_pool.h | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/core/object/worker_thread_pool.h b/core/object/worker_thread_pool.h index d4d9387765..9fe8497eaf 100644 --- a/core/object/worker_thread_pool.h +++ b/core/object/worker_thread_pool.h @@ -202,4 +202,25 @@ public: ~WorkerThreadPool(); }; +template <typename F> +static _FORCE_INLINE_ void for_range(int i_begin, int i_end, bool parallel, String name, F f) { + if (!parallel) { + for (int i = i_begin; i < i_end; i++) { + f(i); + } + return; + } + + auto wrapper = [&](int i, void *unused) { + f(i + i_begin); + }; + + WorkerThreadPool *wtp = WorkerThreadPool::get_singleton(); + WorkerThreadPool::GroupID gid = wtp->add_template_group_task( + &wrapper, &decltype(wrapper)::operator(), nullptr, + i_end - i_begin, -1, + true, name); + wtp->wait_for_group_task_completion(gid); +} + #endif // WORKER_THREAD_POOL_H |