lua/lstate.h

Go to the documentation of this file.
00001 /*
00002 ** $Id: lstate.h,v 1.2 2004/03/23 05:09:14 jbj Exp $
00003 ** Global State
00004 ** See Copyright Notice in lua.h
00005 */
00006 
00007 #ifndef lstate_h
00008 #define lstate_h
00009 
00010 #include "lua.h"
00011 
00012 #include "lobject.h"
00013 #include "ltm.h"
00014 #include "lzio.h"
00015 
00016 
00017 /*
00018 ** macros for thread synchronization inside Lua core machine:
00019 ** all accesses to the global state and to global objects are synchronized.
00020 ** Because threads can read the stack of other threads
00021 ** (when running garbage collection),
00022 ** a thread must also synchronize any write-access to its own stack.
00023 ** Unsynchronized accesses are allowed only when reading its own stack,
00024 ** or when reading immutable fields from global objects
00025 ** (such as string values and udata values). 
00026 */
00027 #ifndef lua_lock
00028 #define lua_lock(L)     ((void) 0)
00029 #endif
00030 
00031 #ifndef lua_unlock
00032 #define lua_unlock(L)   ((void) 0)
00033 #endif
00034 
00035 
00036 #ifndef lua_userstateopen
00037 #define lua_userstateopen(l)
00038 #endif
00039 
00040 
00041 
00042 struct lua_longjmp;  /* defined in ldo.c */
00043 
00044 
00045 /* default meta table (both for tables and udata) */
00046 #define defaultmeta(L)  (&G(L)->_defaultmeta)
00047 
00048 /* table of globals */
00049 #define gt(L)   (&L->_gt)
00050 
00051 /* registry */
00052 #define registry(L)     (&G(L)->_registry)
00053 
00054 
00055 /* extra stack space to handle TM calls and some other extras */
00056 #define EXTRA_STACK   5
00057 
00058 
00059 #define BASIC_CI_SIZE           8
00060 
00061 #define BASIC_STACK_SIZE        (2*LUA_MINSTACK)
00062 
00063 
00064 
00065 typedef struct stringtable {
00066 /*@null@*/
00067   GCObject **hash;
00068   ls_nstr nuse;  /* number of elements */
00069   int size;
00070 } stringtable;
00071 
00072 
00073 /*
00074 ** informations about a call
00075 */
00076 typedef struct CallInfo {
00077 /*@dependent@*/ /*@relnull@*/
00078   StkId base;  /* base for called function */
00079 /*@dependent@*/ /*@relnull@*/
00080   StkId top;  /* top for this function */
00081   int state;  /* bit fields; see below */
00082   union {
00083     struct {  /* for Lua functions */
00084 /*@observer@*/
00085       const Instruction *savedpc;
00086 /*@observer@*/
00087       const Instruction **pc;  /* points to `pc' variable in `luaV_execute' */
00088       int tailcalls;  /* number of tail calls lost under this entry */
00089     } l;
00090     struct {  /* for C functions */
00091       int dummy;  /* just to avoid an empty struct */
00092     } c;
00093   } u;
00094 } CallInfo;
00095 
00096 
00097 /*
00098 ** bit fields for `CallInfo.state'
00099 */
00100 #define CI_C            (1<<0)  /* 1 if function is a C function */
00101 /* 1 if (Lua) function has an active `luaV_execute' running it */
00102 #define CI_HASFRAME     (1<<1)
00103 /* 1 if Lua function is calling another Lua function (and therefore its
00104    `pc' is being used by the other, and therefore CI_SAVEDPC is 1 too) */
00105 #define CI_CALLING      (1<<2)
00106 #define CI_SAVEDPC      (1<<3)  /* 1 if `savedpc' is updated */
00107 #define CI_YIELD        (1<<4)  /* 1 if thread is suspended */
00108 
00109 
00110 #define ci_func(ci)     (clvalue((ci)->base - 1))
00111 
00112 
00113 /*
00114 ** `global state', shared by all threads of this state
00115 */
00116 typedef struct global_State {
00117   stringtable strt;  /* hash table for strings */
00118 /*@owned@*/
00119   GCObject *rootgc;  /* list of (almost) all collectable objects */
00120 /*@dependent@*/ /*@null@*/
00121   GCObject *rootudata;   /* (separated) list of all userdata */
00122 /*@dependent@*/ /*@null@*/
00123   GCObject *tmudata;  /* list of userdata to be GC */
00124   Mbuffer buff;  /* temporary buffer for string concatentation */
00125   lu_mem GCthreshold;
00126   lu_mem nblocks;  /* number of `bytes' currently allocated */
00127   lua_CFunction panic;  /* to be called in unprotected errors */
00128   TObject _registry;
00129   TObject _defaultmeta;
00130   struct lua_State *mainthread;
00131   Node dummynode[1];  /* common node array for all empty tables */
00132   TString *tmname[TM_N];  /* array with tag-method names */
00133 } global_State;
00134 
00135 
00136 /*
00137 ** `per thread' state
00138 */
00139 struct lua_State {
00140   CommonHeader;
00141 /*@dependent@*/ /*@relnull@*/
00142   StkId top;  /* first free slot in the stack */
00143 /*@dependent@*/ /*@relnull@*/
00144   StkId base;  /* base of current function */
00145 /*@relnull@*/
00146   global_State *l_G;
00147 /*@dependent@*/ /*@relnull@*/
00148   CallInfo *ci;  /* call info for current function */
00149 /*@dependent@*/
00150   StkId stack_last;  /* last free slot in the stack */
00151 /*@owned@*/ /*@relnull@*/
00152   StkId stack;  /* stack base */
00153   int stacksize;
00154 /*@dependent@*/ /*@relnull@*/
00155   CallInfo *end_ci;  /* points after end of ci array*/
00156 /*@owned@*/ /*@relnull@*/
00157   CallInfo *base_ci;  /* array of CallInfo's */
00158   unsigned short size_ci;  /* size of array `base_ci' */
00159   unsigned short nCcalls;  /* number of nested C calls */
00160   lu_byte hookmask;
00161   lu_byte allowhook;
00162   lu_byte hookinit;
00163   int basehookcount;
00164   int hookcount;
00165 /*@relnull@*/
00166   lua_Hook hook;
00167   TObject _gt;  /* table of globals */
00168 /*@dependent@*/ /*@relnull@*/
00169   GCObject *openupval;  /* list of open upvalues in this stack */
00170 /*@dependent@*/ /*@relnull@*/
00171   GCObject *gclist;
00172 /*@relnull@*/
00173   struct lua_longjmp *errorJmp;  /* current error recover point */
00174   ptrdiff_t errfunc;  /* current error handling function (stack index) */
00175 };
00176 
00177 
00178 #define G(L)    (L->l_G)
00179 
00180 
00181 /*
00182 ** Union of all collectable objects
00183 */
00184 union GCObject {
00185   GCheader gch;
00186   union TString ts;
00187   union Udata u;
00188   union Closure cl;
00189   struct Table h;
00190   struct Proto p;
00191   struct UpVal uv;
00192   struct lua_State th;  /* thread */
00193 };
00194 
00195 
00196 /* macros to convert a GCObject into a specific value */
00197 #define gcotots(o)      check_exp((o)->gch.tt == LUA_TSTRING, &((o)->ts))
00198 #define gcotou(o)       check_exp((o)->gch.tt == LUA_TUSERDATA, &((o)->u))
00199 #define gcotocl(o)      check_exp((o)->gch.tt == LUA_TFUNCTION, &((o)->cl))
00200 #define gcotoh(o)       check_exp((o)->gch.tt == LUA_TTABLE, &((o)->h))
00201 #define gcotop(o)       check_exp((o)->gch.tt == LUA_TPROTO, &((o)->p))
00202 #define gcotouv(o)      check_exp((o)->gch.tt == LUA_TUPVAL, &((o)->uv))
00203 #define ngcotouv(o) \
00204         check_exp((o) == NULL || (o)->gch.tt == LUA_TUPVAL, &((o)->uv))
00205 #define gcototh(o)      check_exp((o)->gch.tt == LUA_TTHREAD, &((o)->th))
00206 
00207 /* macro to convert any value into a GCObject */
00208 #define valtogco(v)     (cast(GCObject *, (v)))
00209 
00210 
00211 /*@null@*/
00212 lua_State *luaE_newthread (lua_State *L)
00213         /*@modifies L @*/;
00214 void luaE_freethread (lua_State *L, lua_State *L1)
00215         /*@modifies L, L1 @*/;
00216 
00217 #endif
00218 

Generated on Fri Oct 12 08:44:54 2007 for rpm by  doxygen 1.5.2