3 Copyright (C) 2013 sapier, <sapier AT gmx DOT net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "threading/semaphore.h"
27 #include "threading/thread.h"
29 #include "cpp_api/s_base.h"
31 // Forward declarations
37 // Data required to queue a job
40 LuaJobInfo() = default;
42 // Function to be called in async environment
43 std::string serializedFunction = "";
44 // Parameter to be passed to function
45 std::string serializedParams = "";
46 // Result of function call
47 std::string serializedResult = "";
48 // JobID used to identify a job and match it to callback
54 // Asynchronous working environment
55 class AsyncWorkerThread : public Thread, public ScriptApiBase {
57 AsyncWorkerThread(AsyncEngine* jobDispatcher, const std::string &name);
58 virtual ~AsyncWorkerThread();
63 AsyncEngine *jobDispatcher = nullptr;
66 // Asynchornous thread and job management
68 friend class AsyncWorkerThread;
69 typedef void (*StateInitializer)(lua_State *L, int top);
71 AsyncEngine() = default;
75 * Register function to be called on new states
76 * @param func C function to be called
78 void registerStateInitializer(StateInitializer func);
81 * Create async engine tasks and lock function registration
82 * @param numEngines Number of async threads to be started
84 void initialize(unsigned int numEngines);
88 * @param func Serialized lua function
89 * @param params Serialized parameters
90 * @return jobid The job is queued
92 unsigned int queueAsyncJob(const std::string &func, const std::string ¶ms);
95 * Engine step to process finished jobs
96 * the engine step is one way to pass events back, PushFinishedJobs another
97 * @param L The Lua stack
99 void step(lua_State *L);
102 * Push a list of finished jobs onto the stack
103 * @param L The Lua stack
105 void pushFinishedJobs(lua_State *L);
109 * Get a Job from queue to be processed
110 * this function blocks until a job is ready
111 * @return a job to be processed
116 * Put a Job result back to result queue
117 * @param result result of completed job
119 void putJobResult(const LuaJobInfo &result);
122 * Initialize environment with current registred functions
123 * this function adds all functions registred by registerFunction to the
125 * @param L Lua stack to initialize
126 * @param top Stack position
128 void prepareEnvironment(lua_State* L, int top);
131 // Variable locking the engine against further modification
132 bool initDone = false;
134 // Internal store for registred state initializers
135 std::vector<StateInitializer> stateInitializers;
137 // Internal counter to create job IDs
138 unsigned int jobIdCounter = 0;
140 // Mutex to protect job queue
141 std::mutex jobQueueMutex;
144 std::deque<LuaJobInfo> jobQueue;
146 // Mutex to protect result queue
147 std::mutex resultQueueMutex;
149 std::deque<LuaJobInfo> resultQueue;
151 // List of current worker threads
152 std::vector<AsyncWorkerThread*> workerThreads;
154 // Counter semaphore for job dispatching
155 Semaphore jobQueueCounter;