Lua中的线程和状态

1、概述
    线程(thread)作为Lua中一种基本的数据类型,它代表独立的执行线程(independent threads of execution),线程类型是实现协程(coroutines)的基础,注意这里的线程类型不要与操作系统线程混淆,Lua的线程类型是Lua虚拟机实现一种数据类型。
从Lua脚本来看,一个协程就是一个线程类型,比如:
[cpp]  view plain  copy
  1. local co = coroutine.create(function() print("hi") end)  
  2. print(co)  --output: thread: 0038BEE0  
    准确来说,协程是一个线程外加一套良好的操作接口。
从实现角度来看,一个线程类型数据就是一个Lua与C交互的栈,每个栈包含函数调用链和数据栈,还有独立的调试钩子和错误信息。关于Lua与C交互的栈的可以参考之前写的一篇 文章 。线程类型数据与table数据类型类似,它也是需要GC来管理的。
    当调用Lua C API中的大多数函数时,这些函数都是在特定的栈(或线程)上,因此在很多API中,第一个参数是lua_State*(线程类型对应的数据结构就是lua_State,或者说交互的栈就是用结构体lua_State来实现的),这个参数不仅表示了一个Lua状态,还表示一个记录在该状态中的线程。注意这里的状态,对一个虚拟机来说只有一个,而一个虚拟机可以包括多个线程(或者说交互栈)。这个状态也就是虚拟机的全局状态,我们可以通过调用函数luaL_newstate()来创建一个虚拟机的状态,该函数声明如下:
[cpp]  view plain  copy
  1. lua_State *lua_newstate (lua_Alloc f, void *ud);  
没错,在应用程序(比如用C++编写的)中,为了加载和执行Lua脚本,首先要调用的函数就是这个函数来初始化虚拟机。该函数在初始化虚拟机状态的同时,还是创建整个虚拟机的第一个线程(称为主线程),或者说是第一个交互栈。为了在已经初始化的全局状态中创建一个新的线程(或交互栈)可以调用函数lua_newthread,该函数声明如下:

[cpp]  view plain  copy
  1. lua_State *lua_newthread (lua_State *L);  
创建一个线程就拥有一个独立的执行栈了,但是它与其线程共用虚拟机的全局状态。Lua中没有函数去close或destroy 一个线程,创建的线程类型数据与其他GC对象一样,由虚拟机来管理销毁。

    总之,一个Lua虚拟机只有一个全局的状态,但可以包含多个执行环境(或者说多个线程、交互栈,从脚本角度来说,也可以说是多个协程),也就是说多个执行环境共享一个全局状态。如下图所示:


下面将通过Lua 5.2.1的源码来看全局状态的数据结构global_State和脚本执行的相关的上下文环境结构lua_State,以及函数lua_newstate和lua_newthread的实现。


2、源码实现
    首先来分析全局状态的结构体global_State的代码(lstate.h):

[cpp]  view plain  copy
  1. 109 /*                                                                                                  
  2. 110 ** `global state', shared by all threads of this state                                              
  3. 111 */                                                                                                   
  4. 112 typedef struct global_State {                                                                        
  5. 113   lua_Alloc frealloc;  /* function to reallocate memory */                                           
  6. 114   void *ud;         /* auxiliary data to `frealloc' */                                               
  7. 115   lu_mem totalbytes;  /* number of bytes currently allocated - GCdebt */                             
  8. 116   l_mem GCdebt;  /* bytes allocated not yet compensated by the collector */                          
  9. 117   lu_mem GCmemtrav;  /* memory traversed by the GC */                                                
  10. 118   lu_mem GCestimate;  /* an estimate of the non-garbage memory in use */                             
  11. 119   stringtable strt;  /* hash table for strings */                                                    
  12. 120   TValue l_registry;                                                                                 
  13. 121   unsigned int seed;  /* randomized seed for hashes */                                               
  14. 122   lu_byte currentwhite;                                                                              
  15. 123   lu_byte gcstate;  /* state of garbage collector */                                                 
  16. 124   lu_byte gckind;  /* kind of GC running */                                                          
  17. 125   lu_byte gcrunning;  /* true if GC is running */                                                    
  18. 126   int sweepstrgc;  /* position of sweep in `strt' */                                                 
  19. 127   GCObject *allgc;  /* list of all collectable objects */                                            
  20. 128   GCObject *finobj;  /* list of collectable objects with finalizers */                               
  21. 129   GCObject **sweepgc;  /* current position of sweep in list 'allgc' */                               
  22. 130   GCObject **sweepfin;  /* current position of sweep in list 'finobj' */                             
  23. 131   GCObject *gray;  /* list of gray objects */                                                        
  24. 132   GCObject *grayagain;  /* list of objects to be traversed atomically */                             
  25. 133   GCObject *weak;  /* list of tables with weak values */                                             
  26. 134   GCObject *ephemeron;  /* list of ephemeron tables (weak keys) */                                   
  27. 135   GCObject *allweak;  /* list of all-weak tables */                                                  
  28. 136   GCObject *tobefnz;  /* list of userdata to be GC */                                                
  29. 137   UpVal uvhead;  /* head of double-linked list of all open upvalues */                               
  30. 138   Mbuffer buff;  /* temporary buffer for string concatenation */                                     
  31. 139   int gcpause;  /* size of pause between successive GCs */  
  32. 140   int gcmajorinc;  /* how much to wait for a major GC (only in gen. mode) */  
  33. 141   int gcstepmul;  /* GC `granularity' */  
  34. 142   lua_CFunction panic;  /* to be called in unprotected errors */  
  35. 143   struct lua_State *mainthread;  
  36. 144   const lua_Number *version;  /* pointer to version number */  
  37. 145   TString *memerrmsg;  /* memory-error message */  
  38. 146   TString *tmname[TM_N];  /* array with tag-method names */  
  39. 147   struct Table *mt[LUA_NUMTAGS];  /* metatables for basic types */  
  40. 148 } global_State;  
