NAME

AnyEvent::Semaphore - Semaphore implementation for AnyEvent

SYNOPSIS

use AnyEvent::Semaphore;

my $sem = AnyEvent::Semaphore->new(5);
...

my $watcher = $sem->down( sub { ... } );
...

undef $watcher; # semaphore up

DESCRIPTION

This module provides a semaphore implementation intended to be used with the AnyEvent framework.

It tries to be as simple as possible and to follow AnyEvent style.

API

The module provides the following methods:

$sem = AnyEvent::Semaphore->new($size);

Creates a new semaphore object of the given size.

$watcher = $sem->down($callback)

Queues a down (or wait) operation on the semaphore. The given callback will be eventually invoked when the resouce guarded by the semaphore becomes free.

The call returns a watcher object.

After the resource is assigned and the callback invoked, destroying the watcher frees the resource (it is the equivalent of the up or signal operation).

Destroying the watcher before the resource is asigned just cancels the down operation.

$new_size = $sem->size($size)
$new_size = $sem->size

Gets or sets the semaphore size.

When the size is increased, queued operations will be processed.

$n = $sem->running

Returns the number of slots currently used.

SEE ALSO

The Wikipedia page on semaphores.

AnyEvent.

AUTHOR

Salvador Fandiño, <sfandino@yahoo.com>

COPYRIGHT AND LICENSE

Copyright (C) 2013 by Qindel Formación y Servicios S.L.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.14.2 or, at your option, any later version of Perl 5 you may have available.