multithreading - Lua 5.2.2 Broken Threading System -


ey,

i'm making threading system in lua , have crash when resume lot threads... i'm noob in c lua , don't know or bad i'm doing in next codes...

i flagged crash places "//maybe here"

#include<cstdio> #include<ala_lua.h>  extern"c"{     void __stdcall sleep(unsigned long); };    int main(){     lua_state*l=lual_newstate();     lual_openlibs(l);     luaa_libs(l);      lua_state*t=lua_newthread(l);     lual_loadfile(t,"c:/clua.lua");     co_resume(t);      do{         sleep(1);     }while(co_update());      lua_close(l);      gets(new char[1]);     return 0; }; 

#ifndef ala_lua_h #define ala_lua_h  #include<ala_lua_co.h>  void luaa_libs(lua_state*);  static int luaa_sleep(lua_state*handle){     co_sleep(handle,lua_tointeger(handle,1));     return 0; };  static int luaa_rthread(lua_state*handle){     if(lua_isthread(handle,1)){         co_resume(lua_tothread(handle,1));     };     return 0; };  static int luaa_mthread(lua_state*handle){     if(lua_isfunction(handle,1)){         lua_state*thread=lua_newthread(handle);          lua_pushvalue(handle,1);         lua_xmove(handle,thread,1);          return 1;     };     return 0; };  void luaa_libs(lua_state*handle){     lua_register(handle,"mthread",luaa_mthread);     lua_register(handle,"rthread",luaa_rthread);     lua_register(handle,"sleep",luaa_sleep); };  #endif 

#ifndef ala_lua_co_h #define ala_lua_co_h  #include<vector> #include<time.h> #include<stdio.h> #include<cstring> #include<lua.hpp>  std::vector<lua_state*>co_threads; std::vector<time_t*>co_threads_delay;  static const size_t co_npos=-1;  bool co_update(); size_t co_new(lua_state*); size_t co_get(lua_state*); void co_resume(lua_state*); void co_report(lua_state*); void co_rem(lua_state*,size_t); void co_sleep(lua_state*,time_t); void co_remifdead(lua_state*,size_t);    void co_remifdead(lua_state*handle,size_t index=co_npos){     switch(lua_status(handle)){         case lua_ok:{             if(lua_gettop(handle)==0)co_rem(handle,index);             return;         };         case lua_errrun:{             co_rem(handle,index);             return;         };     }; };  void co_rem(lua_state*handle,size_t index=co_npos){     if(index==co_npos){         index=co_get(handle);     };     if(index!=co_npos){         co_threads.erase(co_threads.begin()+index);         delete[]co_threads_delay[index];         co_threads_delay.erase(co_threads_delay.begin()+index);     }; };  bool co_update(){     if(co_threads.empty())return false;     size_t i=co_threads.size();     while(i>0){         --i;          lua_state*handle=co_threads[i];         if(lua_status(handle)==lua_yield){             if(*co_threads_delay[i]<=clock()){                 lua_resume(handle,null,0);//here maybe                 co_remifdead(handle,i);             };         }else{             co_remifdead(handle,i);         };     };     return!co_threads.empty(); };  void co_resume(lua_state*handle){     switch(lua_status(handle)){         case lua_yield:{             lua_resume(handle,null,0);             co_remifdead(handle);             return;         };         case lua_ok:{             if(lua_gettop(handle)!=0){                 size_t index=co_new(handle);                 lua_resume(handle,null,0);//here maybe                 co_remifdead(handle,index);                 return;             }else{return;};         };         default:{return;};     }; };  void co_sleep(lua_state*handle,time_t slp){     size_t index=co_get(handle);     if(index!=co_npos){         *co_threads_delay[index]=slp+clock();         lua_yield(co_threads[index],0);     }; };  void co_report(lua_state*handle){     if(lua_status(handle)==lua_errrun){         const char*error=lua_tostring(handle,-1);         #ifdef ala_lua_errlog             file*file=fopen(ala_lua_errlog,"a");                 fputs(error,file);             fclose(file);         #else             puts(error);         #endif         lua_pop(handle,-1);     }; };  size_t co_get(lua_state*handle){     if(co_threads.empty())return co_npos;     const size_t l=co_threads.size();     for(size_t i=0;i<l;++i){         if(co_threads[i]==handle)return i;     };     return co_npos; };  size_t co_new(lua_state*handle){     if(lua_status(handle)==lua_ok&&lua_gettop(handle)!=0){         time_t*tm=new time_t[1];         co_threads.push_back(handle);         co_threads_delay.push_back(tm);         return co_threads.size()-1;     };     return co_npos; };  #endif 

lua has no built-in support multithreading @ os level. lua threads coroutines, not os threads. cannot use lua threads of same mother state in different os threads. can use separate lua states in different os threads data exchange between 2 states must managed manually.

on other hand, can build lua support os-level multithreading defining lock , unlock macros these called every time app goes , leaves lua.


Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -