| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package IPC::ScoreBoard; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 8 |  |  | 8 |  | 174320 | use 5.008008; | 
|  | 8 |  |  |  |  | 32 |  | 
|  | 8 |  |  |  |  | 264 |  | 
| 4 | 8 |  |  | 8 |  | 88 | use strict; | 
|  | 8 |  |  |  |  | 8 |  | 
|  | 8 |  |  |  |  | 248 |  | 
| 5 | 8 |  |  | 8 |  | 32 | use warnings; | 
|  | 8 |  |  |  |  | 48 |  | 
|  | 8 |  |  |  |  | 264 |  | 
| 6 | 8 |  |  | 8 |  | 8696 | use File::Map qw/:map/; | 
|  | 8 |  |  |  |  | 74296 |  | 
|  | 8 |  |  |  |  | 32 |  | 
| 7 | 8 |  |  | 8 |  | 1648 | use Carp; | 
|  | 8 |  |  |  |  | 8 |  | 
|  | 8 |  |  |  |  | 920 |  | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | our $VERSION = '0.05'; | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | require XSLoader; | 
| 12 |  |  |  |  |  |  | XSLoader::load('IPC::ScoreBoard', $VERSION); | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | use constant { | 
| 15 | 8 |  |  |  |  | 2152 | IVLEN=>length(pack "J", 0), | 
| 16 |  |  |  |  |  |  | MAGIC=>'PCSB', | 
| 17 | 8 |  |  | 8 |  | 48 | }; | 
|  | 8 |  |  |  |  | 64 |  | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | # a slot is always a set of IVs. | 
| 20 |  |  |  |  |  |  | # hence, $slotsize is given in units of IVLEN bytes. | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | sub anon { | 
| 23 | 8 |  |  | 8 | 1 | 64 | my ($class, $how_many, $slotsize, $score_extra); | 
| 24 | 8 | 50 | 33 |  |  | 184 | if( @_>3 or $_[0]!~/^\d/ ) { | 
| 25 | 0 |  |  |  |  | 0 | ($class, $how_many, $slotsize, $score_extra)=@_; | 
| 26 | 0 |  | 0 |  |  | 0 | $class=ref($class) || $class; | 
| 27 |  |  |  |  |  |  | } else { | 
| 28 | 8 |  |  |  |  | 264 | ($how_many, $slotsize, $score_extra)=@_; | 
| 29 | 8 |  |  |  |  | 24 | $class=__PACKAGE__; | 
| 30 |  |  |  |  |  |  | } | 
| 31 |  |  |  |  |  |  |  | 
| 32 | 8 |  |  | 8 |  | 9704 | use integer; | 
|  | 8 |  |  |  |  | 64 |  | 
|  | 8 |  |  |  |  | 32 |  | 
| 33 | 8 | 50 |  |  |  | 48 | $score_extra=0 unless defined $score_extra; | 
| 34 |  |  |  |  |  |  |  | 
| 35 | 8 |  |  |  |  | 24 | my $slsz=$slotsize*IVLEN; | 
| 36 | 8 |  |  |  |  | 56 | map_anonymous my $scoreboard, (4+$how_many*$slotsize+$score_extra)*IVLEN; | 
| 37 | 8 |  |  |  |  | 344 | substr $scoreboard, 0, length(MAGIC), MAGIC; | 
| 38 | 8 |  |  |  |  | 40 | substr $scoreboard, IVLEN, 3*IVLEN, | 
| 39 |  |  |  |  |  |  | pack "J3", $how_many, $slotsize, $score_extra; | 
| 40 |  |  |  |  |  |  |  | 
| 41 | 8 |  |  |  |  | 48 | return bless \$scoreboard, $class; | 
| 42 |  |  |  |  |  |  | } | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | sub named { | 
| 45 | 8 |  |  | 8 | 1 | 144 | my ($class, $filename, $how_many, $slotsize, $score_extra); | 
| 46 | 8 | 50 | 33 |  |  | 48 | if( @_>4 or $_[1]!~/^\d/ ) { | 
| 47 | 8 |  |  |  |  | 40 | ($class, $filename, $how_many, $slotsize, $score_extra)=@_; | 
| 48 | 8 |  | 33 |  |  | 96 | $class=ref($class) || $class; | 
| 49 |  |  |  |  |  |  | } else { | 
| 50 | 0 |  |  |  |  | 0 | ($filename, $how_many, $slotsize, $score_extra)=@_; | 
| 51 | 0 |  |  |  |  | 0 | $class=__PACKAGE__; | 
| 52 |  |  |  |  |  |  | } | 
| 53 |  |  |  |  |  |  |  | 
| 54 | 8 |  |  | 8 |  | 1520 | use integer; | 
|  | 8 |  |  |  |  | 16 |  | 
|  | 8 |  |  |  |  | 32 |  | 
| 55 | 8 | 50 |  |  |  | 24 | $score_extra=0 unless defined $score_extra; | 
| 56 |  |  |  |  |  |  |  | 
| 57 | 8 | 50 |  |  |  | 738080 | open my $fh, '+>', $filename or croak "Cannot open $filename: $!"; | 
| 58 |  |  |  |  |  |  |  | 
| 59 | 8 |  |  |  |  | 800 | syswrite $fh, "\0" x((4+$how_many*$slotsize+$score_extra)*IVLEN); | 
| 60 | 8 |  |  |  |  | 120 | map_handle my $scoreboard, $fh, '+<'; | 
| 61 | 8 |  |  |  |  | 4176 | substr $scoreboard, 0, length(MAGIC), MAGIC; | 
| 62 | 8 |  |  |  |  | 80 | substr $scoreboard, IVLEN, 3*IVLEN, | 
| 63 |  |  |  |  |  |  | pack "J3", $how_many, $slotsize, $score_extra; | 
| 64 |  |  |  |  |  |  |  | 
| 65 | 8 |  |  |  |  | 648 | return bless \$scoreboard, $class; | 
| 66 |  |  |  |  |  |  | } | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | sub open { | 
| 69 | 2 |  |  | 2 | 1 | 2879818 | my ($class, $filename); | 
| 70 | 2 | 50 |  |  |  | 38 | if( @_>1 ) { | 
| 71 | 0 |  |  |  |  | 0 | ($class, $filename)=@_; | 
| 72 | 0 |  | 0 |  |  | 0 | $class=ref($class) || $class; | 
| 73 |  |  |  |  |  |  | } else { | 
| 74 | 2 |  |  |  |  | 18 | ($filename)=@_; | 
| 75 | 2 |  |  |  |  | 24 | $class=__PACKAGE__; | 
| 76 |  |  |  |  |  |  | } | 
| 77 |  |  |  |  |  |  |  | 
| 78 | 2 | 50 |  |  |  | 200 | open my $fh, '+<', $filename or croak "Cannot open $filename: $!"; | 
| 79 | 2 |  |  |  |  | 42 | map_handle my $scoreboard, $fh, '+<'; | 
| 80 |  |  |  |  |  |  |  | 
| 81 | 2 | 50 |  |  |  | 770 | croak "Invalid magic number in $filename" | 
| 82 |  |  |  |  |  |  | unless substr($scoreboard, 0, length(MAGIC)) eq MAGIC; | 
| 83 |  |  |  |  |  |  |  | 
| 84 | 2 |  |  |  |  | 44 | return bless(\$scoreboard, $class), unpack 'x'.IVLEN.'J3', $scoreboard; | 
| 85 |  |  |  |  |  |  | } | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | my $import_done; | 
| 88 |  |  |  |  |  |  | sub import { | 
| 89 | 8 | 50 | 33 | 8 |  | 104 | return if @_>=2 and $_[1] eq ':noshortcuts'; | 
| 90 |  |  |  |  |  |  | # create shortcuts | 
| 91 | 8 | 50 |  |  |  | 24 | return if $import_done; | 
| 92 | 8 |  |  |  |  | 16 | $import_done=1; | 
| 93 | 8 |  |  | 8 |  | 4136 | no strict 'refs'; | 
|  | 8 |  |  |  |  | 16 |  | 
|  | 8 |  |  |  |  | 688 |  | 
| 94 | 8 |  |  |  |  | 24 | for my $n (qw/anon named open get set incr decr sum get_all sum_all | 
| 95 |  |  |  |  |  |  | get_extra set_extra incr_extra decr_extra get_all_extra | 
| 96 |  |  |  |  |  |  | nslots slotsize nextra have_atomics offset_of/) { | 
| 97 | 160 |  |  |  |  | 200 | *{'SB::'.$n}=\&{$n}; | 
|  | 160 |  |  |  |  | 712 |  | 
|  | 160 |  |  |  |  | 256 |  | 
| 98 |  |  |  |  |  |  | } | 
| 99 |  |  |  |  |  |  | } | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | 1; | 
| 102 |  |  |  |  |  |  | __END__ |