]> git.proxmox.com Git - pve-guest-common.git/commitdiff
lock_config: rename lock_config_mode -> lock_config_shared
authorFabian Grünbichler <f.gruenbichler@proxmox.com>
Tue, 5 May 2020 08:27:22 +0000 (10:27 +0200)
committerFabian Grünbichler <f.gruenbichler@proxmox.com>
Tue, 5 May 2020 09:11:57 +0000 (11:11 +0200)
and pull the actual lock_file_full handling into a helper, to make the
public interface clearer:

lock_config -> standard exclusive lock with 10s timeout
lock_config_full -> exclusive lock with configurable timeout
lock_config_shared -> shared lock with configurable timeout

the latter only has a single user (qemu-server's clone API call)
currently.

Signed-off-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
Tested-by: Fabian Ebner <f.ebner@proxmox.com>
PVE/AbstractConfig.pm

index 3a064b7b9804b5e6a11f0a82412ffd1e1c2888bc..f0c5440aa6c3c453ee73643fc16444e52a3ff45f 100644 (file)
@@ -253,15 +253,6 @@ sub load_current_config {
     return $conf;
 }
 
-
-# Lock config file using flock, run $code with @param, unlock config file.
-# $timeout is the maximum time to acquire the flock
-sub lock_config_full {
-    my ($class, $vmid, $timeout, $code, @param) = @_;
-
-    return $class->lock_config_mode($vmid, $timeout, 0, $code, @param);
-}
-
 sub create_and_lock_config {
     my ($class, $vmid, $allow_existing, $lock) = @_;
 
@@ -284,27 +275,41 @@ sub destroy_config {
     unlink $config_fn or die "failed to remove config file: $!\n";
 }
 
-# Lock config file using flock, run $code with @param, unlock config file.
-# $timeout is the maximum time to acquire the flock
-# $shared eq 1 creates a non-exclusive ("read") flock
-sub lock_config_mode {
-    my ($class, $vmid, $timeout, $shared, $code, @param) = @_;
+my $lock_file_full_wrapper = sub {
+    my ($class, $vmid, $timeout, $shared, $realcode, @param) = @_;
 
     my $filename = $class->config_file_lock($vmid);
 
     # make sure configuration file is up-to-date
-    my $realcode = sub {
+    my $code = sub {
        PVE::Cluster::cfs_update();
-       $code->(@param);
+       $realcode->(@_);
     };
 
-    my $res = lock_file_full($filename, $timeout, $shared, $realcode, @param);
+    my $res = lock_file_full($filename, $timeout, $shared, $code, @param);
 
     die $@ if $@;
 
     return $res;
+};
+
+# Lock config file using non-exclusive ("read") flock, run $code with @param, unlock config file.
+# $timeout is the maximum time to acquire the flock
+sub lock_config_shared {
+    my ($class, $vmid, $timeout, $code, @param) = @_;
+
+    return $lock_file_full_wrapper->($class, $vmid, $timeout, 1, $code, @param);
 }
 
+# Lock config file using flock, run $code with @param, unlock config file.
+# $timeout is the maximum time to acquire the flock
+sub lock_config_full {
+    my ($class, $vmid, $timeout, $code, @param) = @_;
+
+    return $lock_file_full_wrapper->($class, $vmid, $timeout, 0, $code, @param);
+}
+
+
 # Lock config file using flock, run $code with @param, unlock config file.
 sub lock_config {
     my ($class, $vmid, $code, @param) = @_;