src/lua_thread.c
author Tero Marttila <terom@fixme.fi>
Thu, 21 May 2009 16:56:42 +0300
changeset 210 05abca972db0
parent 204 7dfc3d7bd92b
child 214 0d5d46ab49d5
permissions -rw-r--r--
implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
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"
210
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
     2
#include "log.h"
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
     3
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 <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
     5
#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
     6
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
#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
     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
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
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
    11
{
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
    // 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
    13
    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
    14
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
    // 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
    16
    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
    17
    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
    18
    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
    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
/**
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
 * 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
    23
 */
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
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
    25
{
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
    // 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
    27
    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
    28
    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
    29
    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
    30
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
    // 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
    32
    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
    33
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
    34
    // 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
    35
    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
    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
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
    38
/**
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
    39
 * 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
    40
 * 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
    41
 */
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
    42
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
    43
{
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
    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
    45
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
    46
    (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
    47
    
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
    48
    // 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
    49
    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
    50
        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
    51
            // 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
    52
            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
    53
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
    54
        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
    55
            // 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
    56
            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
    57
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
        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
    59
            // 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
    60
            // 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
    61
            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
    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
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_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
    66
    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
    67
    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
    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
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
    70
/**
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
    71
 * 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
    72
 */
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
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
    74
{
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
    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
    76
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
    // 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
    78
    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
    79
        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
    80
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
    // 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
    82
    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
    83
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
    // 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
    85
    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
    86
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
    // 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
    88
    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
    89
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
    // 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
    91
    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
    92
    
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
    // 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
    94
    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
    95
}
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
    96
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
    97
/**
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
 * 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
    99
 * (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
   100
 */
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
   101
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
   102
{
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
    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
   104
    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
   105
    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
   106
    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
   107
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
    // 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
   109
    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
   110
        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
   111
    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
   112
        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
   113
    
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
   114
    // 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
   115
    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
   116
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
   117
    // 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
   118
    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
   119
    
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
   120
    // 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
   121
    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
   122
        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
   123
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
   124
    // 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
   125
    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
   126
        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
   127
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
    // 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
   129
    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
   130
        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
   131
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
    // 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
   133
    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
   134
        // 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
   135
        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
   136
    
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
   137
    // 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
   138
    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
   139
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
   140
    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
   141
        
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
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
   143
    // 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
   144
    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
   145
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
    // 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
   147
    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
   148
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
    // 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
   150
    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
   151
}
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
   152
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
   153
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
   154
{
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
    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
   156
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
    // 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
   158
    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
   159
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
    // 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
   161
    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
   162
        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
   163
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
   164
    // 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
   165
    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
   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
210
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   168
int lua_thread_yield_state (lua_State *L)
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   169
{
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   170
    return lua_yield(L, 0);
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   171
}
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   172
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   173
static int _lua_thread_resume_state (lua_State *L)
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
   174
{
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
    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
   176
    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
   177
    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
   178
210
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   179
    // pop irrelevant context arg
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   180
    lua_pop(L, 1);
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   181
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
   182
    // 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
   183
    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
   184
    lua_gettable(L, LUA_REGISTRYINDEX);
210
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   185
    
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   186
    // not registered?
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   187
    if (lua_isnil(L, -1))
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   188
        return luaL_error(L, "not a registered lua_thread state");
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   189
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   190
    else if (lua_isboolean(L, -1) && !lua_toboolean(L, -1))
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   191
        return luaL_error(L, "lua_thread was aborted");
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
   192
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
   193
    // 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
   194
    if ((thread = lua_touserdata(L, -1)) == NULL)
210
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   195
        return luaL_error(L, "lua_touserdata");
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   196
    else
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   197
        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
   198
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
   199
    // 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
   200
    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
   201
        // notify user
210
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   202
        lua_thread_done(thread->L, thread, &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
   203
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
   204
    // finished?
210
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   205
    else if (ret == 0)
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   206
        lua_thread_done(thread->L, thread, NULL);
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   207
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   208
    // ok
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   209
    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
   210
}
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
   211
210
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   212
void lua_thread_resume_state (lua_State *L)
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   213
{
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   214
    error_t err;
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   215
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   216
    // execute in protected mode
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   217
    if (nexus_lua_error(L, lua_cpcall(L, _lua_thread_resume_state, NULL), &err))
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   218
        log_warn_error(&err, "%p", L);
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   219
}
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   220
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   221
static int _lua_thread_abort (lua_State *L)
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   222
{
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   223
    struct lua_thread *thread;
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   224
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   225
    // get context arg
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   226
    if ((thread = lua_touserdata(L, -1)) == NULL)
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   227
        luaL_error(L, "lua_thread_resume_state: lua_touserdata");
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   228
    else
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   229
        lua_pop(L, 1);
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   230
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   231
    // no mechanism to actually abort the underlying wait yet
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   232
    (void) thread;
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   233
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   234
    // invalidate in registry for later lua_thread_resume_state
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   235
    lua_pushthread(L);
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   236
    lua_pushboolean(L, false);
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   237
    lua_settable(L, LUA_REGISTRYINDEX);
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   238
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   239
    // not much else we can do
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   240
    return 0;
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   241
}
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   242
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   243
void lua_thread_abort (struct lua_thread *thread)
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   244
{
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   245
    error_t err;
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   246
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   247
    if (!thread->L)
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   248
        // no thread executing
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   249
        return;
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   250
    
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   251
    // use protected mode
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   252
    if (nexus_lua_error(thread->L, lua_cpcall(thread->L, _lua_thread_abort, thread), &err))
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   253
        log_warn_error(&err, "_lua_thread_abort");
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   254
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   255
    // unset
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   256
    thread->L = NULL;
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   257
}
05abca972db0 implement lua_thread_abort, add lua_thread_yield_state func, and fix lua_thread_resume_state to use protected mode
Tero Marttila <terom@fixme.fi>
parents: 204
diff changeset
   258