diff options
author | sfan5 <sfan5@live.de> | 2021-08-28 12:15:12 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-28 12:15:12 +0200 |
commit | 6a1424f2b18520f40ba8cfd12f7988f6b33db9a6 (patch) | |
tree | 081e49b5f47693cddae8ba6c921cb6c3041b7731 /src/script/cpp_api/s_async.h | |
parent | 0f8a6d78a72731833664b09695bd44471bc014ac (diff) | |
download | minetest-6a1424f2b18520f40ba8cfd12f7988f6b33db9a6.tar.gz minetest-6a1424f2b18520f40ba8cfd12f7988f6b33db9a6.tar.bz2 minetest-6a1424f2b18520f40ba8cfd12f7988f6b33db9a6.zip |
Async-related script cleanups
Diffstat (limited to 'src/script/cpp_api/s_async.h')
-rw-r--r-- | src/script/cpp_api/s_async.h | 39 |
1 files changed, 21 insertions, 18 deletions
diff --git a/src/script/cpp_api/s_async.h b/src/script/cpp_api/s_async.h index 99a4f891c..697cb0221 100644 --- a/src/script/cpp_api/s_async.h +++ b/src/script/cpp_api/s_async.h @@ -21,7 +21,6 @@ with this program; if not, write to the Free Software Foundation, Inc., #include <vector> #include <deque> -#include <map> #include "threading/semaphore.h" #include "threading/thread.h" @@ -39,26 +38,29 @@ struct LuaJobInfo { LuaJobInfo() = default; - // 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 = ""; + // Function to be called in async environment (from string.dump) + std::string function; + // Parameter to be passed to function (serialized) + std::string params; + // Result of function call (serialized) + std::string result; + // Name of the mod who invoked this call + std::string mod_origin; // JobID used to identify a job and match it to callback - unsigned int id = 0; - - bool valid = false; + u32 id; }; // Asynchronous working environment -class AsyncWorkerThread : public Thread, public ScriptApiBase { +class AsyncWorkerThread : public Thread, virtual public ScriptApiBase { + friend class AsyncEngine; public: - AsyncWorkerThread(AsyncEngine* jobDispatcher, const std::string &name); virtual ~AsyncWorkerThread(); void *run(); +protected: + AsyncWorkerThread(AsyncEngine* jobDispatcher, const std::string &name); + private: AsyncEngine *jobDispatcher = nullptr; }; @@ -89,7 +91,8 @@ public: * @param params Serialized parameters * @return jobid The job is queued */ - unsigned int queueAsyncJob(const std::string &func, const std::string ¶ms); + u32 queueAsyncJob(std::string &&func, std::string &¶ms, + const std::string &mod_origin = ""); /** * Engine step to process finished jobs @@ -102,15 +105,16 @@ protected: /** * Get a Job from queue to be processed * this function blocks until a job is ready - * @return a job to be processed + * @param job a job to be processed + * @return whether a job was available */ - LuaJobInfo getJob(); + bool getJob(LuaJobInfo *job); /** * Put a Job result back to result queue * @param result result of completed job */ - void putJobResult(const LuaJobInfo &result); + void putJobResult(LuaJobInfo &&result); /** * Initialize environment with current registred functions @@ -129,11 +133,10 @@ private: std::vector<StateInitializer> stateInitializers; // Internal counter to create job IDs - unsigned int jobIdCounter = 0; + u32 jobIdCounter = 0; // Mutex to protect job queue std::mutex jobQueueMutex; - // Job queue std::deque<LuaJobInfo> jobQueue; |