You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lua_thread_pool.h 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #ifndef LUA_THREAD_POOL_H_
  2. #define LUA_THREAD_POOL_H_
  3. #include <lua.h>
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7. struct thread_entry;
  8. struct lua_thread_pool;
  9. typedef void (*lua_thread_finish_t)(struct thread_entry *thread, int ret);
  10. typedef void (*lua_thread_error_t)(struct thread_entry *thread, int ret, const char *msg);
  11. struct thread_entry {
  12. lua_State *lua_state;
  13. int thread_index;
  14. gpointer cd;
  15. /* function to handle result of called method, can be NULL */
  16. lua_thread_finish_t finish_callback;
  17. /* function to log result, i.e. if you want to modify error logging message or somehow process this state, can be NUL */
  18. lua_thread_error_t error_callback;
  19. struct rspamd_task *task;
  20. struct rspamd_config *cfg;
  21. };
  22. struct lua_callback_state {
  23. lua_State *L;
  24. struct thread_entry *my_thread;
  25. struct thread_entry *previous_thread;
  26. struct lua_thread_pool *thread_pool;
  27. };
  28. /**
  29. * Allocates new thread pool on state L. Pre-creates number of lua-threads to use later on
  30. *
  31. * @param L
  32. * @return
  33. */
  34. struct lua_thread_pool *
  35. lua_thread_pool_new(lua_State *L);
  36. /**
  37. * Destroys the pool
  38. * @param pool
  39. */
  40. void lua_thread_pool_free(struct lua_thread_pool *pool);
  41. /**
  42. * Extracts a thread from the list of available ones.
  43. * It immediately becomes the running one and should be used to run a Lua script/function straight away.
  44. * as soon as the code is finished, it should be either returned into list of available threads by
  45. * calling lua_thread_pool_return() or terminated by calling lua_thread_pool_terminate_entry()
  46. * if the code finished with error.
  47. *
  48. * If the code performed YIELD, the thread is still running and it's live should be controlled by the callee
  49. *
  50. * @param task
  51. * @return
  52. */
  53. struct thread_entry *
  54. lua_thread_pool_get_for_task(struct rspamd_task *task);
  55. /**
  56. * The same, but used when task is not available
  57. *
  58. * @param cfg
  59. * @return
  60. */
  61. struct thread_entry *
  62. lua_thread_pool_get_for_config(struct rspamd_config *cfg);
  63. /**
  64. * Return thread into the list of available ones. It can't be done with yielded or dead threads.
  65. *
  66. * @param pool
  67. * @param thread_entry
  68. */
  69. void lua_thread_pool_return_full(struct lua_thread_pool *pool,
  70. struct thread_entry *thread_entry,
  71. const char *loc);
  72. #define lua_thread_pool_return(pool, thread_entry) \
  73. lua_thread_pool_return_full(pool, thread_entry, G_STRLOC)
  74. /**
  75. * Currently running thread. Typically needed in yielding point - to fill-up continuation.
  76. *
  77. * @param pool
  78. * @return
  79. */
  80. struct thread_entry *
  81. lua_thread_pool_get_running_entry_full(struct lua_thread_pool *pool,
  82. const char *loc);
  83. #define lua_thread_pool_get_running_entry(pool) \
  84. lua_thread_pool_get_running_entry_full(pool, G_STRLOC)
  85. /**
  86. * Updates currently running thread
  87. *
  88. * @param pool
  89. * @param thread_entry
  90. */
  91. void lua_thread_pool_set_running_entry_full(struct lua_thread_pool *pool,
  92. struct thread_entry *thread_entry,
  93. const char *loc);
  94. #define lua_thread_pool_set_running_entry(pool, thread_entry) \
  95. lua_thread_pool_set_running_entry_full(pool, thread_entry, G_STRLOC)
  96. /**
  97. * Prevents yielded thread to be used for callback execution. lua_thread_pool_restore_callback() should be called afterwards.
  98. *
  99. * @param pool
  100. * @param cbs
  101. */
  102. void lua_thread_pool_prepare_callback_full(struct lua_thread_pool *pool,
  103. struct lua_callback_state *cbs, const char *loc);
  104. #define lua_thread_pool_prepare_callback(pool, cbs) \
  105. lua_thread_pool_prepare_callback_full(pool, cbs, G_STRLOC)
  106. /**
  107. * Restores state after lua_thread_pool_prepare_callback () usage
  108. *
  109. * @param cbs
  110. */
  111. void lua_thread_pool_restore_callback_full(struct lua_callback_state *cbs,
  112. const char *loc);
  113. #define lua_thread_pool_restore_callback(cbs) \
  114. lua_thread_pool_restore_callback_full(cbs, G_STRLOC)
  115. /**
  116. * Acts like lua_call but the tread is able to suspend execution.
  117. * As soon as the call is over, call either thread_entry::finish_callback or thread_entry::error_callback.
  118. *
  119. * @param thread_entry
  120. * @param narg
  121. */
  122. void lua_thread_call_full(struct thread_entry *thread_entry,
  123. int narg,
  124. const char *loc);
  125. #define lua_thread_call(thread_entry, narg) \
  126. lua_thread_call_full(thread_entry, narg, G_STRLOC)
  127. /**
  128. * Yields thread. should be only called in return statement
  129. * @param thread_entry
  130. * @param nresults
  131. * @return
  132. */
  133. int lua_thread_yield_full(struct thread_entry *thread_entry, int nresults,
  134. const char *loc);
  135. #define lua_thread_yield(thread_entry, narg) \
  136. lua_thread_yield_full(thread_entry, narg, G_STRLOC)
  137. /**
  138. * Resumes suspended by lua_yield_thread () thread
  139. * @param task
  140. * @param thread_entry
  141. * @param narg
  142. */
  143. void lua_thread_resume_full(struct thread_entry *thread_entry,
  144. int narg,
  145. const char *loc);
  146. #define lua_thread_resume(thread_entry, narg) \
  147. lua_thread_resume_full(thread_entry, narg, G_STRLOC)
  148. /**
  149. * Terminates thread pool entry and fill the pool with another thread entry if needed
  150. * @param pool
  151. * @param thread_entry
  152. * @param loc
  153. */
  154. void lua_thread_pool_terminate_entry_full(struct lua_thread_pool *pool,
  155. struct thread_entry *thread_entry,
  156. const char *loc, bool enforce);
  157. #define lua_thread_pool_terminate_entry(pool, thread_entry) \
  158. lua_thread_pool_terminate_entry_full(pool, thread_entry, G_STRLOC, false)
  159. #ifdef __cplusplus
  160. }
  161. #endif
  162. #endif /* LUA_THREAD_POOL_H_ */