-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
native timer callback #83
Comments
this if passed if passed another function (while one is already set) should override the current function if passed only a
example: function myLoop():void
{
// some loop
}
Program. setTimerListener( 10, myLoop ); //loop every 10sec
Program. setTimerListener( 30 ); //override, now function loop every 30sec |
moved to earlier release but change in API those native timers are extremely interesting they have seconds precision (could be improved to milliseconds precision) yep they do not block nor pause the program flow For now if we were implementing a loop based on a forever loop but with the native timers, they can be called on the first line of a program |
change in design problem with SIGALRM problem with timeSetEvent so what do we do? pretty much stuck there are we? not exactly :) a native timer is interesting because it could allow us to broadcast a So, the change in design, is to simply use a "busy wait" loop An obvious problem with that is as soon as you enter a "busy wait" loop for example:
Our solution to that is to add a native hook which will execute which gives:
And, in fact, we already had this behaviour in the runtime with So, now we added 2 more hooks:
and now here how we define our Runtime.goAsync = function():void
{
/* Note:
Only the primordial worker can execute the main loop
*/
if( !Worker.current.isPrimordial )
{
return;
}
if( !Runtime.isAsync() )
{
Runtime._async = true;
if( Runtime.loop )
{
Program.onLoop = function():void
{
Runtime.loop.start();
}
Program.setLoopListener( Program.onLoop );
}
}
} But then how do we work with "native" timers ? when private function _handleTimers():void
{
var current:uint = Program.getTimer();
var i:uint;
var timer:TimerClient;
for( i = 0; i < _timers.length; i++ )
{
timer = _timers[i];
if( timer.running )
{
var elapsed:uint = current - timer._started;
if( elapsed >= (timer.delay * (timer.currentCount + 1)) )
{
timer.tick();
}
}
}
} the property eg. public class TimerClient
{
AVM2 var _started:uint;
AVM2 function tick():void
{
//...
}
} the namespace allow to "hide" those from public consumption The class End result are:
For now, we keep things "simple" and we let the user be responsible for managing the main loop If you don't want your program to "run forever" you could define your own timeout import flash.utils.*;
function runtime_timeout():void
{
clearTimeout( timeout_id );
Program.exit(0);
}
// a 15 seconds timeout
var timeout_id:uint = setTimeout( runtime_timeout, 15 * 1000 ); Another use case would be to automatically run the main loop only if timers are detected function runtime_autoloop():void
{
if( Runtime.loop.timerPending )
{
Runtime.loop.start();
}
}
// check if you need to run the loop before he program exit
Program.atExit( runtime_autoloop ); |
based on Platform.h interface
implement a
_setTimerListener
that works like_setExitListener
eg.
The text was updated successfully, but these errors were encountered: