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.37 by root, Mon Nov 20 19:56:04 2006 UTC vs.
Revision 1.43 by root, Fri Dec 1 19:41:06 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines