1 | /* |
---|
2 | ** $Id: lstate.h,v 1.109 2003/02/27 11:52:30 roberto Exp $ |
---|
3 | ** Global State |
---|
4 | ** See Copyright Notice in lua.h |
---|
5 | */ |
---|
6 | |
---|
7 | #ifndef lstate_h |
---|
8 | #define lstate_h |
---|
9 | |
---|
10 | #include "lua.h" |
---|
11 | |
---|
12 | #include "lobject.h" |
---|
13 | #include "ltm.h" |
---|
14 | #include "lzio.h" |
---|
15 | |
---|
16 | |
---|
17 | /* |
---|
18 | ** macros for thread synchronization inside Lua core machine: |
---|
19 | ** all accesses to the global state and to global objects are synchronized. |
---|
20 | ** Because threads can read the stack of other threads |
---|
21 | ** (when running garbage collection), |
---|
22 | ** a thread must also synchronize any write-access to its own stack. |
---|
23 | ** Unsynchronized accesses are allowed only when reading its own stack, |
---|
24 | ** or when reading immutable fields from global objects |
---|
25 | ** (such as string values and udata values). |
---|
26 | */ |
---|
27 | #ifndef lua_lock |
---|
28 | #define lua_lock(L) ((void) 0) |
---|
29 | #endif |
---|
30 | |
---|
31 | #ifndef lua_unlock |
---|
32 | #define lua_unlock(L) ((void) 0) |
---|
33 | #endif |
---|
34 | |
---|
35 | |
---|
36 | #ifndef lua_userstateopen |
---|
37 | #define lua_userstateopen(l) |
---|
38 | #endif |
---|
39 | |
---|
40 | |
---|
41 | |
---|
42 | struct lua_longjmp; /* defined in ldo.c */ |
---|
43 | |
---|
44 | |
---|
45 | /* default meta table (both for tables and udata) */ |
---|
46 | #define defaultmeta(L) (&G(L)->_defaultmeta) |
---|
47 | |
---|
48 | /* table of globals */ |
---|
49 | #define gt(L) (&L->_gt) |
---|
50 | |
---|
51 | /* registry */ |
---|
52 | #define registry(L) (&G(L)->_registry) |
---|
53 | |
---|
54 | |
---|
55 | /* extra stack space to handle TM calls and some other extras */ |
---|
56 | #define EXTRA_STACK 5 |
---|
57 | |
---|
58 | |
---|
59 | #define BASIC_CI_SIZE 8 |
---|
60 | |
---|
61 | #define BASIC_STACK_SIZE (2*LUA_MINSTACK) |
---|
62 | |
---|
63 | |
---|
64 | |
---|
65 | typedef struct stringtable { |
---|
66 | GCObject **hash; |
---|
67 | ls_nstr nuse; /* number of elements */ |
---|
68 | int size; |
---|
69 | } stringtable; |
---|
70 | |
---|
71 | |
---|
72 | /* |
---|
73 | ** informations about a call |
---|
74 | */ |
---|
75 | typedef struct CallInfo { |
---|
76 | StkId base; /* base for called function */ |
---|
77 | StkId top; /* top for this function */ |
---|
78 | int state; /* bit fields; see below */ |
---|
79 | union { |
---|
80 | struct { /* for Lua functions */ |
---|
81 | const Instruction *savedpc; |
---|
82 | const Instruction **pc; /* points to `pc' variable in `luaV_execute' */ |
---|
83 | int tailcalls; /* number of tail calls lost under this entry */ |
---|
84 | } l; |
---|
85 | struct { /* for C functions */ |
---|
86 | int dummy; /* just to avoid an empty struct */ |
---|
87 | } c; |
---|
88 | } u; |
---|
89 | } CallInfo; |
---|
90 | |
---|
91 | |
---|
92 | /* |
---|
93 | ** bit fields for `CallInfo.state' |
---|
94 | */ |
---|
95 | #define CI_C (1<<0) /* 1 if function is a C function */ |
---|
96 | /* 1 if (Lua) function has an active `luaV_execute' running it */ |
---|
97 | #define CI_HASFRAME (1<<1) |
---|
98 | /* 1 if Lua function is calling another Lua function (and therefore its |
---|
99 | `pc' is being used by the other, and therefore CI_SAVEDPC is 1 too) */ |
---|
100 | #define CI_CALLING (1<<2) |
---|
101 | #define CI_SAVEDPC (1<<3) /* 1 if `savedpc' is updated */ |
---|
102 | #define CI_YIELD (1<<4) /* 1 if thread is suspended */ |
---|
103 | |
---|
104 | |
---|
105 | #define ci_func(ci) (clvalue((ci)->base - 1)) |
---|
106 | |
---|
107 | |
---|
108 | /* |
---|
109 | ** `global state', shared by all threads of this state |
---|
110 | */ |
---|
111 | typedef struct global_State { |
---|
112 | stringtable strt; /* hash table for strings */ |
---|
113 | GCObject *rootgc; /* list of (almost) all collectable objects */ |
---|
114 | GCObject *rootudata; /* (separated) list of all userdata */ |
---|
115 | GCObject *tmudata; /* list of userdata to be GC */ |
---|
116 | Mbuffer buff; /* temporary buffer for string concatentation */ |
---|
117 | lu_mem GCthreshold; |
---|
118 | lu_mem nblocks; /* number of `bytes' currently allocated */ |
---|
119 | lua_CFunction panic; /* to be called in unprotected errors */ |
---|
120 | TObject _registry; |
---|
121 | TObject _defaultmeta; |
---|
122 | struct lua_State *mainthread; |
---|
123 | Node dummynode[1]; /* common node array for all empty tables */ |
---|
124 | TString *tmname[TM_N]; /* array with tag-method names */ |
---|
125 | } global_State; |
---|
126 | |
---|
127 | |
---|
128 | /* |
---|
129 | ** `per thread' state |
---|
130 | */ |
---|
131 | struct lua_State { |
---|
132 | CommonHeader; |
---|
133 | StkId top; /* first free slot in the stack */ |
---|
134 | StkId base; /* base of current function */ |
---|
135 | global_State *l_G; |
---|
136 | CallInfo *ci; /* call info for current function */ |
---|
137 | StkId stack_last; /* last free slot in the stack */ |
---|
138 | StkId stack; /* stack base */ |
---|
139 | int stacksize; |
---|
140 | CallInfo *end_ci; /* points after end of ci array*/ |
---|
141 | CallInfo *base_ci; /* array of CallInfo's */ |
---|
142 | unsigned short size_ci; /* size of array `base_ci' */ |
---|
143 | unsigned short nCcalls; /* number of nested C calls */ |
---|
144 | lu_byte hookmask; |
---|
145 | lu_byte allowhook; |
---|
146 | lu_byte hookinit; |
---|
147 | int basehookcount; |
---|
148 | int hookcount; |
---|
149 | lua_Hook hook; |
---|
150 | TObject _gt; /* table of globals */ |
---|
151 | GCObject *openupval; /* list of open upvalues in this stack */ |
---|
152 | GCObject *gclist; |
---|
153 | struct lua_longjmp *errorJmp; /* current error recover point */ |
---|
154 | ptrdiff_t errfunc; /* current error handling function (stack index) */ |
---|
155 | }; |
---|
156 | |
---|
157 | |
---|
158 | #define G(L) (L->l_G) |
---|
159 | |
---|
160 | |
---|
161 | /* |
---|
162 | ** Union of all collectable objects |
---|
163 | */ |
---|
164 | union GCObject { |
---|
165 | GCheader gch; |
---|
166 | union TString ts; |
---|
167 | union Udata u; |
---|
168 | union Closure cl; |
---|
169 | struct Table h; |
---|
170 | struct Proto p; |
---|
171 | struct UpVal uv; |
---|
172 | struct lua_State th; /* thread */ |
---|
173 | }; |
---|
174 | |
---|
175 | |
---|
176 | /* macros to convert a GCObject into a specific value */ |
---|
177 | #define gcotots(o) check_exp((o)->gch.tt == LUA_TSTRING, &((o)->ts)) |
---|
178 | #define gcotou(o) check_exp((o)->gch.tt == LUA_TUSERDATA, &((o)->u)) |
---|
179 | #define gcotocl(o) check_exp((o)->gch.tt == LUA_TFUNCTION, &((o)->cl)) |
---|
180 | #define gcotoh(o) check_exp((o)->gch.tt == LUA_TTABLE, &((o)->h)) |
---|
181 | #define gcotop(o) check_exp((o)->gch.tt == LUA_TPROTO, &((o)->p)) |
---|
182 | #define gcotouv(o) check_exp((o)->gch.tt == LUA_TUPVAL, &((o)->uv)) |
---|
183 | #define ngcotouv(o) \ |
---|
184 | check_exp((o) == NULL || (o)->gch.tt == LUA_TUPVAL, &((o)->uv)) |
---|
185 | #define gcototh(o) check_exp((o)->gch.tt == LUA_TTHREAD, &((o)->th)) |
---|
186 | |
---|
187 | /* macro to convert any value into a GCObject */ |
---|
188 | #define valtogco(v) (cast(GCObject *, (v))) |
---|
189 | |
---|
190 | |
---|
191 | lua_State *luaE_newthread (lua_State *L); |
---|
192 | void luaE_freethread (lua_State *L, lua_State *L1); |
---|
193 | |
---|
194 | #endif |
---|
195 | |
---|