#include <s_async.h>
Public Member Functions | |
AsyncEngine ()=default | |
AsyncEngine (Server *server) | |
~AsyncEngine () | |
void | registerStateInitializer (StateInitializer func) |
Register function to be called on new states. | |
void | initialize (unsigned int numEngines) |
Create async engine tasks and lock function registration. | |
u32 | queueAsyncJob (std::string &&func, std::string &¶ms, const std::string &mod_origin="") |
Queue an async job. | |
u32 | queueAsyncJob (std::string &&func, PackedValue *params, const std::string &mod_origin="") |
Queue an async job. | |
void | step (lua_State *L) |
Engine step to process finished jobs. | |
Protected Member Functions | |
bool | getJob (LuaJobInfo *job) |
Get a Job from queue to be processed this function blocks until a job is ready. | |
void | putJobResult (LuaJobInfo &&result) |
Put a Job result back to result queue. | |
void | addWorkerThread () |
Start an additional worker thread. | |
void | stepJobResults (lua_State *L) |
Process finished jobs callbacks. | |
void | stepAutoscale () |
Handle automatic scaling of worker threads. | |
bool | prepareEnvironment (lua_State *L, int top) |
Initialize environment with current registred functions this function adds all functions registred by registerFunction to the passed lua stack. | |
Private Types | |
typedef void(* | StateInitializer) (lua_State *L, int top) |
Private Attributes | |
bool | initDone = false |
unsigned int | autoscaleMaxWorkers = 0 |
u64 | autoscaleTimer = 0 |
std::unordered_set< u32 > | autoscaleSeenJobs |
Server * | server = nullptr |
std::vector< StateInitializer > | stateInitializers |
u32 | jobIdCounter = 0 |
std::mutex | jobQueueMutex |
std::deque< LuaJobInfo > | jobQueue |
std::mutex | resultQueueMutex |
std::deque< LuaJobInfo > | resultQueue |
std::vector< AsyncWorkerThread * > | workerThreads |
Semaphore | jobQueueCounter |
Friends | |
class | AsyncWorkerThread |
|
private |
|
default |
|
inline |
AsyncEngine::~AsyncEngine | ( | ) |
References infostream, jobQueue, jobQueueCounter, jobQueueMutex, Semaphore::post(), and workerThreads.
|
protected |
Start an additional worker thread.
References AsyncWorkerThread, itos(), Thread::start(), and workerThreads.
Referenced by initialize(), and stepAutoscale().
|
protected |
Get a Job from queue to be processed this function blocks until a job is ready.
job | a job to be processed |
References jobQueue, jobQueueCounter, jobQueueMutex, and Semaphore::wait().
Referenced by AsyncWorkerThread::run().
void AsyncEngine::initialize | ( | unsigned int | numEngines | ) |
Create async engine tasks and lock function registration.
numEngines | Number of worker threads, 0 for automatic scaling |
References addWorkerThread(), autoscaleMaxWorkers, Thread::getNumberOfProcessors(), infostream, and initDone.
Referenced by ServerScripting::initAsync(), and MainMenuScripting::initializeModApi().
|
protected |
Initialize environment with current registred functions this function adds all functions registred by registerFunction to the passed lua stack.
L | Lua stack to initialize |
top | Stack position |
References BUILTIN_MOD_NAME, DIR_DELIM, errorstream, FATAL_ERROR, Server::getBuiltinLuaPath(), ModApiBase::getScriptApiBase(), stateInitializers, and BaseException::what().
Referenced by AsyncWorkerThread::AsyncWorkerThread().
|
protected |
Put a Job result back to result queue.
result | result of completed job |
References resultQueue, and resultQueueMutex.
Referenced by AsyncWorkerThread::run().
u32 AsyncEngine::queueAsyncJob | ( | std::string && | func, |
PackedValue * | params, | ||
const std::string & | mod_origin = "" ) |
Queue an async job.
func | Serialized lua function |
params | Serialized parameters (takes ownership!) |
References jobIdCounter, jobQueue, jobQueueCounter, jobQueueMutex, and Semaphore::post().
u32 AsyncEngine::queueAsyncJob | ( | std::string && | func, |
std::string && | params, | ||
const std::string & | mod_origin = "" ) |
Queue an async job.
func | Serialized lua function |
params | Serialized parameters |
References jobIdCounter, jobQueue, jobQueueCounter, jobQueueMutex, and Semaphore::post().
Referenced by MainMenuScripting::queueAsync(), and ServerScripting::queueAsync().
void AsyncEngine::registerStateInitializer | ( | StateInitializer | func | ) |
Register function to be called on new states.
func | C function to be called |
References FATAL_ERROR_IF, initDone, and stateInitializers.
Referenced by ServerScripting::initAsync(), and MainMenuScripting::initializeModApi().
void AsyncEngine::step | ( | lua_State * | L | ) |
Engine step to process finished jobs.
L | The Lua stack |
References stepAutoscale(), and stepJobResults().
Referenced by MainMenuScripting::step(), and ServerScripting::stepAsync().
|
protected |
Handle automatic scaling of worker threads.
References addWorkerThread(), autoscaleMaxWorkers, autoscaleSeenJobs, autoscaleTimer, porting::getTimeMs(), infostream, jobQueue, jobQueueMutex, and workerThreads.
Referenced by step().
|
protected |
Process finished jobs callbacks.
References FATAL_ERROR, ModApiBase::getScriptApiBase(), LuaJobInfo::id, LuaJobInfo::mod_origin, PUSH_ERROR_HANDLER, LuaJobInfo::result, LuaJobInfo::result_ext, resultQueue, resultQueueMutex, script_error(), script_unpack(), and ScriptApiBase::setOriginDirect().
Referenced by step().
|
friend |
Referenced by addWorkerThread().
|
private |
Referenced by initialize(), and stepAutoscale().
|
private |
Referenced by stepAutoscale().
|
private |
Referenced by stepAutoscale().
|
private |
Referenced by initialize(), and registerStateInitializer().
|
private |
Referenced by queueAsyncJob(), and queueAsyncJob().
|
private |
Referenced by ~AsyncEngine(), getJob(), queueAsyncJob(), queueAsyncJob(), and stepAutoscale().
|
private |
Referenced by ~AsyncEngine(), getJob(), queueAsyncJob(), and queueAsyncJob().
|
private |
Referenced by ~AsyncEngine(), getJob(), queueAsyncJob(), queueAsyncJob(), and stepAutoscale().
|
private |
Referenced by putJobResult(), and stepJobResults().
|
private |
Referenced by putJobResult(), and stepJobResults().
|
private |
Referenced by AsyncWorkerThread::AsyncWorkerThread(), and AsyncWorkerThread::run().
|
private |
Referenced by prepareEnvironment(), and registerStateInitializer().
|
private |
Referenced by ~AsyncEngine(), addWorkerThread(), and stepAutoscale().