src/lua_thread.c
author Tero Marttila <terom@fixme.fi>
Thu, 21 May 2009 17:08:47 +0300
changeset 214 0d5d46ab49d5
parent 210 05abca972db0
child 215 85863b89e38b
permissions -rw-r--r--
merge lua_thread_setup bcak into _lua_thread_start, as everything can be done on the main lua 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
     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
 * 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
    72
 * (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
    73
 */
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
    74
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
    75
{
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    76
    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
    77
    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
    78
    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
    79
    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
    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
    // 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
    82
    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
    83
        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
    84
    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
    85
        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
    86
    
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
    87
    // 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
    88
    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
    89
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
    90
    // 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
    91
    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
    92
 
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
    // 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
    94
    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
    95
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
    // 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
    97
    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
    98
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
    99
    // 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
   100
    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
   101
        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
   102
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
    // 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
   104
    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
   105
        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
   106
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
    // 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
   108
    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
   109
        // 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
   110
        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
   111
    
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
   112
    // 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
   113
    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
   114
ffdf53fd0337 implement lua_threads, 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
    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
   116
        
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
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
   118
    // 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
   119
    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
   120
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
    // 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
   122
    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
   123
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
    // 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
   125
    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
   126
}
ffdf53fd0337 implement lua_threads, 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
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
   128
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
   129
{
ffdf53fd0337 implement lua_threads, 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
    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
   131
ffdf53fd0337 implement lua_threads, 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
    // 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
   133
    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
   134
ffdf53fd0337 implement lua_threads, 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
    // 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
   136
    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
   137
        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
   138
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
   139
    // 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
   140
    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
   141
}
ffdf53fd0337 implement lua_threads, 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
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
   143
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
   144
{
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
    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
   146
}
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
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
   149
{
ffdf53fd0337 implement lua_threads, 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
    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
   151
    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
   152
    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
   153
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
   154
    // 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
   155
    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
   156
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
   157
    // 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
   158
    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
   159
    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
   160
    
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
   161
    // 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
   162
    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
   163
        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
   164
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
    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
   166
        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
   167
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   168
    // 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
   169
    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
   170
        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
   171
    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
   172
        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
   173
ffdf53fd0337 implement lua_threads, 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
    // 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
   175
    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
   176
        // 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
   177
        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
   178
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
   179
    // 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
   180
    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
   181
        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
   182
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
    // 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
   184
    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
   185
}
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   186
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
   187
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
   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
    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
   190
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
    // 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
   192
    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
   193
        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
   194
}
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
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
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
   197
{
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
   198
    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
   199
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
   200
    // 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
   201
    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
   202
        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
   203
    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
   204
        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
   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
    // 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
   207
    (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
   208
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
    // 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
   210
    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
   211
    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
   212
    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
   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
    // 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
   215
    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
   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
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
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
   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
    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
   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
    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
   223
        // 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
   224
        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
   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
    // 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
   227
    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
   228
        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
   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
    // 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
   231
    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
   232
}
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