1 # Copyright (C) 2018-2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # Base class for per-inbox locking
5 package PublicInbox::Lock;
8 use Fcntl qw(:flock :DEFAULT);
10 use PublicInbox::OnDestroy;
12 # we only acquire the flock if creating or reindexing;
13 # PublicInbox::Import already has the lock on its own.
16 my $lock_path = $self->{lock_path};
17 croak 'already locked '.($lock_path // '(undef)') if $self->{lockfh};
18 return unless defined($lock_path);
19 sysopen(my $lockfh, $lock_path, O_RDWR|O_CREAT) or
20 croak "failed to open $lock_path: $!\n";
21 flock($lockfh, LOCK_EX) or croak "lock $lock_path failed: $!\n";
22 $self->{lockfh} = $lockfh;
26 my ($self, $wake) = @_;
27 defined(my $lock_path = $self->{lock_path}) or return;
28 my $lockfh = delete $self->{lockfh} or croak "not locked: $lock_path";
30 syswrite($lockfh, '.') if $wake;
32 flock($lockfh, LOCK_UN) or croak "unlock $lock_path failed: $!\n";
33 close $lockfh or croak "close $lock_path failed: $!\n";
36 # caller must use return value
38 my ($self, @single_pid) = @_;
39 lock_acquire($self) or return; # lock_path not set
40 PublicInbox::OnDestroy->new(@single_pid, \&lock_release, $self);
43 sub lock_acquire_fast {
44 $_[0]->{lockfh} or return lock_acquire($_[0]);
45 flock($_[0]->{lockfh}, LOCK_EX) or croak "lock (fast) failed: $!";
48 sub lock_release_fast {
49 flock($_[0]->{lockfh} // return, LOCK_UN) or
50 croak "unlock (fast) $_[0]->{lock_path}: $!";
53 # caller must use return value
54 sub lock_for_scope_fast {
55 my ($self, @single_pid) = @_;
56 lock_acquire_fast($self) or return; # lock_path not set
57 PublicInbox::OnDestroy->new(@single_pid, \&lock_release_fast, $self);