s_async.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. Minetest
  3. Copyright (C) 2013 sapier, <sapier AT gmx DOT net>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #pragma once
  17. #include <vector>
  18. #include <deque>
  19. #include <unordered_set>
  20. #include <memory>
  21. #include <lua.h>
  22. #include "threading/semaphore.h"
  23. #include "threading/thread.h"
  24. #include "common/c_packer.h"
  25. #include "cpp_api/s_base.h"
  26. #include "cpp_api/s_security.h"
  27. // Forward declarations
  28. class AsyncEngine;
  29. // Declarations
  30. // Data required to queue a job
  31. struct LuaJobInfo
  32. {
  33. LuaJobInfo() = default;
  34. // Function to be called in async environment (from string.dump)
  35. std::string function;
  36. // Parameter to be passed to function (serialized)
  37. std::string params;
  38. // Alternative parameters
  39. std::unique_ptr<PackedValue> params_ext;
  40. // Result of function call (serialized)
  41. std::string result;
  42. // Alternative result
  43. std::unique_ptr<PackedValue> result_ext;
  44. // Name of the mod who invoked this call
  45. std::string mod_origin;
  46. // JobID used to identify a job and match it to callback
  47. u32 id;
  48. };
  49. // Asynchronous working environment
  50. class AsyncWorkerThread : public Thread,
  51. virtual public ScriptApiBase, public ScriptApiSecurity {
  52. friend class AsyncEngine;
  53. public:
  54. virtual ~AsyncWorkerThread();
  55. void *run();
  56. protected:
  57. AsyncWorkerThread(AsyncEngine* jobDispatcher, const std::string &name);
  58. private:
  59. AsyncEngine *jobDispatcher = nullptr;
  60. bool isErrored = false;
  61. };
  62. // Asynchornous thread and job management
  63. class AsyncEngine {
  64. friend class AsyncWorkerThread;
  65. typedef void (*StateInitializer)(lua_State *L, int top);
  66. public:
  67. AsyncEngine() = default;
  68. AsyncEngine(Server *server) : server(server) {};
  69. ~AsyncEngine();
  70. /**
  71. * Register function to be called on new states
  72. * @param func C function to be called
  73. */
  74. void registerStateInitializer(StateInitializer func);
  75. /**
  76. * Create async engine tasks and lock function registration
  77. * @param numEngines Number of worker threads, 0 for automatic scaling
  78. */
  79. void initialize(unsigned int numEngines);
  80. /**
  81. * Queue an async job
  82. * @param func Serialized lua function
  83. * @param params Serialized parameters
  84. * @return jobid The job is queued
  85. */
  86. u32 queueAsyncJob(std::string &&func, std::string &&params,
  87. const std::string &mod_origin = "");
  88. /**
  89. * Queue an async job
  90. * @param func Serialized lua function
  91. * @param params Serialized parameters (takes ownership!)
  92. * @return ID of queued job
  93. */
  94. u32 queueAsyncJob(std::string &&func, PackedValue *params,
  95. const std::string &mod_origin = "");
  96. /**
  97. * Engine step to process finished jobs
  98. * @param L The Lua stack
  99. */
  100. void step(lua_State *L);
  101. protected:
  102. /**
  103. * Get a Job from queue to be processed
  104. * this function blocks until a job is ready
  105. * @param job a job to be processed
  106. * @return whether a job was available
  107. */
  108. bool getJob(LuaJobInfo *job);
  109. /**
  110. * Put a Job result back to result queue
  111. * @param result result of completed job
  112. */
  113. void putJobResult(LuaJobInfo &&result);
  114. /**
  115. * Start an additional worker thread
  116. */
  117. void addWorkerThread();
  118. /**
  119. * Process finished jobs callbacks
  120. */
  121. void stepJobResults(lua_State *L);
  122. /**
  123. * Handle automatic scaling of worker threads
  124. */
  125. void stepAutoscale();
  126. /**
  127. * Initialize environment with current registred functions
  128. * this function adds all functions registred by registerFunction to the
  129. * passed lua stack
  130. * @param L Lua stack to initialize
  131. * @param top Stack position
  132. * @return false if a mod error ocurred
  133. */
  134. bool prepareEnvironment(lua_State* L, int top);
  135. private:
  136. // Variable locking the engine against further modification
  137. bool initDone = false;
  138. // Maximum number of worker threads for automatic scaling
  139. // 0 if disabled
  140. unsigned int autoscaleMaxWorkers = 0;
  141. u64 autoscaleTimer = 0;
  142. std::unordered_set<u32> autoscaleSeenJobs;
  143. // Only set for the server async environment (duh)
  144. Server *server = nullptr;
  145. // Internal store for registred state initializers
  146. std::vector<StateInitializer> stateInitializers;
  147. // Internal counter to create job IDs
  148. u32 jobIdCounter = 0;
  149. // Mutex to protect job queue
  150. std::mutex jobQueueMutex;
  151. // Job queue
  152. std::deque<LuaJobInfo> jobQueue;
  153. // Mutex to protect result queue
  154. std::mutex resultQueueMutex;
  155. // Result queue
  156. std::deque<LuaJobInfo> resultQueue;
  157. // List of current worker threads
  158. std::vector<AsyncWorkerThread*> workerThreads;
  159. // Counter semaphore for job dispatching
  160. Semaphore jobQueueCounter;
  161. };