ofs | hex dump | ascii |
---|
0000 | 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 03 20 00 00 01 2c 08 06 00 00 00 3b 2d d7 | .PNG........IHDR.......,.....;-. |
0020 | 3a 00 00 00 06 62 4b 47 44 00 ff 00 ff 00 ff a0 bd a7 93 00 00 00 09 70 48 59 73 00 00 0b 13 00 | :....bKGD..............pHYs..... |
0040 | 00 0b 13 01 00 9a 9c 18 00 00 00 07 74 49 4d 45 07 e2 0a 0f 10 0a 05 13 28 a7 ab 00 00 00 1d 69 | ............tIME........(......i |
0060 | 54 58 74 43 6f 6d 6d 65 6e 74 00 00 00 00 00 43 72 65 61 74 65 64 20 77 69 74 68 20 47 49 4d 50 | TXtComment.....Created.with.GIMP |
0080 | 64 2e 65 07 00 00 20 00 49 44 41 54 78 da ed dd 7b 7c 14 f5 bd ff f1 f7 98 cb 26 26 22 84 a3 21 | d.e.....IDATx...{|........&&"..! |
00a0 | 39 56 0a b1 9a 00 8d 5a c8 03 a1 22 2a 97 72 72 51 44 20 48 ab 16 68 69 d5 0a 5a 7d f4 e1 51 0e | 9V.....Z..."*.rrQD.H..hi..Z}..Q. |
00c0 | 3f 8c 37 30 22 b6 45 1a 4f b8 79 e4 00 87 84 62 08 97 3e 90 d4 48 50 28 41 ca 81 08 3e 0e 82 a0 | ?.70".E.O.y....b..>..HP(A...>... |
00e0 | 35 04 88 90 40 08 84 5c be bf 3f d2 ac 09 d9 24 1b b2 d9 ec e5 f5 7c 3c f6 91 64 76 b2 b3 fb 9d | 5...@..\..?....$......|<..dv.... |
0100 | 99 ef cc 7b 3e 33 b3 96 31 c6 08 00 00 00 00 dc e0 2a 9a 00 00 00 00 00 01 04 00 00 00 00 01 04 | ...{>3..1........*.............. |
0120 | 00 00 00 00 08 20 00 00 00 00 08 20 00 00 00 00 40 00 01 00 00 00 40 00 01 00 00 00 40 00 01 00 | ................@.....@.....@... |
0140 | 00 00 00 02 08 00 00 00 00 02 08 00 00 00 00 10 40 00 00 00 00 10 40 00 00 00 00 10 40 00 00 00 | ................@.....@.....@... |
0160 | 00 80 00 02 00 00 00 80 00 02 00 00 00 00 04 10 00 00 00 00 04 10 00 00 00 00 04 10 00 00 00 00 | ................................ |
0180 | 20 80 00 00 00 00 20 80 00 00 00 00 00 01 04 00 00 00 00 01 04 00 00 00 00 01 04 00 00 00 00 08 | ................................ |
01a0 | 20 00 00 00 00 08 20 00 00 00 00 40 00 01 00 00 00 40 00 01 00 00 00 40 00 01 00 00 00 00 02 08 | ...........@.....@.....@........ |
01c0 | 00 00 00 00 02 08 00 00 00 00 10 40 00 00 00 00 10 40 00 00 00 00 80 00 02 00 00 00 80 00 02 00 | ...........@.....@.............. |
01e0 | 00 00 80 00 02 00 00 00 00 04 10 00 00 00 00 04 10 00 00 00 00 20 80 00 00 00 00 20 80 00 00 00 | ................................ |
0200 | 00 20 80 00 00 00 00 00 01 04 00 00 00 00 01 04 00 00 00 00 08 20 00 00 00 00 08 20 00 00 00 00 | ................................ |
0220 | 08 20 00 00 00 00 40 00 01 00 00 00 40 00 01 00 00 00 00 02 08 00 00 00 00 02 08 00 00 00 00 02 | ......@.....@................... |
0240 | 08 00 00 00 00 10 40 00 00 00 00 10 40 00 00 00 00 80 00 02 00 00 00 80 00 02 00 00 00 80 00 02 | ......@.....@................... |
0260 | 4f 62 b6 6f 97 2c cb fe 30 01 01 32 3d 7a c8 f4 eb 27 f3 f0 c3 32 b9 b9 32 75 75 34 14 00 00 00 | Ob.o.,..0..2=z...'...2..2uu4.... |
0280 | bc 8e 65 8c 31 34 83 e7 05 10 6b d8 b0 d6 c7 19 36 4c 5a bb 56 d6 75 d7 d1 60 00 00 00 f0 1a 54 | ..e.14....k.....6LZ.V.u..`.....T |
02a0 | 40 3c 39 88 3c f3 8c 64 8c 64 8c 4c 45 85 cc fe fd 32 6f be 29 13 19 29 ab a0 40 7a f0 41 1a 09 | @<9.<..d.d.LE....2o.)..)..@z.A.. |
02c0 | 00 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.
*/
#ifndef CPP_API_ASYNC_EVENTS_HEADER
#define CPP_API_ASYNC_EVENTS_HEADER
#include <vector>
#include <deque>
#include <map>
#include "threading/thread.h"
#include "threading/mutex.h"
#include "threading/semaphore.h"
#include "debug.h"
#include "lua.h"
#include "cpp_api/s_base.h"
// Forward declarations
class AsyncEngine;
// Declarations
// Data required to queue a job
struct LuaJobInfo {
// 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
unsigned int id;
bool valid;
};
// Asynchronous working environment
class AsyncWorkerThread : public Thread, public ScriptApiBase {
public:
AsyncWorkerThread(AsyncEngine* jobDispatcher, const std::string &name);
virtual ~AsyncWorkerThread();
void *run();
private:
AsyncEngine *jobDispatcher;
};
// 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
*/
void initialize(unsigned int numEngines);
/**
* Queue an async job
* @param func Serialized lua function
* @param params Serialized parameters
* @return jobid The job is queued
*/
unsigned int queueAsyncJob(std::string func, std::string params);
/**
* Engine step to process finished jobs
* the engine step is one way to pass events back, PushFinishedJobs another
* @param L The Lua stack
*/
void step(lua_State *L);
/**
* Push a list of finished jobs onto the stack
* @param L The Lua stack
*/
void pushFinishedJobs(lua_State *L);
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
*/
void putJobResult(LuaJobInfo result);
/**
* 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
*/
void prepareEnvironment(lua_State* L, int top);
private:
// Variable locking the engine against further modification
bool initDone;
// Internal store for registred functions
std::map<std::string, lua_CFunction> functionList;
// Internal counter to create job IDs
unsigned int jobIdCounter;
// Mutex to protect job queue
Mutex jobQueueMutex;
// Job queue
std::deque<LuaJobInfo> jobQueue;
// Mutex to protect result queue
Mutex resultQueueMutex;
// Result queue
std::deque<LuaJobInfo> resultQueue;
// List of current worker threads
std::vector<AsyncWorkerThread*> workerThreads;
// Counter semaphore for job dispatching
Semaphore jobQueueCounter;
};
#endif // CPP_API_ASYNC_EVENTS_HEADER
|