9.9 Events

     

An event is a notification that something has happened : a timer expired , an I/O operation finished, a thread sent a message to another thread, or the user pressed Ctrl-C to interrupt program execution.

What all of these events have in common is that they arrive asynchronously. It's generally not safe to interrupt program flow at an arbitrary point and continue at a different position, so the event is placed in the interpreter's task queue. The run-loop code regularly checks whether an event needs to be handled. Event handlers may be an internal piece of code or a user-defined event handler subroutine.

Events are still experimental in Parrot, so the implementation and design is subject to change.

9.9.1 Timers

Timer objects are the replacement for Perl 5's alarm handlers. They are also a significant improvement. Timers can fire once or repeatedly, and multiple timers can run independently. The precision of a timer is limited by the operating system on which Parrot runs, but it is always more fine-grained then a whole second. The final syntax isn't yet fixed, so please consult the documentation for examples.

9.9.2 Signals

Signal handling is related to events. When Parrot gets a signal it needs to handle from the operating system, it converts that signal into an event and broadcasts it to all running threads. Each thread independently decides if it's interested in this signal and, if so, how to respond to it.

 newsub P20, .Exception_Handler, _handler   set_eh P20                  # establish signal handler   print "send SIGINT:\n"   sleep 2                     # press ^C after you saw start   print "no SIGINT\n"   end _handler:   .include "signal.pasm"      # get signal definitions   print "caught "   set I0, P5["_type"]         # if _type is negative, the  . . .    neg I0, I0                  #  . . .  negated type is the signal   ne I0, .SIGINT, nok   print "SIGINT\n" nok:   end 

This example creates a signal handler and pushes it on to the control stack. It then prompts the user to send a SIGINT from the shell (this is usually Ctrl-C, but it varies in different shells ), and waits for two seconds. If the user doesn't send a SIGINT in two seconds, the example just prints "no SIGINT" and ends. If the user does send a SIGINT, the signal handler catches it, prints out "caught SIGINT" and ends. [9]

[9] Currently, only Linux installs a SIGINT sigaction handler, so this example won't work on other platforms.



Perl 6 and Parrot Essentials
Perl 6 and Parrot Essentials, Second Edition
ISBN: 059600737X
EAN: 2147483647
Year: 2003
Pages: 116

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net