-# Copyright (C) 2018-2020 all contributors <meta@public-inbox.org>
+# Copyright (C) 2018-2021 all contributors <meta@public-inbox.org>
# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
# Base class for per-inbox locking
package PublicInbox::Lock;
use strict;
-use warnings;
+use v5.10.1;
use Fcntl qw(:flock :DEFAULT);
use Carp qw(croak);
+use PublicInbox::OnDestroy;
+use File::Temp ();
# we only acquire the flock if creating or reindexing;
# PublicInbox::Import already has the lock on its own.
close $lockfh or croak "close $lock_path failed: $!\n";
}
+# caller must use return value
+sub lock_for_scope {
+ my ($self, @single_pid) = @_;
+ $self->lock_acquire;
+ PublicInbox::OnDestroy->new(@single_pid, \&lock_release, $self);
+}
+
+sub new_tmp {
+ my ($cls, $ident) = @_;
+ my $tmp = File::Temp->new("$ident.lock-XXXXXX", TMPDIR => 1);
+ bless { lock_path => $tmp->filename, tmp => $tmp }, $cls;
+}
+
1;