1 # Copyright (C) 2018-2020 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_WRONLY|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
40 PublicInbox::OnDestroy->new(\&lock_release, $self);