| 1 | package Sim::Dispatcher; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 3 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use Carp qw( carp croak ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 3 | our $DEBUG = 0; | |||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 3 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 3 | ▶ | my $self = ref $_[0] ? ref shift : shift; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 3 | my %opts = @_; | |||||||||||||||||||||||||||||||||||||||||
| 15 | 3 | ▶ | croak "No clock given" if !$opts{clock}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | bless { | ||||||||||||||||||||||||||||||||||||||||||
| 17 | clock => $opts{clock}, | ||||||||||||||||||||||||||||||||||||||||||
| 18 | 3 | queue => [], | |||||||||||||||||||||||||||||||||||||||||
| 19 | }, $self; | ||||||||||||||||||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | 84 | ▶ | sub now { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
now: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 84 | $_[0]->{clock}->now; | |||||||||||||||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | 21 | ▶ | sub schedule { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
schedule: called CC=3 SCAR=11.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | 21 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 28 | 21 | my %events = @_; | |||||||||||||||||||||||||||||||||||||||||
| 29 | 21 | ▶ | while (my ($time, $handle) = each %events) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | 29 | ▶ | if ($time < $self->now) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | 0 | carp "out-dated event [$time => $handle] ignored"; | |||||||||||||||||||||||||||||||||||||||||
| 32 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||||||||||||
| 34 | 29 | $self->_insert_event([$time => $handle]); | |||||||||||||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | 29 | ▶ | sub _insert_event { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_insert_event: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 29 | my ($self, $event) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 40 | 29 | my $queue = $self->{queue}; | |||||||||||||||||||||||||||||||||||||||||
| 41 | 29 | ▶ | for (my $i = 0; $i < @$queue; $i++) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | 15 | ▶ | if ($event->[0] < $queue->[$i]->[0]) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 7 | splice( @$queue, $i, 0, $event ); | |||||||||||||||||||||||||||||||||||||||||
| 44 | 7 | return; | |||||||||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||||
| 47 | 22 | push @$queue, $event; | |||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | 26 | ▶ | sub fire_next ($) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
fire_next: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | 26 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 52 | 26 | my $queue = $self->{queue}; | |||||||||||||||||||||||||||||||||||||||||
| 53 | 26 | my $clock = $self->{clock}; | |||||||||||||||||||||||||||||||||||||||||
| 54 | 26 | ▶ | return undef if @$queue == 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 26 | my $event = shift @$queue; | |||||||||||||||||||||||||||||||||||||||||
| 56 | 26 | my ($time, $handle) = @$event; | |||||||||||||||||||||||||||||||||||||||||
| 57 | 26 | my $now = $self->now; | |||||||||||||||||||||||||||||||||||||||||
| 58 | 26 | ▶ | if ($time >= $now) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 26 | $clock->push_to($time); | |||||||||||||||||||||||||||||||||||||||||
| 60 | 26 | $handle->(); | |||||||||||||||||||||||||||||||||||||||||
| 61 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | die "Clock modified outside of the dispatcher: next event is at $time while now is $now"; | |||||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||||||
| 64 | 26 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | 6 | ▶ | sub run ($@) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
run: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | 6 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 69 | 6 | my %opts = @_; | |||||||||||||||||||||||||||||||||||||||||
| 70 | 6 | ▶ | my $end_time = $self->now + $opts{duration} if defined $opts{duration}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 6 | ▶ | my $fires = $opts{fires} || 100_000_000; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts{'fires'} || 100000000
MC/DC100%1/1
$opts{'fires'} || 100000000
$opts{'fires'}
Truth table100%2/2
A
A= $opts{'fires'}
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 6 | my $i = 0; | |||||||||||||||||||||||||||||||||||||||||
| 73 | 6 | while (1) { | |||||||||||||||||||||||||||||||||||||||||
| 74 | #warn "run: next!"; | ||||||||||||||||||||||||||||||||||||||||||
| 75 | 32 | ▶ | last if ++$i > $fires; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | 30 | my $t = $self->time_of_next; | |||||||||||||||||||||||||||||||||||||||||
| 77 | 30 | ▶ | last if !defined $t or (defined $end_time and $t > $end_time); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
not defined $t or defined $end_time and $t > $end_time
defined $end_time and $t > $end_time
MC/DC100%3/3
not defined $t or defined $end_time and $t > $end_time
not defined $t
defined $end_time
$t > $end_time
Truth table100%4/4
not(A) or B and C
A= not defined $tB= defined $end_timeC= $t > $end_time
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | 26 | $self->fire_next; | |||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | 32 | ▶ | sub time_of_next ($) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
time_of_next: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 32 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 84 | 32 | my $queue = $self->{queue}; | |||||||||||||||||||||||||||||||||||||||||
| 85 | 32 | ▶ | return @$queue ? $queue->[0]->[0] : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | 4 | ▶ | sub reset ($) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
reset: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 4 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 90 | 4 | $self->{queue} = []; | |||||||||||||||||||||||||||||||||||||||||
| 91 | 4 | $self->{clock}->reset(); | |||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 95 | __END__ | ||||||||||||||||||||||||||||||||||||||||||