author | Tero Marttila <terom@fixme.fi> |
Wed, 27 May 2009 23:57:48 +0300 | |
branch | new-lib-errors |
changeset 217 | 7728d6ec3abf |
parent 210 | 05abca972db0 |
permissions | -rw-r--r-- |
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 |
/** |
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
|
65 |
* Protected-mode function to yield execution of the given lua_State, which must be a luaState created with |
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
|
66 |
* lua_thread_start. |
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
|
67 |
* |
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
|
68 |
* This should be called the same way as lua_yield, except no results are supported 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
|
69 |
*/ |
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
|
70 |
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
|
71 |
|
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
|
72 |
/** |
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
|
73 |
* Resume execution of the given lua_State, which must be a lua_State created with lua_thread_start. |
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 |
*/ |
ffdf53fd0337
implement lua_threads, 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 |
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
|
76 |
|
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
|
77 |
/** |
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
|
78 |
* Abort execution of the given thread, if it's running. |
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
|
79 |
* |
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
|
80 |
* Currently, there is no mechanism to actually abort a running thread, so this will instead release the lua_thread for |
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
|
81 |
* new use, and then cause the lua_thread_resume to do nothing. |
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
|
82 |
*/ |
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
|
83 |
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
|
84 |
|
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
|
85 |
#endif |