lstate.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. ** $Id: lstate.h,v 2.1 2003/12/10 12:13:36 roberto Exp roberto $
  3. ** Global State
  4. ** See Copyright Notice in lua.h
  5. */
  6. #ifndef lstate_h
  7. #define lstate_h
  8. #include "lua.h"
  9. #include "lobject.h"
  10. #include "ltm.h"
  11. #include "lzio.h"
  12. /*
  13. ** macros for thread synchronization inside Lua core machine:
  14. ** all accesses to the global state and to global objects are synchronized.
  15. ** Because threads can read the stack of other threads
  16. ** (when running garbage collection),
  17. ** a thread must also synchronize any write-access to its own stack.
  18. ** Unsynchronized accesses are allowed only when reading its own stack,
  19. ** or when reading immutable fields from global objects
  20. ** (such as string values and udata values).
  21. */
  22. #ifndef lua_lock
  23. #define lua_lock(L) ((void) 0)
  24. #endif
  25. #ifndef lua_unlock
  26. #define lua_unlock(L) ((void) 0)
  27. #endif
  28. #ifndef lua_userstateopen
  29. #define lua_userstateopen(l)
  30. #endif
  31. struct lua_longjmp; /* defined in ldo.c */
  32. /* table of globals */
  33. #define gt(L) (&L->_gt)
  34. /* registry */
  35. #define registry(L) (&G(L)->_registry)
  36. /* extra stack space to handle TM calls and some other extras */
  37. #define EXTRA_STACK 5
  38. #define BASIC_CI_SIZE 8
  39. #define BASIC_STACK_SIZE (2*LUA_MINSTACK)
  40. typedef struct stringtable {
  41. GCObject **hash;
  42. lu_int32 nuse; /* number of elements */
  43. int size;
  44. } stringtable;
  45. /*
  46. ** informations about a call
  47. */
  48. typedef struct CallInfo {
  49. StkId base; /* base for called function */
  50. StkId top; /* top for this function */
  51. union {
  52. struct { /* for Lua functions */
  53. const Instruction *savedpc;
  54. int tailcalls; /* number of tail calls lost under this entry */
  55. } l;
  56. struct { /* for C functions */
  57. int dummy; /* just to avoid an empty struct */
  58. } c;
  59. } u;
  60. } CallInfo;
  61. #define curr_func(L) (clvalue(L->base - 1))
  62. #define ci_func(ci) (clvalue((ci)->base - 1))
  63. #define f_isLua(ci) (!ci_func(ci)->c.isC)
  64. #define isLua(ci) (ttisfunction((ci)->base - 1) && f_isLua(ci))
  65. /*
  66. ** `global state', shared by all threads of this state
  67. */
  68. typedef struct global_State {
  69. stringtable strt; /* hash table for strings */
  70. lua_Alloc realloc; /* function to reallocate memory */
  71. void *ud; /* auxiliary data to `realloc' */
  72. int currentwhite;
  73. GCObject *rootgc; /* list of all collectable objects */
  74. GCObject *firstudata; /* udata go to the end of `rootgc' */
  75. GCObject **sweepgc; /* position of sweep in `rootgc' */
  76. int sweepstrgc; /* position of sweep in `strt' */
  77. GCObject *gray; /* list of gray objects */
  78. GCObject *grayagain; /* list of objects to be traversed atomically */
  79. GCObject *weak; /* list of weak tables (to be cleared) */
  80. GCObject *tmudata; /* list of userdata to be GC */
  81. int gcstate; /* state of garbage collector */
  82. Mbuffer buff; /* temporary buffer for string concatentation */
  83. lu_mem GCthreshold;
  84. lu_mem nblocks; /* number of `bytes' currently allocated */
  85. lua_CFunction panic; /* to be called in unprotected errors */
  86. TValue _registry;
  87. struct lua_State *mainthread;
  88. Node dummynode[1]; /* common node array for all empty tables */
  89. TString *tmname[TM_N]; /* array with tag-method names */
  90. } global_State;
  91. /*
  92. ** `per thread' state
  93. */
  94. struct lua_State {
  95. CommonHeader;
  96. StkId top; /* first free slot in the stack */
  97. StkId base; /* base of current function */
  98. global_State *l_G;
  99. CallInfo *ci; /* call info for current function */
  100. StkId stack_last; /* last free slot in the stack */
  101. StkId stack; /* stack base */
  102. int stacksize;
  103. CallInfo *end_ci; /* points after end of ci array*/
  104. CallInfo *base_ci; /* array of CallInfo's */
  105. unsigned short size_ci; /* size of array `base_ci' */
  106. unsigned short nCcalls; /* number of nested C calls */
  107. lu_byte hookmask;
  108. lu_byte allowhook;
  109. lu_byte isSuspended;
  110. int basehookcount;
  111. int hookcount;
  112. lua_Hook hook;
  113. TValue _gt; /* table of globals */
  114. GCObject *openupval; /* list of open upvalues in this stack */
  115. GCObject *gclist;
  116. struct lua_longjmp *errorJmp; /* current error recover point */
  117. ptrdiff_t errfunc; /* current error handling function (stack index) */
  118. };
  119. #define G(L) (L->l_G)
  120. /*
  121. ** Union of all collectable objects
  122. */
  123. union GCObject {
  124. GCheader gch;
  125. union TString ts;
  126. union Udata u;
  127. union Closure cl;
  128. struct Table h;
  129. struct Proto p;
  130. struct UpVal uv;
  131. struct lua_State th; /* thread */
  132. };
  133. /* macros to convert a GCObject into a specific value */
  134. #define rawgco2ts(o) check_exp((o)->gch.tt == LUA_TSTRING, &((o)->ts))
  135. #define gco2ts(o) (&rawgco2ts(o)->tsv)
  136. #define rawgco2u(o) check_exp((o)->gch.tt == LUA_TUSERDATA, &((o)->u))
  137. #define gco2u(o) (&rawgco2u(o)->uv)
  138. #define gco2cl(o) check_exp((o)->gch.tt == LUA_TFUNCTION, &((o)->cl))
  139. #define gco2h(o) check_exp((o)->gch.tt == LUA_TTABLE, &((o)->h))
  140. #define gco2p(o) check_exp((o)->gch.tt == LUA_TPROTO, &((o)->p))
  141. #define gco2uv(o) check_exp((o)->gch.tt == LUA_TUPVAL, &((o)->uv))
  142. #define ngcotouv(o) \
  143. check_exp((o) == NULL || (o)->gch.tt == LUA_TUPVAL, &((o)->uv))
  144. #define gco2th(o) check_exp((o)->gch.tt == LUA_TTHREAD, &((o)->th))
  145. /* macro to convert any Lua object into a GCObject */
  146. #define obj2gco(v) (cast(GCObject *, (v)))
  147. lua_State *luaE_newthread (lua_State *L);
  148. void luaE_freethread (lua_State *L, lua_State *L1);
  149. #endif