Main thread is a regular field of global_State · lua/lua@fa1382b · GitHub
Skip to content

Commit fa1382b

Browse files
committed
Main thread is a regular field of global_State
They were already allocated as a single block, so there is no need for the global_State to point to its main thread.
1 parent d1e677c commit fa1382b

6 files changed

Lines changed: 65 additions & 75 deletions

File tree

‎lapi.c‎

Lines changed: 1 addition & 1 deletion

‎ldo.c‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,12 @@ l_noret luaD_throw (lua_State *L, TStatus errcode) {
132132
}
133133
else { /* thread has no error handler */
134134
global_State *g = G(L);
135+
lua_State *mainth = mainthread(g);
135136
errcode = luaE_resetthread(L, errcode); /* close all upvalues */
136137
L->status = errcode;
137-
if (g->mainthread->errorJmp) { /* main thread has a handler? */
138-
setobjs2s(L, g->mainthread->top.p++, L->top.p - 1); /* copy error obj. */
139-
luaD_throw(g->mainthread, errcode); /* re-throw in main thread */
138+
if (mainth->errorJmp) { /* main thread has a handler? */
139+
setobjs2s(L, mainth->top.p++, L->top.p - 1); /* copy error obj. */
140+
luaD_throw(mainth, errcode); /* re-throw in main thread */
140141
}
141142
else { /* no handler at all; abort */
142143
if (g->panic) { /* panic function? */
@@ -961,7 +962,7 @@ LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx,
961962
ci = L->ci;
962963
api_checkpop(L, nresults);
963964
if (l_unlikely(!yieldable(L))) {
964-
if (L != G(L)->mainthread)
965+
if (L != mainthread(G(L)))
965966
luaG_runerror(L, "attempt to yield across a C-call boundary");
966967
else
967968
luaG_runerror(L, "attempt to yield from outside a coroutine");

‎lgc.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
((*getArrTag(t,i) & BIT_ISCOLLECTABLE) ? getArrVal(t,i)->gc : NULL)
7979

8080

81-
#define markvalue(g,o) { checkliveness(g->mainthread,o); \
81+
#define markvalue(g,o) { checkliveness(mainthread(g),o); \
8282
if (valiswhite(o)) reallymarkobject(g,gcvalue(o)); }
8383

8484
#define markkey(g, n) { if keyiswhite(n) reallymarkobject(g,gckey(n)); }
@@ -441,7 +441,7 @@ static void cleargraylists (global_State *g) {
441441
static void restartcollection (global_State *g) {
442442
cleargraylists(g);
443443
g->GCmarked = 0;
444-
markobject(g, g->mainthread);
444+
markobject(g, mainthread(g));
445445
markvalue(g, &g->l_registry);
446446
markmt(g);
447447
markbeingfnz(g); /* mark any finalizing object left from previous cycle */
@@ -1513,7 +1513,7 @@ void luaC_freeallobjects (lua_State *L) {
15131513
separatetobefnz(g, 1); /* separate all objects with finalizers */
15141514
lua_assert(g->finobj == NULL);
15151515
callallpendingfinalizers(L);
1516-
deletelist(L, g->allgc, obj2gco(g->mainthread));
1516+
deletelist(L, g->allgc, obj2gco(mainthread(g)));
15171517
lua_assert(g->finobj == NULL); /* no new finalizers */
15181518
deletelist(L, g->fixedgc, NULL); /* collect fixed objects */
15191519
lua_assert(g->strt.nuse == 0);
@@ -1526,7 +1526,7 @@ static void atomic (lua_State *L) {
15261526
GCObject *grayagain = g->grayagain; /* save original list */
15271527
g->grayagain = NULL;
15281528
lua_assert(g->ephemeron == NULL && g->weak == NULL);
1529-
lua_assert(!iswhite(g->mainthread));
1529+
lua_assert(!iswhite(mainthread(g)));
15301530
g->gcstate = GCSatomic;
15311531
markobject(g, L); /* mark running thread */
15321532
/* registry and global metatables may be changed by API */

‎lstate.c‎

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,6 @@
2929

3030

3131

32-
/*
33-
** thread state + extra space
34-
*/
35-
typedef struct LX {
36-
lu_byte extra_[LUA_EXTRASPACE];
37-
lua_State l;
38-
} LX;
39-
40-
41-
/*
42-
** Main thread combines a thread state and the global state
43-
*/
44-
typedef struct LG {
45-
LX l;
46-
global_State g;
47-
} LG;
48-
49-
50-
5132
#define fromstate(L) (cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l)))
5233

5334

@@ -278,8 +259,8 @@ static void close_state (lua_State *L) {
278259
}
279260
luaM_freearray(L, G(L)->strt.hash, cast_sizet(G(L)->strt.size));
280261
freestack(L);
281-
lua_assert(gettotalbytes(g) == sizeof(LG));
282-
(*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */
262+
lua_assert(gettotalbytes(g) == sizeof(global_State));
263+
(*g->frealloc)(g->ud, g, sizeof(global_State), 0); /* free main block */
283264
}
284265

285266

@@ -301,7 +282,7 @@ LUA_API lua_State *lua_newthread (lua_State *L) {
301282
L1->hook = L->hook;
302283
resethookcount(L1);
303284
/* initialize L1 extra space */
304-
memcpy(lua_getextraspace(L1), lua_getextraspace(g->mainthread),
285+
memcpy(lua_getextraspace(L1), lua_getextraspace(mainthread(g)),
305286
LUA_EXTRASPACE);
306287
luai_userstatethread(L, L1);
307288
stack_init(L1, L); /* init stack */
@@ -352,11 +333,10 @@ LUA_API int lua_closethread (lua_State *L, lua_State *from) {
352333
LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud, unsigned seed) {
353334
int i;
354335
lua_State *L;
355-
global_State *g;
356-
LG *l = cast(LG *, (*f)(ud, NULL, LUA_TTHREAD, sizeof(LG)));
357-
if (l == NULL) return NULL;
358-
L = &l->l.l;
359-
g = &l->g;
336+
global_State *g = cast(global_State*,
337+
(*f)(ud, NULL, LUA_TTHREAD, sizeof(global_State)));
338+
if (g == NULL) return NULL;
339+
L = &g->mainth.l;
360340
L->tt = LUA_VTHREAD;
361341
g->currentwhite = bitmask(WHITE0BIT);
362342
L->marked = luaC_white(g);
@@ -368,7 +348,6 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud, unsigned seed) {
368348
g->ud = ud;
369349
g->warnf = NULL;
370350
g->ud_warn = NULL;
371-
g->mainthread = L;
372351
g->seed = seed;
373352
g->gcstp = GCSTPGC; /* no GC while building state */
374353
g->strt.size = g->strt.nuse = 0;
@@ -386,7 +365,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud, unsigned seed) {
386365
g->gray = g->grayagain = NULL;
387366
g->weak = g->ephemeron = g->allweak = NULL;
388367
g->twups = NULL;
389-
g->GCtotalbytes = sizeof(LG);
368+
g->GCtotalbytes = sizeof(global_State);
390369
g->GCmarked = 0;
391370
g->GCdebt = 0;
392371
setivalue(&g->nilvalue, 0); /* to signal that state is not yet built */
@@ -408,7 +387,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud, unsigned seed) {
408387

409388
LUA_API void lua_close (lua_State *L) {
410389
lua_lock(L);
411-
L = G(L)->mainthread; /* only the main thread can be closed */
390+
L = mainthread(G(L)); /* only the main thread can be closed */
412391
close_state(L);
413392
}
414393

‎lstate.h‎

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,48 @@ struct CallInfo {
283283
#define getoah(ci) (((ci)->callstatus & CIST_OAH) ? 1 : 0)
284284

285285

286+
/*
287+
** 'per thread' state
288+
*/
289+
struct lua_State {
290+
CommonHeader;
291+
lu_byte allowhook;
292+
TStatus status;
293+
unsigned short nci; /* number of items in 'ci' list */
294+
StkIdRel top; /* first free slot in the stack */
295+
struct global_State *l_G;
296+
CallInfo *ci; /* call info for current function */
297+
StkIdRel stack_last; /* end of stack (last element + 1) */
298+
StkIdRel stack; /* stack base */
299+
UpVal *openupval; /* list of open upvalues in this stack */
300+
StkIdRel tbclist; /* list of to-be-closed variables */
301+
GCObject *gclist;
302+
struct lua_State *twups; /* list of threads with open upvalues */
303+
struct lua_longjmp *errorJmp; /* current error recover point */
304+
CallInfo base_ci; /* CallInfo for first level (C host) */
305+
volatile lua_Hook hook;
306+
ptrdiff_t errfunc; /* current error handling function (stack index) */
307+
l_uint32 nCcalls; /* number of nested non-yieldable or C calls */
308+
int oldpc; /* last pc traced */
309+
int basehookcount;
310+
int hookcount;
311+
volatile l_signalT hookmask;
312+
struct { /* info about transferred values (for call/return hooks) */
313+
int ftransfer; /* offset of first value transferred */
314+
int ntransfer; /* number of values transferred */
315+
} transferinfo;
316+
};
317+
318+
319+
/*
320+
** thread state + extra space
321+
*/
322+
typedef struct LX {
323+
lu_byte extra_[LUA_EXTRASPACE];
324+
lua_State l;
325+
} LX;
326+
327+
286328
/*
287329
** 'global state', shared by all threads of this state
288330
*/
@@ -324,50 +366,18 @@ typedef struct global_State {
324366
GCObject *finobjrold; /* list of really old objects with finalizers */
325367
struct lua_State *twups; /* list of threads with open upvalues */
326368
lua_CFunction panic; /* to be called in unprotected errors */
327-
struct lua_State *mainthread;
328369
TString *memerrmsg; /* message for memory-allocation errors */
329370
TString *tmname[TM_N]; /* array with tag-method names */
330371
struct Table *mt[LUA_NUMTYPES]; /* metatables for basic types */
331372
TString *strcache[STRCACHE_N][STRCACHE_M]; /* cache for strings in API */
332373
lua_WarnFunction warnf; /* warning function */
333374
void *ud_warn; /* auxiliary data to 'warnf' */
375+
LX mainth; /* main thread of this state */
334376
} global_State;
335377

336378

337-
/*
338-
** 'per thread' state
339-
*/
340-
struct lua_State {
341-
CommonHeader;
342-
lu_byte allowhook;
343-
TStatus status;
344-
unsigned short nci; /* number of items in 'ci' list */
345-
StkIdRel top; /* first free slot in the stack */
346-
global_State *l_G;
347-
CallInfo *ci; /* call info for current function */
348-
StkIdRel stack_last; /* end of stack (last element + 1) */
349-
StkIdRel stack; /* stack base */
350-
UpVal *openupval; /* list of open upvalues in this stack */
351-
StkIdRel tbclist; /* list of to-be-closed variables */
352-
GCObject *gclist;
353-
struct lua_State *twups; /* list of threads with open upvalues */
354-
struct lua_longjmp *errorJmp; /* current error recover point */
355-
CallInfo base_ci; /* CallInfo for first level (C host) */
356-
volatile lua_Hook hook;
357-
ptrdiff_t errfunc; /* current error handling function (stack index) */
358-
l_uint32 nCcalls; /* number of nested non-yieldable or C calls */
359-
int oldpc; /* last pc traced */
360-
int basehookcount;
361-
int hookcount;
362-
volatile l_signalT hookmask;
363-
struct { /* info about transferred values (for call/return hooks) */
364-
int ftransfer; /* offset of first value transferred */
365-
int ntransfer; /* number of values transferred */
366-
} transferinfo;
367-
};
368-
369-
370379
#define G(L) (L->l_G)
380+
#define mainthread(G) (&(G)->mainth.l)
371381

372382
/*
373383
** 'g->nilvalue' being a nil value flags that the state was completely

‎ltests.c‎

Lines changed: 2 additions & 2 deletions

0 commit comments

Comments
 (0)