src/spbot/lua_thread.c
author Tero Marttila <terom@fixme.fi>
Thu, 28 May 2009 00:35:02 +0300
branchnew-lib-errors
changeset 218 5229a5d098b2
parent 215 src/lua_thread.c@85863b89e38b
permissions -rw-r--r--
some of spbot and lib compiles
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"
218
5229a5d098b2 some of spbot and lib compiles
Tero Marttila <terom@fixme.fi>
parents: 215
diff changeset
     2
#include <lib/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
 */
215
85863b89e38b fix lua_thread_resume_state to not call lua_resume from a CFunction executing in the same state; lua_thread_done is now unprotected...
Tero Marttila <terom@fixme.fi>
parents: 214
diff changeset
    24
static void lua_thread_done (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
    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
215
85863b89e38b fix lua_thread_resume_state to not call lua_resume from a CFunction executing in the same state; lua_thread_done is now unprotected...
Tero Marttila <terom@fixme.fi>
parents: 214
diff changeset
    27
    // XXX: unsafe
85863b89e38b fix lua_thread_resume_state to not call lua_resume from a CFunction executing in the same state; lua_thread_done is now unprotected...
Tero Marttila <terom@fixme.fi>
parents: 214
diff changeset
    28
    lua_pushthread(thread->L);
85863b89e38b fix lua_thread_resume_state to not call lua_resume from a CFunction executing in the same state; lua_thread_done is now unprotected...
Tero Marttila <terom@fixme.fi>
parents: 214
diff changeset
    29
    lua_pushnil(thread->L);
85863b89e38b fix lua_thread_resume_state to not call lua_resume from a CFunction executing in the same state; lua_thread_done is now unprotected...
Tero Marttila <terom@fixme.fi>
parents: 214
diff changeset
    30
    lua_settable(thread->L, LUA_REGISTRYINDEX);
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
    31
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
    // 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
    33
    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
    34
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
    // 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
    36
    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
    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
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
    39
/**
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
    40
 * 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
    41
 * 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
    42
 */
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
    43
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
    44
{
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
    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
    46
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
    47
    (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
    48
    
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
    49
    // 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
    50
    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
    51
        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
    52
            // 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
    53
            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
    54
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
    55
        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
    56
            // 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
    57
            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
    58
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
        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
    60
            // 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
    61
            // 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
    62
            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
    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
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_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
    67
    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
    68
    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
    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
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
/**
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
    72
 * 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
    73
 * (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
    74
 */
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
    75
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
    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
    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
    78
    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
    79
    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
    80
    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
    81
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
    // 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
    83
    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
    84
        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
    85
    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
    86
        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
    87
    
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
    88
    // 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
    89
    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
    90
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
    91
    // 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
    92
    TL = lua_newthread(L);
214
0d5d46ab49d5 merge lua_thread_setup bcak into _lua_thread_start, as everything can be done on the main lua state
Tero Marttila <terom@fixme.fi>
parents: 210
diff changeset
    93
 
0d5d46ab49d5 merge lua_thread_setup bcak into _lua_thread_start, as everything can be done on the main lua state
Tero Marttila <terom@fixme.fi>
parents: 210
diff changeset
    94
    // push the lua_thread as the value
0d5d46ab49d5 merge lua_thread_setup bcak into _lua_thread_start, as everything can be done on the main lua state
Tero Marttila <terom@fixme.fi>
parents: 210
diff changeset
    95
    lua_pushlightuserdata(L, ctx->thread);
0d5d46ab49d5 merge lua_thread_setup bcak into _lua_thread_start, as everything can be done on the main lua state
Tero Marttila <terom@fixme.fi>
parents: 210
diff changeset
    96
0d5d46ab49d5 merge lua_thread_setup bcak into _lua_thread_start, as everything can be done on the main lua state
Tero Marttila <terom@fixme.fi>
parents: 210
diff changeset
    97
    // store L -> lua_thread mapping in the registry
0d5d46ab49d5 merge lua_thread_setup bcak into _lua_thread_start, as everything can be done on the main lua state
Tero Marttila <terom@fixme.fi>
parents: 210
diff changeset
    98
    lua_settable(L, LUA_REGISTRYINDEX);
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
    99
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
   100
    // 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
   101
    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
   102
        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
   103
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
   104
    // 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
   105
    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
   106
        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
   107
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
   108
    // 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
   109
    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
   110
        // 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
   111
        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
   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
    // 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
   114
    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
   115
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
    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
   117
        
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
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
   119
    // 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
   120
    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
   121
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
    // 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
   123
    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
   124
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
    // 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
   126
    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
   127
}
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
   128
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
   129
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
   130
{
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
   131
    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
   132
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
   133
    // 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
   134
    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
   135
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
    // 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
   137
    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
   138
        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
   139
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
    // 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
   141
    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
   142
}
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
   143
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
   144
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
   145
{
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
   146
    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
   147
}
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
   148
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
   149
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
   150
{
215
85863b89e38b fix lua_thread_resume_state to not call lua_resume from a CFunction executing in the same state; lua_thread_done is now unprotected...
Tero Marttila <terom@fixme.fi>
parents: 214
diff changeset
   151
    struct lua_thread *thread, **thread_ptr;
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
   152
215
85863b89e38b fix lua_thread_resume_state to not call lua_resume from a CFunction executing in the same state; lua_thread_done is now unprotected...
Tero Marttila <terom@fixme.fi>
parents: 214
diff changeset
   153
    // read the ctx argument off the stack
85863b89e38b fix lua_thread_resume_state to not call lua_resume from a CFunction executing in the same state; lua_thread_done is now unprotected...
Tero Marttila <terom@fixme.fi>
parents: 214
diff changeset
   154
    if ((thread_ptr = lua_touserdata(L, 1)) == NULL)
85863b89e38b fix lua_thread_resume_state to not call lua_resume from a CFunction executing in the same state; lua_thread_done is now unprotected...
Tero Marttila <terom@fixme.fi>
parents: 214
diff changeset
   155
        return luaL_error(L, "lua_touserdata");
85863b89e38b fix lua_thread_resume_state to not call lua_resume from a CFunction executing in the same state; lua_thread_done is now unprotected...
Tero Marttila <terom@fixme.fi>
parents: 214
diff changeset
   156
    else
85863b89e38b fix lua_thread_resume_state to not call lua_resume from a CFunction executing in the same state; lua_thread_done is now unprotected...
Tero Marttila <terom@fixme.fi>
parents: 214
diff changeset
   157
        lua_pop(L, 1);
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
   158
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
   159
    // 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
   160
    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
   161
    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
   162
    
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
   163
    // 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
   164
    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
   165
        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
   166
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
   167
    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
   168
        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
   169
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
    // 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
   171
    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
   172
        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
   173
    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
   174
        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
   175
215
85863b89e38b fix lua_thread_resume_state to not call lua_resume from a CFunction executing in the same state; lua_thread_done is now unprotected...
Tero Marttila <terom@fixme.fi>
parents: 214
diff changeset
   176
    // store it
85863b89e38b fix lua_thread_resume_state to not call lua_resume from a CFunction executing in the same state; lua_thread_done is now unprotected...
Tero Marttila <terom@fixme.fi>
parents: 214
diff changeset
   177
    *thread_ptr = thread;
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
   178
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
    // 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
   180
    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
   181
}
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
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
   183
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
   184
{
215
85863b89e38b fix lua_thread_resume_state to not call lua_resume from a CFunction executing in the same state; lua_thread_done is now unprotected...
Tero Marttila <terom@fixme.fi>
parents: 214
diff changeset
   185
    struct lua_thread *thread = 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
   186
    error_t err;
215
85863b89e38b fix lua_thread_resume_state to not call lua_resume from a CFunction executing in the same state; lua_thread_done is now unprotected...
Tero Marttila <terom@fixme.fi>
parents: 214
diff changeset
   187
    int ret;
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
   188
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
    // execute in protected mode
215
85863b89e38b fix lua_thread_resume_state to not call lua_resume from a CFunction executing in the same state; lua_thread_done is now unprotected...
Tero Marttila <terom@fixme.fi>
parents: 214
diff changeset
   190
    if (nexus_lua_error(L, lua_cpcall(L, _lua_thread_resume_state, &thread), &err))
85863b89e38b fix lua_thread_resume_state to not call lua_resume from a CFunction executing in the same state; lua_thread_done is now unprotected...
Tero Marttila <terom@fixme.fi>
parents: 214
diff changeset
   191
        return log_warn_error(&err, "%p", L);
85863b89e38b fix lua_thread_resume_state to not call lua_resume from a CFunction executing in the same state; lua_thread_done is now unprotected...
Tero Marttila <terom@fixme.fi>
parents: 214
diff changeset
   192
85863b89e38b fix lua_thread_resume_state to not call lua_resume from a CFunction executing in the same state; lua_thread_done is now unprotected...
Tero Marttila <terom@fixme.fi>
parents: 214
diff changeset
   193
    // handle the resume
85863b89e38b fix lua_thread_resume_state to not call lua_resume from a CFunction executing in the same state; lua_thread_done is now unprotected...
Tero Marttila <terom@fixme.fi>
parents: 214
diff changeset
   194
    if ((ret = lua_thread_resume(L, thread, &err)) < 0)
85863b89e38b fix lua_thread_resume_state to not call lua_resume from a CFunction executing in the same state; lua_thread_done is now unprotected...
Tero Marttila <terom@fixme.fi>
parents: 214
diff changeset
   195
        // notify user
85863b89e38b fix lua_thread_resume_state to not call lua_resume from a CFunction executing in the same state; lua_thread_done is now unprotected...
Tero Marttila <terom@fixme.fi>
parents: 214
diff changeset
   196
        lua_thread_done(thread, &err);
85863b89e38b fix lua_thread_resume_state to not call lua_resume from a CFunction executing in the same state; lua_thread_done is now unprotected...
Tero Marttila <terom@fixme.fi>
parents: 214
diff changeset
   197
85863b89e38b fix lua_thread_resume_state to not call lua_resume from a CFunction executing in the same state; lua_thread_done is now unprotected...
Tero Marttila <terom@fixme.fi>
parents: 214
diff changeset
   198
    // finished?
85863b89e38b fix lua_thread_resume_state to not call lua_resume from a CFunction executing in the same state; lua_thread_done is now unprotected...
Tero Marttila <terom@fixme.fi>
parents: 214
diff changeset
   199
    else if (ret == 0)
85863b89e38b fix lua_thread_resume_state to not call lua_resume from a CFunction executing in the same state; lua_thread_done is now unprotected...
Tero Marttila <terom@fixme.fi>
parents: 214
diff changeset
   200
        lua_thread_done(thread, NULL);
85863b89e38b fix lua_thread_resume_state to not call lua_resume from a CFunction executing in the same state; lua_thread_done is now unprotected...
Tero Marttila <terom@fixme.fi>
parents: 214
diff changeset
   201
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
}
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
   203
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
   204
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
   205
{
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
    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
   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
    // 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
   209
    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
   210
        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
   211
    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
   212
        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
   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
    // 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
   215
    (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
   216
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
    // 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
   218
    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
   219
    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
   220
    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
   221
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
    // 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
   223
    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
   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
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
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
   227
{
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
    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
   229
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
    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
   231
        // 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
   232
        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
   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
    // 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
   235
    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
   236
        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
   237
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
    // 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
   239
    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
   240
}
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