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 "jthread/jthread.h"
28 #include "jthread/jmutex.h"
29 #include "jthread/jsemaphore.h"
32 #include "cpp_api/s_base.h"
34 // Forward declarations
40 // Data required to queue a job
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 JThread, public ScriptApiBase {
59 * @param pointer to job dispatcher
61 AsyncWorkerThread(AsyncEngine* jobDispatcher, unsigned int threadNum);
63 virtual ~AsyncWorkerThread();
68 AsyncEngine *jobDispatcher;
70 // Thread number. Used for debug output
71 unsigned int threadnum;
75 // Asynchornous thread and job management
77 friend class AsyncWorkerThread;
83 * Register function to be used within engine
84 * @param name Function name to be used within Lua environment
85 * @param func C function to be called
87 bool registerFunction(const char* name, lua_CFunction 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(std::string func, std::string params);
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
107 * @param errorhandler Stack index of the Lua error handler
109 void step(lua_State *L, int errorhandler);
112 * Push a list of finished jobs onto the stack
113 * @param L The Lua stack
115 void pushFinishedJobs(lua_State *L);
119 * Get a Job from queue to be processed
120 * this function blocks until a job is ready
121 * @return a job to be processed
126 * Put a Job result back to result queue
127 * @param result result of completed job
129 void putJobResult(LuaJobInfo result);
132 * Initialize environment with current registred functions
133 * this function adds all functions registred by registerFunction to the
135 * @param L Lua stack to initialize
136 * @param top Stack position
138 void prepareEnvironment(lua_State* L, int top);
141 // Variable locking the engine against further modification
144 // Internal store for registred functions
145 std::map<std::string, lua_CFunction> functionList;
147 // Internal counter to create job IDs
148 unsigned int jobIdCounter;
150 // Mutex to protect job queue
151 JMutex jobQueueMutex;
154 std::deque<LuaJobInfo> jobQueue;
156 // Mutex to protect result queue
157 JMutex resultQueueMutex;
159 std::deque<LuaJobInfo> resultQueue;
161 // List of current worker threads
162 std::vector<AsyncWorkerThread*> workerThreads;
164 // Counter semaphore for job dispatching
165 JSemaphore jobQueueCounter;
168 #endif // CPP_API_ASYNC_EVENTS_HEADER