lua-users home
lua-l archive

coroutine & busy wait?

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hello,
I'm playing with coroutines and I wrote a little timer of sort:
http://dev.alt.textdrive.com/file/lu/LUTimer.lua
This works alright as far as the timer itself goes, but unfortunately the dispatcher loop consume an inordinate amount of CPU looping thought all the tasks... :/
Here is how it looks like:
LUTimer.new( ATask.new(), 1, true )
LUTimer.new( ATask.new(), 2, true )
02/04 00:46:20 (Debug) ATask.run: "@1176928: 1"
02/04 00:46:22 (Debug) ATask.run: "@1188224: 1"
"ATask" simply print out itself to standout.
The timer checks the os.clock and invokes its target run method from time to time:
 this.run = function()
 repeat
 if os.clock() - clock() >= this.interval() then
 this.target().run()
 resetClock()
 end
 this.task().yield()
 until this.shouldRepeat() == false
 end
So far, so good.
The problem is with the dispatcher loop:
thisClass.loop = function()
 while thisClass.tasks().size() > 0 do
 thisClass.run()
 end
end
Which repetitively runs through all the registered tasks ad nauseam:
 -- class method to 'run' through all the tasks
thisClass.run = function()
 local someTasks = thisClass.tasks()
 local count = someTasks.size()
 -- someTasks.shuffle()
 if count > 0 then
 for index = 1, count do
 local aTask = someTasks.get( index )
 if aTask ~= nil then
 local aStatus = aTask.status()
 -- print( "aStatus: "..aStatus )
 if aStatus == "dead" then
 aTask.target().setTask( nil )
 someTasks.remove( index )
 elseif aStatus == "suspended" then
 aTask.resume()
 end
 end
 end
 end
 return this
end
I'm not quite sure how to deal with the "busy wait" introduced by the dispatcher run loop though. Ideally the dispatching should be based on some kind of events. But at first glance I don't quite see how to pull this out using only Lua itself... ideas much appreciated :)
TIA.
Cheers
--
PA, Onnay Equitursay
http://alt.textdrive.com/

AltStyle によって変換されたページ (->オリジナル) /