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.21 by pcg, Sat Mar 13 06:45:04 2004 UTC vs.
Revision 1.48 by root, Wed Jan 24 16:24:21 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.
32function - it will be managed by this module. 37function - it will be managed by this module.
33 38
34Your application should just create all necessary coroutines and then call 39Your application should just create all necessary coroutines and then call
35Coro::Event::loop. 40Coro::Event::loop.
36 41
42Please note that even programs or modules (such as
43L<Coro::Handle|Coro::Handle>) that use "traditional"
44event-based/continuation style will run more efficient with this module
45then when using only Event.
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
37=over 4 71=over 4
38 72
39=cut 73=cut
40 74
41package Coro::Event; 75package Coro::Event;
42 76
43BEGIN { eval { require warnings } && warnings->unimport ("uninitialized") } 77no warnings;
44 78
45use Carp; 79use Carp;
80no warnings;
46 81
47use Coro; 82use Coro;
83use Coro::Timer;
48use Event qw(loop unloop); # we are re-exporting this, cooool! 84use Event qw(loop unloop); # we are re-exporting this, cooool!
49 85
86use XSLoader;
87
50use base 'Exporter'; 88use base Exporter::;
51 89
52@EXPORT = qw(loop unloop sweep reschedule); 90our @EXPORT = qw(loop unloop sweep);
53 91
54BEGIN { 92BEGIN {
55 $VERSION = 0.95; 93 our $VERSION = '2.1';
56 94
57 local $^W = 0; # avoid redefine warning for Coro::ready; 95 local $^W = 0; # avoid redefine warning for Coro::ready;
58 96 XSLoader::load __PACKAGE__, $VERSION;
59 require DynaLoader;
60 push @ISA, 'DynaLoader';
61 bootstrap Coro::Event $VERSION;
62} 97}
63 98
64=item $w = Coro::Event->flavour(args...) 99=item $w = Coro::Event->flavour (args...)
65 100
66Create and return a watcher of the given type. 101Create and return a watcher of the given type.
67 102
68Examples: 103Examples:
69 104
72 107
73=cut 108=cut
74 109
75=item $w->next 110=item $w->next
76 111
77Return 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.
78 116
79=cut 117=cut
80 118
81=item do_flavour(args...) 119=item do_flavour args...
82 120
83Create 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
84method. This is less efficient then calling the constructor once and the 124This is less efficient then calling the constructor once and the next
85next method often, but it does save typing sometimes. 125method often, but it does save typing sometimes.
86 126
87=cut 127=cut
88 128
89for my $flavour (qw(idle var timer io signal)) { 129for my $flavour (qw(idle var timer io signal)) {
90 push @EXPORT, "do_$flavour"; 130 push @EXPORT, "do_$flavour";
98 138
99 shift eq Coro::Event:: 139 shift eq Coro::Event::
100 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";
101 141
102 my $w = $new->($class, 142 my $w = $new->($class,
103 desc => $flavour, 143 desc => $flavour,
104 @_, 144 @_,
105 parked => 1, 145 parked => 1,
106 ); 146 );
147
107 _install_std_cb($w, $type); 148 _install_std_cb $w, $type;
108 bless $w, $class; # reblessing due to broken Event 149
150 # reblessing due to Event being broken
151 bless $w, $class
109 }; 152 };
110 *{ $flavour } = $coronew; 153 *{ $flavour } = $coronew;
111 *{"do_$flavour"} = sub { 154 *{"do_$flavour"} = sub {
112 unshift @_, Coro::Event::; 155 unshift @_, Coro::Event::;
113 my $e = (&$coronew)->next; 156 @_ = &$coronew;
114 $e->cancel; # $e === $e->w 157 &Coro::schedule while &_next;
115 $e; 158 $_[0]->cancel;
159 &_event
116 }; 160 };
117} 161}
118 162
119# double calls to avoid stack-cloning ;() 163# do schedule in perl to avoid forcing a stack allocation.
120# is about 10% slower, though. 164# this is about 10% slower, though.
121sub next($) { 165sub next($) {
122 &Coro::schedule if &_next; $_[0]; 166 &Coro::schedule while &_next;
123}
124 167
168 &_event
169}
170
171sub Coro::Event::Event::hits { $_[0][3] }
125sub Coro::Event::w { $_[0] } 172sub Coro::Event::Event::got { $_[0][4] }
126sub Coro::Event::prio { $_[0]{Coro::Event}[3] }
127sub Coro::Event::hits { $_[0]{Coro::Event}[4] }
128sub Coro::Event::got { $_[0]{Coro::Event}[5] }
129 173
130=item sweep 174=item sweep
131 175
132Similar 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
133(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
139into the Event dispatcher. 183into the Event dispatcher.
140 184
141=cut 185=cut
142 186
143sub sweep { 187sub sweep {
144 Event::one_event(0); # for now 188 Event::one_event 0; # for now
145} 189}
146 190
147=item $result = loop([$timeout]) 191=item $result = loop([$timeout])
148 192
149This 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>
150when using this module - it will ensure correct scheduling in the presence 194when using this module - it will ensure correct scheduling in the presence
151of events. 195of events.
152 196
153=begin comment
154
155Unlike loop's counterpart it is not an error when no watchers are active -
156loop silently returns in this case, as if unloop(undef) were called.
157
158=end comment
159
160=cut
161
162# no longer do something special - it's done internally now
163
164#sub loop(;$) {
165# #local $Coro::idle = $Coro::current;
166# #Coro::schedule; # become idle task, which is implicitly ready
167# &Event::loop;
168#}
169
170=item unloop([$result]) 197=item unloop([$result])
171 198
172Same as Event::unloop (provided here for your convinience only). 199Same as Event::unloop (provided here for your convinience only).
173 200
174=cut 201=cut
175 202
176$Coro::idle = new Coro sub { 203$Coro::idle = \&Event::one_event; # inefficient
177 while () {
178 Event::one_event; # inefficient
179 Coro::schedule;
180 }
181};
182
183# provide hooks for Coro::Timer
184
185package Coro::Timer;
186
187unless ($override) {
188 $override = 1;
189 *_new_timer = sub {
190 Event->timer(at => $_[0], cb => $_[1]);
191 };
192}
193 204
1941; 2051;
195 206
207=back
208
196=head1 AUTHOR 209=head1 AUTHOR
197 210
198 Marc Lehmann <pcg@goof.com> 211 Marc Lehmann <schmorp@schmorp.de>
199 http://www.goof.com/pcg/marc/ 212 http://home.schmorp.de/
200 213
201=cut 214=cut
202 215

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines