Now setting thread_count to 0 or less means get the processor count, and add it to it (it is negative). Also added a fallback thread count, in case it ends up being invalid. (Like get_porocessor_count returns something bogus, or you give it a too high number.)

This commit is contained in:
Relintai 2020-08-04 18:09:21 +02:00
parent a9d55cc1c4
commit b4cda4e527
2 changed files with 33 additions and 1 deletions

View File

@ -61,6 +61,13 @@ void ThreadPool::set_thread_count(const bool value) {
_thread_count = value;
}
int ThreadPool::get_thread_fallback_count() const {
return _thread_fallback_count;
}
void ThreadPool::set_thread_fallback_count(const bool value) {
_thread_fallback_count = value;
}
float ThreadPool::get_max_work_per_frame_percent() const {
return _max_work_per_frame_percent;
}
@ -339,7 +346,15 @@ ThreadPool::ThreadPool() {
_current_queue_tail = 0;
_use_threads = GLOBAL_DEF("thread_pool/use_threads", true);
_thread_count = GLOBAL_DEF("thread_pool/thread_count", 4);
_thread_count = GLOBAL_DEF("thread_pool/thread_count", -1);
_thread_fallback_count = GLOBAL_DEF("thread_pool/thread_fallback_count", 4);
if (_thread_fallback_count <= 0) {
print_error("ThreadPool: thread_fallback_count is invalid! Check ProjectSettings/ThreadPool/thread_fallback_count! Needs to be > 0! Set to 1!");
_thread_fallback_count = 1;
}
//Todo Add help text, as this will only come into play if threading is disabled, or not available
_max_work_per_frame_percent = GLOBAL_DEF("thread_pool/max_work_per_frame_percent", 25);
@ -355,6 +370,15 @@ ThreadPool::ThreadPool() {
}
if (_use_threads) {
if (_thread_count <= 0) {
_thread_count = OS::get_singleton()->get_processor_count() + _thread_count;
}
//a.k.a OS::get_singleton()->get_processor_count() is not implemented, or returns something unexpected, or too high negative number
if (_thread_count <= 0) {
_thread_count = _thread_fallback_count;
}
_threads.resize(_thread_count);
for (int i = 0; i < _threads.size(); ++i) {
@ -408,6 +432,10 @@ void ThreadPool::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_thread_count", "value"), &ThreadPool::set_thread_count);
ADD_PROPERTY(PropertyInfo(Variant::INT, "thread_count"), "set_thread_count", "get_thread_count");
ClassDB::bind_method(D_METHOD("get_thread_fallback_count"), &ThreadPool::get_thread_fallback_count);
ClassDB::bind_method(D_METHOD("set_thread_fallback_count", "value"), &ThreadPool::set_thread_fallback_count);
ADD_PROPERTY(PropertyInfo(Variant::INT, "thread_fallback_count"), "set_thread_fallback_count", "get_thread_fallback_count");
ClassDB::bind_method(D_METHOD("get_max_work_per_frame_percent"), &ThreadPool::get_max_work_per_frame_percent);
ClassDB::bind_method(D_METHOD("set_max_work_per_frame_percent", "value"), &ThreadPool::set_max_work_per_frame_percent);
ADD_PROPERTY(PropertyInfo(Variant::REAL, "max_work_per_frame_percent"), "set_max_work_per_frame_percent", "get_max_work_per_frame_percent");

View File

@ -63,6 +63,9 @@ public:
int get_thread_count() const;
void set_thread_count(const bool value);
int get_thread_fallback_count() const;
void set_thread_fallback_count(const bool value);
float get_max_work_per_frame_percent() const;
void set_max_work_per_frame_percent(const bool value);
@ -102,6 +105,7 @@ private:
bool _use_threads;
int _thread_count;
int _thread_fallback_count;
float _max_work_per_frame_percent;
float _max_time_per_frame;