1 # Copyright (C) 2018 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);
11 # we only acquire the flock if creating or reindexing;
12 # PublicInbox::Import already has the lock on its own.
15 croak 'already locked' if $self->{lockfh};
16 my $lock_path = $self->{lock_path} or return;
17 sysopen(my $lockfh, $lock_path, O_WRONLY|O_CREAT) or
18 die "failed to open lock $lock_path: $!\n";
19 flock($lockfh, LOCK_EX) or die "lock failed: $!\n";
20 $self->{lockfh} = $lockfh;
25 return unless $self->{lock_path};
26 my $lockfh = delete $self->{lockfh} or croak 'not locked';
27 flock($lockfh, LOCK_UN) or die "unlock failed: $!\n";
28 close $lockfh or die "close failed: $!\n";