Apache2-SSI
view release on metacpan or search on metacpan
lib/Apache2/SSI/SharedMem.pod view on Meta::CPAN
This instantiates a shared memory object. It takes the following parameters:
=over 4
=item C<debug>
A debug value will enable debugging output (equal or above 3 actually)
=item C<create>
A boolean value to indicate whether the shared memory block should be created if it does not exist. Default to false.
=item C<destroy>
A boolean value to indicate if the shared memory block should be removed when the object is destroyed. See L<perlmod> for more about object destruction.
=item C<exclusive>
A boolean value to set the shared memory as exclusive. This will affect the flags set by L</flags> which are used by L</open>.
=item C<key>
The shared memory key identifier to use. It defaults to C<IPC::SysV::IPC_PRIVATE>
If you provide an empty value, it will revert to C<IPC::SysV::IPC_PRIVATE>.
If you provide a number, it will be used to call L<IPC::SysV/ftok>.
Otherwise, if you provide a key as string, the characters in the string will be converted to their numeric value and added up. The resulting id, called C<project id> by L<IPC::SysV>, will be used to call L<IPC::SysV/ftok> and will produce an hopefull...
Either way, the resulting value is used to create a shared memory segment and a semaphore by L</open>.
=item C<mode>
The octal mode value to use when opening the shared memory block.
Shared memory are owned by system users and access to shared memory segment is ruled by the initial permissions set to it.
If you do not want to share it with any other user than yourself, setting mode to C<0600> is fine.
=item C<size>
The size in byte of the shared memory.
This is set once it is created. You can create again the shared memory segment with a smaller size, but not a bigger one. If you want to increase the size, you would need to remove it first.
=back
An object will be returned if it successfully initiated, or undef() upon error, which can then be retrieved with C<Apache2::SSI::SharedMem->error>. You should always check the return value of the methods used here for their definedness.
my $shmem = Apache2::SSI::SharedMem->new(
create => 1,
destroy => 0,
key => 'my_memory',
# 64K
size => 65536,
) || die( Apache2::SSI::SharedMem->error );
=head2 addr
Returns the address of the shared memory segment once it has been attached to this address space.
=head2 attach
Attach the shared memory segment to this address space and returns its address.
Upon error, it returns C<undef> and sets an error that can be retrieved with the error method:
my $addr = $shem->attach || die( $shem->error );
A shared memory segment object must be first created with the L</open> method, because L</attach> calls L<IPC::SysV/shmat> with the shared memory id and this id is returned upon using the L</open> method.
=head2 create
Set or get the boolean value to true to indicate you want to create the shared memory block if it does not exist already. Default to false.
=head2 destroy
Set or get the boolean value to indicate that the shared memory should be automatically destroyed when the module object is destroyed. See L<perlmod> for more information about module object destruction.
=head2 detach
Quoting the IPC documentation, this detaches the shared memory segment located at the address specified by L</attach> from this address space.
It returns C<undef> if it is not attached anymore, but without setting an error.
=head2 exclusive
Set or get the boolean value to affect the open flags in exclusive mode.
=head2 exists
Checks if the shared memory identified with C<key> exists.
It takes the same arguments as L</open> and returns 1 if the shared memory exists or 0 otherwise.
It does this by performing a L<perlfunc/shmget> such as:
shmget( $shared_mem_key, $size, 0444 );
This will typically return the shared memory id if it exists or C<undef()> with an error set in C<$!> by perl otherwise.
=head2 flags
Provided with an optional hash or hash reference and this return a bitwise value of flags used by L</open>.
my $flags = $shmem->flags({
create => 1,
exclusive => 0,
mode => 0600,
}) || die( $shmem->error );
=head2 id
Returns the id of the shared memory once it has been opened with L</open>
my $s = $shmem->open || die( $shmem->error );
my $id = $s->id;
=head2 key
Sets or gets the shared memory key identifier.
$shem->key( 'some_identifier' );
=head2 lock
It takes an optional bitwise lock value, and defaults to C<LOCK_SH> if none is provided and issues a lock on the shared memory.
use Apache2::SSI::SharedMem qw( :all );
my $s = $shem->open || die( $shmem->error );
$s->lock( LOCK_EX );
# Do something
$s->unlock;
=head2 locked
Returns a positive value when a lock is active or 0 when there is no active lock.
The value is the bitwise value of the lock used.
=head2 mode
Sets or gets the mode for the shared memory as used by L</open>
( run in 0.833 second using v1.01-cache-2.11-cpan-e1769b4cff6 )