… | |
… | |
8 | |
8 | |
9 | my $loop = EV::Loop::Async::default; |
9 | my $loop = EV::Loop::Async::default; |
10 | my $timer; |
10 | my $timer; |
11 | my $flag; |
11 | my $flag; |
12 | |
12 | |
|
|
13 | # create a watcher, but make sure the loop is locked |
13 | { |
14 | { |
14 | $loop->scope_lock; # lock the loop structures |
15 | $loop->scope_lock; # lock the loop structures |
15 | $timer = $loop->timer (5, 1, sub { $flag = 1 }); |
16 | $timer = $loop->timer (5, 1, sub { $flag = 1 }); |
16 | $loop->nudge; # tell loop to take note of the timer |
17 | $loop->notify; # tell loop to take note of the timer |
17 | } |
18 | } |
18 | |
19 | |
19 | 1 while $flag; # $flag will be set asynchronously |
20 | 1 while $flag; # $flag will be set asynchronously |
20 | |
21 | |
|
|
22 | # implement a critical section, uninterrupted by any callbacks |
21 | { |
23 | { |
22 | $loop->interrupt->scope_block; |
24 | $loop->interrupt->scope_block; |
23 | # critical section, no watcher callback interruptions |
25 | # critical section, no watcher callback interruptions |
24 | } |
26 | } |
25 | |
27 | |
26 | # stop timer again |
28 | # stop the timer watcher again - locking is required once more |
27 | |
|
|
28 | { |
29 | { |
29 | $loop->scope_lock; # lock the loop structures |
30 | $loop->scope_lock; # lock the loop structures |
30 | $timer->stop; |
31 | $timer->stop; |
31 | # no need to nudge |
32 | # no need to notify |
32 | } |
33 | } |
33 | |
34 | |
34 | =head1 DESCRIPTION |
35 | =head1 DESCRIPTION |
35 | |
36 | |
36 | This module implements a rather specialised event loop - it takes a normal |
37 | This module implements a rather specialised event loop - it takes a normal |
… | |
… | |
45 | See the documentation for L<Async::Interrupt> for details on when and how |
46 | 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 | your perl program can be interrupted (and how to avoid it), and how to |
47 | integrate background event loops into foreground ones. |
48 | integrate background event loops into foreground ones. |
48 | |
49 | |
49 | =head1 FAQ |
50 | =head1 FAQ |
|
|
51 | |
|
|
52 | =over 4 |
|
|
53 | |
|
|
54 | =item Why on earth...??? |
|
|
55 | |
|
|
56 | Sometimes you need lower latency for specific events, but it's too heavy |
|
|
57 | to continuously poll for events. And perl already does this for you |
|
|
58 | anyways, so this module only uses this existing mechanism. |
|
|
59 | |
|
|
60 | =item When do I have to lock? |
|
|
61 | |
|
|
62 | When in doubt, lock. Do not start or stop a watcher, do not create a |
|
|
63 | watcher (unless with the C<_ns> methods) and do not DESTROY an active |
|
|
64 | watcher without locking either. |
|
|
65 | |
|
|
66 | Any other event loop modifications need to be done while locked as |
|
|
67 | well. So when in doubt, lock (best using C<scope_lock>). |
|
|
68 | |
|
|
69 | =item Why explicit locking? |
|
|
70 | |
|
|
71 | Because I was too lazy to wrap everything and there are probably only a |
|
|
72 | few people on this world using this module. |
|
|
73 | |
|
|
74 | =back |
50 | |
75 | |
51 | =head1 FUNCTIONS, METHODS AND VARIABLES OF THIS MODULE |
76 | =head1 FUNCTIONS, METHODS AND VARIABLES OF THIS MODULE |
52 | |
77 | |
53 | =over 4 |
78 | =over 4 |
54 | |
79 | |
… | |
… | |
139 | $self->_attach ($asy, $asy->signal_func); |
164 | $self->_attach ($asy, $asy->signal_func); |
140 | |
165 | |
141 | $self |
166 | $self |
142 | } |
167 | } |
143 | |
168 | |
144 | =item $loop->nudge |
169 | =item $loop->notify |
145 | |
170 | |
146 | Wake up the asynchronous loop. This is useful after registering a new |
171 | Wake up the asynchronous loop. This is useful after registering a new |
147 | watcher, to ensure that the background event loop integrates the new |
172 | watcher, to ensure that the background event loop integrates the new |
148 | watcher(s) (which only happens when it iterates, which you can force by |
173 | watcher(s) (which only happens when it iterates, which you can force by |
149 | calling this method). |
174 | calling this method). |
150 | |
175 | |
151 | Without calling this method, the event loop I<eventually> takes notice |
176 | Without calling this method, the event loop I<eventually> takes notice |
152 | of new watchers, bit when this happens is not wlel-defined (can be |
177 | of new watchers, bit when this happens is not well-defined (can be |
153 | instantaneous, or take a few hours). |
178 | instantaneous, or take a few hours). |
154 | |
179 | |
155 | No locking is required. |
180 | No locking is required. |
156 | |
181 | |
157 | Example: lock the loop, create a timer, nudge the loop so it takes notice |
182 | Example: lock the loop, create a timer, nudge the loop so it takes notice |
… | |
… | |
161 | my $flag; |
186 | my $flag; |
162 | |
187 | |
163 | { |
188 | { |
164 | $loop->scope_lock; |
189 | $loop->scope_lock; |
165 | $timer = $loop->timer (1, 0, sub { $flag = 1 }); |
190 | $timer = $loop->timer (1, 0, sub { $flag = 1 }); |
166 | $loop->nudge; |
191 | $loop->notify; |
167 | } |
192 | } |
168 | |
193 | |
169 | 1 until $flag; |
194 | 1 until $flag; |
170 | |
195 | |
171 | =item $loop->lock |
196 | =item $loop->lock |