Skip to content

Commit

Permalink
Now setting thread_count to 0 or less means get the processor count, …
Browse files Browse the repository at this point in the history
…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.)
  • Loading branch information
Relintai committed Aug 4, 2020
1 parent a9d55cc commit b4cda4e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
30 changes: 29 additions & 1 deletion thread_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);

Expand All @@ -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) {
Expand Down Expand Up @@ -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");
Expand Down
4 changes: 4 additions & 0 deletions thread_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ class ThreadPool : public Object {
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);

Expand Down Expand Up @@ -102,6 +105,7 @@ class ThreadPool : public Object {

bool _use_threads;
int _thread_count;
int _thread_fallback_count;
float _max_work_per_frame_percent;
float _max_time_per_frame;

Expand Down

0 comments on commit b4cda4e

Please sign in to comment.