NAME

IPC::Semaphore::Set

DESCRIPTION

An abstract interface to semaphore sets and their resources.

A semaphore is an abstract data type that is provided by the system to give access control to common resources by multiple processes in parallel programming or in a multi-user environment.

A semaphore 'set' is the set of resources the system provides by an identification number, and the values (availability) of those resources.

You could, for instance, use a semaphore to lock on a single file between multiple processes by saying that the set has one resource (one file) and that the resource has one availability (one process can use it at one time). You could also represent a series of network printers. Perhaps you have five printers that all have the ability to do ten jobs. You could create the semaphore set with five resources, each resource with ten availability.

This module tries to "Do The Right Thing". It assumes a lot about what you're looking for when you call '->new', and basically will set you up with a semaphore set that has at least one resource with at least one availability. If this assumption is wrong for your purposes, pay close attention to the options for '->new'.

SYNOPSIS

Get/Create a semaphore set:

my $semset = IPC::Semaphore::Set->new(
	key_name     => 'my_lock',
	resources    => 2,
	availability => 3,
);

The above semaphore set has two resource, each of those resources has an availability of three.

Now you can get the first resource (resource 0):

my $resource = $semset->resource;

Or you can select the resource explicitly:

my $resource = $semset->resource(1);

But note that with two resources total, one is our last resource because we start at "0".

You can make conditionals checking whether or not a lock is available on the resource:

if ($semset->resource->lock) {
	# ... can use resource!
} else {
	# ... can't use resource!
}

You can simply wait for resource availability:

$semset->resource->lockWait;
# ... resource is now available

You can die if the resource isn't currently available:

$semset->resource->lockOrDie;
# ... if we're here we have a lock

METHODS

new

Get a new IPC::Semaphore::Set object. If 'key' is provided, get or create a semaphore with that key. if 'key_name' is provided, generate a key based off of the ascii character codes of that name. If neither is provided, a new 'private' semaphore set will be created (note that 'private' is how SysV refers to it, but this is something of a misnomer).

New uses the following flags by default:

S_IRUSR | S_IWUSR | IPC_CREAT | SEM_UNDO

Which means it creates it if it doesn't exist, keeps track of ownership, and will clean up it's changes after exit.

resource

Returns a IPC::Semaphore::Set::Resource object given the resource number, or the last Resource accessed by number.

If a number isn't passed to it, and it hasn't yet encountered a resource, it assumes resource 0 (the first resource in the set) is what you wanted and tries to get that.

RESOURCE METHODS
lock

Attempts to get a lock on the resource and returns boolean.

lockWait

Waits until a lock becomes available on the resource then returns 1.

lockOrDie

Attempts to get a lock on the resource and dies if it can not. Returns 1 otherwise.

unlock

Unlocks on the resource manually (this will also happen when the program exits).

available

Returns the number 'value' of the resource, indicating it's current availability.

number

Returns the number of the resource in its semaphore set

set

Returns the IPC::Semaphore::Set the resource is a part of.

value

Same as available, just more traditional language.

resources

Returns a list or arrayref of all the IPC::Semaphore::Set::Resource objects available for this semaphore set.

id

Returns the numeric system ID of the semaphore set.

key

Returns the numeric key if available.

keyName

Returns the 'word' key if used.

remove

Remove the semaphore set entirely from the system.

sem

Returns the internal 'IPC::Semaphore' object.