From c43e7c9c8c5a870550640788b1fe8ac9ef4e0a1e Mon Sep 17 00:00:00 2001 From: jbjohnso Date: Fri, 26 Jun 2009 14:45:50 +0000 Subject: [PATCH] -Add convenience functions for lock management git-svn-id: https://svn.code.sf.net/p/xcat/code/xcat-core/trunk@3650 8638fb3e-16cb-4fca-ae20-7b5d299a9bcd --- perl-xCAT/xCAT/Utils.pm | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/perl-xCAT/xCAT/Utils.pm b/perl-xCAT/xCAT/Utils.pm index 4292bc6ed..d6834e2fd 100644 --- a/perl-xCAT/xCAT/Utils.pm +++ b/perl-xCAT/xCAT/Utils.pm @@ -4082,6 +4082,46 @@ sub getsynclistfile() } +#----------------------------------------------------------------------------- +=head3 acquire_lock + Get a lock on an arbirtrary named resource. For now, this is only across the scope of one service node/master node, an argument may be added later if/when 'global' locks are supported. This call will block until the lock is free. + Arguments: + A string name for the lock to acquire + Returns: + false on failure + A reference for the lock being held. +=cut + +sub acquire_lock { + my $lock_name = shift; + use File::Path; + mkpath("/var/lock/xcat/"); + use Fcntl ":flock"; + my $tlock; + $tlock->{path}="/var/lock/xcat/".$lock_name; + open($tlock->{fd},">",$tlock->{path}) or return undef; + unless ($tlock->{fd}) { return undef; } + flock($tlock->{fd},LOCK_EX) or return undef; + return $tlock; +} + +#--------------------- +=head3 release_lock + Release an acquired lock + Arguments: + reference to lock + Returns: + false on failure, true on success +=cut + +sub release_lock { + my $tlock = shift; + unlink($tlock->{path}); + flock($tlock->{fd},LOCK_UN); + close($tlock->{fd}); +} + + #-----------------------------------------------------------------------------