Luanti 5.11.0-dev
 
Loading...
Searching...
No Matches
AsyncEngine Class Reference

#include <s_async.h>

+ Collaboration diagram for AsyncEngine:

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 &&params, 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
 
Serverserver = nullptr
 
std::vector< StateInitializerstateInitializers
 
u32 jobIdCounter = 0
 
std::mutex jobQueueMutex
 
std::deque< LuaJobInfojobQueue
 
std::mutex resultQueueMutex
 
std::deque< LuaJobInforesultQueue
 
std::vector< AsyncWorkerThread * > workerThreads
 
Semaphore jobQueueCounter
 

Friends

class AsyncWorkerThread
 

Member Typedef Documentation

◆ StateInitializer

typedef void(* AsyncEngine::StateInitializer) (lua_State *L, int top)
private

Constructor & Destructor Documentation

◆ AsyncEngine() [1/2]

AsyncEngine::AsyncEngine ( )
default

◆ AsyncEngine() [2/2]

AsyncEngine::AsyncEngine ( Server * server)
inline

◆ ~AsyncEngine()

AsyncEngine::~AsyncEngine ( )

References infostream, jobQueue, jobQueueCounter, jobQueueMutex, Semaphore::post(), and workerThreads.

+ Here is the call graph for this function:

Member Function Documentation

◆ addWorkerThread()

void AsyncEngine::addWorkerThread ( )
protected

Start an additional worker thread.

References AsyncWorkerThread, itos(), Thread::start(), and workerThreads.

Referenced by initialize(), and stepAutoscale().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getJob()

bool AsyncEngine::getJob ( LuaJobInfo * job)
protected

Get a Job from queue to be processed this function blocks until a job is ready.

Parameters
joba job to be processed
Returns
whether a job was available

References jobQueue, jobQueueCounter, jobQueueMutex, and Semaphore::wait().

Referenced by AsyncWorkerThread::run().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ initialize()

void AsyncEngine::initialize ( unsigned int numEngines)

Create async engine tasks and lock function registration.

Parameters
numEnginesNumber of worker threads, 0 for automatic scaling

References addWorkerThread(), autoscaleMaxWorkers, Thread::getNumberOfProcessors(), infostream, and initDone.

Referenced by ServerScripting::initAsync(), and MainMenuScripting::initializeModApi().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ prepareEnvironment()

bool AsyncEngine::prepareEnvironment ( lua_State * L,
int top )
protected

Initialize environment with current registred functions this function adds all functions registred by registerFunction to the passed lua stack.

Parameters
LLua stack to initialize
topStack position
Returns
false if a mod error ocurred

References BUILTIN_MOD_NAME, DIR_DELIM, errorstream, FATAL_ERROR, Server::getBuiltinLuaPath(), ModApiBase::getScriptApiBase(), stateInitializers, and BaseException::what().

Referenced by AsyncWorkerThread::AsyncWorkerThread().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ putJobResult()

void AsyncEngine::putJobResult ( LuaJobInfo && result)
protected

Put a Job result back to result queue.

Parameters
resultresult of completed job

References resultQueue, and resultQueueMutex.

Referenced by AsyncWorkerThread::run().

+ Here is the caller graph for this function:

◆ queueAsyncJob() [1/2]

u32 AsyncEngine::queueAsyncJob ( std::string && func,
PackedValue * params,
const std::string & mod_origin = "" )

Queue an async job.

Parameters
funcSerialized lua function
paramsSerialized parameters (takes ownership!)
Returns
ID of queued job

References jobIdCounter, jobQueue, jobQueueCounter, jobQueueMutex, and Semaphore::post().

+ Here is the call graph for this function:

◆ queueAsyncJob() [2/2]

u32 AsyncEngine::queueAsyncJob ( std::string && func,
std::string && params,
const std::string & mod_origin = "" )

Queue an async job.

Parameters
funcSerialized lua function
paramsSerialized parameters
Returns
jobid The job is queued

References jobIdCounter, jobQueue, jobQueueCounter, jobQueueMutex, and Semaphore::post().

Referenced by MainMenuScripting::queueAsync(), and ServerScripting::queueAsync().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ registerStateInitializer()

void AsyncEngine::registerStateInitializer ( StateInitializer func)

Register function to be called on new states.

Parameters
funcC function to be called

References FATAL_ERROR_IF, initDone, and stateInitializers.

Referenced by ServerScripting::initAsync(), and MainMenuScripting::initializeModApi().

+ Here is the caller graph for this function:

◆ step()

void AsyncEngine::step ( lua_State * L)

Engine step to process finished jobs.

Parameters
LThe Lua stack

References stepAutoscale(), and stepJobResults().

Referenced by MainMenuScripting::step(), and ServerScripting::stepAsync().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ stepAutoscale()

void AsyncEngine::stepAutoscale ( )
protected

Handle automatic scaling of worker threads.

References addWorkerThread(), autoscaleMaxWorkers, autoscaleSeenJobs, autoscaleTimer, porting::getTimeMs(), infostream, jobQueue, jobQueueMutex, and workerThreads.

Referenced by step().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ stepJobResults()

void AsyncEngine::stepJobResults ( lua_State * L)
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().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Friends And Related Symbol Documentation

◆ AsyncWorkerThread

friend class AsyncWorkerThread
friend

Referenced by addWorkerThread().

Member Data Documentation

◆ autoscaleMaxWorkers

unsigned int AsyncEngine::autoscaleMaxWorkers = 0
private

Referenced by initialize(), and stepAutoscale().

◆ autoscaleSeenJobs

std::unordered_set<u32> AsyncEngine::autoscaleSeenJobs
private

Referenced by stepAutoscale().

◆ autoscaleTimer

u64 AsyncEngine::autoscaleTimer = 0
private

Referenced by stepAutoscale().

◆ initDone

bool AsyncEngine::initDone = false
private

◆ jobIdCounter

u32 AsyncEngine::jobIdCounter = 0
private

Referenced by queueAsyncJob(), and queueAsyncJob().

◆ jobQueue

std::deque<LuaJobInfo> AsyncEngine::jobQueue
private

◆ jobQueueCounter

Semaphore AsyncEngine::jobQueueCounter
private

◆ jobQueueMutex

std::mutex AsyncEngine::jobQueueMutex
private

◆ resultQueue

std::deque<LuaJobInfo> AsyncEngine::resultQueue
private

Referenced by putJobResult(), and stepJobResults().

◆ resultQueueMutex

std::mutex AsyncEngine::resultQueueMutex
private

Referenced by putJobResult(), and stepJobResults().

◆ server

◆ stateInitializers

std::vector<StateInitializer> AsyncEngine::stateInitializers
private

◆ workerThreads

std::vector<AsyncWorkerThread*> AsyncEngine::workerThreads
private

The documentation for this class was generated from the following files: