src/spbot/lua_thread.h
branchnew-lib-errors
changeset 218 5229a5d098b2
parent 210 05abca972db0
equal deleted inserted replaced
217:7728d6ec3abf 218:5229a5d098b2
       
     1 #ifndef SPBOT_LUA_THREAD_H
       
     2 #define SPBOT_LUA_THREAD_H
       
     3 
       
     4 /**
       
     5  * @file
       
     6  *
       
     7  * Running code as lua coroutines for a nexus_lua
       
     8  */
       
     9 #include "nexus_lua.h"
       
    10 #include <lua5.1/lua.h>
       
    11 
       
    12 /*
       
    13  * Forward-declare
       
    14  */
       
    15 struct lua_thread;
       
    16 
       
    17 /**
       
    18  * Callback for async execution completion. The thread will be cleaned up for re-use before this is called.
       
    19  *
       
    20  * Called with err == NULL for success, error code otherwise.
       
    21  *
       
    22  * XXX: also return execution results (as a lua_State arg)?
       
    23  */
       
    24 typedef void (*lua_thread_cb) (const error_t *err, void *arg);
       
    25 
       
    26 /**
       
    27  * A thread of execution
       
    28  */
       
    29 struct lua_thread {
       
    30     /** The lua_nexus environment we're running under */
       
    31     struct nexus_lua *lua;
       
    32 
       
    33     /** Callback */
       
    34     lua_thread_cb cb_func;
       
    35 
       
    36     /** Callback context argument */
       
    37     void *cb_arg;
       
    38 
       
    39     /** Real lua thread state */
       
    40     lua_State *L;
       
    41 };
       
    42 
       
    43 /**
       
    44  * Initialize the given lua_thread state for execution.
       
    45  *
       
    46  * You only need to call this once for each lua_thread that you use.
       
    47  */
       
    48 void lua_thread_init (struct lua_thread *thread, struct nexus_lua *lua, lua_thread_cb cb_func, void *cb_arg);
       
    49 
       
    50 /**
       
    51  * Execute the given chunk in a thread context.
       
    52  *
       
    53  * This should be called from unprotected mode, and the thread must currently not be active.
       
    54  *
       
    55  * This will load the chunk, create a new lua thread state off the lua_nexus, and then do the initial 'lua_resume' call
       
    56  * on the loaded chunk, to execute up to the first yield or final return.
       
    57  *
       
    58  * If this process raises an error, it will be returned directly. If the called chunk simply returns without yielding,
       
    59  * this returns 0, and the callback is *NOT* called. Otherwise, this returns >0 and the thread's callback function will
       
    60  * eventually be called once the thread either errors out or finishes execution.
       
    61  */
       
    62 int lua_thread_start (struct lua_thread *thread, const char *chunk, error_t *err);
       
    63 
       
    64 /**
       
    65  * Protected-mode function to yield execution of the given lua_State, which must be a luaState created with
       
    66  * lua_thread_start.
       
    67  *
       
    68  * This should be called the same way as lua_yield, except no results are supported yet.
       
    69  */
       
    70 int lua_thread_yield_state (lua_State *L);
       
    71 
       
    72 /**
       
    73  * Resume execution of the given lua_State, which must be a lua_State created with lua_thread_start.
       
    74  */
       
    75 void lua_thread_resume_state (lua_State *L);
       
    76 
       
    77 /**
       
    78  * Abort execution of the given thread, if it's running.
       
    79  *
       
    80  * Currently, there is no mechanism to actually abort a running thread, so this will instead release the lua_thread for
       
    81  * new use, and then cause the lua_thread_resume to do nothing. 
       
    82  */
       
    83 void lua_thread_abort (struct lua_thread *thread);
       
    84 
       
    85 #endif