mirror of
https://github.com/Relintai/thread_pool.git
synced 2024-11-12 10:25:29 +01:00
Tasks can be cancelled now.
This commit is contained in:
parent
222e9fa6cd
commit
61fffea0a4
@ -73,6 +73,64 @@ void ThreadPool::set_max_time_per_frame(const bool value) {
|
||||
_max_time_per_frame = value;
|
||||
}
|
||||
|
||||
void ThreadPool::cancel_task_wait(Ref<ThreadPoolJob> job) {
|
||||
ERR_FAIL_COND(!job.is_valid());
|
||||
|
||||
_THREAD_SAFE_LOCK_
|
||||
|
||||
for (int i = 0; i < _queue.size(); ++i) {
|
||||
Ref<ThreadPoolJob> j = _queue[i];
|
||||
|
||||
if (j == job) {
|
||||
_queue.write[i].unref();
|
||||
_THREAD_SAFE_UNLOCK_
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
_THREAD_SAFE_UNLOCK_
|
||||
|
||||
for (int i = 0; i < _threads.size(); ++i) {
|
||||
Ref<ThreadPoolJob> j = _threads[i]->job;
|
||||
|
||||
if (j == job) {
|
||||
job->set_cancelled(true);
|
||||
|
||||
while (_threads[i]->job == job) {
|
||||
OS::get_singleton()->delay_usec(1000);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
void ThreadPool::cancel_task(Ref<ThreadPoolJob> job) {
|
||||
ERR_FAIL_COND(!job.is_valid());
|
||||
|
||||
_THREAD_SAFE_LOCK_
|
||||
|
||||
for (int i = 0; i < _queue.size(); ++i) {
|
||||
Ref<ThreadPoolJob> j = _queue[i];
|
||||
|
||||
if (j == job) {
|
||||
_queue.write[i].unref();
|
||||
_THREAD_SAFE_UNLOCK_
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
_THREAD_SAFE_UNLOCK_
|
||||
|
||||
for (int i = 0; i < _threads.size(); ++i) {
|
||||
Ref<ThreadPoolJob> j = _threads[i]->job;
|
||||
|
||||
if (j == job) {
|
||||
job->set_cancelled(true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ref<ThreadPoolJob> ThreadPool::get_running_job(const Variant &object, const StringName &method) {
|
||||
for (int i = 0; i < _threads.size(); ++i) {
|
||||
Ref<ThreadPoolJob> j = _threads[i]->job;
|
||||
@ -371,4 +429,7 @@ void ThreadPool::_bind_methods() {
|
||||
|
||||
ClassDB::bind_method(D_METHOD("reqister_update"), &ThreadPool::reqister_update);
|
||||
ClassDB::bind_method(D_METHOD("update"), &ThreadPool::update);
|
||||
|
||||
ClassDB::bind_method(D_METHOD("cancel_task_wait", "job"), &ThreadPool::cancel_task_wait);
|
||||
ClassDB::bind_method(D_METHOD("cancel_task", "job"), &ThreadPool::cancel_task);
|
||||
}
|
||||
|
@ -68,6 +68,9 @@ public:
|
||||
float get_max_time_per_frame() const;
|
||||
void set_max_time_per_frame(const bool value);
|
||||
|
||||
void cancel_task_wait(Ref<ThreadPoolJob> job);
|
||||
void cancel_task(Ref<ThreadPoolJob> job);
|
||||
|
||||
Ref<ThreadPoolJob> get_running_job(const Variant &object, const StringName &method);
|
||||
Ref<ThreadPoolJob> get_queued_job(const Variant &object, const StringName &method);
|
||||
|
||||
|
@ -32,6 +32,13 @@ void ThreadPoolJob::set_complete(const bool value) {
|
||||
_complete = value;
|
||||
}
|
||||
|
||||
bool ThreadPoolJob::get_cancelled() const {
|
||||
return _cancelled;
|
||||
}
|
||||
void ThreadPoolJob::set_cancelled(const bool value) {
|
||||
_cancelled = value;
|
||||
}
|
||||
|
||||
bool ThreadPoolJob::get_limit_execution_time() const {
|
||||
return _limit_execution_time;
|
||||
}
|
||||
@ -101,6 +108,9 @@ bool ThreadPoolJob::should_skip(const bool just_check) {
|
||||
return false;
|
||||
}
|
||||
bool ThreadPoolJob::should_continue() {
|
||||
if (_cancelled)
|
||||
return false;
|
||||
|
||||
if (!_limit_execution_time)
|
||||
return true;
|
||||
|
||||
@ -121,6 +131,7 @@ void ThreadPoolJob::execute() {
|
||||
|
||||
void ThreadPoolJob::setup(const Variant &obj, const StringName &p_method, VARIANT_ARG_DECLARE) {
|
||||
_complete = false;
|
||||
_cancelled = false;
|
||||
_object = obj;
|
||||
_method = p_method;
|
||||
|
||||
@ -231,6 +242,7 @@ Variant ThreadPoolJob::_setup_bind(const Variant **p_args, int p_argcount, Calla
|
||||
|
||||
ThreadPoolJob::ThreadPoolJob() {
|
||||
_complete = true;
|
||||
_cancelled = false;
|
||||
|
||||
_limit_execution_time = false;
|
||||
_max_allocated_time = 0;
|
||||
|
@ -34,6 +34,9 @@ public:
|
||||
bool get_complete() const;
|
||||
void set_complete(const bool value);
|
||||
|
||||
bool get_cancelled() const;
|
||||
void set_cancelled(const bool value);
|
||||
|
||||
bool get_limit_execution_time() const;
|
||||
void set_limit_execution_time(const bool value);
|
||||
|
||||
@ -76,6 +79,7 @@ protected:
|
||||
|
||||
private:
|
||||
bool _complete;
|
||||
bool _cancelled;
|
||||
|
||||
bool _limit_execution_time;
|
||||
float _max_allocated_time;
|
||||
|
Loading…
Reference in New Issue
Block a user