| 1 | # You may distribute under the terms of either the GNU General Public License | ||||||||||||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||||||||||||
| 3 | # | ||||||||||||
| 4 | # (C) Paul Evans, 2016-2026 -- leonerd@leonerd.org.uk | ||||||||||||
| 5 | |||||||||||||
| 6 | package Future::Mutex 0.54; | ||||||||||||
| 7 | |||||||||||||
| 8 | 1 | ▶ | use v5.20; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 1 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | |||||||||||||
| 11 | 1 | ▶ | use feature qw( postderef signatures ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | 1 | ▶ | no warnings qw( experimental::postderef experimental::signatures ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | |||||||||||||
| 14 | 1 | ▶ | use Future; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | |||||||||||||
| 16 | =head1 NAME | ||||||||||||
| 17 | |||||||||||||
| 18 | C<Future::Mutex> - mutual exclusion lock around code that returns L<Future>s | ||||||||||||
| 19 | |||||||||||||
| 20 | =head1 SYNOPSIS | ||||||||||||
| 21 | |||||||||||||
| 22 | =for highlighter language=perl | ||||||||||||
| 23 | |||||||||||||
| 24 | use Future::Mutex; | ||||||||||||
| 25 | |||||||||||||
| 26 | my $mutex = Future::Mutex->new; | ||||||||||||
| 27 | |||||||||||||
| 28 | sub do_atomically | ||||||||||||
| 29 | { | ||||||||||||
| 30 | return $mutex->enter( sub { | ||||||||||||
| 31 | ... | ||||||||||||
| 32 | return $f; | ||||||||||||
| 33 | }); | ||||||||||||
| 34 | } | ||||||||||||
| 35 | |||||||||||||
| 36 | =head1 DESCRIPTION | ||||||||||||
| 37 | |||||||||||||
| 38 | Most L<Future>-using code expects to run with some level of concurrency, using | ||||||||||||
| 39 | future instances to represent still-pending operations that will complete at | ||||||||||||
| 40 | some later time. There are occasions however, when this concurrency needs to | ||||||||||||
| 41 | be restricted - some operations that, once started, must not be interrupted | ||||||||||||
| 42 | until they are complete. Subsequent requests to perform the same operation | ||||||||||||
| 43 | while one is still outstanding must therefore be queued to wait until the | ||||||||||||
| 44 | first is finished. These situations call for a mutual-exclusion lock, or | ||||||||||||
| 45 | "mutex". | ||||||||||||
| 46 | |||||||||||||
| 47 | A C<Future::Mutex> instance provides one basic operation, which will execute a | ||||||||||||
| 48 | given block of code which returns a future, and itself returns a future to | ||||||||||||
| 49 | represent that. The mutex can be in one of two states; either unlocked or | ||||||||||||
| 50 | locked. While it is unlocked, requests to execute code are handled | ||||||||||||
| 51 | immediately. Once a block of code is invoked, the mutex is now considered to | ||||||||||||
| 52 | be locked, causing any subsequent requests to invoke code to be queued behind | ||||||||||||
| 53 | the first one, until it completes. Once the initial code indicates completion | ||||||||||||
| 54 | (by its returned future providing a result or failing), the next queued code | ||||||||||||
| 55 | is invoked. | ||||||||||||
| 56 | |||||||||||||
| 57 | An instance may also be a counting mutex if initialised with a count greater | ||||||||||||
| 58 | than one. In this case, it can keep multiple blocks outstanding up to that | ||||||||||||
| 59 | limit, with subsequent requests queued as before. This allows it to act as a | ||||||||||||
| 60 | concurrency-bounding limit around some operation that can run concurrently, | ||||||||||||
| 61 | but an application wishes to apply overall limits to stop it growing too much, | ||||||||||||
| 62 | such as communications with external services or executing other programs. | ||||||||||||
| 63 | |||||||||||||
| 64 | =cut | ||||||||||||
| 65 | |||||||||||||
| 66 | =head1 CONSTRUCTOR | ||||||||||||
| 67 | |||||||||||||
| 68 | =cut | ||||||||||||
| 69 | |||||||||||||
| 70 | =head2 new | ||||||||||||
| 71 | |||||||||||||
| 72 | $mutex = Future::Mutex->new( count => $n ); | ||||||||||||
| 73 | |||||||||||||
| 74 | Returns a new C<Future::Mutex> instance. It is initially unlocked. | ||||||||||||
| 75 | |||||||||||||
| 76 | Takes the following named arguments: | ||||||||||||
| 77 | |||||||||||||
| 78 | =over 8 | ||||||||||||
| 79 | |||||||||||||
| 80 | =item count => INT | ||||||||||||
| 81 | |||||||||||||
| 82 | Optional number to limit outstanding concurrency. Will default to 1 if not | ||||||||||||
| 83 | supplied. | ||||||||||||
| 84 | |||||||||||||
| 85 | =back | ||||||||||||
| 86 | |||||||||||||
| 87 | =cut | ||||||||||||
| 88 | |||||||||||||
| 89 | 10 | ▶ | sub new ( $class, %params ) | ||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 90 | { | ||||||||||||
| 91 | return bless { | ||||||||||||
| 92 | 10 | ▶ | avail => $params{count} // 1, | ||||||||||
|
Condition100%2/2
$params{"count"} // 1
MC/DC100%1/1
$params{"count"} // 1
$params{"count"}
Truth table100%2/2
A
A= $params{"count"}
| |||||||||||||
| 93 | waitf => undef, | ||||||||||||
| 94 | queue => [], | ||||||||||||
| 95 | }, $class; | ||||||||||||
| 96 | } | ||||||||||||
| 97 | |||||||||||||
| 98 | =head1 METHODS | ||||||||||||
| 99 | |||||||||||||
| 100 | =cut | ||||||||||||
| 101 | |||||||||||||
| 102 | =head2 enter | ||||||||||||
| 103 | |||||||||||||
| 104 | $f = $mutex->enter( \&code ); | ||||||||||||
| 105 | |||||||||||||
| 106 | Returns a new C<Future> that represents the eventual result of calling the | ||||||||||||
| 107 | code. If the mutex is currently unlocked, the code will be invoked | ||||||||||||
| 108 | immediately. If it is currently locked, the code will be queued waiting for | ||||||||||||
| 109 | the next time it becomes unlocked. | ||||||||||||
| 110 | |||||||||||||
| 111 | The code is invoked with no arguments, and is expected to return a C<Future>. | ||||||||||||
| 112 | The eventual result of that future determines the result of the future that | ||||||||||||
| 113 | C<enter> returned. | ||||||||||||
| 114 | |||||||||||||
| 115 | =cut | ||||||||||||
| 116 | |||||||||||||
| 117 | 17 | ▶ | sub enter ( $self, $code ) | ||||||||||
|
Subroutine
enter: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||
| 118 | { | ||||||||||||
| 119 | 17 | my $down_f; | |||||||||||
| 120 | 17 | ▶ | if( $self->{avail} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 121 | 11 | $self->{avail}--; | |||||||||||
| 122 | 11 | $down_f = Future->done; | |||||||||||
| 123 | } | ||||||||||||
| 124 | else { | ||||||||||||
| 125 | 6 | ▶ | die "ARGH Need to clone an existing future\n" unless defined $self->{waitf}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 126 | 6 | push $self->{queue}->@*, $down_f = $self->{waitf}->new; | |||||||||||
| 127 | } | ||||||||||||
| 128 | |||||||||||||
| 129 | 17 | ▶ | my $up = sub ( $ ) { | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||
| 130 | 17 | ▶ | if( my $next_f = shift $self->{queue}->@* ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 131 | 6 | $next_f->done; | |||||||||||
| 132 | } | ||||||||||||
| 133 | else { | ||||||||||||
| 134 | 11 | $self->{avail}++; | |||||||||||
| 135 | 11 | undef $self->{waitf}; | |||||||||||
| 136 | } | ||||||||||||
| 137 | 17 | }; | |||||||||||
| 138 | |||||||||||||
| 139 | 17 | my $retf = $down_f->then( $code )->on_ready( $up ); | |||||||||||
| 140 | 17 | ▶ | $self->{waitf} or $self->{waitf} = $retf; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 141 | 17 | return $retf; | |||||||||||
| 142 | } | ||||||||||||
| 143 | |||||||||||||
| 144 | =head2 available | ||||||||||||
| 145 | |||||||||||||
| 146 | $avail = $mutex->available; | ||||||||||||
| 147 | |||||||||||||
| 148 | Returns true if the mutex is currently unlocked, or false if it is locked. | ||||||||||||
| 149 | |||||||||||||
| 150 | =cut | ||||||||||||
| 151 | |||||||||||||
| 152 | 12 | ▶ | sub available ( $self ) | ||||||||||
|
Subroutine
available: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 153 | { | ||||||||||||
| 154 | 12 | return $self->{avail}; | |||||||||||
| 155 | } | ||||||||||||
| 156 | |||||||||||||
| 157 | =head1 AUTHOR | ||||||||||||
| 158 | |||||||||||||
| 159 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||||
| 160 | |||||||||||||
| 161 | =cut | ||||||||||||
| 162 | |||||||||||||
| 163 | 1 | 0x55AA; | |||||||||||