| 813 | #endif |
| 814 | |
| 815 | void WorkerThreadPool::init(int p_thread_count, float p_low_priority_task_ratio) { |
| 816 | ERR_FAIL_COND(threads.size() > 0); |
| 817 | |
| 818 | runlevel = RUNLEVEL_NORMAL; |
| 819 | |
| 820 | if (p_thread_count < 0) { |
| 821 | p_thread_count = OS::get_singleton()->get_default_thread_pool_size(); |
| 822 | } |
| 823 | |
| 824 | max_low_priority_threads = CLAMP(p_thread_count * p_low_priority_task_ratio, 1, p_thread_count - 1); |
| 825 | |
| 826 | print_verbose(vformat("WorkerThreadPool: %d threads, %d max low-priority.", p_thread_count, max_low_priority_threads)); |
| 827 | |
| 828 | #ifdef THREADS_ENABLED |
| 829 | // Reserve 5 threads in case we need separate threads for 1) 2D physics 2) 3D physics 3) rendering 4) GPU texture compression, 5) all other tasks. |
| 830 | // We cannot safely increase the Vector size at runtime, so reserve enough up front, but only launch those needed. |
| 831 | threads.reserve(5); |
| 832 | #endif |
| 833 | threads.resize(p_thread_count); |
| 834 | |
| 835 | Thread::Settings settings; |
| 836 | #ifdef __APPLE__ |
| 837 | // The default stack size for new threads on Apple platforms is 512KiB. |
| 838 | // This is insufficient when using a library like SPIRV-Cross, |
| 839 | // which can generate deep stacks and result in a stack overflow. |
| 840 | #ifdef DEV_ENABLED |
| 841 | // Debug builds need an even larger stack size. |
| 842 | settings.stack_size = 2 * 1024 * 1024; // 2 MiB |
| 843 | #else |
| 844 | settings.stack_size = 1 * 1024 * 1024; // 1 MiB |
| 845 | #endif |
| 846 | #endif |
| 847 | |
| 848 | for (uint32_t i = 0; i < threads.size(); i++) { |
| 849 | threads[i].index = i; |
| 850 | threads[i].pool = this; |
| 851 | threads[i].thread.start(&WorkerThreadPool::_thread_function, &threads[i], settings); |
| 852 | thread_ids.insert(threads[i].thread.get_id(), i); |
| 853 | } |
| 854 | } |
| 855 | |
| 856 | void WorkerThreadPool::exit_languages_threads() { |
| 857 | if (threads.is_empty()) { |
no test coverage detected