mirror of
https://github.com/luanti-org/luanti.git
synced 2025-07-27 17:28:41 +00:00
Implement API to cancel async jobs
Co-authored-by: sfan5 <sfan5@live.de>
This commit is contained in:
parent
535d757563
commit
660151972f
12 changed files with 232 additions and 78 deletions
|
@ -26,6 +26,12 @@ class AsyncEngine;
|
|||
struct LuaJobInfo
|
||||
{
|
||||
LuaJobInfo() = default;
|
||||
LuaJobInfo(std::string &&func, std::string &¶ms, const std::string &mod_origin = "") :
|
||||
function(func), params(params), mod_origin(mod_origin) {}
|
||||
LuaJobInfo(std::string &&func, PackedValue *params, const std::string &mod_origin = "") :
|
||||
function(func), mod_origin(mod_origin) {
|
||||
params_ext.reset(params);
|
||||
}
|
||||
|
||||
// Function to be called in async environment (from string.dump)
|
||||
std::string function;
|
||||
|
@ -102,12 +108,26 @@ public:
|
|||
u32 queueAsyncJob(std::string &&func, PackedValue *params,
|
||||
const std::string &mod_origin = "");
|
||||
|
||||
/**
|
||||
* Try to cancel an async job
|
||||
* @param id The ID of the job
|
||||
* @return Whether the job was cancelled
|
||||
*/
|
||||
bool cancelAsyncJob(u32 id);
|
||||
|
||||
/**
|
||||
* Engine step to process finished jobs
|
||||
* @param L The Lua stack
|
||||
*/
|
||||
void step(lua_State *L);
|
||||
|
||||
/**
|
||||
* Get the maximum number of threads that can be used by the async environment
|
||||
*/
|
||||
unsigned int getThreadingCapacity() const {
|
||||
return MYMAX(workerThreads.size(), autoscaleMaxWorkers);
|
||||
}
|
||||
|
||||
protected:
|
||||
/**
|
||||
* Get a Job from queue to be processed
|
||||
|
@ -117,6 +137,13 @@ protected:
|
|||
*/
|
||||
bool getJob(LuaJobInfo *job);
|
||||
|
||||
/**
|
||||
* Queue an async job
|
||||
* @param job The job to queue (takes ownership!)
|
||||
* @return Id of the queued job
|
||||
*/
|
||||
u32 queueAsyncJob(LuaJobInfo &&job);
|
||||
|
||||
/**
|
||||
* Put a Job result back to result queue
|
||||
* @param result result of completed job
|
||||
|
@ -206,3 +233,23 @@ private:
|
|||
// Counter semaphore for job dispatching
|
||||
Semaphore jobQueueCounter;
|
||||
};
|
||||
|
||||
class ScriptApiAsync:
|
||||
virtual public ScriptApiBase
|
||||
{
|
||||
public:
|
||||
ScriptApiAsync(Server *server): asyncEngine(server) {}
|
||||
|
||||
virtual void initAsync() = 0;
|
||||
void stepAsync();
|
||||
|
||||
u32 queueAsync(std::string &&serialized_func,
|
||||
PackedValue *param, const std::string &mod_origin);
|
||||
bool cancelAsync(u32 id);
|
||||
unsigned int getThreadingCapacity() const {
|
||||
return asyncEngine.getThreadingCapacity();
|
||||
}
|
||||
|
||||
protected:
|
||||
AsyncEngine asyncEngine;
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue