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.36 by root, Sun Oct 29 01:57:18 2006 UTC vs.
Revision 1.46 by root, Fri Dec 29 12:05:55 2006 UTC

32function - it will be managed by this module. 32function - it will be managed by this module.
33 33
34Your application should just create all necessary coroutines and then call 34Your application should just create all necessary coroutines and then call
35Coro::Event::loop. 35Coro::Event::loop.
36 36
37Please note that even programs or modules (such as
38L<Coro::Handle|Coro::Handle>) that use "traditional"
39event-based/continuation style will run more efficient with this module
40then when using only Event.
41
42=head1 WARNING
43
44Please note that Event does not support coroutines or threads. That
45means that you B<MUST NOT> block in an event callback. Again: In Event
46callbacks, you I<must never ever> call a Coroutine fucntion that blocks
47the current coroutine.
48
49While this seems to work superficially, it will eventually cause memory
50corruption.
51
52=head1 SEMANTICS
53
54Whenever Event blocks (e.g. in a call to C<one_event>, C<loop> etc.),
55this module cede's to all other coroutines with the same or higher
56priority. When any coroutines of lower priority are ready, it will not
57block but run one of them and then check for events.
58
59The effect is that coroutines with the same or higher priority than
60the blocking coroutine will keep Event from checking for events, while
61coroutines with lower priority are being run, but Event checks for new
62events after every cede.
63
64=head1 FUNCTIONS
65
37=over 4 66=over 4
38 67
39=cut 68=cut
40 69
41package Coro::Event; 70package Coro::Event;
42 71
43BEGIN { eval { require warnings } && warnings->unimport ("uninitialized") } 72no warnings;
44 73
45use Carp; 74use Carp;
46no warnings; 75no warnings;
47 76
48use Coro; 77use Coro;
78use Coro::Timer;
49use Event qw(loop unloop); # we are re-exporting this, cooool! 79use Event qw(loop unloop); # we are re-exporting this, cooool!
50 80
51use XSLoader; 81use XSLoader;
52 82
53use base Exporter::; 83use base Exporter::;
54 84
55our @EXPORT = qw(loop unloop sweep reschedule); 85our @EXPORT = qw(loop unloop sweep);
56 86
57BEGIN { 87BEGIN {
58 our $VERSION = 1.9; 88 our $VERSION = '2.0';
59 89
60 local $^W = 0; # avoid redefine warning for Coro::ready; 90 local $^W = 0; # avoid redefine warning for Coro::ready;
61 XSLoader::load __PACKAGE__, $VERSION; 91 XSLoader::load __PACKAGE__, $VERSION;
62} 92}
63 93
64=item $w = Coro::Event->flavour(args...) 94=item $w = Coro::Event->flavour (args...)
65 95
66Create and return a watcher of the given type. 96Create and return a watcher of the given type.
67 97
68Examples: 98Examples:
69 99
76 106
77Return the next event of the event queue of the watcher. 107Return the next event of the event queue of the watcher.
78 108
79=cut 109=cut
80 110
81=item do_flavour(args...) 111=item do_flavour args...
82 112
83Create a watcher of the given type and immediately call it's next 113Create a watcher of the given type and immediately call it's next
84method. This is less efficient then calling the constructor once and the 114method. This is less efficient then calling the constructor once and the
85next method often, but it does save typing sometimes. 115next method often, but it does save typing sometimes.
86 116
98 128
99 shift eq Coro::Event:: 129 shift eq Coro::Event::
100 or croak "event constructor \"Coro::Event->$flavour\" must be called as a static method"; 130 or croak "event constructor \"Coro::Event->$flavour\" must be called as a static method";
101 131
102 my $w = $new->($class, 132 my $w = $new->($class,
103 desc => $flavour, 133 desc => $flavour,
104 @_, 134 @_,
105 parked => 1, 135 parked => 1,
106 ); 136 );
137
107 _install_std_cb($w, $type); 138 _install_std_cb $w, $type;
108 bless $w, $class; # reblessing due to broken Event 139
140 # reblessing due to Event being broken
141 bless $w, $class
109 }; 142 };
110 *{ $flavour } = $coronew; 143 *{ $flavour } = $coronew;
111 *{"do_$flavour"} = sub { 144 *{"do_$flavour"} = sub {
112 unshift @_, Coro::Event::; 145 unshift @_, Coro::Event::;
113 my $e = (&$coronew)->next; 146 @_ = &$coronew;
114 $e->cancel; # $e === $e->w 147 &Coro::schedule while &_next;
115 $e; 148 $_[0]->cancel;
149 &_event
116 }; 150 };
117} 151}
118 152
119# double calls to avoid stack-cloning ;() 153# do schedule in perl to avoid forcing a stack allocation.
120# is about 10% slower, though. 154# this is about 10% slower, though.
121sub next($) { 155sub next($) {
122 &Coro::schedule if &_next; $_[0]; 156 &Coro::schedule while &_next;
157
158 &_event
123} 159}
124 160
125sub Coro::Event::w { $_[0] } 161sub Coro::Event::w { $_[0] }
126sub Coro::Event::prio { $_[0]{Coro::Event}[3] } 162sub Coro::Event::prio { $_[0]{Coro::Event}[3] }
127sub Coro::Event::hits { $_[0]{Coro::Event}[4] } 163sub Coro::Event::hits { $_[0]{Coro::Event}[4] }
139into the Event dispatcher. 175into the Event dispatcher.
140 176
141=cut 177=cut
142 178
143sub sweep { 179sub sweep {
144 Event::one_event(0); # for now 180 Event::one_event 0; # for now
145} 181}
146 182
147=item $result = loop([$timeout]) 183=item $result = loop([$timeout])
148 184
149This is the version of C<loop> you should use instead of C<Event::loop> 185This 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 186when using this module - it will ensure correct scheduling in the presence
151of events. 187of events.
152 188
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]) 189=item unloop([$result])
171 190
172Same as Event::unloop (provided here for your convinience only). 191Same as Event::unloop (provided here for your convinience only).
173 192
174=cut 193=cut
175 194
176$Coro::idle = new Coro sub { 195$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 196
1941; 1971;
195 198
196=back 199=back
197 200

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines