| 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
| |||||||||||||||||||||
| 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())
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())
| |||||||||||||||||||||
| 31 | || | ||||||||||||||||||||
| 32 | shmget(IPC_PRIVATE, $N, S_IRWXU); | ||||||||||||||||||||
| 33 | 22 | ▶ | die "$$: Unable to create shared memory: $!\n" unless $self->{mem}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 88 | 184 | ▶ | if($slot >= $self->{N}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 29 | ▶ | if($slot >= $self->{N}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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__ | ||||||||||||||||||||