]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/Lock.pm
update copyrights for 2021
[public-inbox.git] / lib / PublicInbox / Lock.pm
index b2c8227f0a66c8940d9be8e60afb8571015f0fc0..2c5ebf270a3aed694a9f3c68156f1c431bd69b30 100644 (file)
@@ -1,12 +1,14 @@
-# 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.
@@ -32,4 +34,17 @@ sub lock_release {
        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;