Skip to content

Commit

Permalink
Merge pull request #20 from learno/kill
Browse files Browse the repository at this point in the history
add levent.kill(co)
  • Loading branch information
xjdrew committed Dec 16, 2016
2 parents 9fd2c6f + 7f33151 commit b2ee2d9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
6 changes: 6 additions & 0 deletions levent/exceptions.lua
Expand Up @@ -29,10 +29,16 @@ function DnsError:__tostring()
return "DnsError"
end

local KillError = class("KillError", BaseException)
function KillError:__tostring()
return "KillError"
end

local all = {}
all.BaseException = BaseException
all.CancelWaitError = CancelWaitError
all.DnsError = DnsError
all.KillError = KillError
function all.is_exception(exception)
return class.isinstance(exception, BaseException)
end
Expand Down
4 changes: 3 additions & 1 deletion levent/hub.lua
Expand Up @@ -60,7 +60,9 @@ function Hub:throw(co, exception)
end

function Hub:handle_error(co, msg)
print("error:", co, msg)
if not class.isinstance(msg, exceptions.KillError) then
print("error:", co, msg)
end
end

function Hub:_yield(waiter)
Expand Down
10 changes: 10 additions & 0 deletions levent/levent.lua
Expand Up @@ -4,9 +4,14 @@
--]]
local hub = require "levent.hub"
local coroutines = require "levent.coroutines"
local exceptions = require "levent.exceptions"
local kill_error = exceptions.KillError.new()
local levent = {}
levent.kill_error = kill_error
function levent.now()
return hub.loop:now()
end
Expand Down Expand Up @@ -53,6 +58,11 @@ levent.check_coroutine = coroutines.check
function levent.spawn(f, ...)
local co = coroutines.create(f)
hub.loop:run_callback(assert_resume, co, ...)
return co
end
function levent.kill(co)
hub.loop:run_callback(hub.throw, hub, co, kill_error)
end
function levent.start(f, ...)
Expand Down

0 comments on commit b2ee2d9

Please sign in to comment.