… | |
… | |
2 | |
2 | |
3 | EV::Loop::Async - run an EV event loop asynchronously |
3 | EV::Loop::Async - run an EV event loop asynchronously |
4 | |
4 | |
5 | =head1 SYNOPSIS |
5 | =head1 SYNOPSIS |
6 | |
6 | |
7 | use EV::Loop::Async; |
7 | use EV::Loop::Async; |
|
|
8 | |
|
|
9 | my $loop = EV::Loop::Async::default; |
|
|
10 | my $timer; |
|
|
11 | my $flag; |
|
|
12 | |
|
|
13 | { |
|
|
14 | $loop->scope_lock; # lock the loop structures |
|
|
15 | $timer = $loop->timer (5, 1, sub { $flag = 1 }); |
|
|
16 | $loop->nudge; # tell loop to take note of the timer |
|
|
17 | } |
|
|
18 | |
|
|
19 | 1 while $flag; # $flag will be set asynchronously |
|
|
20 | |
|
|
21 | { |
|
|
22 | $loop->interrupt->scope_block; |
|
|
23 | # critical section, no watcher callback interruptions |
|
|
24 | } |
|
|
25 | |
|
|
26 | # stop timer again |
|
|
27 | |
|
|
28 | { |
|
|
29 | $loop->scope_lock; # lock the loop structures |
|
|
30 | $timer->stop; |
|
|
31 | # no need to nudge |
|
|
32 | } |
8 | |
33 | |
9 | =head1 DESCRIPTION |
34 | =head1 DESCRIPTION |
10 | |
35 | |
11 | TODO. |
36 | This module implements a rather specialised event loop - it takes a normal |
|
|
37 | L<EV> event loop and runs it in a separate thread. That means it will poll |
|
|
38 | for events even while your foreground Perl interpreter is busy (you don't |
|
|
39 | need to have perls pseudo-threads enabled for this either). |
|
|
40 | |
|
|
41 | Whenever the event loop detecs new events, it will interrupt perl and ask |
|
|
42 | it to invoke all the pending watcher callbacks. This invocation will be |
|
|
43 | "synchronous" (in the perl thread), but it can happen at any time. |
|
|
44 | |
|
|
45 | See the documentation for L<Async::Interrupt> for details on when and how |
|
|
46 | your perl program can be interrupted (and how to avoid it), and how to |
|
|
47 | integrate background event loops into foreground ones. |
|
|
48 | |
|
|
49 | =head1 FAQ |
|
|
50 | |
|
|
51 | =head1 FUNCTIONS, METHODS AND VARIABLES OF THIS MODULE |
12 | |
52 | |
13 | =over 4 |
53 | =over 4 |
14 | |
54 | |
15 | =cut |
55 | =cut |
16 | |
56 | |
… | |
… | |
34 | |
74 | |
35 | Return the default loop, usable by all programs. The default loop will be |
75 | Return the default loop, usable by all programs. The default loop will be |
36 | created on the first call to C<default> by calling X<new EV::Loop>, and |
76 | created on the first call to C<default> by calling X<new EV::Loop>, and |
37 | should be used by all programs unless they have special requirements. |
77 | should be used by all programs unless they have special requirements. |
38 | |
78 | |
39 | =cut |
79 | The associated L<Async::Interrupt> object is stored in |
|
|
80 | C<$EV::Loop::Async::AI>, and can be used to lock critical sections etc. |
40 | |
81 | |
41 | our ($LOOP, $ASYNC); |
82 | =cut |
|
|
83 | |
|
|
84 | our ($LOOP, $INTERRUPT); |
42 | |
85 | |
43 | sub default() { |
86 | sub default() { |
44 | $LOOP || do { |
87 | $LOOP || do { |
45 | $LOOP = new EV::Loop::Async; |
88 | $LOOP = new EV::Loop::Async; |
46 | # $ASYNC = $LOOP->async; |
89 | $INTERRUPT = $LOOP->interrupt; |
47 | |
90 | |
48 | $LOOP |
91 | $LOOP |
49 | } |
92 | } |
50 | } |
93 | } |
51 | |
94 | |
|
|
95 | =item $EV::Loop::Async::LOOP |
52 | |
96 | |
|
|
97 | The default async loop, available after the first call to |
|
|
98 | C<EV::Loop::Async::default>. |
|
|
99 | |
|
|
100 | =item $EV::Loop::Async::INTERRUPT |
|
|
101 | |
|
|
102 | The default loop's L<Async::Interrupt> object, for easy access. |
|
|
103 | |
|
|
104 | Example: create a section of code where no callback invocations will |
|
|
105 | interrupt: |
|
|
106 | |
|
|
107 | { |
|
|
108 | $EV::Loop::Async::INTERRUPT->scope_block; |
|
|
109 | # no default loop callbacks will be executed here. |
|
|
110 | # the loop will not be locked, however. |
|
|
111 | } |
|
|
112 | |
53 | =item $loop = new EV::Loop $flags, [Async-Interrupt-Arguments...] |
113 | =item $loop = new EV::Loop::Async $flags, [Async-Interrupt-Arguments...] |
54 | |
114 | |
55 | This constructor: |
115 | This constructor: |
56 | |
116 | |
57 | =over 4 |
117 | =over 4 |
58 | |
118 | |
59 | =item 1. creates a new C<EV::Loop> (similar C<new EV::Loop>). |
119 | =item 1. creates a new C<EV::Loop> (similar C<new EV::Loop>). |
60 | |
120 | |
61 | =item 2. creates a new L<Async::Interrupt> object and attaches itself to it. |
121 | =item 2. creates a new L<Async::Interrupt> object and attaches itself to it. |
62 | |
122 | |
63 | =item 3. locks the loop (see below). |
|
|
64 | |
|
|
65 | =item 4. creates a new background thread. |
123 | =item 3. creates a new background thread. |
66 | |
124 | |
67 | =item 5. runs C<< $loop->run >> in that thread. |
125 | =item 4. runs C<< $loop->run >> in that thread. |
68 | |
126 | |
69 | =back |
127 | =back |
|
|
128 | |
|
|
129 | The resulting loop will be running and unlocked when it is returned. |
70 | |
130 | |
71 | =cut |
131 | =cut |
72 | |
132 | |
73 | sub new { |
133 | sub new { |
74 | my ($class, $flags, @asy) = @_; |
134 | my ($class, $flags, @asy) = @_; |
… | |
… | |
106 | $loop->nudge; |
166 | $loop->nudge; |
107 | } |
167 | } |
108 | |
168 | |
109 | 1 until $flag; |
169 | 1 until $flag; |
110 | |
170 | |
|
|
171 | =item $loop->lock |
|
|
172 | |
|
|
173 | =item $loop->unlock |
|
|
174 | |
|
|
175 | Lock/unlock the loop data structures. Since the event loop runs in |
|
|
176 | a separate thread, you have to lock the loop data structures before |
|
|
177 | accessing them in any way. Since I was lazy, you have to do this manually. |
|
|
178 | |
|
|
179 | You must lock under the same conditions as you would have to lock the |
|
|
180 | underlying C library, e.g. when starting or stopping watchers (but not |
|
|
181 | when creating or destroying them, but note that create and destroy often |
|
|
182 | starts and stops for you, in which case you have to lock). |
|
|
183 | |
|
|
184 | When in doubt, lock. |
|
|
185 | |
|
|
186 | See also the next method, C<< $loop->scope_lock >> for a more failsafe way |
|
|
187 | to lock parts of your code. |
|
|
188 | |
|
|
189 | Note that there must be exactly one call of "unblock" for every previous |
|
|
190 | call to "block" (i.e. calls can nest). |
|
|
191 | |
|
|
192 | =item $loop->scope_lock |
|
|
193 | |
|
|
194 | Calls C<lock> immediately, and C<unlock> automatically whent he current |
|
|
195 | scope is left. |
|
|
196 | |
|
|
197 | =back |
|
|
198 | |
111 | =head1 SEE ALSO |
199 | =head1 SEE ALSO |
112 | |
200 | |
113 | L<EV>, L<Async::Interrupt>. |
201 | L<EV>, L<Async::Interrupt>. |
114 | |
202 | |
115 | =head1 AUTHOR |
203 | =head1 AUTHOR |