lstate.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. ** $Id: lstate.h,v 1.118 2003/12/04 17:22:42 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 ci_func(ci) (clvalue((ci)->base - 1))
  62. #define f_isLua(ci) (!ci_func(ci)->c.isC)
  63. #define isLua(ci) (ttisfunction((ci)->base - 1) && f_isLua(ci))
  64. /*
  65. ** `global state', shared by all threads of this state
  66. */
  67. typedef struct global_State {
  68. stringtable strt; /* hash table for strings */
  69. lua_Alloc realloc; /* function to reallocate memory */
  70. void *ud; /* auxiliary data to `realloc' */
  71. int currentwhite;
  72. GCObject *rootgc; /* list of all collectable objects */
  73. GCObject *firstudata; /* udata go to the end of `rootgc' */
  74. GCObject **sweepgc; /* position of sweep in `rootgc' */
  75. int sweepstrgc; /* position of sweep in `strt' */
  76. GCObject *gray; /* list of gray objects */
  77. GCObject *grayagain; /* list of objects to be traversed atomically */
  78. GCObject *weak; /* list of weak tables (to be cleared) */
  79. GCObject *tmudata; /* list of userdata to be GC */
  80. int gcstate; /* state of garbage collector */
  81. Mbuffer buff; /* temporary buffer for string concatentation */
  82. lu_mem GCthreshold;
  83. lu_mem nblocks; /* number of `bytes' currently allocated */
  84. lua_CFunction panic; /* to be called in unprotected errors */
  85. TObject _registry;
  86. struct lua_State *mainthread;
  87. Node dummynode[1]; /* common node array for all empty tables */
  88. TString *tmname[TM_N]; /* array with tag-method names */
  89. } global_State;
  90. /*
  91. ** `per thread' state
  92. */
  93. struct lua_State {
  94. CommonHeader;
  95. StkId top; /* first free slot in the stack */
  96. StkId base; /* base of current function */
  97. global_State *l_G;
  98. CallInfo *ci; /* call info for current function */
  99. StkId stack_last; /* last free slot in the stack */
  100. StkId stack; /* stack base */
  101. int stacksize;
  102. CallInfo *end_ci; /* points after end of ci array*/
  103. CallInfo *base_ci; /* array of CallInfo's */
  104. unsigned short size_ci; /* size of array `base_ci' */
  105. unsigned short nCcalls; /* number of nested C calls */
  106. lu_byte hookmask;
  107. lu_byte allowhook;
  108. lu_byte isSuspended;
  109. int basehookcount;
  110. int hookcount;
  111. lua_Hook hook;
  112. TObject _gt; /* table of globals */
  113. GCObject *openupval; /* list of open upvalues in this stack */
  114. GCObject *gclist;
  115. struct lua_longjmp *errorJmp; /* current error recover point */
  116. ptrdiff_t errfunc; /* current error handling function (stack index) */
  117. };
  118. #define G(L) (L->l_G)
  119. /*
  120. ** Union of all collectable objects
  121. */
  122. union GCObject {
  123. GCheader gch;
  124. union TString ts;
  125. union Udata u;
  126. union Closure cl;
  127. struct Table h;
  128. struct Proto p;
  129. struct UpVal uv;
  130. struct lua_State th; /* thread */
  131. };
  132. /* macros to convert a GCObject into a specific value */
  133. #define gcotots(o) check_exp((o)->gch.tt == LUA_TSTRING, &((o)->ts))
  134. #define gcotou(o) check_exp((o)->gch.tt == LUA_TUSERDATA, &((o)->u))
  135. #define gcotocl(o) check_exp((o)->gch.tt == LUA_TFUNCTION, &((o)->cl))
  136. #define gcotoh(o) check_exp((o)->gch.tt == LUA_TTABLE, &((o)->h))
  137. #define gcotop(o) check_exp((o)->gch.tt == LUA_TPROTO, &((o)->p))
  138. #define gcotouv(o) check_exp((o)->gch.tt == LUA_TUPVAL, &((o)->uv))
  139. #define ngcotouv(o) \
  140. check_exp((o) == NULL || (o)->gch.tt == LUA_TUPVAL, &((o)->uv))
  141. #define gcototh(o) check_exp((o)->gch.tt == LUA_TTHREAD, &((o)->th))
  142. /* macro to convert any value into a GCObject */
  143. #define valtogco(v) (cast(GCObject *, (v)))
  144. lua_State *luaE_newthread (lua_State *L);
  145. void luaE_freethread (lua_State *L, lua_State *L1);
  146. #endif