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.40 by root, Fri Nov 24 15:34:33 2006 UTC vs.
Revision 1.47 by root, Wed Jan 24 16:22:08 2007 UTC

18 } 18 }
19 } 19 }
20 20
21 loop; 21 loop;
22 22
23 # wait for input on stdin for one second
24
25 Coro::Event::do_io (fd => \*STDIN, timeout => 1) & Event::Watcher::R
26 or die "no input received";
27
23=head1 DESCRIPTION 28=head1 DESCRIPTION
24 29
25This module enables you to create programs using the powerful Event model 30This module enables you to create programs using the powerful Event model
26(and module), while retaining the linear style known from simple or 31(and module), while retaining the linear style known from simple or
27threaded programs. 32threaded programs.
37Please note that even programs or modules (such as 42Please note that even programs or modules (such as
38L<Coro::Handle|Coro::Handle>) that use "traditional" 43L<Coro::Handle|Coro::Handle>) that use "traditional"
39event-based/continuation style will run more efficient with this module 44event-based/continuation style will run more efficient with this module
40then when using only Event. 45then when using only Event.
41 46
47=head1 WARNING
48
49Please note that Event does not support coroutines or threads. That
50means that you B<MUST NOT> block in an event callback. Again: In Event
51callbacks, you I<must never ever> call a Coroutine fucntion that blocks
52the current coroutine.
53
54While this seems to work superficially, it will eventually cause memory
55corruption.
56
57=head1 SEMANTICS
58
59Whenever Event blocks (e.g. in a call to C<one_event>, C<loop> etc.),
60this module cede's to all other coroutines with the same or higher
61priority. When any coroutines of lower priority are ready, it will not
62block but run one of them and then check for events.
63
64The effect is that coroutines with the same or higher priority than
65the blocking coroutine will keep Event from checking for events, while
66coroutines with lower priority are being run, but Event checks for new
67events after every cede.
68
69=head1 FUNCTIONS
70
42=over 4 71=over 4
43 72
44=cut 73=cut
45 74
46package Coro::Event; 75package Coro::Event;
47 76
48BEGIN { eval { require warnings } && warnings->unimport ("uninitialized") } 77no warnings;
49 78
50use Carp; 79use Carp;
51no warnings; 80no warnings;
52 81
53use Coro; 82use Coro;
56 85
57use XSLoader; 86use XSLoader;
58 87
59use base Exporter::; 88use base Exporter::;
60 89
61our @EXPORT = qw(loop unloop sweep reschedule); 90our @EXPORT = qw(loop unloop sweep);
62 91
63BEGIN { 92BEGIN {
64 our $VERSION = 1.9; 93 our $VERSION = '2.0';
65 94
66 local $^W = 0; # avoid redefine warning for Coro::ready; 95 local $^W = 0; # avoid redefine warning for Coro::ready;
67 XSLoader::load __PACKAGE__, $VERSION; 96 XSLoader::load __PACKAGE__, $VERSION;
68} 97}
69 98
70=item $w = Coro::Event->flavour(args...) 99=item $w = Coro::Event->flavour (args...)
71 100
72Create and return a watcher of the given type. 101Create and return a watcher of the given type.
73 102
74Examples: 103Examples:
75 104
78 107
79=cut 108=cut
80 109
81=item $w->next 110=item $w->next
82 111
83Return the next event of the event queue of the watcher. 112Wait for and return the next event of the event queue of the watcher. The
113returned event objects support two methods only: C<hits> and C<got>, both
114of which return integers: the number this watcher was hit for this event,
115and the mask of poll events received.
84 116
85=cut 117=cut
86 118
87=item do_flavour(args...) 119=item do_flavour args...
88 120
89Create a watcher of the given type and immediately call it's next 121Create a watcher of the given type and immediately call it's next method,
122returning the event.
123
90method. This is less efficient then calling the constructor once and the 124This is less efficient then calling the constructor once and the next
91next method often, but it does save typing sometimes. 125method often, but it does save typing sometimes.
92 126
93=cut 127=cut
94 128
95for my $flavour (qw(idle var timer io signal)) { 129for my $flavour (qw(idle var timer io signal)) {
96 push @EXPORT, "do_$flavour"; 130 push @EXPORT, "do_$flavour";
104 138
105 shift eq Coro::Event:: 139 shift eq Coro::Event::
106 or croak "event constructor \"Coro::Event->$flavour\" must be called as a static method"; 140 or croak "event constructor \"Coro::Event->$flavour\" must be called as a static method";
107 141
108 my $w = $new->($class, 142 my $w = $new->($class,
109 desc => $flavour, 143 desc => $flavour,
110 @_, 144 @_,
111 parked => 1, 145 parked => 1,
112 ); 146 );
147
113 _install_std_cb($w, $type); 148 _install_std_cb $w, $type;
114 bless $w, $class; # reblessing due to broken Event 149
150 # reblessing due to Event being broken
151 bless $w, $class
115 }; 152 };
116 *{ $flavour } = $coronew; 153 *{ $flavour } = $coronew;
117 *{"do_$flavour"} = sub { 154 *{"do_$flavour"} = sub {
118 unshift @_, Coro::Event::; 155 unshift @_, Coro::Event::;
119 my $e = (&$coronew)->next; 156 @_ = &$coronew;
120 $e->cancel; # $e === $e->w 157 &Coro::schedule while &_next;
121 $e; 158 $_[0]->cancel;
159 &_event
122 }; 160 };
123} 161}
124 162
125# double calls to avoid stack-cloning ;() 163# do schedule in perl to avoid forcing a stack allocation.
126# is about 10% slower, though. 164# this is about 10% slower, though.
127sub next($) { 165sub next($) {
128 &Coro::schedule if &_next; $_[0]; 166 &Coro::schedule while &_next;
129}
130 167
168 &_event
169}
170
171sub Coro::Event::Event::hits { $_[0][3] }
131sub Coro::Event::w { $_[0] } 172sub Coro::Event::Event::got { $_[0][4] }
132sub Coro::Event::prio { $_[0]{Coro::Event}[3] }
133sub Coro::Event::hits { $_[0]{Coro::Event}[4] }
134sub Coro::Event::got { $_[0]{Coro::Event}[5] }
135 173
136=item sweep 174=item sweep
137 175
138Similar to Event::one_event and Event::sweep: The idle task is called once 176Similar to Event::one_event and Event::sweep: The idle task is called once
139(this has the effect of jumping back into the Event loop once to serve new 177(this has the effect of jumping back into the Event loop once to serve new
145into the Event dispatcher. 183into the Event dispatcher.
146 184
147=cut 185=cut
148 186
149sub sweep { 187sub sweep {
150 Event::one_event(0); # for now 188 Event::one_event 0; # for now
151} 189}
152 190
153=item $result = loop([$timeout]) 191=item $result = loop([$timeout])
154 192
155This is the version of C<loop> you should use instead of C<Event::loop> 193This is the version of C<loop> you should use instead of C<Event::loop>

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines