xopxe / lumen

Lua Multitasking Environment.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

on task completion emit `DIE` and `FINISH` signals lazily?

lePereT opened this issue · comments

I'm trying to wait until two Lumen tasks have finished, basically using the following code:

a = sched.new_task(function()
    -- Does some work
end)

b = sched.new_task(function()
    -- Does some work
end)

while a.status == 'ready' or b.status == 'ready' do
    sched.wait({a.EVENT_DIE, a.EVENT_FINISH, b.EVENT_DIE, b.EVENT_FINISH})
end 

This doesn't work, because the following lines in sched.lua emit these signals before changing the status of the task.

lumen/sched.lua

Line 133 in efa8e6b

emit_signal(taskd.EVENT_FINISH, false, ...) --per task

Therefore we have the strange situation where anything woken up by a signal saying a task has ended, when checking the status of the task, will find that the task remains ready. Should the status be changed earlier? Or should the signals be emitted lazily? Or is there a better way of doing what I'm attempting?

For the moment I've worked around this by examining the status of the underlying coroutines but this seems a little hacky

commented

Yes, I agree that your code seems perfectly reasonable.
As you say there are two solutions, either mark the task as dead as soon as we know the coroutine is dead, or delay the signal until after the death of the task is completed.
The first solution would be move the line 144 (taskd.status='dead') to line 129.
The other would be changing the four emit_signal on lines 133, 134, 138 and 139 to M.schedule_signal.
I believe the first option of marking the task as dead before signalling is the simplest way. Could you test it?

I will!
(I've also seen the discussion on the Lua mailing list re the Trio implementation that's being done. It seems interesting but it still seems to suffer from the function colouring problem. I'd wondered whether you'd seen Andy Wingo's post on fibers (CML) in Lua. It's a very beautiful thing https://wingolog.org/archives/2018/05/16/lightweight-concurrency-in-lua . There's a fuller implementation in the Snabb fibers library that he authored https://github.com/snabbco/snabb/tree/master/src/lib/fibers . I've made some of the code there standalone but haven't had time to do the epoll stuff. Anyway, though this might be of interest to you, as some nice building blocks)

commented

Thanks, I'll look into it!
Off course, as you say such a system becomes useful only when it is integrated with some OS library. And that is actually the part of Lumen that I find ugliest, the Luasocket/nixio integration is nasty.

commented

committed ## e56e64c