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;
13 # we only acquire the flock if creating or reindexing;
14 # PublicInbox::Import already has the lock on its own.
17 my $lock_path = $self->{lock_path};
18 croak 'already locked '.($lock_path // '(undef)') if $self->{lockfh};
19 return unless defined($lock_path);
20 sysopen(my $lockfh, $lock_path, O_WRONLY|O_CREAT) or
21 croak "failed to open $lock_path: $!\n";
22 flock($lockfh, LOCK_EX) or croak "lock $lock_path failed: $!\n";
23 $self->{lockfh} = $lockfh;
27 my ($self, $wake) = @_;
28 defined(my $lock_path = $self->{lock_path}) or return;
29 my $lockfh = delete $self->{lockfh} or croak "not locked: $lock_path";
31 syswrite($lockfh, '.') if $wake;
33 flock($lockfh, LOCK_UN) or croak "unlock $lock_path failed: $!\n";
34 close $lockfh or croak "close $lock_path failed: $!\n";
37 # caller must use return value
41 PublicInbox::OnDestroy->new(\&lock_release, $self);
45 my ($cls, $ident) = @_;
46 my $tmp = File::Temp->new("$ident.lock-XXXXXX", TMPDIR => 1);
47 bless { lock_path => $tmp->filename, tmp => $tmp }, $cls;