-
-
Notifications
You must be signed in to change notification settings - Fork 372
/
ThreadPoolScheduler.pm
803 lines (728 loc) Β· 30.2 KB
/
ThreadPoolScheduler.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
my class ThreadPoolScheduler does Scheduler {
# A concurrent, blocking-on-receive queue.
my class Queue is repr('ConcBlockingQueue') {
method elems() is raw { nqp::elems(self) }
}
# Initialize $*PID here, as we need it for the debug message
# anyway *and* it appears to have a positive effect on stability
# specifically wrt GH #1202.
PROCESS::<$PID> := nqp::p6box_i(my $pid := nqp::getpid);
# Scheduler debug, controlled by an environment variable.
my int $scheduler-debug = so %*ENV<RAKUDO_SCHEDULER_DEBUG>;
my int $scheduler-debug-status = so %*ENV<RAKUDO_SCHEDULER_DEBUG_STATUS>;
sub scheduler-debug($message --> Nil) {
if $scheduler-debug {
note "[SCHEDULER $pid] $message";
}
}
sub scheduler-debug-status($message --> Nil) {
if $scheduler-debug-status {
note "[SCHEDULER $pid] $message";
}
}
# Infrastructure for non-blocking `await` for code running on the
# scheduler.
my constant THREAD_POOL_PROMPT = Mu.new;
class ThreadPoolAwaiter does Awaiter {
has $!queue;
submethod BUILD(:$queue!) {
$!queue := nqp::decont($queue);
}
sub holding-locks() {
nqp::p6bool(nqp::threadlockcount(nqp::currentthread()))
}
method await(Awaitable:D $a) {
holding-locks() || !nqp::isnull(nqp::getlexdyn('$*RAKUDO-AWAIT-BLOCKING'))
?? Awaiter::Blocking.await($a)
!! self!do-await($a)
}
method !do-await(Awaitable:D $a) {
my $handle := $a.get-await-handle;
if $handle.already {
$handle.success
?? $handle.result
!! $handle.cause.rethrow
}
else {
my $success;
my $result;
nqp::continuationcontrol(1, THREAD_POOL_PROMPT, -> Mu \c {
$handle.subscribe-awaiter(-> \success, \result {
$success := success;
$result := result;
nqp::push($!queue, { nqp::continuationinvoke(c, nqp::null()) });
Nil
});
});
$success
?? $result
!! $result.rethrow
}
}
method await-all(Iterable:D \i) {
holding-locks() || !nqp::isnull(nqp::getlexdyn('$*RAKUDO-AWAIT-BLOCKING'))
?? Awaiter::Blocking.await-all(i)
!! self!do-await-all(i)
}
method !do-await-all(Iterable:D \i) {
# Collect results that are already available, and handles where the
# results are not yet available together with the matching insertion
# indices.
my \results = nqp::list();
my \handles = nqp::list();
my \indices = nqp::list_i();
my int $insert = 0;
my $saw-slip = False;
for i -> $awaitable {
unless nqp::istype($awaitable, Awaitable) {
die "Can only specify Awaitable objects to await (got a $awaitable.^name())";
}
unless nqp::isconcrete($awaitable) {
die "Must specify a defined Awaitable to await (got an undefined $awaitable.^name())";
}
my $handle := $awaitable.get-await-handle;
if $handle.already {
if $handle.success {
my \result = $handle.result;
nqp::bindpos(results, $insert, result);
$saw-slip = True if nqp::istype(result, Slip);
}
else {
$handle.cause.rethrow
}
}
else {
nqp::push(handles, $handle);
nqp::push_i(indices, $insert);
}
++$insert;
}
# See if we have anything that we really need to suspend for. If
# so, we need to take great care that the continuation taking is
# complete before we try to resume it (completions can happen on
# different threads, and so concurrent with us subscribing, not
# to mention concurrent with each other wanting to resume). We
# use a lock to take care of this, holding the lock until the
# continuation has been taken.
my int $num-handles = nqp::elems(handles);
if $num-handles {
my $continuation;
my $exception;
my $l = Lock.new;
$l.lock;
{
my int $remaining = $num-handles;
loop (my int $i = 0; $i < $num-handles; ++$i) {
my $handle := nqp::atpos(handles, $i);
my int $insert = nqp::atpos_i(indices, $i);
$handle.subscribe-awaiter(-> \success, \result {
my int $resume;
$l.protect: {
if success && $remaining {
nqp::bindpos(results, $insert, result);
$saw-slip = True if nqp::istype(result, Slip);
--$remaining;
$resume = 1 unless $remaining;
}
elsif !nqp::isconcrete($exception) {
$exception := result;
$remaining = 0;
$resume = 1;
}
}
if $resume {
nqp::push($!queue, {
nqp::continuationinvoke($continuation, nqp::null())
});
}
});
}
CATCH {
# Unlock if we fail here, and let the exception
# propagate outwards.
$l.unlock();
}
}
nqp::continuationcontrol(1, THREAD_POOL_PROMPT, -> Mu \c {
$continuation := c;
$l.unlock;
});
# If we got an exception, throw it.
$exception.rethrow if nqp::isconcrete($exception);
}
my \result-list = nqp::p6bindattrinvres(nqp::create(List), List, '$!reified', results);
$saw-slip ?? result-list.map(-> \val { val }).List !! result-list
}
}
# There are three kinds of worker:
# * General worker threads all pull from the main queue. If they have no
# work, they may steal from timer threads.
# * Timer worker threads are intended to handle time-based events. They
# pull events from the time-sensitive queue, and they will not do any
# work stealing so as to be ready and available for timer events. The
# time-sensitive queue will only be returned when a queue is requested
# with the :hint-time-sensitive named argument. Only one timer worker
# will be created on the first request for such a queue; the supervisor
# will then monitor the time-sensitive queue length and add more if
# needed.
# * Affinity worker threads each have their own queue. They are used when
# a queue is requested and :hint-affinity is passed. These are useful
# for things like Proc::Async and IO::Socket::Async, where events will
# be processed using a Supply, which is serial, and so there's no point
# at all in contending over the data. Work will not be stolen from an
# affinity worker thread.
my role Worker {
has $.thread;
has $!scheduler;
# Completed is the number of tasks completed since the last time the
# supervisor checked in.
#?if moar
has atomicint $.completed;
#?endif
#?if !moar
has int $.completed;
#?endif
# Total number of tasks completed since creation.
has int $.total;
# Working is 1 if the worker is currently busy, 0 if not.
has int $.working;
# Number of times take-completed has returned zero in a row.
has int $.times-nothing-completed;
# Resets the completed to zero and updates the total.
method take-completed() {
#?if moar
my atomicint $taken;
cas $!completed, -> atomicint $current { $taken = $current; 0 }
#?endif
#?if !moar
my int $taken = $!completed;
$!completed = 0;
#?endif
if $taken == 0 {
++$!times-nothing-completed;
}
else {
$!times-nothing-completed = 0;
}
$taken
}
method !run-one(\task --> Nil) {
$!working = 1;
nqp::continuationreset(THREAD_POOL_PROMPT, {
if nqp::istype(task, List) {
my Mu $code := nqp::shift(nqp::getattr(task, List, '$!reified'));
$code(|task);
}
else {
task.();
}
CONTROL {
default {
my Mu $vm-ex := nqp::getattr(nqp::decont($_), Exception, '$!ex');
nqp::getcomp('perl6').handle-control($vm-ex);
}
}
CATCH {
default {
$!scheduler.handle_uncaught($_)
}
}
});
$!working = 0;
#?if moar
++β$!completed;
#?endif
#?if !moar
++$!completed;
#?endif
++$!total;
}
}
my class GeneralWorker does Worker {
has Queue $!queue;
submethod BUILD(Queue :$queue!, :$!scheduler!) {
$!queue := $queue;
$!thread = Thread.start(:app_lifetime, :name<GeneralWorker>, {
my $*AWAITER := ThreadPoolAwaiter.new(:$!queue);
loop {
self!run-one(nqp::shift($queue));
}
});
}
}
my class TimerWorker does Worker {
has Queue $!queue;
submethod BUILD(Queue :$queue!, :$!scheduler!) {
$!queue := $queue;
$!thread = Thread.start(:app_lifetime, :name<TimerWorker>, {
my $*AWAITER := ThreadPoolAwaiter.new(:$!queue);
loop {
self!run-one(nqp::shift($queue));
}
});
}
}
my class AffinityWorker does Worker {
has Queue $.queue;
submethod BUILD(:$!scheduler!) {
my $queue := $!queue := Queue.CREATE;
$!thread = Thread.start(:app_lifetime, :name<AffinityWorker>, {
my $*AWAITER := ThreadPoolAwaiter.new(:$!queue);
loop {
self!run-one(nqp::shift($queue));
}
});
}
}
# Initial and maximum threads allowed.
has Int $.initial_threads;
has Int $.max_threads;
# All of the worker and queue state below is guarded by this lock.
has Lock $!state-lock = Lock.new;
# The general queue and timer queue, if created.
has Queue $!general-queue;
has Queue $!timer-queue;
# The current lists of workers. Immutable lists; new ones are produced
# upon changes.
has $!general-workers;
has $!timer-workers;
has $!affinity-workers;
has int $!exhausted;
# The supervisor thread, if started.
has Thread $!supervisor;
method !general-queue() {
unless $!general-queue.DEFINITE {
$!state-lock.protect: {
unless $!general-queue.DEFINITE {
# We don't have any workers yet, so start one.
$!general-queue := nqp::create(Queue);
$!general-workers := first-worker(
GeneralWorker.new(
queue => $!general-queue,
scheduler => self
)
);
scheduler-debug "Created initial general worker thread";
self!maybe-start-supervisor();
}
}
}
$!general-queue
}
method !timer-queue() {
unless $!timer-queue.DEFINITE {
$!state-lock.protect: {
unless $!timer-queue.DEFINITE {
# We don't have any workers yet, so start one.
$!timer-queue := nqp::create(Queue);
$!timer-workers := first-worker(
TimerWorker.new(
queue => $!timer-queue,
scheduler => self
)
);
scheduler-debug "Created initial timer worker thread";
self!maybe-start-supervisor();
}
}
}
$!timer-queue
}
constant @affinity-add-thresholds = 1, 5, 10, 20, 50, 100;
method !affinity-queue() {
# If there's no affinity workers, start one.
my $cur-affinity-workers := $!affinity-workers;
if $cur-affinity-workers.elems == 0 {
$!state-lock.protect: {
if $!affinity-workers.elems == 0 {
# We don't have any affinity workers yet, so start one
# and return its queue.
$!affinity-workers := first-worker(
AffinityWorker.new(
scheduler => self
)
);
scheduler-debug "Created initial affinity worker thread";
self!maybe-start-supervisor();
return $!affinity-workers[0].queue;
}
}
$cur-affinity-workers := $!affinity-workers; # lost race for first
}
# Otherwise, see which has the least load (this is inherently racey
# and approximate, but enough to help us avoid a busy worker). If we
# find an empty queue, return it immediately.
my $most-free-worker;
my int $i = -1;
nqp::while(
++$i < nqp::elems($cur-affinity-workers),
nqp::if(
$most-free-worker.DEFINITE,
nqp::stmts(
(my $cand := nqp::atpos($cur-affinity-workers,$i)),
nqp::unless(
(my $queue := $cand.queue).elems,
(return $queue)
),
nqp::if(
nqp::islt_i($queue.elems,$most-free-worker.queue.elems),
$most-free-worker := $cand
)
),
($most-free-worker := nqp::atpos($cur-affinity-workers,$i))
)
);
# Otherwise, check if the queue beats the threshold to add another
# worker thread.
my $chosen-queue := $most-free-worker.queue;
my $threshold = @affinity-add-thresholds[
($cur-affinity-workers.elems min @affinity-add-thresholds) - 1
];
if $chosen-queue.elems > $threshold {
# Add another one, unless another thread did too.
$!state-lock.protect: {
if self!total-workers() >= $!max_threads {
scheduler-debug "Will not add extra affinity worker; hit $!max_threads thread limit";
return $chosen-queue;
}
if $cur-affinity-workers.elems != $!affinity-workers.elems {
return $chosen-queue;
}
my $new-worker := AffinityWorker.new(scheduler => self);
$!affinity-workers := push-worker($!affinity-workers,$new-worker);
scheduler-debug "Added an affinity worker thread";
$new-worker.queue
}
}
else {
$chosen-queue
}
}
# Initializing a worker list with a worker, is straightforward and devoid
# of concurrency issues, as we're already in protected code when we do this.
sub first-worker(\first) is raw {
my $workers := nqp::create(IterationBuffer);
nqp::push($workers,first);
$workers
}
# Since the worker lists can be changed during copying, we need to
# just take whatever we can get and assume that it may be gone by
# the time we get to it.
sub push-worker(\workers, \to-push) is raw {
my $new-workers := nqp::clone(workers);
nqp::push($new-workers,to-push);
$new-workers
}
# The supervisor sits in a loop, mostly sleeping. Each time it wakes up,
# it takes stock of the current situation and decides whether or not to
# add threads.
my constant SUPERVISION_INTERVAL = 0.01;
my constant NUM_SAMPLES = 5;
my constant EXHAUSTED_RETRY_AFTER = 100;
method !maybe-start-supervisor(--> Nil) {
unless $!supervisor.DEFINITE {
$!supervisor = Thread.start(:app_lifetime, :name<Supervisor>, {
sub add-general-worker(--> Nil) {
$!state-lock.protect: {
$!general-workers := push-worker(
$!general-workers,
GeneralWorker.new(
queue => $!general-queue,
scheduler => self
)
);
}
scheduler-debug "Added a general worker thread";
}
sub add-timer-worker(--> Nil) {
$!state-lock.protect: {
$!timer-workers := push-worker(
$!timer-workers,
TimerWorker.new(
queue => $!timer-queue,
scheduler => self
)
);
}
scheduler-debug "Added a timer worker thread";
}
sub getrusage-total() is raw {
my \rusage = nqp::getrusage();
nqp::atpos_i(rusage, nqp::const::RUSAGE_UTIME_SEC) * 1000000
+ nqp::atpos_i(rusage, nqp::const::RUSAGE_UTIME_MSEC)
+ nqp::atpos_i(rusage, nqp::const::RUSAGE_STIME_SEC) * 1000000
+ nqp::atpos_i(rusage, nqp::const::RUSAGE_STIME_MSEC)
}
scheduler-debug "Supervisor started";
my num $last-rusage-time = nqp::time_n;
my int $last-usage = getrusage-total;
#?if !jvm
my num @last-utils = 0e0 xx NUM_SAMPLES;
#?endif
#?if jvm
my @last-utils = 0e0 xx NUM_SAMPLES;
#?endif
my int $cpu-cores = nqp::cpucores();
scheduler-debug "Supervisor thinks there are $cpu-cores CPU cores";
loop {
# Wait until the next time we should check how things
# are.
sleep SUPERVISION_INTERVAL;
# Work out the delta of CPU usage since last supervision
# and the time period that measurement spans.
my num $now = nqp::time_n;
my num $rusage-period = $now - $last-rusage-time;
$last-rusage-time = $now;
my int $current-usage = getrusage-total();
my int $usage-delta = $current-usage - $last-usage;
$last-usage = $current-usage;
# Scale this by the time between rusage calls and turn it
# into a per-core utilization percentage.
my num $normalized-delta = $usage-delta / $rusage-period;
my num $per-core = $normalized-delta / $cpu-cores;
my num $per-core-util =
100 * ($per-core / (1000000 * NUM_SAMPLES));
# Since those values are noisy, average the last
# NUM_SAMPLES values to get a smoothed value.
nqp::shift_n(@last-utils);
nqp::push_n(@last-utils,$per-core-util);
my $smooth-per-core-util = @last-utils.sum;
scheduler-debug-status "Per-core utilization (approx): $smooth-per-core-util%"
if $scheduler-debug-status;
# exhausted the system allotment of low level threads
if $!exhausted {
$!exhausted = 0 if ++$!exhausted > EXHAUSTED_RETRY_AFTER;
}
# we can still add threads if necessary
else {
self!tweak-workers($!general-queue, $!general-workers,
&add-general-worker, $cpu-cores, $smooth-per-core-util)
if $!general-queue.DEFINITE && $!general-queue.elems;
self!tweak-workers($!timer-queue, $!timer-workers,
&add-timer-worker, $cpu-cores, $smooth-per-core-util)
if $!timer-queue.DEFINITE && $!timer-queue.elems;
}
# always need to prod affinity workers
self!prod-affinity-workers: $!affinity-workers
if $!affinity-workers.DEFINITE;
CATCH {
when X::AdHoc {
if .payload.starts-with("Could not create a new Thread") {
$!exhausted = 1;
scheduler-debug "Refraining from trying to start new threads";
}
else {
scheduler-debug .gist;
}
}
default {
scheduler-debug .gist;
}
}
}
});
}
}
method !prod-affinity-workers (\worker-list --> Nil) {
for ^worker-list.elems {
my $worker := worker-list[$_];
if $worker.working {
$worker.take-completed;
# If an affinity worker completed nothing for some time,
# steal an item from its queue, moving it to general queue.
# This resolves deadlocks in certain cases.
if $worker.times-nothing-completed > 10 {
scheduler-debug "Stealing queue from affinity worker";
my $item := nqp::queuepoll($worker.queue);
nqp::push(self!general-queue, $item)
unless nqp::isnull($item);
}
}
}
}
# Tweak workers for non-empty queues
method !tweak-workers(\queue, \worker-list, &add-worker, $cores, $per-core-util) {
# Go through the worker list. If something is not working, then there
# is at least one worker free to process things in the queue, so we
# don't need to add one.
my int $total-completed;
my int $total-times-nothing-completed;
my int $i = -1;
nqp::while(
++$i < nqp::elems(worker-list),
nqp::if(
(my $worker := nqp::atpos(worker-list,$i)).working,
nqp::stmts(
($total-completed += $worker.take-completed),
($total-times-nothing-completed += $worker.times-nothing-completed)
),
return
)
);
sub heuristic-check-for-deadlock(--> Nil) {
my int $average-times-nothing-completed
= $total-times-nothing-completed div (worker-list.elems || 1);
if $average-times-nothing-completed > 20 {
scheduler-debug "Heuristic queue progress deadlock situation detected";
add-worker();
}
}
# If we didn't complete anything, then consider adding more threads.
my int $total-workers = self!total-workers();
if $total-completed == 0 {
if $total-workers < $!max_threads {
# There's something in the queue and we haven't completed it.
# If we are still below the CPU core count, just add a worker.
if $total-workers < $cores {
add-worker();
}
# Otherwise, consider utilization. If it's very little then a
# further thread may be needed for deadlock breaking.
elsif $per-core-util < 2 {
scheduler-debug "Heuristic low utilization deadlock situation detected";
add-worker();
}
# Another form of deadlock can happen when one kind of queue
# is being processed but another is not. In that case, the
# number of iterations since nothing was completed by any
# worker will grow.
else {
heuristic-check-for-deadlock
}
}
else {
scheduler-debug "Will not add extra worker; hit $!max_threads thread limit [branch with 0 total completed]";
}
}
elsif $total-times-nothing-completed > 20*$cores {
if $total-workers < $!max_threads {
heuristic-check-for-deadlock
}
else {
scheduler-debug "Will not add extra worker; hit $!max_threads thread limit [branch with some total completed]";
}
}
}
method !total-workers() is raw {
nqp::elems($!general-workers)
+ nqp::elems($!timer-workers)
+ nqp::elems($!affinity-workers)
}
submethod BUILD(
Int :$!initial_threads = 0,
Int :$!max_threads = (%*ENV<RAKUDO_MAX_THREADS> // 64).Int
--> Nil
) {
die "Initial thread pool threads ($!initial_threads) must be less than or equal to maximum threads ($!max_threads)"
if $!initial_threads > $!max_threads;
$!general-workers := nqp::create(IterationBuffer);
$!timer-workers := nqp::create(IterationBuffer);
$!affinity-workers := nqp::create(IterationBuffer);
if $!initial_threads > 0 {
# We've been asked to make some initial threads; we interpret this
# as general workers.
$!general-queue := nqp::create(Queue);
nqp::push(
$!general-workers,
GeneralWorker.new(
queue => $!general-queue,
scheduler => self
)
) for ^$!initial_threads;
scheduler-debug "Created scheduler with $!initial_threads initial general workers";
self!maybe-start-supervisor();
}
else {
scheduler-debug "Created scheduler without initial general workers";
}
}
method queue(Bool :$hint-time-sensitive, :$hint-affinity) {
if $hint-affinity {
self!affinity-queue()
}
elsif $hint-time-sensitive {
self!timer-queue()
}
else {
self!general-queue()
}
}
my class TimerCancellation is repr('AsyncTask') { }
method cue(&code, :$at, :$in, :$every, :$times = 1, :&stop is copy, :&catch ) {
die "Cannot specify :at and :in at the same time"
if $at.defined and $in.defined;
die "Cannot specify :every, :times and :stop at the same time"
if $every.defined and $times > 1 and &stop;
# For $in/$at times, if the resultant delay is less than 0.001 (including
# negatives) equate those to zero. For $every intervals, we convert
# such values to minimum resolution of 0.001 and warn about that
sub to-millis(Numeric() $value, $allow-zero = False) {
my $proposed := (1000 * $value).Int;
$proposed > 0 ?? $proposed
!! $allow-zero ?? 0
!! do {warn "Minimum timer resolution is 1ms; using that "
~ "instead of {1000 * $value}ms";
1}
}
my $delay = to-millis ($at ?? $at - now !! $in // 0), True;
# Wrap any catch handler around the code to run.
my &run := &catch ?? wrap-catch(&code, &catch) !! &code;
# need repeating
if $every {
# generate a stopper if needed
if $times > 1 {
my $todo = $times;
&stop = sub { $todo ?? !$todo-- !! True }
}
# we have a stopper
if &stop {
my $handle;
my $cancellation;
sub cancellation() {
$cancellation //=
Cancellation.new(async_handles => [$handle]);
}
$handle := nqp::timer(self!timer-queue(),
{ stop() ?? cancellation().cancel !! run() },
$delay, to-millis($every),
TimerCancellation);
cancellation()
}
# no stopper
else {
my $handle := nqp::timer(self!timer-queue(), &run,
$delay, to-millis($every),
TimerCancellation);
Cancellation.new(async_handles => [$handle])
}
}
# only after waiting a bit or more than once
elsif $delay or $times > 1 {
my @async_handles;
@async_handles.push(
nqp::timer(self!timer-queue(), &run, $delay, 0, TimerCancellation)
) for 1 .. $times;
Cancellation.new(:@async_handles)
}
# just cue the code
else {
nqp::push(self!general-queue(), &run);
Nil
}
}
sub wrap-catch(&code, &catch) {
-> { code(); CATCH { default { catch($_) } } }
}
method loads() is raw {
my int $loads = 0;
$loads = $loads + $!general-queue.elems if $!general-queue;
$loads = $loads + $!timer-queue.elems if $!timer-queue;
my int $i = -1;
nqp::while(
++$i < nqp::elems($!affinity-workers),
$loads = $loads + nqp::atpos($!affinity-workers,$i).queue.elems
);
$loads
}
}
# vim: ft=perl6 expandtab sw=4