51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
-#ifndef L_ASYNC_EVENTS_H_
-#define L_ASYNC_EVENTS_H_
+#ifndef CPP_API_ASYNC_EVENTS_HEADER
+#define CPP_API_ASYNC_EVENTS_HEADER
#include <vector>
+#include <deque>
#include <map>
-#include "jthread/jthread.h"
-#include "jthread/jmutex.h"
-#include "jthread/jsemaphore.h"
+#include "threading/thread.h"
+#include "threading/semaphore.h"
#include "debug.h"
#include "lua.h"
#include "cpp_api/s_base.h"
// Declarations
// Data required to queue a job
-struct LuaJobInfo {
+struct LuaJobInfo
+{
+ LuaJobInfo() {};
+
// Function to be called in async environment
- std::string serializedFunction;
+ std::string serializedFunction = "";
// Parameter to be passed to function
- std::string serializedParams;
+ std::string serializedParams = "";
// Result of function call
- std::string serializedResult;
+ std::string serializedResult = "";
// JobID used to identify a job and match it to callback
- unsigned int JobId;
+ unsigned int id = 0;
- bool valid;
+ bool valid = false;
};
// Asynchronous working environment
-class AsyncWorkerThread : public JThread, public ScriptApiBase {
+class AsyncWorkerThread : public Thread, public ScriptApiBase {
public:
- /**
- * default constructor
- * @param pointer to job dispatcher
- */
- AsyncWorkerThread(AsyncEngine* jobDispatcher, unsigned int threadNum);
-
+ AsyncWorkerThread(AsyncEngine* jobDispatcher, const std::string &name);
virtual ~AsyncWorkerThread();
- void* Thread();
+ void *run();
private:
- AsyncEngine* m_JobDispatcher;
-
- // Thread number. Used for debug output
- unsigned int m_threadnum;
-
+ AsyncEngine *jobDispatcher = nullptr;
};
// Asynchornous thread and job management
class AsyncEngine {
friend class AsyncWorkerThread;
+ typedef void (*StateInitializer)(lua_State *L, int top);
public:
- AsyncEngine();
+ AsyncEngine() {};
~AsyncEngine();
/**
- * Register function to be used within engine
- * @param name Function name to be used within Lua environment
+ * Register function to be called on new states
* @param func C function to be called
*/
- bool registerFunction(const char* name, lua_CFunction func);
+ void registerStateInitializer(StateInitializer func);
/**
* Create async engine tasks and lock function registration
* @param numEngines Number of async threads to be started
*/
- void Initialize(unsigned int numEngines);
+ void initialize(unsigned int numEngines);
/**
- * queue/run a async job
+ * Queue an async job
* @param func Serialized lua function
* @param params Serialized parameters
* @return jobid The job is queued
*/
- unsigned int doAsyncJob(std::string func, std::string params);
+ unsigned int queueAsyncJob(const std::string &func, const std::string ¶ms);
/**
* Engine step to process finished jobs
* the engine step is one way to pass events back, PushFinishedJobs another
* @param L The Lua stack
- * @param errorhandler Stack index of the Lua error handler
*/
- void Step(lua_State *L, int errorhandler);
+ void step(lua_State *L);
/**
* Push a list of finished jobs onto the stack
* @param L The Lua stack
*/
- void PushFinishedJobs(lua_State *L);
+ void pushFinishedJobs(lua_State *L);
protected:
/**
* Put a Job result back to result queue
* @param result result of completed job
*/
- void putJobResult(LuaJobInfo result);
+ void putJobResult(const LuaJobInfo &result);
/**
* Initialize environment with current registred functions
* @param L Lua stack to initialize
* @param top Stack position
*/
- void PrepareEnvironment(lua_State* L, int top);
+ void prepareEnvironment(lua_State* L, int top);
private:
+ // Variable locking the engine against further modification
+ bool initDone = false;
- // Stack index of error handler
- int m_errorhandler;
-
- // variable locking the engine against further modification
- bool m_initDone;
-
- // Internal store for registred functions
- std::map<std::string, lua_CFunction> m_FunctionList;
+ // Internal store for registred state initializers
+ std::vector<StateInitializer> stateInitializers;
// Internal counter to create job IDs
- unsigned int m_JobIdCounter;
+ unsigned int jobIdCounter = 0;
// Mutex to protect job queue
- JMutex m_JobQueueMutex;
+ std::mutex jobQueueMutex;
// Job queue
- std::vector<LuaJobInfo> m_JobQueue;
+ std::deque<LuaJobInfo> jobQueue;
// Mutex to protect result queue
- JMutex m_ResultQueueMutex;
+ std::mutex resultQueueMutex;
// Result queue
- std::vector<LuaJobInfo> m_ResultQueue;
+ std::deque<LuaJobInfo> resultQueue;
// List of current worker threads
- std::vector<AsyncWorkerThread*> m_WorkerThreads;
+ std::vector<AsyncWorkerThread*> workerThreads;
// Counter semaphore for job dispatching
- JSemaphore m_JobQueueCounter;
+ Semaphore jobQueueCounter;
};
-#endif // L_ASYNC_EVENTS_H_
+#endif // CPP_API_ASYNC_EVENTS_HEADER