src/lua_thread.c
author Tero Marttila <terom@fixme.fi>
Thu, 21 May 2009 16:22:57 +0300
branchlua-threads
changeset 204 7dfc3d7bd92b
parent 203 ffdf53fd0337
child 210 05abca972db0
permissions -rw-r--r--
fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
203
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
#include "lua_thread.h"
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     2
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     3
#include <string.h>
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     4
#include <assert.h>
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     5
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     6
#include <lua5.1/lauxlib.h>
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     7
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     8
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     9
void lua_thread_init (struct lua_thread *thread, struct nexus_lua *lua, lua_thread_cb cb_func, void *cb_arg)
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    10
{
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    11
    // zero
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    12
    memset(thread, 0, sizeof(*thread));
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    13
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    14
    // store
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    15
    thread->lua = lua;
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    16
    thread->cb_func = cb_func;
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    17
    thread->cb_arg = cb_arg;
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    18
}
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    19
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    20
/**
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    21
 * Thread completed, cleanup and call user callback with given error (NULL for success).
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    22
 */
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    23
static void lua_thread_done (lua_State *L, struct lua_thread *thread, error_t *err)
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    24
{
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    25
    // remove from registry so thread can be GC'd
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    26
    lua_pushthread(L);
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    27
    lua_pushnil(L);
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    28
    lua_settable(L, LUA_REGISTRYINDEX);
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    29
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    30
    // drop our ref
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    31
    thread->L = NULL;
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    32
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    33
    // call
204
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
    34
    thread->cb_func(err, thread->cb_arg);
203
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    35
}
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    36
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    37
/**
204
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
    38
 * Execute the lua_resume with zero arguments. If the thread finished, this returns zero, if it yielded, >0. For
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
    39
 * errors, returns -err_t.
203
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    40
 */
204
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
    41
static int lua_thread_resume (lua_State *L, struct lua_thread *thread, error_t *err)
203
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    42
{
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    43
    int ret;
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    44
204
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
    45
    (void) thread;
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
    46
    
203
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    47
    // invoke it
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    48
    switch ((ret = lua_resume(L, 0))) {
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    49
        case LUA_YIELD:
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    50
            // ok, running, wait
204
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
    51
            return 1;
203
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    52
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    53
        case 0:
204
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
    54
            // done
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
    55
            return 0;
203
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    56
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    57
        default:
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    58
            // let caller handle
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    59
            // XXX: backtrace...
204
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
    60
            return -nexus_lua_error(L, ret, err);
203
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    61
    }
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    62
}
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    63
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    64
struct lua_thread_start_ctx {
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    65
    struct lua_thread *thread;
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    66
    const char *chunk;
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    67
};
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    68
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    69
/**
204
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
    70
 * Setup the given newthread's state
203
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    71
 */
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    72
static int lua_thread_setup (lua_State *L)
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    73
{
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    74
    struct lua_thread_start_ctx *ctx;
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    75
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    76
    // read the ctx argument off the stack
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    77
    if ((ctx = lua_touserdata(L, 1)) == NULL)
204
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
    78
        return luaL_error(L, "lua_touserdata");
203
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    79
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    80
    // push the thread state as the key
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    81
    lua_pushthread(L);
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    82
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    83
    // push the lua_thread as the value
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    84
    lua_pushlightuserdata(L, ctx->thread);
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    85
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    86
    // store L -> lua_thread mapping in the registry
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    87
    lua_settable(L, LUA_REGISTRYINDEX);
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    88
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    89
    // clean up the stack
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    90
    lua_pop(L, 1);
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    91
    
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    92
    // ok
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    93
    return 0;
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    94
}
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    95
204
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
    96
/**
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
    97
 * Create a new thread, set it up, and run the initial resume, returning the boolean result of that
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
    98
 * (true for yielded, false for done).
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
    99
 */
203
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   100
static int _lua_thread_start (lua_State *L)
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   101
{
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   102
    struct lua_thread_start_ctx *ctx;
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   103
    lua_State *TL;
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   104
    error_t err;
204
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
   105
    int ret;
203
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   106
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   107
    // read the ctx argument off the stack
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   108
    if ((ctx = lua_touserdata(L, 1)) == NULL)
204
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
   109
        return luaL_error(L, "lua_touserdata");
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
   110
    else
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
   111
        lua_pop(L, 1);
203
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   112
    
204
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
   113
    // check
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
   114
    assert(!ctx->thread->L);
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
   115
203
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   116
    // create new thread
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   117
    TL = lua_newthread(L);
204
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
   118
    
203
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   119
    // create thread and do setup on it
204
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
   120
    if (nexus_lua_error(TL, lua_cpcall(TL, lua_thread_setup, ctx), &err))
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
   121
        goto error;
203
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   122
204
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
   123
    // load the chunk as a lua function into the thread's state
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
   124
    if (nexus_lua_error(TL, luaL_loadstring(TL, ctx->chunk), &err))
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
   125
        goto error;
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
   126
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
   127
    // initial resume on the loaded chunk
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
   128
    if ((ret = lua_thread_resume(TL, ctx->thread, &err)) < 0)
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
   129
        goto error;
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
   130
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
   131
    // yielded?
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
   132
    if (ret) 
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
   133
        // store
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
   134
        ctx->thread->L = TL;
203
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   135
    
204
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
   136
    // pop thread to release for GC
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
   137
    lua_pop(L, 1);
203
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   138
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   139
    return 0;
204
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
   140
        
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
   141
error:
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
   142
    // pop thread to release for GC
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
   143
    lua_pop(L, 1);
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
   144
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
   145
    // drop ref
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
   146
    ctx->thread->L = NULL;
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
   147
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
   148
    // pass on error
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
   149
    return luaL_error(L, "%s", error_msg(&err));
203
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   150
}
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   151
204
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
   152
int lua_thread_start (struct lua_thread *thread, const char *chunk, error_t *err)
203
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   153
{
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   154
    struct lua_thread_start_ctx ctx = { thread, chunk };
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   155
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   156
    // sanity-check
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   157
    assert(!thread->L);
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   158
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   159
    // use protected mode to setup
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   160
    if (nexus_lua_error(thread->lua->st, lua_cpcall(thread->lua->st, _lua_thread_start, &ctx), err))
204
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
   161
        return -ERROR_CODE(err);
203
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   162
204
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
   163
    // return true if yielded, false if done
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
   164
    return (thread->L != NULL);
203
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   165
}
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   166
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   167
void lua_thread_resume_state (lua_State *L)
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   168
{
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   169
    struct lua_thread *thread;
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   170
    error_t err;
204
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
   171
    int ret;
203
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   172
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   173
    // lookup the thread's context
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   174
    lua_pushthread(L);
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   175
    lua_gettable(L, LUA_REGISTRYINDEX);
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   176
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   177
    // get it as a pointer
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   178
    if ((thread = lua_touserdata(L, -1)) == NULL)
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   179
        luaL_error(L, "lua_thread_resume_state: lua_touserdata");
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   180
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   181
    // handle the resume
204
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
   182
    if ((ret = lua_thread_resume(L, thread, &err)) < 0)
203
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   183
        // notify user
204
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
   184
        return lua_thread_done(thread->L, thread, &err);
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
   185
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
   186
    // finished?
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
   187
    if (ret == 0)
7dfc3d7bd92b fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
Tero Marttila <terom@fixme.fi>
parents: 203
diff changeset
   188
        return lua_thread_done(thread->L, thread, NULL);
203
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   189
}
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   190