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