windows lua 多線程 線程同步


今天在改一個程序,改成部分邏輯用lua寫,這個程序是多線程的。將程序中部分邏輯改成lua之后,各種非法訪問內存錯誤,各種奇奇怪怪的問題,不分時間,不分地點的出現崩潰。從調用堆棧來看,基本都是使用lua造成的。在多線程中使用lua_newthread得到的lus_State仍然有時候程序會崩潰。基本上可以確定為多線程中操作lua 的問題了。

前幾天我轉載的一篇文章,文章寫了關於lua多線程的作法。作法有二

1.每一個線程函數用lua_newthread產生一個新的lua_state 以后對lua操作都用這個lua_state

2.修改lua源碼使之成為支持多線程的(修改lua 源代碼中lua_lock lua_unlock宏)

對於第2條,那篇文中的例子是用 pthread_mutex_t 來進行線程同步的pthread_mutex_t,我對pthread_mutex_t 不熟,網上一查才知道一般是在linux下C語言進行線程同步用的,在windows下面一般多線程都用CRITICAL_SECTION,和內核對象來進行線程同步的。於是產生了這篇文章。

修改代碼如下:

1.global_State加一個成員變量  m_cs

Code Snippet
  1. typedef struct global_State {
  2.   stringtable strt;  /* hash table for strings */
  3.   lua_Alloc frealloc;  /* function to reallocate memory */
  4.   void *ud;         /* auxiliary data to `frealloc' */
  5.   lu_byte currentwhite;
  6.   lu_byte gcstate;  /* state of garbage collector */
  7.   int sweepstrgc;  /* position of sweep in `strt' */
  8.   GCObject *rootgc;  /* list of all collectable objects */
  9.   GCObject **sweepgc;  /* position of sweep in `rootgc' */
  10.   GCObject *gray;  /* list of gray objects */
  11.   GCObject *grayagain;  /* list of objects to be traversed atomically */
  12.   GCObject *weak;  /* list of weak tables (to be cleared) */
  13.   GCObject *tmudata;  /* last element of list of userdata to be GC */
  14.   Mbuffer buff;  /* temporary buffer for string concatentation */
  15.   lu_mem GCthreshold;
  16.   lu_mem totalbytes;  /* number of bytes currently allocated */
  17.   lu_mem estimate;  /* an estimate of number of bytes actually in use */
  18.   lu_mem gcdept;  /* how much GC is `behind schedule' */
  19.   int gcpause;  /* size of pause between successive GCs */
  20.   int gcstepmul;  /* GC `granularity' */
  21.   lua_CFunction panic;  /* to be called in unprotected errors */
  22.   TValue l_registry;
  23.   struct lua_State *mainthread;
  24.   UpVal uvhead;  /* head of double-linked list of all open upvalues */
  25.   struct Table *mt[NUM_TAGS];  /* metatables for basic types */
  26.   TString *tmname[TM_N];  /* array with tag-method names */
  27.   CRITICAL_SECTION m_cs; /*windows mutithread */
  28. } global_State;

 

2.在lua_newstate加入初始化Critical_section的代碼 InitializeCriticalSection(&g->m_cs);

Code Snippet
  1. LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
  2.   int i;
  3.   lua_State *L;
  4.   global_State *g;
  5.   void *l = (*f)(ud, NULL, 0, state_size(LG));
  6.   if (l == NULL) return NULL;
  7.   L = tostate(l);
  8.   g = &((LG *)L)->g;
  9.   L->next = NULL;
  10.   L->tt = LUA_TTHREAD;
  11.   g->currentwhite = bit2mask(WHITE0BIT, FIXEDBIT);
  12.   L->marked = luaC_white(g);
  13.   set2bits(L->marked, FIXEDBIT, SFIXEDBIT);
  14.   preinit_state(L, g);
  15.   g->frealloc = f;
  16.   g->ud = ud;
  17.   g->mainthread = L;
  18.   g->uvhead.u.l.prev = &g->uvhead;
  19.   g->uvhead.u.l.next = &g->uvhead;
  20.   g->GCthreshold = 0;  /* mark it as unfinished state */
  21.   g->strt.size = 0;
  22.   g->strt.nuse = 0;
  23.   g->strt.hash = NULL;
  24.   setnilvalue(registry(L));
  25.   luaZ_initbuffer(L, &g->buff);
  26.   g->panic = NULL;
  27.   g->gcstate = GCSpause;
  28.   g->rootgc = obj2gco(L);
  29.   g->sweepstrgc = 0;
  30.   g->sweepgc = &g->rootgc;
  31.   g->gray = NULL;
  32.   g->grayagain = NULL;
  33.   g->weak = NULL;
  34.   g->tmudata = NULL;
  35.   g->totalbytes = sizeof(LG);
  36.   g->gcpause = LUAI_GCPAUSE;
  37.   g->gcstepmul = LUAI_GCMUL;
  38.   g->gcdept = 0;
  39.   InitializeCriticalSection(&g->m_cs);
  40.   for (i=0; i<NUM_TAGS; i++) g->mt[i] = NULL;
  41.   if (luaD_rawrunprotected(L, f_luaopen, NULL) != 0) {
  42.     /* memory allocation error: free partial state */
  43.     close_state(L);
  44.     L = NULL;
  45.   }
  46.   else
  47.     luai_userstateopen(L);
  48.   return L;
  49. }

3.在close_state 加入刪除CriticalSection的代碼 DeleteCriticalSection(&g->m_cs);

Code Snippet
  1. static void close_state (lua_State *L) {
  2.   global_State *g = G(L);
  3.   luaF_close(L, L->stack);  /* close all upvalues for this thread */
  4.   luaC_freeall(L);  /* collect all objects */
  5.   lua_assert(g->rootgc == obj2gco(L));
  6.   lua_assert(g->strt.nuse == 0);
  7.   luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size, TString *);
  8.   luaZ_freebuffer(L, &g->buff);
  9.   freestack(L, L);
  10.   lua_assert(g->totalbytes == sizeof(LG));
  11.   DeleteCriticalSection(&g->m_cs);
  12.   (*g->frealloc)(g->ud, fromstate(L), state_size(LG), 0);
  13. }

4.修改lua_lock和lua_unlock宏如下

Code Snippet
  1. #ifndef lua_lock
  2. #define lua_lock(L) EnterCriticalSection(&(G(L)->m_cs))
  3. #define lua_unlock(L) LeaveCriticalSection(&(G(L)->m_cs))
  4. #endif

5.為了使用CRITICAL_SECTION我們比較通用的方法是包含頭文件 windows.h,但包含后發現,lua庫中的LoadString函數正好與windows  api LoadString同名。所以把lua庫內部所使用的LoadString通通改為LUA_LoadString即可

修改完之后,在控制台寫了四個線程一個一個主lua_state,四個線程使用lua_newthread得到的lua_state,沒有再出現錯誤了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM