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
|
package Test::SharedObject;
use 5.008001;
use strict;
use warnings;
our $VERSION = "0.01";
use Storable qw/store_fd fd_retrieve/;
use File::Temp qw/tempfile/;
use Test::SharedObject::Lock;
sub new {
my ($class, $data) = @_;
my ($fh, $file) = tempfile(EXLOCK => 0);
my $self = bless {
file => $file,
ppid => $$,
} => $class;
seek $fh, 0, 0;
truncate $fh, 0;
store_fd \$data, $fh;
close $fh;
return $self;
}
sub txn {
my ($self, $code) = @_;
my $lock = Test::SharedObject::Lock->new($self);
my $fh = $lock->fh;
my $ref = fd_retrieve $fh;
my $data = $code->($$ref);
seek $fh, 0, 0;
truncate $fh, 0;
store_fd \$data, $fh;
return $data; ## unlock
}
sub set {
my ($self, $data) = @_;
return $self->txn(sub { $data });
}
sub _identity { $_[0] }
sub get {
my $self = shift;
return $self->txn(\&_identity);
}
sub DESTROY {
my $self = shift;
unlink $self->{file} if $$ == $self->{ppid};
}
1;
__END__
=encoding utf-8
=head1 NAME
Test::SharedObject - Data sharing in multi process.
=head1 SYNOPSIS
use strict;
use warnings;
use Test::More tests => 2;
use Test::SharedFork;
use Test::SharedObject;
my $shared = Test::SharedObject->new(0);
is $shared->get, 0;
my $pid = fork;
die $! unless defined $pid;
if ($pid == 0) {# child
$shared->txn(sub {
my $counter = shift;
$counter++;
return $counter;
});
exit;
}
wait;
is $shared->get, 1;
=head1 DESCRIPTION
Test::SharedObject provides atomic data operation between multiple process.
=head1 METHODS
=over
=item my $shared = Test::SharedObject->new($value)
Creates a new Test::SharedObject instance.
And set C<$value> as initial value.
Internally, Creates temporary file, and serialize C<$value> by L<Storable>, and save.
=item $shared->txn(\&coderef)
Provides atomic data operation between multiple process in C<\&coderef>.
Set shared value as first arguments in C<\&coderef>, and return value as new shared value.
Internally:
=over
=item Lock temporary file.
=item Read shared value.
=item Executes C<\&coderef>. (Set shared value as first arguments)
=item Write return value as shared value.
=item Unlock temporary file.
=back
Good Example:
$shared->txn(sub {
my $counter = shift;
$counter++; # atomic!!
return $counter;
});
Bad Example:
my $counter;
$shared->txn(sub {
$counter = shift;
});
$counter++; # *NOT* atomic!!
$shared->txn(sub {
return $counter;
});
=item $shared->set($value)
Set C<$value> as shared value.
The syntactic sugar for C<$shared-E<gt>txn()>.
=item my $value = $shared->get()
Get shared value.
The syntactic sugar for C<$shared-E<gt>txn()>.
=back
=head1 LICENSE
Copyright (C) karupanerura.
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=head1 AUTHOR
karupanerura E<lt>karupa@cpan.orgE<gt>
=cut
|