Browse Source

better control for GC running or stopped

Roberto Ierusalimschy 14 years ago
parent
commit
737f119187
6 changed files with 31 additions and 31 deletions
  1. 8 6
      lapi.c
  2. 12 9
      lgc.c
  3. 1 5
      lgc.h
  4. 5 8
      lmem.c
  5. 3 2
      lstate.c
  6. 2 1
      lstate.h

+ 8 - 6
lapi.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lapi.c,v 2.141 2010/11/18 19:15:00 roberto Exp roberto $
+** $Id: lapi.c,v 2.141 2010/11/26 14:32:31 roberto Exp roberto $
 ** Lua API
 ** Lua API
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -960,14 +960,16 @@ LUA_API int lua_gc (lua_State *L, int what, int data) {
   g = G(L);
   g = G(L);
   switch (what) {
   switch (what) {
     case LUA_GCSTOP: {
     case LUA_GCSTOP: {
-      stopgc(g);
+      g->gcrunning = 0;
       break;
       break;
     }
     }
     case LUA_GCRESTART: {
     case LUA_GCRESTART: {
       g->GCdebt = 0;
       g->GCdebt = 0;
+      g->gcrunning = 1;
       break;
       break;
     }
     }
     case LUA_GCCOLLECT: {
     case LUA_GCCOLLECT: {
+      g->gcrunning = 1;  /* restart collector if stopped ?? */
       luaC_fullgc(L, 0);
       luaC_fullgc(L, 0);
       break;
       break;
     }
     }
@@ -981,7 +983,8 @@ LUA_API int lua_gc (lua_State *L, int what, int data) {
       break;
       break;
     }
     }
     case LUA_GCSTEP: {
     case LUA_GCSTEP: {
-      int stopped = gcstopped(g);
+      int running = g->gcrunning;
+      g->gcrunning = 1;  /* allow steps */
       if (g->gckind == KGC_GEN) {  /* generational mode? */
       if (g->gckind == KGC_GEN) {  /* generational mode? */
         res = (g->lastmajormem == 0);  /* 1 if will do major collection */
         res = (g->lastmajormem == 0);  /* 1 if will do major collection */
         luaC_step(L);  /* do a single step */
         luaC_step(L);  /* do a single step */
@@ -995,8 +998,7 @@ LUA_API int lua_gc (lua_State *L, int what, int data) {
           }
           }
         }
         }
       }
       }
-      if (stopped)  /* collector was stopped? */
-        stopgc(g);  /* keep it that way */
+      g->gcrunning = running;  /* restore previous state */
       break;
       break;
     }
     }
     case LUA_GCSETPAUSE: {
     case LUA_GCSETPAUSE: {
@@ -1015,7 +1017,7 @@ LUA_API int lua_gc (lua_State *L, int what, int data) {
       break;
       break;
     }
     }
     case LUA_GCISRUNNING: {
     case LUA_GCISRUNNING: {
-      res = !gcstopped(g);
+      res = g->gcrunning;
       break;
       break;
     }
     }
     case LUA_GCGEN: {  /* change collector to generational mode */
     case LUA_GCGEN: {  /* change collector to generational mode */

+ 12 - 9
lgc.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lgc.c,v 2.104 2010/11/26 14:32:31 roberto Exp roberto $
+** $Id: lgc.c,v 2.105 2010/12/03 11:48:25 roberto Exp roberto $
 ** Garbage Collector
 ** Garbage Collector
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -715,15 +715,15 @@ static void GCTM (lua_State *L, int propagateerrors) {
   if (tm != NULL && ttisfunction(tm)) {  /* is there a finalizer? */
   if (tm != NULL && ttisfunction(tm)) {  /* is there a finalizer? */
     int status;
     int status;
     lu_byte oldah = L->allowhook;
     lu_byte oldah = L->allowhook;
-    lu_mem oldd = g->GCdebt;
+    int running  = g->gcrunning;
     L->allowhook = 0;  /* stop debug hooks during GC tag method */
     L->allowhook = 0;  /* stop debug hooks during GC tag method */
-    stopgc(g);  /* avoid GC steps */
+    g->gcrunning = 0;  /* avoid GC steps */
     setobj2s(L, L->top, tm);  /* push finalizer... */
     setobj2s(L, L->top, tm);  /* push finalizer... */
     setobj2s(L, L->top + 1, &v);  /* ... and its argument */
     setobj2s(L, L->top + 1, &v);  /* ... and its argument */
     L->top += 2;  /* and (next line) call the finalizer */
     L->top += 2;  /* and (next line) call the finalizer */
     status = luaD_pcall(L, dothecall, NULL, savestack(L, L->top - 2), 0);
     status = luaD_pcall(L, dothecall, NULL, savestack(L, L->top - 2), 0);
     L->allowhook = oldah;  /* restore hooks */
     L->allowhook = oldah;  /* restore hooks */
-    g->GCdebt = oldd;  /* restore threshold */
+    g->gcrunning = running;  /* restore state */
     if (status != LUA_OK && propagateerrors) {  /* error while running __gc? */
     if (status != LUA_OK && propagateerrors) {  /* error while running __gc? */
       if (status == LUA_ERRRUN) {  /* is there an error msg.? */
       if (status == LUA_ERRRUN) {  /* is there an error msg.? */
         luaO_pushfstring(L, "error in __gc tag method (%s)",
         luaO_pushfstring(L, "error in __gc tag method (%s)",
@@ -984,11 +984,14 @@ static void step (lua_State *L) {
 
 
 
 
 void luaC_step (lua_State *L) {
 void luaC_step (lua_State *L) {
-  int i;
-  if (isgenerational(G(L))) generationalcollection(L);
-  else step(L);
-  for (i = 0; i < GCFINALIZENUM && G(L)->tobefnz; i++)
-    GCTM(L, 1);  /* Call a few pending finalizers */
+  global_State *g = G(L);
+  if (g->gcrunning) {
+    int i;
+    if (isgenerational(g)) generationalcollection(L);
+    else step(L);
+    for (i = 0; i < GCFINALIZENUM && g->tobefnz; i++)
+      GCTM(L, 1);  /* Call a few pending finalizers */
+  }
 }
 }
 
 
 
 

+ 1 - 5
lgc.h

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lgc.h,v 2.46 2010/12/02 19:51:15 roberto Exp roberto $
+** $Id: lgc.h,v 2.47 2010/12/17 12:02:29 roberto Exp roberto $
 ** Garbage Collector
 ** Garbage Collector
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -52,10 +52,6 @@
 #define keepinvariant(g)  (isgenerational(g) || g->gcstate <= GCSatomic)
 #define keepinvariant(g)  (isgenerational(g) || g->gcstate <= GCSatomic)
 
 
 
 
-#define gcstopped(g)	((g)->GCdebt == MIN_LMEM)
-#define stopgc(g)	((g)->GCdebt = MIN_LMEM)
-
-
 /*
 /*
 ** some useful bit tricks
 ** some useful bit tricks
 */
 */

+ 5 - 8
lmem.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lmem.c,v 1.78 2010/05/04 18:10:02 roberto Exp roberto $
+** $Id: lmem.c,v 1.79 2010/05/05 18:49:56 roberto Exp roberto $
 ** Interface to Memory Manager
 ** Interface to Memory Manager
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -79,14 +79,14 @@ void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {
   size_t realosize = (block) ? osize : 0;
   size_t realosize = (block) ? osize : 0;
   lua_assert((realosize == 0) == (block == NULL));
   lua_assert((realosize == 0) == (block == NULL));
 #if defined(HARDMEMTESTS)
 #if defined(HARDMEMTESTS)
-  if (nsize > realosize && !gcstopped(g))
+  if (nsize > realosize && g->gcrunning)
     luaC_fullgc(L, 1);  /* force a GC whenever possible */
     luaC_fullgc(L, 1);  /* force a GC whenever possible */
 #endif
 #endif
   newblock = (*g->frealloc)(g->ud, block, osize, nsize);
   newblock = (*g->frealloc)(g->ud, block, osize, nsize);
   if (newblock == NULL && nsize > 0) {
   if (newblock == NULL && nsize > 0) {
     api_check(L, nsize > realosize,
     api_check(L, nsize > realosize,
                  "realloc cannot fail when shrinking a block");
                  "realloc cannot fail when shrinking a block");
-    if (!gcstopped(g)) {
+    if (g->gcrunning) {
       luaC_fullgc(L, 1);  /* try to free some memory... */
       luaC_fullgc(L, 1);  /* try to free some memory... */
       newblock = (*g->frealloc)(g->ud, block, osize, nsize);  /* try again */
       newblock = (*g->frealloc)(g->ud, block, osize, nsize);  /* try again */
     }
     }
@@ -95,8 +95,7 @@ void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {
   }
   }
   lua_assert((nsize == 0) == (newblock == NULL));
   lua_assert((nsize == 0) == (newblock == NULL));
   g->totalbytes = (g->totalbytes - realosize) + nsize;
   g->totalbytes = (g->totalbytes - realosize) + nsize;
-  if (!gcstopped(g))
-    g->GCdebt += nsize;  /* give some credit to garbage collector */
+  g->GCdebt += nsize;  /* give some credit to garbage collector */
 #if defined(TRACEMEM)
 #if defined(TRACEMEM)
   { /* auxiliary patch to monitor garbage collection.
   { /* auxiliary patch to monitor garbage collection.
     ** To plot, gnuplot with following command:
     ** To plot, gnuplot with following command:
@@ -108,9 +107,7 @@ void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) {
     if ((total % 200) == 0) {
     if ((total % 200) == 0) {
       if (f == NULL) f = fopen(TRACEMEM, "w");
       if (f == NULL) f = fopen(TRACEMEM, "w");
       fprintf(f, "%lu %u %d %d\n", total,
       fprintf(f, "%lu %u %d %d\n", total,
-              g->totalbytes,
-              gcstopped(g) ? 0 : g->GCdebt,
-              g->gcstate * 1000);
+              g->totalbytes, g->GCdebt, g->gcstate * 1000);
     }
     }
   }
   }
 #endif
 #endif

+ 3 - 2
lstate.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lstate.c,v 2.86 2010/09/03 14:14:01 roberto Exp roberto $
+** $Id: lstate.c,v 2.87 2010/11/26 14:32:31 roberto Exp roberto $
 ** Global State
 ** Global State
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -155,6 +155,7 @@ static void f_luaopen (lua_State *L, void *ud) {
   g->memerrmsg = luaS_newliteral(L, MEMERRMSG);
   g->memerrmsg = luaS_newliteral(L, MEMERRMSG);
   luaS_fix(g->memerrmsg);  /* it should never be collected */
   luaS_fix(g->memerrmsg);  /* it should never be collected */
   g->GCdebt = 0;
   g->GCdebt = 0;
+  g->gcrunning = 1;  /* allow gc */
 }
 }
 
 
 
 
@@ -241,7 +242,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
   g->mainthread = L;
   g->mainthread = L;
   g->uvhead.u.l.prev = &g->uvhead;
   g->uvhead.u.l.prev = &g->uvhead;
   g->uvhead.u.l.next = &g->uvhead;
   g->uvhead.u.l.next = &g->uvhead;
-  stopgc(g);  /* no GC while building state */
+  g->gcrunning = 0;  /* no GC while building state */
   g->lastmajormem = 0;
   g->lastmajormem = 0;
   g->strt.size = 0;
   g->strt.size = 0;
   g->strt.nuse = 0;
   g->strt.nuse = 0;

+ 2 - 1
lstate.h

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lstate.h,v 2.68 2010/10/29 17:52:46 roberto Exp roberto $
+** $Id: lstate.h,v 2.69 2010/11/26 14:32:31 roberto Exp roberto $
 ** Global State
 ** Global State
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -123,6 +123,7 @@ typedef struct global_State {
   lu_byte currentwhite;
   lu_byte currentwhite;
   lu_byte gcstate;  /* state of garbage collector */
   lu_byte gcstate;  /* state of garbage collector */
   lu_byte gckind;  /* kind of GC running */
   lu_byte gckind;  /* kind of GC running */
+  lu_byte gcrunning;  /* true if GC is running */
   int sweepstrgc;  /* position of sweep in `strt' */
   int sweepstrgc;  /* position of sweep in `strt' */
   GCObject *allgc;  /* list of all collectable objects */
   GCObject *allgc;  /* list of all collectable objects */
   GCObject *finobj;  /* list of collectable objects with finalizers */
   GCObject *finobj;  /* list of collectable objects with finalizers */