diff --git a/changes.md b/changes.md index c93fe5e..58b5efe 100644 --- a/changes.md +++ b/changes.md @@ -3,7 +3,23 @@ Update 14.1.0 Bug Fixes and a change ------------- # 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 package.path="?/init.lua;?.lua;"..package.path multi,thread = require("multi"):init() @@ -13,7 +29,7 @@ multi:newThread(function() thread.sleep(1) return 1,2 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 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. @@ -28,7 +44,6 @@ end) multi:newAlarm(2):OnRing(function() print(a,b) end) ---min,hour,day,wday,month multi:mainloop() ``` - multi:scheduleJob(time,func) diff --git a/multi/init.lua b/multi/init.lua index 3c0fd3c..a67e9fe 100644 --- a/multi/init.lua +++ b/multi/init.lua @@ -1334,6 +1334,20 @@ function multi:scheduleJob(time,func) end table.insert(scheduledjobs,{time, func,false}) 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 multi.GlobalVariables={} if os.getOS()=="windows" then diff --git a/test.lua b/test.lua index d391b82..9c85db4 100644 --- a/test.lua +++ b/test.lua @@ -1,22 +1,5 @@ package.path="?/init.lua;?.lua;"..package.path multi,thread = require("multi"):init() -a,b = 6,7 -multi:newThread(function() - 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) - -- This waits for the returns since we are demanding them +multi.OnExit(function(n) + print("Code Exited") end) -multi:newAlarm(2):OnRing(function() - print(a,b) -end) ---min,hour,day,wday,month -multi:mainloop()