… | |
… | |
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. |
… | |
… | |
47 | the current coroutine. |
52 | the current coroutine. |
48 | |
53 | |
49 | While this seems to work superficially, it will eventually cause memory |
54 | While this seems to work superficially, it will eventually cause memory |
50 | corruption. |
55 | corruption. |
51 | |
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 | |
52 | =head1 FUNCTIONS |
69 | =head1 FUNCTIONS |
53 | |
70 | |
54 | =over 4 |
71 | =over 4 |
55 | |
72 | |
56 | =cut |
73 | =cut |
… | |
… | |
71 | use base Exporter::; |
88 | use base Exporter::; |
72 | |
89 | |
73 | our @EXPORT = qw(loop unloop sweep); |
90 | our @EXPORT = qw(loop unloop sweep); |
74 | |
91 | |
75 | BEGIN { |
92 | BEGIN { |
76 | our $VERSION = 1.9; |
93 | our $VERSION = '2.0'; |
77 | |
94 | |
78 | local $^W = 0; # avoid redefine warning for Coro::ready; |
95 | local $^W = 0; # avoid redefine warning for Coro::ready; |
79 | XSLoader::load __PACKAGE__, $VERSION; |
96 | XSLoader::load __PACKAGE__, $VERSION; |
80 | } |
97 | } |
81 | |
98 | |
… | |
… | |
90 | |
107 | |
91 | =cut |
108 | =cut |
92 | |
109 | |
93 | =item $w->next |
110 | =item $w->next |
94 | |
111 | |
95 | 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. |
96 | |
116 | |
97 | =cut |
117 | =cut |
98 | |
118 | |
99 | =item do_flavour args... |
119 | =item do_flavour args... |
100 | |
120 | |
101 | 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 | |
102 | 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 |
103 | next method often, but it does save typing sometimes. |
125 | method often, but it does save typing sometimes. |
104 | |
126 | |
105 | =cut |
127 | =cut |
106 | |
128 | |
107 | for my $flavour (qw(idle var timer io signal)) { |
129 | for my $flavour (qw(idle var timer io signal)) { |
108 | push @EXPORT, "do_$flavour"; |
130 | push @EXPORT, "do_$flavour"; |
… | |
… | |
144 | &Coro::schedule while &_next; |
166 | &Coro::schedule while &_next; |
145 | |
167 | |
146 | &_event |
168 | &_event |
147 | } |
169 | } |
148 | |
170 | |
|
|
171 | sub Coro::Event::Event::hits { $_[0][3] } |
149 | sub Coro::Event::w { $_[0] } |
172 | sub Coro::Event::Event::got { $_[0][4] } |
150 | sub Coro::Event::prio { $_[0]{Coro::Event}[3] } |
|
|
151 | sub Coro::Event::hits { $_[0]{Coro::Event}[4] } |
|
|
152 | sub Coro::Event::got { $_[0]{Coro::Event}[5] } |
|
|
153 | |
173 | |
154 | =item sweep |
174 | =item sweep |
155 | |
175 | |
156 | 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 |
157 | (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 |