一个Lua虚拟机只有一个全局的global_State,在调用lua_newstate时候,会创建和初始化这个全局结构,这个全局结构管理着lua中全局唯一的信息, 主要包括以下信息:
    lua_Alloc frealloc:虚拟机内存分配策略,可以在调用lua_newstate时指定参数,修改该策略,或者调用luaL_newstate函数使用默认的内存分配策略。也可以通过函数               lua_setallocf:来设置内存分配策略。
    stringtable strt:全局的字符串哈希表,即保存那些短字符串,使得整个虚拟机中短字符串只有一份实例。
    TValue l_registry:保存全局的注册表,注册表就是一个全局的table(即整个虚拟机中只有一个注册表),它只能被C代码访问,通常,它用来保存那些需要在几个模块中共享的数据。比如通过luaL_newmetatable创建的元表就是放在全局的注册表中。
    lua_CFunction panic:但出现无包含错误(unprotected errors)时,会调用这个函数。这个函数可以通过lua_atpanic来修改。
    UpVal uvhead:指向保存所有open upvalues双向链表的头部。
    struct Table *mt[LUA_NUMTAGS]:保存基本类型的元表,注意table和userdata都有自己的元表。
    struct lua_State *mainthread:指向主lua_State,或者说是主线程、主执行栈。Lua虚拟机在调用函数lua_newstate初始化全局状态global_State时也会创建一个主线程, 当然根据需要也可以调用lua_newthread来创建新的线程,但是整个虚拟机,只有一个全局的状态global_State。
    全局状态结构体中其他成员基本都是与内存管理和GC相关的。
    下面来看线程对应的数据结构lua_State的实现,代码如下(lstate.h):
[cpp]  view plain  copy
  1. 151 /* 
  2. 152 ** `per thread' state 
  3. 153 */  
  4. 154 struct lua_State {  
  5. 155   CommonHeader;  
  6. 156   lu_byte status;  
  7. 157   StkId top;  /* first free slot in the stack */  
  8. 158   global_State *l_G;  
  9. 159   CallInfo *ci;  /* call info for current function */  
  10. 160   const Instruction *oldpc;  /* last pc traced */  
  11. 161   StkId stack_last;  /* last free slot in the stack */  
  12. 162   StkId stack;  /* stack base */   
  13. 163   int stacksize;  
  14. 164   unsigned short nny;  /* number of non-yieldable calls in stack */  
  15. 165   unsigned short nCcalls;  /* number of nested C calls */  
  16. 166   lu_byte hookmask;  
  17. 167   lu_byte allowhook;  
  18. 168   int basehookcount;  
  19. 169   int hookcount;  
  20. 170   lua_Hook hook;  
  21. 171   GCObject *openupval;  /* list of open upvalues in this stack */  
  22. 172   GCObject *gclist;  
  23. 173   struct lua_longjmp *errorJmp;  /* current error recover point */  
  24. 174   ptrdiff_t errfunc;  /* current error handling function (stack index) */  
  25. 175   CallInfo base_ci;  /* CallInfo for first level (C calling Lua) */  
  26. 176 };  

    可以看到,lua_State结构跟其他可回收的数据类型类型,结构体带用CommonHeader的头,它也GC回收的对象之一。它主要包括以下成员信息:
lu_byte status:线程脚本的状态,线程可能状态如下(lua.h):

[cpp]  view plain  copy
  1. 44 /* thread status */  
  2. 45 #define LUA_OK      0  
  3. 46 #define LUA_YIELD   1  
  4. 47 #define LUA_ERRRUN  2  
  5. 48 #define LUA_ERRSYNTAX   3  
  6. 49 #define LUA_ERRMEM  4  
  7. 50 #define LUA_ERRGCMM 5  
  8. 51 #define LUA_ERRERR  6  
  global_State *l_G:指向全局状态;
    其他成员主要是数据栈和函数调用栈相关的,这也是lua_State结构中主要信息。还有成员ptrdiff_t errfunc是错误处理函数相关,也就是每个调用栈都有独立的错误处理函数,以及调试相关的lua_Hook hook成员等。


3、总结
    在调用lua_newstate 初始化Lua虚拟机时,会创建一个全局状态和一个线程(或称为调用栈),这个全局状态在整个虚拟机中是唯一的,供其他线程共享。一个Lua虚拟机中可以包括多个线程,这些线程共享一个全局状态,线程之间也可以调用lua_xmove函数来交换数据。


参考资料

http://www.cnblogs.com/ringofthec/archive/2010/11/09/lua_State.html lua API 小记3(lua虚拟机初始化)
http://blog.aliyun.com/795 Lua数据结构 — lua_State(六)
Lua 5.2.1源码

猜你喜欢

转载自blog.csdn.net/jinxinliu1/article/details/80625919
今日推荐