blib/lib/POE/Component/Daemon/Scoreboard.pm

filter: Statementstmt 89.6% 69 / 77 Branchbran 54.5% 12 / 22 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 12 / 12 Podpod 0.0% 0 / 7 totaltotal 76.4% 94 / 123 CC 13 SCAR 11.1
CC
13
Cov
80%
write
4.6
read
4.6
new
4.5
CRAP 3.0
« eg/forking ↑ Summary
1 #$Id: Scoreboard.pm 760 2011-05-18 18:14:30Z fil $
2 ########################################################
3 package POE::Component::Daemon::Scoreboard;
4  
5 24 ▶use 5.00405;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 24 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 24 ▶use vars qw($VERSION $UNIQUE);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 24 ▶use IPC::SysV qw(IPC_PRIVATE S_IRWXU IPC_CREAT SEM_UNDO);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 24 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 24 $VERSION = '0.1300';
14  
15 sub DEBUG () { 0 }
16  
17 ########################################################
18 sub new
19 22 ▶{
Subroutine new: called CC=4 SCAR=15.0
Pod undocumented
20 22 my($package, $N)=@_;
21 22 ▶ if($UNIQUE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($POE::Component::Daemon::Scoreboard::UNIQUE) 0 22 22
22 0 warn "This should be only one $package. Reusing previous one.";
23 0 return $UNIQUE;
24 }
25  
26 22 my $self=bless {N=>$N}, $package;
27  
28 # On linux, 2.6 kernels (at least), the first call after a reboot will
29 # fail, second and subsequent will succeed.
30 22 ▶ $self->{mem} = shmget(IPC_PRIVATE, $N, S_IRWXU)
Condition33%1/3
shmget(IPC_PRIVATE(), $N, S_IRWXU()) || shmget(IPC_PRIVATE(), $N, S_IRWXU())
l!l&&r!l&&!r
2200
MC/DC0%0/2
shmget(IPC_PRIVATE(), $N, S_IRWXU()) || shmget(IPC_PRIVATE(), $N, S_IRWXU())
shmget(IPC_PRIVATE(), $N, S_IRWXU()) shmget(IPC_PRIVATE(), $N, S_IRWXU())
Truth table33%1/3
A || B
A= shmget(IPC_PRIVATE(), $N, S_IRWXU())B= shmget(IPC_PRIVATE(), $N, S_IRWXU())
ABresult
1X1
011
000
31 ||
32 shmget(IPC_PRIVATE, $N, S_IRWXU);
33 22 ▶ die "$$: Unable to create shared memory: $!\n" unless $self->{mem};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"mem"} 0 22 22
34  
35 22 $self->{slots}=[reverse 0..($N-1)];
36  
37 22 my $blank=' ' x $N;
38 22 shmwrite($self->{mem}, $blank, 0, $N);
39  
40 22 $UNIQUE=$self;
41  
42 22 return $self;
43 }
44  
45 ########################################################
46 sub read_all
47 164 ▶{
Subroutine read_all: called CC=2 SCAR=7.0
Pod undocumented
48 164 my($self)=@_;
49  
50 164 my $str=" " x $self->{N};
51 shmread($self->{mem}, $str, 0, $self->{N})
52 164 ▶ or die "Unable to read shared memory: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
shmread $self->{"mem"}, $str, 0, $self->{"N"} or die "Unable to read shared memory: $!\n" 164 0 164
53  
54 164 my $ret=[split //, $str];
55 164 return $ret;
56 }
57  
58 ########################################################
59 sub add
60 103 ▶{
Subroutine add: called CC=2 SCAR=6.9
Pod undocumented
61 103 my($self, $value)=@_;
62 103 ▶ return unless @{$self->{slots}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"slots"};} 1 102 103
63 102 my $slot=pop @{$self->{slots}};
64 102 DEBUG and warn "Adding slot $slot";
65 102 $self->write($slot, $value);
66 102 return $slot;
67 }
68  
69 ########################################################
70 sub drop
71 20 ▶{
Subroutine drop: called CC=2 SCAR=7.3
Pod undocumented
72 20 my($self, $slot)=@_;
73 20 ▶ if($slot >= $self->{N}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($slot >= $self->{"N"}) 0 20 20
74 0 carp "$slot isn't a known slot\n";
75 0 return;
76 }
77 20 $self->write($slot, '.');
78 20 DEBUG and warn "Dropped slot $slot";
79 20 push @{$self->{slots}}, $slot;
80 20 return;
81 }
82  
83 ########################################################
84 sub write
85 184 ▶{
Subroutine write: called CC=4 SCAR=15.2
Pod undocumented
86 184 my($self, $slot, $value)=@_;
87 184 ▶ croak "$$: Missing slot" unless defined $slot;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $slot 0 184 184
88 184 ▶ if($slot >= $self->{N}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($slot >= $self->{"N"}) 0 184 184
89 0 carp "$slot isn't a known slot\n";
90 0 return;
91 }
92  
93 184 $value=substr($value, 0, 1);
94 184 DEBUG and warn "Setting slot $slot to $value";
95  
96 184 ▶ shmwrite($self->{mem}, $value, $slot, 1)
Branch: 1 branch
BranchTrueFalseTotal
shmwrite $self->{"mem"}, $value, $slot, 1 or warn "Writing shared memory slot ${slot}: $!" 184 0 184
97 or warn "Writing shared memory slot $slot: $!";
98  
99 184 return;
100 }
101  
102 ########################################################
103 sub read
104 29 ▶{
Subroutine read: called CC=4 SCAR=15.2
Pod undocumented
105 29 my($self, $slot)=@_;
106 29 ▶ return unless defined $slot;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $slot 0 29 29
107  
108 29 ▶ if($slot >= $self->{N}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($slot >= $self->{"N"}) 0 29 29
109 0 carp "$slot isn't a known slot\n";
110 0 return;
111 }
112 29 DEBUG and warn "Reading value $slot";
113  
114 29 my $value=" ";
115 29 ▶ shmread($self->{mem}, $value, $slot, 1)
Branch: 1 branch
BranchTrueFalseTotal
shmread $self->{"mem"}, $value, $slot, 1 or warn "Reading shared memory slot ${slot}: $!" 29 0 29
116 or warn "Reading shared memory slot $slot: $!";
117 29 return $value;
118 }
119  
120 ########################################################
121 sub status
122 1 ▶{
Subroutine status: called CC=1 SCAR=0.0
Pod undocumented
123 1 my($self)=@_;
124 1 my @ret;
125  
126 1 my $n=$self->read_all();
127 1 push @ret, ref($self);
128 1 push @ret, "$self->{N} slots in the scoreboard";
129 1 push @ret, join '', "Slots [", @$n, "]";
130 1 push @ret, (0+@{$self->{slots}})." slots free";
131  
132 1 return join "\n ", @ret;
133 }
134  
135 24 1;
136  
137 __DATA__
« eg/forking ↑ Summary