lstate.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. ** $Id: lstate.c,v 1.66 2001/07/17 17:54:46 roberto Exp $
  3. ** Global State
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdio.h>
  7. #define LUA_PRIVATE
  8. #include "lua.h"
  9. #include "ldo.h"
  10. #include "lgc.h"
  11. #include "llex.h"
  12. #include "lmem.h"
  13. #include "lstate.h"
  14. #include "lstring.h"
  15. #include "ltable.h"
  16. #include "ltm.h"
  17. struct Sopen {
  18. int stacksize;
  19. lua_State *L;
  20. };
  21. static void close_state (lua_State *L, lua_State *OL);
  22. /*
  23. ** open parts that may cause memory-allocation errors
  24. */
  25. static void f_luaopen (lua_State *L, void *ud) {
  26. struct Sopen *so = cast(struct Sopen *, ud);
  27. if (so->stacksize == 0)
  28. so->stacksize = DEFAULT_STACK_SIZE;
  29. else
  30. so->stacksize += LUA_MINSTACK;
  31. if (so->L != NULL) { /* shared global state? */
  32. L->G = G(so->L);
  33. L->gt = so->L->gt; /* share table of globals */
  34. so->L->next->previous = L; /* insert L into linked list */
  35. L->next = so->L->next;
  36. so->L->next = L;
  37. L->previous = so->L;
  38. luaD_init(L, so->stacksize); /* init stack */
  39. }
  40. else { /* create a new global state */
  41. L->G = luaM_new(L, global_State);
  42. G(L)->strt.size = 0;
  43. G(L)->strt.nuse = 0;
  44. G(L)->strt.hash = NULL;
  45. G(L)->Mbuffer = NULL;
  46. G(L)->Mbuffsize = 0;
  47. G(L)->rootproto = NULL;
  48. G(L)->rootcl = NULL;
  49. G(L)->roottable = NULL;
  50. G(L)->rootudata = NULL;
  51. G(L)->rootupval = NULL;
  52. G(L)->TMtable = NULL;
  53. G(L)->sizeTM = 0;
  54. G(L)->ntag = 0;
  55. G(L)->nblocks = sizeof(lua_State) + sizeof(global_State);
  56. luaD_init(L, so->stacksize); /* init stack */
  57. L->gt = luaH_new(L, 10); /* table of globals */
  58. G(L)->type2tag = luaH_new(L, 10);
  59. G(L)->registry = luaH_new(L, 0);
  60. G(L)->weakregistry = luaH_new(L, 0);
  61. /* make weakregistry weak */
  62. G(L)->weakregistry->weakmode = LUA_WEAK_KEY | LUA_WEAK_VALUE;
  63. luaS_resize(L, MINPOWER2);
  64. luaX_init(L);
  65. luaT_init(L);
  66. G(L)->GCthreshold = 4*G(L)->nblocks;
  67. }
  68. }
  69. LUA_API lua_State *lua_newthread (lua_State *OL, int stacksize) {
  70. struct Sopen so;
  71. lua_State *L;
  72. if (OL) lua_lock(OL);
  73. L = luaM_new(OL, lua_State);
  74. if (L) { /* allocation OK? */
  75. L->G = NULL;
  76. L->stack = NULL;
  77. L->stacksize = 0;
  78. L->ci = &L->basefunc;
  79. L->basefunc.prev = NULL;
  80. L->errorJmp = NULL;
  81. L->callhook = NULL;
  82. L->linehook = NULL;
  83. L->opencl = NULL;
  84. L->allowhooks = 1;
  85. L->next = L->previous = L;
  86. so.stacksize = stacksize;
  87. so.L = OL;
  88. if (luaD_runprotected(L, f_luaopen, &so) != 0) {
  89. /* memory allocation error: free partial state */
  90. close_state(L, OL);
  91. L = NULL;
  92. }
  93. }
  94. if (OL) lua_unlock(OL);
  95. return L;
  96. }
  97. static void close_state (lua_State *L, lua_State *OL) {
  98. if (OL != NULL) { /* are there other threads? */
  99. lua_assert(L->previous != L);
  100. L->previous->next = L->next;
  101. L->next->previous = L->previous;
  102. }
  103. else if (G(L)) { /* last thread; close global state */
  104. luaC_callallgcTM(L); /* call GC tag methods for all udata */
  105. luaC_collect(L, 1); /* collect all elements */
  106. lua_assert(G(L)->rootproto == NULL);
  107. lua_assert(G(L)->rootudata == NULL);
  108. lua_assert(G(L)->rootcl == NULL);
  109. lua_assert(G(L)->roottable == NULL);
  110. luaS_freeall(L);
  111. luaM_freearray(L, G(L)->TMtable, G(L)->sizeTM, struct TM);
  112. luaM_freearray(L, G(L)->Mbuffer, G(L)->Mbuffsize, l_char);
  113. luaM_freelem(NULL, L->G);
  114. }
  115. luaM_freearray(OL, L->stack, L->stacksize, TObject);
  116. luaM_freelem(OL, L);
  117. }
  118. LUA_API void lua_close (lua_State *L) {
  119. lua_State *OL;
  120. lua_assert(L != lua_state || lua_gettop(L) == 0);
  121. lua_lock(L);
  122. OL = L->next; /* any surviving thread (if there is one) */
  123. if (OL == L) OL = NULL; /* no surviving threads */
  124. close_state(L, OL);
  125. if (OL) lua_unlock(OL); /* cannot unlock over a freed state */
  126. lua_assert(L != lua_state || memdebug_numblocks == 0);
  127. lua_assert(L != lua_state || memdebug_total == 0);
  128. }