Minetest  5.4.0
AsyncEngine Class Reference

#include <s_async.h>

+ Collaboration diagram for AsyncEngine:

Public Member Functions

 AsyncEngine ()=default
 
 ~AsyncEngine ()
 
void registerStateInitializer (StateInitializer func)
 Register function to be called on new states. More...
 
void initialize (unsigned int numEngines)
 Create async engine tasks and lock function registration. More...
 
unsigned int queueAsyncJob (const std::string &func, const std::string &params)
 Queue an async job. More...
 
void step (lua_State *L)
 Engine step to process finished jobs the engine step is one way to pass events back, PushFinishedJobs another. More...
 

Protected Member Functions

LuaJobInfo getJob ()
 Get a Job from queue to be processed this function blocks until a job is ready. More...
 
void putJobResult (const LuaJobInfo &result)
 Put a Job result back to result queue. More...
 
void 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. More...
 

Private Types

typedef void(* StateInitializer) (lua_State *L, int top)
 

Private Attributes

bool initDone = false
 
std::vector< StateInitializerstateInitializers
 
unsigned int 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()

AsyncEngine::AsyncEngine ( )
default

◆ ~AsyncEngine()

AsyncEngine::~AsyncEngine ( )

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

+ Here is the call graph for this function:

Member Function Documentation

◆ getJob()

LuaJobInfo AsyncEngine::getJob ( )
protected

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

Returns
a job to be processed

References jobQueue, jobQueueCounter, jobQueueMutex, LuaJobInfo::valid, 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 async threads to be started

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

Referenced by MainMenuScripting::initializeModApi().

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

◆ prepareEnvironment()

void 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

References stateInitializers.

Referenced by AsyncWorkerThread::AsyncWorkerThread().

+ Here is the caller graph for this function:

◆ putJobResult()

void AsyncEngine::putJobResult ( const 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()

unsigned int AsyncEngine::queueAsyncJob ( const std::string &  func,
const std::string &  params 
)

Queue an async job.

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

References LuaJobInfo::id, jobIdCounter, jobQueue, jobQueueCounter, jobQueueMutex, params, Semaphore::post(), LuaJobInfo::serializedFunction, and LuaJobInfo::serializedParams.

Referenced by MainMenuScripting::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 stateInitializers.

Referenced by MainMenuScripting::initializeModApi().

+ Here is the caller graph for this function:

◆ step()

void AsyncEngine::step ( lua_State *  L)

Engine step to process finished jobs the engine step is one way to pass events back, PushFinishedJobs another.

Parameters
LThe Lua stack

References FATAL_ERROR, LuaJobInfo::id, PCALL_RESL, PUSH_ERROR_HANDLER, resultQueue, resultQueueMutex, and LuaJobInfo::serializedResult.

Referenced by MainMenuScripting::step().

+ Here is the caller graph for this function:

Friends And Related Function Documentation

◆ AsyncWorkerThread

friend class AsyncWorkerThread
friend

Referenced by initialize().

Member Data Documentation

◆ initDone

bool AsyncEngine::initDone = false
private

Referenced by initialize().

◆ jobIdCounter

unsigned int AsyncEngine::jobIdCounter = 0
private

Referenced by queueAsyncJob().

◆ jobQueue

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

Referenced by ~AsyncEngine(), getJob(), and queueAsyncJob().

◆ jobQueueCounter

Semaphore AsyncEngine::jobQueueCounter
private

Referenced by ~AsyncEngine(), getJob(), and queueAsyncJob().

◆ jobQueueMutex

std::mutex AsyncEngine::jobQueueMutex
private

Referenced by ~AsyncEngine(), getJob(), and queueAsyncJob().

◆ resultQueue

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

Referenced by putJobResult(), and step().

◆ resultQueueMutex

std::mutex AsyncEngine::resultQueueMutex
private

Referenced by putJobResult(), and step().

◆ stateInitializers

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

◆ workerThreads

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

Referenced by ~AsyncEngine(), and initialize().


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