/usr/share/perl5/MCE/Mutex.pm is in libmce-perl 1.608-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | ###############################################################################
## ----------------------------------------------------------------------------
## MCE::Mutex - Simple semaphore for Many-Core Engine.
##
###############################################################################
package MCE::Mutex;
use strict;
use warnings;
no warnings 'threads';
no warnings 'recursion';
no warnings 'uninitialized';
use MCE::Util qw( $LF );
use bytes;
our $VERSION = '1.608';
sub DESTROY {
my ($_mutex, $_arg) = @_;
my $_id = $INC{'threads.pm'} ? $$ .'.'. threads->tid() : $$;
$_mutex->unlock() if ($_mutex->{ $_id });
if (!defined $_arg || $_arg ne 'shutdown') {
return if (defined $MCE::VERSION && !defined $MCE::MCE->{_wid});
return if (defined $MCE::MCE && $MCE::MCE->{_wid});
}
MCE::Util::_destroy_sockets($_mutex, qw(_w_sock _r_sock));
return;
}
###############################################################################
## ----------------------------------------------------------------------------
## Public methods.
##
###############################################################################
sub new {
my ($_class, %_argv) = @_; @_ = ();
my $_mutex = {}; bless($_mutex, ref($_class) || $_class);
MCE::Util::_make_socket_pair($_mutex, qw(_w_sock _r_sock));
syswrite($_mutex->{_w_sock}, '0');
return $_mutex;
}
sub lock {
my $_mutex = shift;
my $_id = $INC{'threads.pm'} ? $$ .'.'. threads->tid() : $$;
unless ($_mutex->{ $_id }) {
sysread($_mutex->{_r_sock}, my $_b, 1);
$_mutex->{ $_id } = 1;
}
return;
}
sub unlock {
my $_mutex = shift;
my $_id = $INC{'threads.pm'} ? $$ .'.'. threads->tid() : $$;
if ($_mutex->{ $_id }) {
syswrite($_mutex->{_w_sock}, '0');
$_mutex->{ $_id } = 0;
}
return;
}
sub synchronize {
my ($_mutex, $_code) = (shift, shift);
if (ref $_code eq 'CODE') {
if (defined wantarray) {
$_mutex->lock(); my @_a = $_code->(@_);
$_mutex->unlock();
return wantarray ? @_a : $_a[0];
}
else {
$_mutex->lock(); $_code->(@_);
$_mutex->unlock();
}
}
return;
}
1;
__END__
###############################################################################
## ----------------------------------------------------------------------------
## Module usage.
##
###############################################################################
=head1 NAME
MCE::Mutex - Simple semaphore for Many-Core Engine
=head1 VERSION
This document describes MCE::Mutex version 1.608
=head1 SYNOPSIS
use MCE::Flow max_workers => 4;
use MCE::Mutex;
print "## running a\n";
my $a = MCE::Mutex->new;
mce_flow sub {
$a->lock;
## access shared resource
my $wid = MCE->wid; MCE->say($wid); sleep 1;
$a->unlock;
};
print "## running b\n";
my $b = MCE::Mutex->new;
mce_flow sub {
$b->synchronize( sub {
## access shared resource
my ($wid) = @_; MCE->say($wid); sleep 1;
}, MCE->wid );
};
=head1 DESCRIPTION
This module implements locking methods that can be used to coordinate access
to shared data from multiple workers spawned as processes or threads.
The inspiration for this module came from reading Mutex for Ruby.
=head1 API DOCUMENTATION
=head2 MCE::Mutex->new ( void )
Creates a new mutex.
=head2 $m->lock ( void )
Attempts to grab the lock and waits if not available. Multiple calls to
mutex->lock by the same process or thread is safe. The mutex will remain
locked until mutex->unlock is called.
=head2 $m->unlock ( void )
Releases the lock. A held lock by an exiting process or thread is released
automatically.
=head2 $m->synchronize ( sub { ... }, @_ )
Obtains a lock, runs the code block, and releases the lock after the block
completes. Optionally, the method is wantarray aware.
my $value = $m->synchronize( sub {
## access shared resource
'value';
});
=head1 INDEX
L<MCE|MCE>
=head1 AUTHOR
Mario E. Roy, S<E<lt>marioeroy AT gmail DOT comE<gt>>
=cut
|