src/lua_thread.h
author Tero Marttila <terom@fixme.fi>
Thu, 21 May 2009 16:22:57 +0300
branchlua-threads
changeset 204 7dfc3d7bd92b
parent 203 ffdf53fd0337
child 210 05abca972db0
permissions -rw-r--r--
fix lua_thread to call lua_resume from outside TL state, and to never call the callback directly
203
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
#ifndef LUA_THREAD_H
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     2
#define LUA_THREAD_H
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     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
/**
ffdf53fd0337 implement lua_threads, 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
 * @file
ffdf53fd0337 implement lua_threads, 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
 * Running code as lua coroutines for a nexus_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
     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
#include "nexus_lua.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
    10
#include <lua5.1/lua.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
    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
/*
ffdf53fd0337 implement lua_threads, 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
 * Forward-declare
ffdf53fd0337 implement lua_threads, 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
struct lua_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
    16
ffdf53fd0337 implement lua_threads, 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
/**
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
    18
 * Callback for async execution completion. The thread will be cleaned up for re-use before this is called.
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
    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
 * Called with err == NULL for success, error code otherwise.
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
    21
 *
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
    22
 * XXX: also return execution results (as a lua_State 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
    23
 */
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
    24
typedef void (*lua_thread_cb) (const error_t *err, void *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
    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
/**
ffdf53fd0337 implement lua_threads, 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
 * A thread of execution
ffdf53fd0337 implement lua_threads, 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
 */
ffdf53fd0337 implement lua_threads, 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
struct lua_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
    30
    /** The lua_nexus environment we're running under */
ffdf53fd0337 implement lua_threads, 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
    struct nexus_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
    32
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    33
    /** Callback */
ffdf53fd0337 implement lua_threads, 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
    lua_thread_cb 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
    35
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    36
    /** Callback context argument */
ffdf53fd0337 implement lua_threads, 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
    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
    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
    /** Real lua thread state */
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    40
    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
    41
};
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    42
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    43
/**
ffdf53fd0337 implement lua_threads, 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
 * Initialize the given lua_thread state for execution.
ffdf53fd0337 implement lua_threads, 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
 *
ffdf53fd0337 implement lua_threads, 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
 * You only need to call this once for each lua_thread that you use.
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    47
 */
ffdf53fd0337 implement lua_threads, 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
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
    49
ffdf53fd0337 implement lua_threads, 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
/**
ffdf53fd0337 implement lua_threads, 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
 * Execute the given chunk in a thread 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
    52
 *
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    53
 * This should be called from unprotected mode, and the thread must currently not be active.
ffdf53fd0337 implement lua_threads, 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
 * This will load the chunk, create a new lua thread state off the lua_nexus, and then do the initial 'lua_resume' 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
    56
 * on the loaded chunk, to execute up to the first yield or final return.
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
 *
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
    58
 * If this process raises an error, it will be returned directly. If the called chunk simply returns without yielding,
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
    59
 * this returns 0, and the callback is *NOT* called. Otherwise, this returns >0 and the thread's callback function will
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
    60
 * eventually be called once the thread either errors out or finishes execution.
ffdf53fd0337 implement lua_threads, 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
 */
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
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
    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
 * Protected-mode function to resume execution of the given lua_State, which must be a lua_State created with
ffdf53fd0337 implement lua_threads, 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
 * lua_thread_start.
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    67
 */
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    68
void lua_thread_resume_state (lua_State *L);
ffdf53fd0337 implement lua_threads, nexus:sleep test func, and basic support in console/lua_console... doesn't actually work yet
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    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
#endif