lstate.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. ** $Id: lstate.c,v 1.65 2001/06/21 16:41:34 roberto Exp roberto $
  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 = (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)->TMtable = NULL;
  52. G(L)->sizeTM = 0;
  53. G(L)->ntag = 0;
  54. G(L)->nblocks = sizeof(lua_State) + sizeof(global_State);
  55. luaD_init(L, so->stacksize); /* init stack */
  56. L->gt = luaH_new(L, 10); /* table of globals */
  57. G(L)->type2tag = luaH_new(L, 10);
  58. G(L)->registry = luaH_new(L, 0);
  59. G(L)->weakregistry = luaH_new(L, 0);
  60. /* make weakregistry weak */
  61. G(L)->weakregistry->weakmode = LUA_WEAK_KEY | LUA_WEAK_VALUE;
  62. luaS_resize(L, MINPOWER2);
  63. luaX_init(L);
  64. luaT_init(L);
  65. G(L)->GCthreshold = 4*G(L)->nblocks;
  66. }
  67. }
  68. LUA_API lua_State *lua_newthread (lua_State *OL, int stacksize) {
  69. struct Sopen so;
  70. lua_State *L;
  71. if (OL) lua_lock(OL);
  72. L = luaM_new(OL, lua_State);
  73. if (L) { /* allocation OK? */
  74. L->G = NULL;
  75. L->stack = NULL;
  76. L->stacksize = 0;
  77. L->ci = &L->basefunc;
  78. L->basefunc.prev = NULL;
  79. L->errorJmp = NULL;
  80. L->callhook = NULL;
  81. L->linehook = NULL;
  82. L->allowhooks = 1;
  83. L->next = L->previous = L;
  84. so.stacksize = stacksize;
  85. so.L = OL;
  86. if (luaD_runprotected(L, f_luaopen, &so) != 0) {
  87. /* memory allocation error: free partial state */
  88. close_state(L, OL);
  89. L = NULL;
  90. }
  91. }
  92. if (OL) lua_unlock(OL);
  93. return L;
  94. }
  95. static void close_state (lua_State *L, lua_State *OL) {
  96. if (OL != NULL) { /* are there other threads? */
  97. lua_assert(L->previous != L);
  98. L->previous->next = L->next;
  99. L->next->previous = L->previous;
  100. }
  101. else if (G(L)) { /* last thread; close global state */
  102. luaC_callallgcTM(L); /* call GC tag methods for all udata */
  103. luaC_collect(L, 1); /* collect all elements */
  104. lua_assert(G(L)->rootproto == NULL);
  105. lua_assert(G(L)->rootudata == NULL);
  106. lua_assert(G(L)->rootcl == NULL);
  107. lua_assert(G(L)->roottable == NULL);
  108. luaS_freeall(L);
  109. luaM_freearray(L, G(L)->TMtable, G(L)->sizeTM, struct TM);
  110. luaM_freearray(L, G(L)->Mbuffer, G(L)->Mbuffsize, l_char);
  111. luaM_freelem(NULL, L->G, global_State);
  112. }
  113. luaM_freearray(OL, L->stack, L->stacksize, TObject);
  114. luaM_freelem(OL, L, lua_State);
  115. }
  116. LUA_API void lua_close (lua_State *L) {
  117. lua_State *OL;
  118. lua_assert(L != lua_state || lua_gettop(L) == 0);
  119. lua_lock(L);
  120. OL = L->next; /* any surviving thread (if there is one) */
  121. if (OL == L) OL = NULL; /* no surviving threads */
  122. close_state(L, OL);
  123. if (OL) lua_unlock(OL); /* cannot unlock over a freed state */
  124. lua_assert(L != lua_state || memdebug_numblocks == 0);
  125. lua_assert(L != lua_state || memdebug_total == 0);
  126. }