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.38 by root, Mon Nov 20 20:14:02 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;
83use Coro::Timer;
54use Event qw(loop unloop); # we are re-exporting this, cooool! 84use Event qw(loop unloop); # we are re-exporting this, cooool!
55 85
56use XSLoader; 86use XSLoader;
57 87
58use base Exporter::; 88use base Exporter::;
59 89
60our @EXPORT = qw(loop unloop sweep reschedule); 90our @EXPORT = qw(loop unloop sweep);
61 91
62BEGIN { 92BEGIN {
63 our $VERSION = 1.9; 93 our $VERSION = '2.0';
64 94
65 local $^W = 0; # avoid redefine warning for Coro::ready; 95 local $^W = 0; # avoid redefine warning for Coro::ready;
66 XSLoader::load __PACKAGE__, $VERSION; 96 XSLoader::load __PACKAGE__, $VERSION;
67} 97}
68 98
69=item $w = Coro::Event->flavour(args...) 99=item $w = Coro::Event->flavour (args...)
70 100
71Create and return a watcher of the given type. 101Create and return a watcher of the given type.
72 102
73Examples: 103Examples:
74 104
77 107
78=cut 108=cut
79 109
80=item $w->next 110=item $w->next
81 111
82Return 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.
83 116
84=cut 117=cut
85 118
86=item do_flavour(args...) 119=item do_flavour args...
87 120
88Create 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
89method. This is less efficient then calling the constructor once and the 124This is less efficient then calling the constructor once and the next
90next method often, but it does save typing sometimes. 125method often, but it does save typing sometimes.
91 126
92=cut 127=cut
93 128
94for my $flavour (qw(idle var timer io signal)) { 129for my $flavour (qw(idle var timer io signal)) {
95 push @EXPORT, "do_$flavour"; 130 push @EXPORT, "do_$flavour";
103 138
104 shift eq Coro::Event:: 139 shift eq Coro::Event::
105 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";
106 141
107 my $w = $new->($class, 142 my $w = $new->($class,
108 desc => $flavour, 143 desc => $flavour,
109 @_, 144 @_,
110 parked => 1, 145 parked => 1,
111 ); 146 );
147
112 _install_std_cb($w, $type); 148 _install_std_cb $w, $type;
113 bless $w, $class; # reblessing due to broken Event 149
150 # reblessing due to Event being broken
151 bless $w, $class
114 }; 152 };
115 *{ $flavour } = $coronew; 153 *{ $flavour } = $coronew;
116 *{"do_$flavour"} = sub { 154 *{"do_$flavour"} = sub {
117 unshift @_, Coro::Event::; 155 unshift @_, Coro::Event::;
118 my $e = (&$coronew)->next; 156 @_ = &$coronew;
119 $e->cancel; # $e === $e->w 157 &Coro::schedule while &_next;
120 $e; 158 $_[0]->cancel;
159 &_event
121 }; 160 };
122} 161}
123 162
124# double calls to avoid stack-cloning ;() 163# do schedule in perl to avoid forcing a stack allocation.
125# is about 10% slower, though. 164# this is about 10% slower, though.
126sub next($) { 165sub next($) {
127 &Coro::schedule if &_next; $_[0]; 166 &Coro::schedule while &_next;
128}
129 167
168 &_event
169}
170
171sub Coro::Event::Event::hits { $_[0][3] }
130sub Coro::Event::w { $_[0] } 172sub Coro::Event::Event::got { $_[0][4] }
131sub Coro::Event::prio { $_[0]{Coro::Event}[3] }
132sub Coro::Event::hits { $_[0]{Coro::Event}[4] }
133sub Coro::Event::got { $_[0]{Coro::Event}[5] }
134 173
135=item sweep 174=item sweep
136 175
137Similar 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
138(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
144into the Event dispatcher. 183into the Event dispatcher.
145 184
146=cut 185=cut
147 186
148sub sweep { 187sub sweep {
149 Event::one_event(0); # for now 188 Event::one_event 0; # for now
150} 189}
151 190
152=item $result = loop([$timeout]) 191=item $result = loop([$timeout])
153 192
154This 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>
159 198
160Same as Event::unloop (provided here for your convinience only). 199Same as Event::unloop (provided here for your convinience only).
161 200
162=cut 201=cut
163 202
164$Coro::idle = new Coro sub { 203$Coro::idle = \&Event::one_event; # inefficient
165 while () {
166 Coro::schedule;
167 Event::one_event; # inefficient
168 }
169};
170
171# provide hooks for Coro::Timer
172
173package Coro::Timer;
174
175unless ($override) {
176 $override = 1;
177 *_new_timer = sub {
178 Event->timer(at => $_[0], cb => $_[1]);
179 };
180}
181 204
1821; 2051;
183 206
184=back 207=back
185 208

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines