[pve-devel] [PATCH guest-common 3/3] lock_config: rename lock_config_mode -> lock_config_shared

Fabian Grünbichler f.gruenbichler at proxmox.com
Mon Apr 27 10:24:29 CEST 2020


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 at proxmox.com>
---

Notes:
    requires breaks on qemu-server that uses lock_config_mode
    possibly better to postpone since it does not fix an actual issue
    but just improves the readability/design

 PVE/AbstractConfig.pm | 39 ++++++++++++++++++++++-----------------
 1 file changed, 22 insertions(+), 17 deletions(-)

diff --git a/PVE/AbstractConfig.pm b/PVE/AbstractConfig.pm
index 8ec27a6..1abe41c 100644
--- a/PVE/AbstractConfig.pm
+++ b/PVE/AbstractConfig.pm
@@ -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) = @_;
-- 
2.20.1





More information about the pve-devel mailing list