Method::Protected

add "is protected" trait to methods

NAME

Method::Protected - add "is protected" trait to methods

SYNOPSIS

use Method::Protected;

my @words = "/usr/share/dict/words".IO.lines;

class Frobnicate {
    has %!hash;
    method pick() is protected { %!hash{@words.pick}++ }
    method hash() is protected { %!hash.clone          }
}

# Set up 2 threads massively updating a single hash
my $frobnicator = Frobnicate.new;
start { loop { $frobnicator.pick }
start { loop { $frobnicator.pick }

# Start reporter
await start {
    until $frobnicator.hash.elems == @words.elems {
        say $frobnicator.hash.elems;
        sleep .1;
    }
}

DESCRIPTION

Method::Protected provides an is protected trait to methods in a class. If applied to a method, all calls to that method will be protected by a Lock, making sure that any other method call to this method (or any other method protected by the trait) from another thread, will block until the call is finished.

Functionally this is similar to what the OO::Monitor does, except that with this module this logic is only applied to methods that actuall have the is protected trait applied.

THEORY OF OPERATION

When the trait is applied on a method, the class of the method id checked whether it already has a $!LOCK attribute. If not, then that attribute is added, and the associated accessor method LOCK is also added. Then the method will be wrapped with code that will protect the execution of the original body of the method.

AUTHOR

Elizabeth Mattijsen [email protected]

Source can be located at: https://github.com/lizmat/Method-Protected . Comments and Pull Requests are welcome.

If you like this module, or what Iā€™m doing more generally, committing to a small sponsorship would mean a great deal to me!

COPYRIGHT AND LICENSE

Copyright 2024 Elizabeth Mattijsen

This library is free software; you can redistribute it and/or modify it under the Artistic License 2.0.

Method::Protected v0.0.1

add "is protected" trait to methods

Authors

  • Elizabeth Mattijsen

License

Artistic-2.0

Dependencies

Test Dependencies

Provides

  • Method::Protected

Documentation

The Camelia image is copyright 2009 by Larry Wall. "Raku" is trademark of the Yet Another Society. All rights reserved.