Added multi.OnExit()

This commit is contained in:
Ryan Ward 2020-01-29 14:25:27 -05:00
parent aaff2244b1
commit 06574d2d45
3 changed files with 34 additions and 22 deletions

View File

@ -3,7 +3,23 @@
Update 14.1.0 Bug Fixes and a change Update 14.1.0 Bug Fixes and a change
------------- -------------
# Added: # Added:
- thread enviroments are able to interact with threaded functions and wait when there is the presence of variables. Only works when creating "Globals" inside of a thread. The way the enviroment has been set up is that it sets your "Global" as a "GLocal" a global variable local to the threaded enviroment. This does not have the access speed benifits that using pure locals have.. - multi.OnExit(func) -- A special connection that allows you to connect onto the lua state closing event.
```lua
package.path="?/init.lua;?.lua;"..package.path
multi,thread = require("multi"):init()
multi.OnExit(function(n)
print("Code Exited")
end)
sdf() -- Non existing function being called to trigger an error
```
```lua
package.path="?/init.lua;?.lua;"..package.path
multi,thread = require("multi"):init()
multi.OnExit(function(n)
print("Code Exited")
end) -- The code finishing also triggers this event
```
- thread enviroments are able to interact with threaded functions and wait when there is the presence of variables. Only works when creating "Globals" inside of a thread. The way the enviroment has been set up is that it sets your "Global" as a "GLocal" a global variable local to the threaded enviroment. This does not have the access speed benifits that using pure locals have.
```lua ```lua
package.path="?/init.lua;?.lua;"..package.path package.path="?/init.lua;?.lua;"..package.path
multi,thread = require("multi"):init() multi,thread = require("multi"):init()
@ -13,7 +29,7 @@ multi:newThread(function()
thread.sleep(1) thread.sleep(1)
return 1,2 return 1,2
end end
a,b = test().wait() -- Will modify Global -- a,b = test().wait() -- Will modify Global
-- when wait is used the special metamethod routine is not triggered and variables are set as normal -- when wait is used the special metamethod routine is not triggered and variables are set as normal
a,b = test() -- Will modify GLocal a,b = test() -- Will modify GLocal
-- the threaded function test triggers a special routine within the metamethod that alters the thread's enviroment instead of the global enviroment. -- the threaded function test triggers a special routine within the metamethod that alters the thread's enviroment instead of the global enviroment.
@ -28,7 +44,6 @@ end)
multi:newAlarm(2):OnRing(function() multi:newAlarm(2):OnRing(function()
print(a,b) print(a,b)
end) end)
--min,hour,day,wday,month
multi:mainloop() multi:mainloop()
``` ```
- multi:scheduleJob(time,func) - multi:scheduleJob(time,func)

View File

@ -1334,6 +1334,20 @@ function multi:scheduleJob(time,func)
end end
table.insert(scheduledjobs,{time, func,false}) table.insert(scheduledjobs,{time, func,false})
end end
-- Special Events
local _os = os.exit
function os.exit(n)
multi.OnExit:Fire(n or 0)
_os(n)
end
multi.OnExit = multi:newConnection()
multi.m = {onexit = function() multi.OnExit:Fire() end}
if _VERSION >= "Lua 5.2" then
setmetatable(multi.m, {__gc = multi.m.onexit})
else
multi.m.sentinel = newproxy(true)
getmetatable(multi.m.sentinel).__gc = multi.m.onexit
end
-- Threading stuff -- Threading stuff
multi.GlobalVariables={} multi.GlobalVariables={}
if os.getOS()=="windows" then if os.getOS()=="windows" then

View File

@ -1,22 +1,5 @@
package.path="?/init.lua;?.lua;"..package.path package.path="?/init.lua;?.lua;"..package.path
multi,thread = require("multi"):init() multi,thread = require("multi"):init()
a,b = 6,7 multi.OnExit(function(n)
multi:newThread(function() print("Code Exited")
function test()
thread.sleep(1)
return 1,2
end
a,b = test().wait()
print("Waited:",a,b)
--This returns instantly even though the function isn't done!
test().connect(function(a,b)
print("Connected:",a,b)
os.exit()
end) end)
-- This waits for the returns since we are demanding them
end)
multi:newAlarm(2):OnRing(function()
print(a,b)
end)
--min,hour,day,wday,month
multi:mainloop()