Skip to content

Commit f597d20

Browse files
committed
Scheduler refinements/defilements
Renamed basic scheduler method to .cue Combined various other scheduler methods to use named parameters instead Added :at($instant) for a way to specify an absolute time Removed .outstanding because... Added .loads to give info on how many cues are in delayed/startable/running states
1 parent eb0f216 commit f597d20

File tree

1 file changed

+27
-21
lines changed

1 file changed

+27
-21
lines changed

S17-concurrency.pod

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -158,28 +158,28 @@ dynamically scoped, this means that test scheduler modules can be developed
158158
that poke a C<$*SCHEDULER> into C<EXPORT>, and then provide the test writer
159159
with control over time.
160160

161-
The simplest operation available on a scheduler is C<schedule>, which takes a
162-
C<Callable> object and schedules it.
161+
The C<cue> method takes a C<Callable> object and schedules it.
163162

164-
$*SCHEDULER.schedule({ say "Golly, I got scheduled!" });
163+
$*SCHEDULER.cue: { say "Golly, I got scheduled!" }
165164

166-
There is also a method to schedule an operation to run after a certain time
167-
period:
165+
Various options may be supplied as named arguments. (All references to time
166+
are taken to be in seconds, which may be fractional.) You may schedule an
167+
event to fire off after some number of seconds:
168168

169-
$*SCHEDULER.schedule-in({ say "10s later" }, 10);
169+
$*SCHEDULER.cue: in=>10, { say "10s later" }
170170

171-
And one to schedule an operation to run at a fixed interval, possibly with a
171+
or at a given absolute time, specified as an C<Instant>:
172+
173+
$*SCHEDULER.cue: at=>$instant, { say "10s later" }
174+
175+
Use C<:every> to schedule an operation to run at a fixed interval, possibly with a
172176
delay before the first scheduling.
173177

174178
# Every second, from now
175-
$*SCHEDULER.schedule-every({ say "Oh wow, a kangaroo!" }, 1);
176-
$*SCHEDULER.schedule-every({ say "Oh wow, a kangaroo!" }, 1, 0);
179+
$*SCHEDULER.cue: :every(1), { say "Oh wow, a kangaroo!" }
177180

178181
# Every 0.5s, but don't start for 2s.
179-
$*SCHEDULER.schedule-every({ say "Kenya believe it?" }, 0.5, 2);
180-
181-
[To consider: we could have a single schedule method taking named parameters
182-
:$catch, :$in, and :$every instead.]
182+
$*SCHEDULER.cue({ say "Kenya believe it?" }, :every(0.5), :in(2));
183183

184184
If a scheduled item dies, the scheduler will catch this exception and pass it
185185
to a C<handle_uncaught> method, a default implementation of which is provided
@@ -193,22 +193,28 @@ the entire application to terminate. However, it is possible to replace this:
193193
For more fine-grained handling, it is possible to schedule code along with a
194194
code object to be invoked with the thrown exception if it dies:
195195

196-
$*SCHEDULER.schedule_with_catch(
196+
$*SCHEDULER.cue:
197197
{ upload_progress($stuff) },
198-
-> $ex { warn "Could not upload latest progress" });
198+
catch => -> $ex { warn "Could not upload latest progress" }
199+
200+
Schedulers also provide counts of the number of operations in various states:
199201

200-
Schedulers also provide a count of the number of outstanding operations to be
201-
performed:
202+
say $*SCHEDULER.loads;
202203

203-
say $*SCHEDULER.outstanding;
204+
This returns, in order, the number of cues that are not yet runnable due to
205+
delays, the number of cues that are runnable but not yet assigned to a thread,
206+
and the number of cues that are now assigned to a thread (and presumably running).
207+
[Conjecture: perhaps these should be separate methods.]
204208

205-
They may optionally provide further introspection in order to support tools
209+
Schedulers may optionally provide further introspection in order to support tools
206210
such as debuggers.
207211

208212
There is also a C<CurrentThreadScheduler>, which always schedules things on
209213
the current thread. It provides the same methods, just no concurrency, and
210214
any exceptions are thrown immediately. This is mostly useful for forcing
211-
synchrony in places that default to asynchrony.
215+
synchrony in places that default to asynchrony. (Note that C<.loads> can never
216+
return anything but 0 for the currently running cues, since they're waiting
217+
on the current thread to stop scheduling first!)
212218

213219
=head1 Promises
214220

@@ -324,7 +330,7 @@ C<Promise.alarm> factory is implemented:
324330
method alarm(Promise:U: $seconds, :$scheduler = $*SCHEDULER) {
325331
my $p = Promise.new(:$scheduler);
326332
my $k = $p.keeper;
327-
$scheduler.schedule-in({ $k.keep(True) }, $seconds);
333+
$scheduler.cue: { $k.keep(True) }, :in($seconds);
328334
$p
329335
}
330336

0 commit comments

Comments
 (0)