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.
20 #ifndef CPP_API_ASYNC_EVENTS_HEADER
21 #define CPP_API_ASYNC_EVENTS_HEADER
27 #include "threading/thread.h"
28 #include "threading/mutex.h"
29 #include "threading/semaphore.h"
32 #include "cpp_api/s_base.h"
34 // Forward declarations
40 // Data required to queue a job
44 serializedFunction(""),
51 // Function to be called in async environment
52 std::string serializedFunction;
53 // Parameter to be passed to function
54 std::string serializedParams;
55 // Result of function call
56 std::string serializedResult;
57 // JobID used to identify a job and match it to callback
63 // Asynchronous working environment
64 class AsyncWorkerThread : public Thread, public ScriptApiBase {
66 AsyncWorkerThread(AsyncEngine* jobDispatcher, const std::string &name);
67 virtual ~AsyncWorkerThread();
72 AsyncEngine *jobDispatcher;
75 // Asynchornous thread and job management
77 friend class AsyncWorkerThread;
78 typedef void (*StateInitializer)(lua_State *L, int top);
84 * Register function to be called on new states
85 * @param func C function to be called
87 void registerStateInitializer(StateInitializer func);
90 * Create async engine tasks and lock function registration
91 * @param numEngines Number of async threads to be started
93 void initialize(unsigned int numEngines);
97 * @param func Serialized lua function
98 * @param params Serialized parameters
99 * @return jobid The job is queued
101 unsigned int queueAsyncJob(const std::string &func, const std::string ¶ms);
104 * Engine step to process finished jobs
105 * the engine step is one way to pass events back, PushFinishedJobs another
106 * @param L The Lua stack
108 void step(lua_State *L);
111 * Push a list of finished jobs onto the stack
112 * @param L The Lua stack
114 void pushFinishedJobs(lua_State *L);
118 * Get a Job from queue to be processed
119 * this function blocks until a job is ready
120 * @return a job to be processed
125 * Put a Job result back to result queue
126 * @param result result of completed job
128 void putJobResult(const LuaJobInfo &result);
131 * Initialize environment with current registred functions
132 * this function adds all functions registred by registerFunction to the
134 * @param L Lua stack to initialize
135 * @param top Stack position
137 void prepareEnvironment(lua_State* L, int top);
140 // Variable locking the engine against further modification
143 // Internal store for registred state initializers
144 std::vector<StateInitializer> stateInitializers;
146 // Internal counter to create job IDs
147 unsigned int jobIdCounter;
149 // Mutex to protect job queue
153 std::deque<LuaJobInfo> jobQueue;
155 // Mutex to protect result queue
156 Mutex resultQueueMutex;
158 std::deque<LuaJobInfo> resultQueue;
160 // List of current worker threads
161 std::vector<AsyncWorkerThread*> workerThreads;
163 // Counter semaphore for job dispatching
164 Semaphore jobQueueCounter;
167 #endif // CPP_API_ASYNC_EVENTS_HEADER