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.45 by root, Fri Dec 29 11:37:49 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 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
42=over 4 66=over 4
43 67
44=cut 68=cut
45 69
46package Coro::Event; 70package Coro::Event;
47 71
48BEGIN { eval { require warnings } && warnings->unimport ("uninitialized") } 72no warnings;
49 73
50use Carp; 74use Carp;
51no warnings; 75no warnings;
52 76
53use Coro; 77use Coro;
56 80
57use XSLoader; 81use XSLoader;
58 82
59use base Exporter::; 83use base Exporter::;
60 84
61our @EXPORT = qw(loop unloop sweep reschedule); 85our @EXPORT = qw(loop unloop sweep);
62 86
63BEGIN { 87BEGIN {
64 our $VERSION = 1.9; 88 our $VERSION = 1.9;
65 89
66 local $^W = 0; # avoid redefine warning for Coro::ready; 90 local $^W = 0; # avoid redefine warning for Coro::ready;
67 XSLoader::load __PACKAGE__, $VERSION; 91 XSLoader::load __PACKAGE__, $VERSION;
68} 92}
69 93
70=item $w = Coro::Event->flavour(args...) 94=item $w = Coro::Event->flavour (args...)
71 95
72Create and return a watcher of the given type. 96Create and return a watcher of the given type.
73 97
74Examples: 98Examples:
75 99
82 106
83Return the next event of the event queue of the watcher. 107Return the next event of the event queue of the watcher.
84 108
85=cut 109=cut
86 110
87=item do_flavour(args...) 111=item do_flavour args...
88 112
89Create 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
90method. This is less efficient then calling the constructor once and the 114method. This is less efficient then calling the constructor once and the
91next method often, but it does save typing sometimes. 115next method often, but it does save typing sometimes.
92 116
104 128
105 shift eq Coro::Event:: 129 shift eq Coro::Event::
106 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";
107 131
108 my $w = $new->($class, 132 my $w = $new->($class,
109 desc => $flavour, 133 desc => $flavour,
110 @_, 134 @_,
111 parked => 1, 135 parked => 1,
112 ); 136 );
137
113 _install_std_cb($w, $type); 138 _install_std_cb $w, $type;
114 bless $w, $class; # reblessing due to broken Event 139
140 # reblessing due to Event being broken
141 bless $w, $class
115 }; 142 };
116 *{ $flavour } = $coronew; 143 *{ $flavour } = $coronew;
117 *{"do_$flavour"} = sub { 144 *{"do_$flavour"} = sub {
118 unshift @_, Coro::Event::; 145 unshift @_, Coro::Event::;
119 my $e = (&$coronew)->next; 146 @_ = &$coronew;
120 $e->cancel; # $e === $e->w 147 &Coro::schedule while &_next;
121 $e; 148 $_[0]->cancel;
149 &_event
122 }; 150 };
123} 151}
124 152
125# double calls to avoid stack-cloning ;() 153# do schedule in perl to avoid forcing a stack allocation.
126# is about 10% slower, though. 154# this is about 10% slower, though.
127sub next($) { 155sub next($) {
128 &Coro::schedule if &_next; $_[0]; 156 &Coro::schedule while &_next;
157
158 &_event
129} 159}
130 160
131sub Coro::Event::w { $_[0] } 161sub Coro::Event::w { $_[0] }
132sub Coro::Event::prio { $_[0]{Coro::Event}[3] } 162sub Coro::Event::prio { $_[0]{Coro::Event}[3] }
133sub Coro::Event::hits { $_[0]{Coro::Event}[4] } 163sub Coro::Event::hits { $_[0]{Coro::Event}[4] }
145into the Event dispatcher. 175into the Event dispatcher.
146 176
147=cut 177=cut
148 178
149sub sweep { 179sub sweep {
150 Event::one_event(0); # for now 180 Event::one_event 0; # for now
151} 181}
152 182
153=item $result = loop([$timeout]) 183=item $result = loop([$timeout])
154 184
155This 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>

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines