AnyEvent-Tools

 view release on metacpan or  search on metacpan

lib/AnyEvent/Tools.pm  view on Meta::CPAN

share one resource between a lot of subprocesses.
Sometimes You also want to do  something with a  lot of data placed
in hashes/arrays.


=head1 FUNCTIONS

=head2 mutex

returns unlocked mutex.

This object provides the following methods:

=head3 lock(CODEREF)

You declare that You want to lock mutex. When it is possible the mutex will
be locked and Your callback will be called.

If the method is called in non-void context it returns guard object which can
be destroyed. So if You want You can cancel Your lockrequest.

Example:

    $mutex->lock(sub {
        my $guard = shift;
        ... # do something

        undef $guard;       # unlock mutex
    });

The callback receives a guard (see L<AnyEvent::Util#guard>) which unlocks the
mutex. Hold the guard while You need locked resourse.

=head3 is_locked

Returns B<TRUE> if mutex is locked now. Usually You shoudn't
use the function.


=head2 rw_mutex

returns unlocked read-write mutex.

This object provides the following methods:

=head3 rlock(CODEREF)

You declare that You want to lock mutex for reading. When it is
possible the mutex will be locked and Your callback will be called.

There may be a lot of read processes running simultaneously
that catch the lock.

=head3 wlock(CODEREF).

You declare that You want to lock mutex for writing. When it is
possible the mutex will be locked and Your callback will be called.

There may be only one write process that catches the lock.

Both callbacks receive a guard to hold the mutex locked.


=head3 rlock_limit(NUMBER)

Get/Set count limit for rlock. If an rlock request is come and this limit
is reached the request will be queued.


=head3 is_locked

Returns B<TRUE> if the mutex has 'read' or 'write' lock status.

=head3 is_rlocked

Returns B<TRUE> if the mutex has 'read' lock status.

B<Important>: this method returns B<FALSE> if the mutex is
wlocked (L<is_wlocked>), so if You want to know if any lock
is set, use the function L<is_locked>.

=head3 is_wlocked

Returns B<TRUE> if the mutex has 'write' lock status.

Usually You shoudn't use is_[rw]?locked functions.


=head2 async_repeat(COUNT, CALLBACK [, DONE_CALLBACK ])

Repeats calling Your callback(s).

    async_repeat 10, sub { $count++ };
    async_repeat 20, sub { $count++ }, sub { $done = 1 };

The function async_repeat returns the guard if it is called in non-void
context. Destroy the guard if You want to cancel iterations.

Iteration callback receives the following arguments:

=over

=item 1. guard

The next iteration will not start until the guard is destroyed.

=item 2. iteration number

The number of current iteration.

=item 3. first_flag

TRUE on the first iteration.

=item 4. last_flag

TRUE on the last iteration.

=back

=head2 async_for(HASREF|ARRAYREF, CALLBACK [, DONE_CALLBACK ]);

Calls Your callbacks for each array or hash element.

The function returns the guard if it is called in non-void
context. Destroy the guard if You want to cancel iterations.

If You process an array using the function, iteration callback
will receive the following arguments:

=over

=item 1. guard

The next iteration will not start until the guard is destroyed.

=item 2. element

Next array element.

=item 3. index

Index of array element.

=item 4. first_flag

The iteration is the first.

=item 5. last_flag

The iteration is the last.

=back

If You process a hash using the function, iteration callback
will receive the following arguments:

=over

=item 1. guard

The next iteration will not start until the guard is destroyed.

=item 2. key

=item 3. value

=item 4. first_flag

The iteration is the first.

=item 5. last_flag

The iteration is the last.

=back

=head2 async_rfor(HASREF|ARRAYREF, CALLBACK [, DONE_CALLBACK ]);

The same as async_for but has reverse sequence.


=head2 pool



( run in 0.507 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )