Advertisement
Starkkz

BlitzMax Multithreading script for Lua

Dec 17th, 2013
2,990
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Type TLuaThread
  2.     Field ParentState:Byte Ptr
  3.     Field State:Byte Ptr
  4.     Field Code:String
  5.     Field Thread:TThread
  6.    
  7.     Function RunThread:Object(LT:Object)
  8.         Local LuaObject:TLuaThread = TLuaThread(LT)
  9.         If LuaObject Then
  10.             LuaObject.State = lua_newthread(LuaObject.ParentState)
  11.             luaL_dostring LuaObject.State, LuaObject.Code
  12.            
  13.             Local Error:String = lua_tostring(LuaObject.State, -1)
  14.             If Len(Error) > 0 Then Print "Error: "+Error
  15.         EndIf
  16.     EndFunction
  17.    
  18.     Method Run:TThread()
  19.         Self.Thread = CreateThread(TLuaThread.RunThread, Self)
  20.     EndMethod
  21.    
  22.     Method Create:TLuaThread(ParentState:Byte Ptr,Code:String)
  23.         Self.ParentState = ParentState
  24.         Self.Code = Code
  25.         Self.Run()
  26.         If Self.Thread Then Return Self
  27.     EndMethod
  28. EndType
  29.  
  30. Function lua_CreateThread(L:Byte Ptr)
  31.     Local Code:String = luaL_checkstring(L, 1)
  32.     If Len(Code) > 0 Then
  33.         Local LuaThread:TLuaThread = New TLuaThread.Create(L, Code)
  34.         If LuaThread Then
  35.             Local Thread:TThread = LuaThread.Thread
  36.             If Thread Then
  37.                 lua_pushinteger L, HandleFromObject(Thread)
  38.                 Return 1
  39.             EndIf
  40.         EndIf
  41.     EndIf
  42. EndFunction
  43.  
  44. Function lua_DetachThread(L:Byte Ptr)
  45.     Local ThreadID = lua_tointeger(L, 1)
  46.     If ThreadID Then
  47.         Local ThreadObject:Object = HandleToObject(ThreadID)
  48.         If ThreadObject Then
  49.             Local Thread:TThread = TThread(ThreadObject)
  50.             If Thread Then
  51.                 DetachThread Thread
  52.             EndIf
  53.         EndIf
  54.     EndIf
  55. EndFunction
  56.  
  57. Function lua_ThreadRunning(L:Byte Ptr)
  58.     Local ThreadID = lua_tointeger(L, 1)
  59.     If ThreadID Then
  60.         Local ThreadObject:Object = HandleToObject(ThreadID)
  61.         If ThreadObject Then
  62.             Local Thread:TThread = TThread(ThreadObject)
  63.             If Thread Then
  64.                 lua_pushboolean L, ThreadRunning(Thread)
  65.                 Return 1
  66.             EndIf
  67.         EndIf
  68.     EndIf
  69. EndFunction
  70.  
  71. Function lua_WaitThread(L:Byte Ptr)
  72.     Local ThreadID = lua_tointeger(L, 1)
  73.     If ThreadID Then
  74.         Local ThreadObject:Object = HandleToObject(ThreadID)
  75.         If ThreadObject Then
  76.             Local Thread:TThread = TThread(ThreadObject)
  77.             If Thread Then
  78.                 WaitThread Thread
  79.             EndIf
  80.         EndIf
  81.     EndIf
  82. EndFunction
  83.  
  84. Function luaopen_luamt(L:Byte Ptr)
  85.     lua_register(L, "CreateThread", lua_CreateThread)
  86.     lua_register(L, "DetachThread", lua_DetachThread)
  87.     lua_register(L, "ThreadRunning", lua_ThreadRunning)
  88.     lua_register(L, "WaitThread", lua_WaitThread)
  89. EndFunction
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement