Acme-Parataxis
view release on metacpan or search on metacpan
lib/Acme/Parataxis/Sync/Mutex.pod view on Meta::CPAN
Returns the number of fibers currently parked waiting to acquire the lock. Note that when C<unlock()> is called,
ownership is transferred and the waiter is dequeued immediately, so C<waiters> decrements before the waking fiber
actually resumes execution.
=head1 Recipes
=head2 Scoped critical sections
Always prefer guards over manual C<lock()>/C<unlock()> pairs to ensure releases occur even during exceptions:
sub update_shared_state ($mutex, $data) {
my $guard = $mutex->guard;
validate($data); # if this dies, $guard->DESTROY safely unlocks!
apply($data);
}
=head2 Non-blocking skip
Use C<try_lock()> to perform work only when no contention exists:
fiber {
lib/Acme/Parataxis/Sync/RwLock.pod view on Meta::CPAN
async {
my $outer_guard = $rwlock->read_guard;
my $val1 = fetch_field('name');
my $val2 = fetch_field('role');
};
=head2 Deadlock prevention during writes
Because upgrading a read hold to a write hold is forbidden, release read holds before requesting a write lock:
sub update_record ($id, $new_val) {
# Check current value under read lock:
my $needs_update = 0;
{
my $guard = $rwlock->read_guard;
$needs_update = 1 if $data{$id} ne $new_val;
}
# Drop read hold before requesting write lock:
if ($needs_update) {
my $guard = $rwlock->write_guard;
( run in 0.533 second using v1.01-cache-2.11-cpan-145b269f2f3 )