mirror of
https://github.com/luanti-org/luanti.git
synced 2025-09-05 18:41:05 +00:00
Add API to cancel async jobs (#14602)
* Implement API to cancel async jobs Co-authored-by: sfan5 <sfan5@live.de> * update AsyncJob:cancel documentation from review * Use IPC to unblock async * review * review async unblocking * review * Apply suggestions from code review Co-authored-by: sfan5 <sfan5@live.de> * minor licensing --------- Co-authored-by: y5nw <y5nw@protonmail.com> Co-authored-by: sfan5 <sfan5@live.de>
This commit is contained in:
parent
7cbe62fe7b
commit
f390137d6e
12 changed files with 229 additions and 78 deletions
|
@ -96,38 +96,44 @@ void AsyncEngine::addWorkerThread()
|
|||
}
|
||||
|
||||
/******************************************************************************/
|
||||
u32 AsyncEngine::queueAsyncJob(std::string &&func, std::string &¶ms,
|
||||
const std::string &mod_origin)
|
||||
|
||||
u32 AsyncEngine::queueAsyncJob(LuaJobInfo &&job)
|
||||
{
|
||||
MutexAutoLock autolock(jobQueueMutex);
|
||||
u32 jobId = jobIdCounter++;
|
||||
|
||||
jobQueue.emplace_back();
|
||||
auto &to_add = jobQueue.back();
|
||||
to_add.id = jobId;
|
||||
to_add.function = std::move(func);
|
||||
to_add.params = std::move(params);
|
||||
to_add.mod_origin = mod_origin;
|
||||
assert(!job.function.empty());
|
||||
job.id = jobId;
|
||||
jobQueue.push_back(std::move(job));
|
||||
|
||||
jobQueueCounter.post();
|
||||
return jobId;
|
||||
}
|
||||
|
||||
u32 AsyncEngine::queueAsyncJob(std::string &&func, std::string &¶ms,
|
||||
const std::string &mod_origin)
|
||||
{
|
||||
LuaJobInfo to_add(std::move(func), std::move(params), mod_origin);
|
||||
return queueAsyncJob(std::move(to_add));
|
||||
}
|
||||
|
||||
u32 AsyncEngine::queueAsyncJob(std::string &&func, PackedValue *params,
|
||||
const std::string &mod_origin)
|
||||
{
|
||||
LuaJobInfo to_add(std::move(func), params, mod_origin);
|
||||
return queueAsyncJob(std::move(to_add));
|
||||
}
|
||||
|
||||
bool AsyncEngine::cancelAsyncJob(u32 id)
|
||||
{
|
||||
MutexAutoLock autolock(jobQueueMutex);
|
||||
u32 jobId = jobIdCounter++;
|
||||
|
||||
jobQueue.emplace_back();
|
||||
auto &to_add = jobQueue.back();
|
||||
to_add.id = jobId;
|
||||
to_add.function = std::move(func);
|
||||
to_add.params_ext.reset(params);
|
||||
to_add.mod_origin = mod_origin;
|
||||
|
||||
jobQueueCounter.post();
|
||||
return jobId;
|
||||
for (auto job = jobQueue.begin(); job != jobQueue.end(); job++) {
|
||||
if (job->id == id) {
|
||||
jobQueue.erase(job);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
|
@ -419,3 +425,19 @@ void* AsyncWorkerThread::run()
|
|||
return 0;
|
||||
}
|
||||
|
||||
u32 ScriptApiAsync::queueAsync(std::string &&serialized_func,
|
||||
PackedValue *param, const std::string &mod_origin)
|
||||
{
|
||||
return asyncEngine.queueAsyncJob(std::move(serialized_func),
|
||||
param, mod_origin);
|
||||
}
|
||||
|
||||
bool ScriptApiAsync::cancelAsync(u32 id)
|
||||
{
|
||||
return asyncEngine.cancelAsyncJob(id);
|
||||
}
|
||||
|
||||
void ScriptApiAsync::stepAsync()
|
||||
{
|
||||
asyncEngine.step(getStack());
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue