2014-04-15 19:41:07 +02:00
|
|
|
/*
|
|
|
|
Minetest
|
|
|
|
Copyright (C) 2013 sapier, <sapier AT gmx DOT net>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2014-04-15 21:10:30 +02:00
|
|
|
#ifndef CPP_API_ASYNC_EVENTS_HEADER
|
|
|
|
#define CPP_API_ASYNC_EVENTS_HEADER
|
2014-04-15 19:41:07 +02:00
|
|
|
|
|
|
|
#include <vector>
|
2014-04-15 21:10:30 +02:00
|
|
|
#include <deque>
|
2014-04-15 19:41:07 +02:00
|
|
|
#include <map>
|
|
|
|
|
2015-04-07 12:13:12 +02:00
|
|
|
#include "threading/thread.h"
|
|
|
|
#include "threading/mutex.h"
|
|
|
|
#include "threading/semaphore.h"
|
2014-04-15 19:41:07 +02:00
|
|
|
#include "debug.h"
|
|
|
|
#include "lua.h"
|
|
|
|
#include "cpp_api/s_base.h"
|
|
|
|
|
|
|
|
// Forward declarations
|
|
|
|
class AsyncEngine;
|
|
|
|
|
|
|
|
|
|
|
|
// Declarations
|
|
|
|
|
|
|
|
// Data required to queue a job
|
2017-04-20 00:12:52 +02:00
|
|
|
struct LuaJobInfo
|
|
|
|
{
|
|
|
|
LuaJobInfo() :
|
|
|
|
serializedFunction(""),
|
|
|
|
serializedParams(""),
|
|
|
|
serializedResult(""),
|
|
|
|
id(0),
|
|
|
|
valid(false)
|
|
|
|
{}
|
|
|
|
|
2014-04-15 19:41:07 +02:00
|
|
|
// Function to be called in async environment
|
|
|
|
std::string serializedFunction;
|
|
|
|
// Parameter to be passed to function
|
|
|
|
std::string serializedParams;
|
|
|
|
// Result of function call
|
|
|
|
std::string serializedResult;
|
|
|
|
// JobID used to identify a job and match it to callback
|
2014-04-15 21:10:30 +02:00
|
|
|
unsigned int id;
|
2014-04-15 19:41:07 +02:00
|
|
|
|
|
|
|
bool valid;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Asynchronous working environment
|
2015-04-07 12:13:12 +02:00
|
|
|
class AsyncWorkerThread : public Thread, public ScriptApiBase {
|
2014-04-15 19:41:07 +02:00
|
|
|
public:
|
2015-04-07 12:13:12 +02:00
|
|
|
AsyncWorkerThread(AsyncEngine* jobDispatcher, const std::string &name);
|
2014-04-15 19:41:07 +02:00
|
|
|
virtual ~AsyncWorkerThread();
|
|
|
|
|
2015-04-07 12:13:12 +02:00
|
|
|
void *run();
|
2014-04-15 19:41:07 +02:00
|
|
|
|
|
|
|
private:
|
2014-04-15 21:10:30 +02:00
|
|
|
AsyncEngine *jobDispatcher;
|
2014-04-15 19:41:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Asynchornous thread and job management
|
|
|
|
class AsyncEngine {
|
|
|
|
friend class AsyncWorkerThread;
|
|
|
|
public:
|
|
|
|
AsyncEngine();
|
|
|
|
~AsyncEngine();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register function to be used within engine
|
|
|
|
* @param name Function name to be used within Lua environment
|
|
|
|
* @param func C function to be called
|
|
|
|
*/
|
|
|
|
bool registerFunction(const char* name, lua_CFunction func);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create async engine tasks and lock function registration
|
|
|
|
* @param numEngines Number of async threads to be started
|
|
|
|
*/
|
2014-04-15 21:10:30 +02:00
|
|
|
void initialize(unsigned int numEngines);
|
2014-04-15 19:41:07 +02:00
|
|
|
|
|
|
|
/**
|
2014-04-15 21:10:30 +02:00
|
|
|
* Queue an async job
|
2014-04-15 19:41:07 +02:00
|
|
|
* @param func Serialized lua function
|
|
|
|
* @param params Serialized parameters
|
|
|
|
* @return jobid The job is queued
|
|
|
|
*/
|
2017-04-20 00:12:52 +02:00
|
|
|
unsigned int queueAsyncJob(const std::string &func, const std::string ¶ms);
|
2014-04-15 19:41:07 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Engine step to process finished jobs
|
|
|
|
* the engine step is one way to pass events back, PushFinishedJobs another
|
|
|
|
* @param L The Lua stack
|
|
|
|
*/
|
2015-08-25 07:44:53 +02:00
|
|
|
void step(lua_State *L);
|
2014-04-15 19:41:07 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Push a list of finished jobs onto the stack
|
|
|
|
* @param L The Lua stack
|
|
|
|
*/
|
2014-04-15 21:10:30 +02:00
|
|
|
void pushFinishedJobs(lua_State *L);
|
2014-04-15 19:41:07 +02:00
|
|
|
|
|
|
|
protected:
|
|
|
|
/**
|
|
|
|
* Get a Job from queue to be processed
|
|
|
|
* this function blocks until a job is ready
|
|
|
|
* @return a job to be processed
|
|
|
|
*/
|
|
|
|
LuaJobInfo getJob();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Put a Job result back to result queue
|
|
|
|
* @param result result of completed job
|
|
|
|
*/
|
2017-04-20 00:12:52 +02:00
|
|
|
void putJobResult(const LuaJobInfo &result);
|
2014-04-15 19:41:07 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize environment with current registred functions
|
|
|
|
* this function adds all functions registred by registerFunction to the
|
|
|
|
* passed lua stack
|
|
|
|
* @param L Lua stack to initialize
|
|
|
|
* @param top Stack position
|
|
|
|
*/
|
2014-04-15 21:10:30 +02:00
|
|
|
void prepareEnvironment(lua_State* L, int top);
|
2014-04-15 19:41:07 +02:00
|
|
|
|
|
|
|
private:
|
2014-04-15 21:10:30 +02:00
|
|
|
// Variable locking the engine against further modification
|
|
|
|
bool initDone;
|
2014-04-15 19:41:07 +02:00
|
|
|
|
|
|
|
// Internal store for registred functions
|
2016-10-06 19:20:12 +02:00
|
|
|
UNORDERED_MAP<std::string, lua_CFunction> functionList;
|
2014-04-15 19:41:07 +02:00
|
|
|
|
|
|
|
// Internal counter to create job IDs
|
2014-04-15 21:10:30 +02:00
|
|
|
unsigned int jobIdCounter;
|
2014-04-15 19:41:07 +02:00
|
|
|
|
|
|
|
// Mutex to protect job queue
|
2015-04-07 12:13:12 +02:00
|
|
|
Mutex jobQueueMutex;
|
2014-04-15 19:41:07 +02:00
|
|
|
|
|
|
|
// Job queue
|
2014-04-15 21:10:30 +02:00
|
|
|
std::deque<LuaJobInfo> jobQueue;
|
2014-04-15 19:41:07 +02:00
|
|
|
|
|
|
|
// Mutex to protect result queue
|
2015-04-07 12:13:12 +02:00
|
|
|
Mutex resultQueueMutex;
|
2014-04-15 19:41:07 +02:00
|
|
|
// Result queue
|
2014-04-15 21:10:30 +02:00
|
|
|
std::deque<LuaJobInfo> resultQueue;
|
2014-04-15 19:41:07 +02:00
|
|
|
|
|
|
|
// List of current worker threads
|
2014-04-15 21:10:30 +02:00
|
|
|
std::vector<AsyncWorkerThread*> workerThreads;
|
2014-04-15 19:41:07 +02:00
|
|
|
|
|
|
|
// Counter semaphore for job dispatching
|
2015-04-07 12:13:12 +02:00
|
|
|
Semaphore jobQueueCounter;
|
2014-04-15 19:41:07 +02:00
|
|
|
};
|
|
|
|
|
2014-04-15 21:10:30 +02:00
|
|
|
#endif // CPP_API_ASYNC_EVENTS_HEADER
|