]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Added callback helpers
authorMikhail Galanin <mgalanin@mimecast.com>
Fri, 17 Aug 2018 10:03:57 +0000 (11:03 +0100)
committerMikhail Galanin <mgalanin@mimecast.com>
Fri, 17 Aug 2018 10:03:57 +0000 (11:03 +0100)
src/lua/lua_common.h
src/lua/lua_thread_pool.c
src/lua/lua_thread_pool.h

index 40d752704c0e44b460daf1dce2b97017af23c429..cf171c313672f88da5196448ad6a63f0c4fb0d5c 100644 (file)
@@ -419,16 +419,14 @@ gint
 lua_yield_thread (struct thread_entry *thread_entry, gint nresults);
 
 /**
- *
- * @param pool
+ * Resumes suspended by lua_yield_thread () thread
+ * @param task
  * @param thread_entry
  * @param narg
- * @return
  */
 void
 lua_resume_thread (struct rspamd_task *task, struct thread_entry *thread_entry, gint narg);
 
-
 /* Paths defs */
 #define RSPAMD_CONFDIR_INDEX "CONFDIR"
 #define RSPAMD_RUNDIR_INDEX "RUNDIR"
index 865b41fce8492ec9ff7432157a8dae1771cab1e3..979b31f6b937c6f625375c9cc810fc05d45c7eb2 100644 (file)
@@ -61,7 +61,7 @@ lua_thread_pool_free (struct lua_thread_pool *pool)
 }
 
 struct thread_entry *
-lua_thread_pool_get(struct lua_thread_pool *pool)
+lua_thread_pool_get (struct lua_thread_pool *pool)
 {
        gpointer cur;
        struct thread_entry *ent = NULL;
@@ -81,7 +81,7 @@ lua_thread_pool_get(struct lua_thread_pool *pool)
 }
 
 void
-lua_thread_pool_return(struct lua_thread_pool *pool, struct thread_entry *thread_entry)
+lua_thread_pool_return (struct lua_thread_pool *pool, struct thread_entry *thread_entry)
 {
        g_assert (lua_status (thread_entry->lua_state) == 0); /* we can't return a running/yielded thread into the pool */
 
@@ -99,7 +99,7 @@ lua_thread_pool_return(struct lua_thread_pool *pool, struct thread_entry *thread
 }
 
 void
-lua_thread_pool_terminate_entry(struct lua_thread_pool *pool, struct thread_entry *thread_entry)
+lua_thread_pool_terminate_entry (struct lua_thread_pool *pool, struct thread_entry *thread_entry)
 {
        struct thread_entry *ent = NULL;
 
@@ -119,13 +119,30 @@ lua_thread_pool_terminate_entry(struct lua_thread_pool *pool, struct thread_entr
 }
 
 struct thread_entry *
-lua_thread_pool_get_running_entry(struct lua_thread_pool *pool)
+lua_thread_pool_get_running_entry (struct lua_thread_pool *pool)
 {
        return pool->running_entry;
 }
 
 void
-lua_thread_pool_set_running_entry(struct lua_thread_pool *pool, struct thread_entry *thread_entry)
+lua_thread_pool_set_running_entry (struct lua_thread_pool *pool, struct thread_entry *thread_entry)
 {
        pool->running_entry = thread_entry;
 }
+
+
+void
+lua_thread_pool_prepare_callback (struct lua_thread_pool *pool, struct lua_callback_state *cbs)
+{
+       cbs->thread_pool = pool;
+       cbs->previous_thread = lua_thread_pool_get_running_entry (pool);
+       cbs->my_thread = lua_thread_pool_get (pool);
+       cbs->L = cbs->my_thread->lua_state;
+}
+
+void
+lua_thread_pool_restore_callback (struct lua_callback_state *cbs)
+{
+       lua_thread_pool_return (cbs->thread_pool, cbs->my_thread);
+       lua_thread_pool_set_running_entry (cbs->thread_pool, cbs->previous_thread);
+}
index 64708c4884dff3d1fd0d52bed8af191d58383fe9..b72b72e8d43cc92572ef79aed6605e6f0e3db16c 100644 (file)
@@ -11,6 +11,13 @@ struct thread_entry {
 
 struct thread_pool;
 
+struct lua_callback_state {
+       lua_State *L;
+       struct thread_entry *my_thread;
+       struct thread_entry *previous_thread;
+       struct lua_thread_pool *thread_pool;
+};
+
 /**
  * Allocates new thread pool on state L. Pre-creates number of lua-threads to use later on
  *
@@ -67,7 +74,7 @@ lua_thread_pool_terminate_entry(struct lua_thread_pool *pool, struct thread_entr
  * @return
  */
 struct thread_entry *
-lua_thread_pool_get_running_entry(struct lua_thread_pool *pool);
+lua_thread_pool_get_running_entry (struct lua_thread_pool *pool);
 
 /**
  * Updates currently running thread
@@ -76,7 +83,24 @@ lua_thread_pool_get_running_entry(struct lua_thread_pool *pool);
  * @param thread_entry
  */
 void
-lua_thread_pool_set_running_entry(struct lua_thread_pool *pool, struct thread_entry *thread_entry);
+lua_thread_pool_set_running_entry (struct lua_thread_pool *pool, struct thread_entry *thread_entry);
+
+/**
+ * Prevents yielded thread to be used for callback execution. lua_thread_pool_restore_callback() should be called afterwards.
+ *
+ * @param pool
+ * @param cbs
+ */
+void
+lua_thread_pool_prepare_callback (struct lua_thread_pool *pool, struct lua_callback_state *cbs);
+
+/**
+ * Restores state after lua_thread_pool_prepare_callback () usage
+ *
+ * @param cbs
+ */
+void
+lua_thread_pool_restore_callback (struct lua_callback_state *cbs);
 
 #endif /* LUA_THREAD_POOL_H_ */