ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Event/Event.pm
(Generate patch)

Comparing Coro/Event/Event.pm (file contents):
Revision 1.82 by root, Fri May 29 07:01:18 2009 UTC vs.
Revision 1.83 by root, Sat Jun 20 07:16:31 2009 UTC

19 } 19 }
20 20
21 loop; 21 loop;
22 22
23 # wait for input on stdin for one second 23 # wait for input on stdin for one second
24
25 Coro::Event::do_io (fd => \*STDIN, timeout => 1) & Event::Watcher::R 24 Coro::Event::do_io (fd => \*STDIN, timeout => 1) & Event::Watcher::R
26 or die "no input received"; 25 or die "no input received";
27 26
28 # use a separate coroutine for event processing, if impossible in main: 27 # use a separate thread for event processing, if impossible in main:
29 Coro::async { Event::loop }; 28 Coro::async { Event::loop };
30 29
31=head1 DESCRIPTION 30=head1 DESCRIPTION
32 31
33This module enables you to create programs using the powerful Event model 32This module enables you to create programs using the powerful Event model
37This module provides a method and a function for every watcher type 36This module provides a method and a function for every watcher type
38(I<flavour>) (see L<Event>). The only difference between these and the 37(I<flavour>) (see L<Event>). The only difference between these and the
39watcher constructors from Event is that you do not specify a callback 38watcher constructors from Event is that you do not specify a callback
40function - it will be managed by this module. 39function - it will be managed by this module.
41 40
42Your application should just create all necessary coroutines and then call 41Your application should just create all necessary threads and then call
43Coro::Event::loop. 42C<Event::loop>.
44 43
45Please note that even programs or modules (such as 44Please note that even programs or modules (such as L<Coro::Handle>) that
46L<Coro::Handle|Coro::Handle>) that use "traditional"
47event-based/continuation style will run more efficient with this module 45use "traditional" event-based/continuation style will run more efficient
48then when using only Event. 46with this module then when using only Event.
49 47
50=head1 WARNING 48=head1 WARNING
51 49
52Please note that Event does not support coroutines or threads. That 50Please note that Event does not support multithreading. That means that
53means that you B<MUST NOT> block in an event callback. Again: In Event 51you B<MUST NOT> block in an event callback. Again: In Event callbacks,
54callbacks, you I<must never ever> call a Coroutine function that blocks 52you I<must never ever> call a Coro function that blocks the current
55the current coroutine. 53thread.
56 54
57While this seems to work superficially, it will eventually cause memory 55While this seems to work superficially, it will eventually cause memory
58corruption and often results in deadlocks. 56corruption and often results in deadlocks.
59 57
60Best practise is to always use B<Coro::unblock_sub> for your callbacks. 58Best practise is to always use B<Coro::unblock_sub> for your callbacks.
61 59
62=head1 SEMANTICS 60=head1 SEMANTICS
63 61
64Whenever Event blocks (e.g. in a call to C<one_event>, C<loop> etc.), 62Whenever Event blocks (e.g. in a call to C<one_event>, C<loop> etc.),
65this module cede's to all other coroutines with the same or higher 63this module cede's to all other threads with the same or higher
66priority. When any coroutines of lower priority are ready, it will not 64priority. When any threads of lower priority are ready, it will not
67block but run one of them and then check for events. 65block but run one of them and then check for events.
68 66
69The effect is that coroutines with the same or higher priority than 67The effect is that coroutines with the same or higher priority than
70the blocking coroutine will keep Event from checking for events, while 68the blocking coroutine will keep Event from checking for events, while
71coroutines with lower priority are being run, but Event checks for new 69coroutines with lower priority are being run, but Event checks for new
72events after every cede. 70events after every cede. Note that for this to work you actually need to
71run the event loop in some thread.
73 72
74=head1 FUNCTIONS 73=head1 FUNCTIONS
75 74
76=over 4 75=over 4
77 76
83 82
84use Carp; 83use Carp;
85no warnings; 84no warnings;
86 85
87use Coro; 86use Coro;
88use Event qw(loop unloop); # we are re-exporting this, cooool! 87use Event qw(loop unloop); # we are re-exporting this for historical reasons
89 88
90use XSLoader; 89use XSLoader;
91 90
92use base Exporter::; 91use base Exporter::;
93 92
189 188
190sub sweep { 189sub sweep {
191 Event::one_event 0; # for now 190 Event::one_event 0; # for now
192} 191}
193 192
194=item $result = loop([$timeout])
195
196This is the version of C<loop> you should use instead of C<Event::loop>
197when using this module - it will ensure correct scheduling in the presence
198of events.
199
200=item unloop([$result])
201
202Same as Event::unloop (provided here for your convinience only).
203
204=cut
205
206# very inefficient 193# very inefficient
207our $IDLE = new Coro sub { 194our $IDLE = new Coro sub {
208 while () { 195 while () {
209 &Event::one_event; 196 &Event::one_event;
210 &Coro::schedule; 197 &Coro::schedule;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines